Can someone help DRY a noob? thanks


#1

hi all

i’m trying to pass a variable name right round the block and need a hand
to ease my aching brain.

##in my sidebar i have

  • <%= SidebarItem('catalogue') %>
  • <%= SidebarItem('provenance') %>
  • <%= SidebarItem('status') %>
  • ##linked to a helper

    def SidebarItem(name)
    link_to_remote “#{name}”,
    :url=> {:action => name, :id => @work.id, :layout => false }
    end

    but at the moment i have three almost identical actions in the
    controller: ‘catalogue’, ‘provenance’, and ‘status’ containg a line like

    def catalogue

       render (:template => 'admin/works/catalogue', :layout => false)
    end
    

    all of which call an RJS with same three names - ie catalogue.rjs - each
    RJS contains something like…

    if @updated

       page.replace_html 'EditFields', :partial => 'catalogue'
     end
    

    so how can i dry it up and end up with something like this?

    ##helper

    def SidebarItem(NAME)
    link_to_remote “#{NAME}”,
    :url=> {:action => SOMEACTION(NAME), :id => @work.id, :layout =>
    false}
    end

    ##action

    def SOMEACTION(NAME)
    render (:template => ‘admin/works/NAME’, :layout => false)
    end

    where NAME.rjs contains

    if @updated
    page.replace_html ‘EditFields’, :partial => ‘NAME’
    end

    TIA for help and patience with newbie questions


    #2

    oops scratch that, i meant

    hi all

    i’m trying to pass a variable name right round the block and need a
    hand
    to ease my aching brain.

    ##in my sidebar i have

    <li><%= SidebarItem('catalogue') %></li>
    <li><%= SidebarItem('provenance') %></li>
    <li><%= SidebarItem('status') %></li>
    

    ##linked to a helper

    def SidebarItem(name)
           link_to_remote "#{name}",
           :url=> {:action => name, :id => @work.id, :layout => false }
    end
    

    but at the moment i have three almost identical actions in the
    controller: ‘catalogue’, ‘provenance’, and ‘status’ containg a line
    like

    def catalogue

        render (:template => 'admin/works/catalogue', :layout => false)
     end
    

    all of which call an RJS with same three names - ie catalogue.rjs -
    each
    RJS contains something like…

    if @updated

        page.replace_html 'EditFields', :partial => 'catalogue'
      end
    

    so how can i dry it up and end up with something like this?

    ##helper

    def SidebarItem(NAME)
         link_to_remote "#{NAME}",
         :url=> {:action => SOMEACTION(NAME), :id => @work.id, :layout 
    

    =>
    false}
    end

    ##action

    def SOMEACTION(NAME)
    render (:template => ‘admin/works/SOMERJS’, :layout => false)
    end

    where SOMERJS.rjs contains

    if @updated
      page.replace_html 'EditFields', :partial => 'NAME'
    end
    

    TIA for help and patience with newbie questions


    #3

    no takers?

    my brain still hurts :~(


    #4

    Mike, you’re a star.

    D


    #5

    On 4/28/06, Damian leGassick removed_email_address@domain.invalid wrote:

  • <%= SidebarItem('status') %>
  • ##linked to a helper

    def SidebarItem(name)
    link_to_remote “#{name}”,
    :url=> {:action => name, :id => @work.id, :layout => false }
    end

    I would take out this helper method altogether - it’s specifically
    tied to the ivar @work, so you can’t really use it in any other
    general sense. You could of course change it to:

    def SidebarItem(name,ivar)
    link_to_remote “#{name}”, :url => {:action => name, :id =
    ivar.id, :layout => false }
    end

    and then call it using:

    SidebarItem(‘catalogue’,@work)

    so that it would be applicable to more cases, but then you may as well
    just use the regular link_to_remote function. I only really use
    helpers where they replace a large chunk of code (not just a one
    liner), or I’ll be calling them over and over again from different
    parts of the application (which of course you could do with the second
    form I presented here, since it’s more general - but like I said, for
    a simple one liner like link_to_remote, you may as well keep it
    there).

    In any case, why not do something like this:

    myview.rhtml:

  • <%= link_to_remote 'catalogue', :url => {:action => my_dispatcher, :id = @work.id, :arg => 'catalogue', :layout => false } %>
  • <%= link_to_remote 'provenance', :url => {:action => my_dispatcher, :id = @work.id, :arg => 'provenance', :layout => false } %>
  • <%= link_to_remote 'status', :url => {:action => my_dispatcher, :id = @work.id, :arg => 'status', :layout => false } %>

    my_controller.rb:

    def my_dispatcher
    return unless @updated

     partial_name = params[:arg]
    
     #use inline rjs
     render :update do |page|
          page.replace_html ('EditFields', :partial => partial_name)
    end
    

    end

    You’ll still need three different partial templates,
    ‘_catalogue.rhtml’, ‘_provenance.rhtml’ and ‘_status.rhtml’, but at
    least this will allow you to remove the three separate .rjs template
    files. If the three partials contain essentially the exact same
    layout, you could also condense them into a single partial file, and
    determine which values to change in the partial by passing local
    variables. ie, if your partials only do something like:

    _catalogue.rhtml:
    Enter catalogue name: <%= text_field (‘model’, ‘catalogue_name’, …) %>

    then you could change the render method in my_displatcher to:

    def my_dispatcher
    return unless @updated

     item_name = params[:arg]
    
     #use inline rjs
     render :update do |page|
          page.replace_html ('EditFields', :partial => 'standard',
    

    :locals => { :item => item_name })
    end
    end

    and then remove _catalogue.rhtml, _provenance.rhtml, _status.rhtml and
    replace it with _standard.rhtml:

    Enter <%= item_name %> name: <%= text_field (‘model’, item_name, …) %>

    or something to that effect.

    Mike