SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Feb 2006
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    what goes where with ruby on rails

    I think this subject deserves a cheat page.

    Where would you put the following

    1. enhacements to base classes
    (like Numeric.format_integer or Time.format_shor_date)

    I created files in \lib (like \lib\format.rb) and added
    require "format"
    in \app\controllers\application.rb

    2. general helpers I want to be available to every view

    I created a file in \lib (like \lib\debug_helper_module.rb), with a module definition (like "module DebugHelper")

    then in \app\helpers\application_helper.rb

    require "debug_helper_modules"
    # Methods added to this helper will be available to all templates in the application.
    module ApplicationHelper
    include DebugHelper
    end

    but it didn't work

    Well, I'm just giving my first steps with ruby, but it would be great to have a list telling where to put everything, in order to have a clean and tidy environment.

  2. #2
    SitePoint Guru silver trophy Luke Redpath's Avatar
    Join Date
    Mar 2003
    Location
    London
    Posts
    794
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    1. In the lib folder is the best place. If you are doing more than small modifications you might consider wrapping it up as a Rails plugin instead.

    2. Either in application_helper.rb or in their own separate files (in the lib folder) which you can then mixin to ApplicationHelper. Not sure why that isn't working for you, I've never had problems with it. I may be missing something obvious that somebody else could point out.

  3. #3
    SitePoint Member
    Join Date
    Feb 2006
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks a lot for your answer

    I don't know what happened, but now everything seems to work

    Just in case it might be useful to someone:

    1. Extension to base classes (in this case CGI::Session and Numeric, String, Time )
    =========================================================

    ------------------
    #file: \lib\format.rb

    class Numeric

    def format_integer
    sprintf( "%0.0f", self )
    end

    def format_code
    format_integer
    end

    end

    class String

    def format_code
    to_s
    end

    end

    class Time

    def format_short_date
    self.strftime("%d/%m/%Y")
    end

    end
    -------------

    #file: \lib\session.rb
    #allows treating the session as a hash

    class CGI::Session

    extend Forwardable
    def_delegators :data, :each, :each_pair, :length, :size

    def data
    unless @data
    @data = @dbman.restore
    end
    @data
    end
    end

    ----------

    file: \app\controllers\application.rb

    # Filters added to this controller will be run for all controllers in the application.
    # Likewise, all the methods added will be available for all controllers.
    class ApplicationController < ActionController::Base
    end

    #format methods for class Numeric String Time
    require "format"

    #session as a hash for class CGI::Session
    require "session"

    ---------------------

    2. Making a helper available to each and every controller
    ==========================================

    ---------------------
    #file: \lib\dbug_helper_module.rb

    module DebugHelper

    def debug_object(object)
    "<pre>" << object.to_yaml << "</pre>"
    end

    def debug_hash hash, title = ""
    s =
    "
    <table border='1'>
    <tr><th colspan='2'>#{title}(#{hash.size})</th></tr>
    <tr><th>key</th><th>value</th></tr>
    "
    hash.each_pair { |key, value|
    s += " <tr><td>#{key}</td><td>#{value}</td></tr>\r"
    }
    s +=
    "</table>\n\n"
    end

    end

    --------------------------
    #file: \app\helpers\application_helper.rb

    require "debug_helper_modules"

    # Methods added to this helper will be available to all templates in the application.
    module ApplicationHelper

    include DebugHelper

    end

    -------------------------------

    Well, that's it.

    The difference is that in the first place, I just need to add a couple of methos to existing classes, so with just "requiring" them from application.rb is enought.

    In the second place, I want to "include" methods inside a module that will be mixed-in every controller. That is, I want to mix a module inside a module that will be mixed (clear as mud, I guess)

    Well, am I right?

    Saludos

    Sas


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •