Forum: Ruby on Rails can someone help DRY a noob? thanks...

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
65c7c3dd95869e1430a260b049eabd51?d=identicon&s=25 Damian Legassick (dlg)
on 2006-04-28 21:08
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/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
65c7c3dd95869e1430a260b049eabd51?d=identicon&s=25 Damian Legassick (dlg)
on 2006-04-28 21:13
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
65c7c3dd95869e1430a260b049eabd51?d=identicon&s=25 Damian Legassick (dlg)
on 2006-04-28 23:37
no takers?

my brain still hurts :~(
0900e6a4828bd989f96427082c6c74ca?d=identicon&s=25 Mike Garey (random52k)
on 2006-05-03 18:56
(Received via mailing list)
On 4/28/06, Damian leGassick <damianlegassick@mac.com> wrote:
>    <li><%= SidebarItem('status') %></li>
>
>
> ##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:

<li><%= link_to_remote 'catalogue', :url => {:action => my_dispatcher,
:id = @work.id, :arg => 'catalogue', :layout => false } %>
<li><%= link_to_remote 'provenance', :url => {:action =>
my_dispatcher, :id = @work.id, :arg => 'provenance', :layout => false
} %>
<li><%= 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
65c7c3dd95869e1430a260b049eabd51?d=identicon&s=25 Damian Legassick (dlg)
on 2006-05-03 21:14
Mike, you're a star.

D
This topic is locked and can not be replied to.