Wondering how to do some clever DRYing up


#1

Hi there,
I’m now getting to the point in my rails app where I can use all of the
nice basic features like AJAX and engines etc…
but, I want to get my hands a bit dirtier, whilst learning to DRY up my
code a bit.

I’m using AJAX quite a bit to dynamically populate/edit/sort has_many
relationships in forms, and consequently on a model with many has_many’s
I end up repeating functions to deal with adding, sorting & deleting
different types of record.

For instance I have the folllowing basic sorting controller method for a
sortable_element:

def form_sort_performers
i = 1
params[:performers].each {
|id|
Performer.update(id, :position => i)
i = i+1
}
render :nothing => true
end

but, I have about 6 things that all need the same basic method
(Producers, ImagePersonnel etc…)
is there a way I can somehow move this into the application controller
and run it generically such that it takes a Model as an argument,
something like:

def form_sort(model)
i = 1
params[:#{model}].each { # would need to pluralise
|id|
#{model}.update(id, :position => i)
i = i+1
}
render :nothing => true
end

I’m sure something like this is very possible, and most likely very neat
and easy to do in ruby/rails - I’m just not sure where to start!
any pointer gratefully recieved

thanks

dorian


I do things for love or money


#2

Dorian M. removed_email_address@domain.invalid writes:

params[:performers].each {
def form_sort(model)
any pointer gratefully recieved

I also had a requirement (similar to yours), where I wanted to combine
the new
and create action functionality of many controllers into one, so I made
a base
controller class and made the other controllers classes inherit from it,
this
makes all the inherited controllers get the new and create action.

And for determining the model, I wrote a new model class which takes the
controller name, and then returns the equivalent model.

The same thing can be also done by a protected method in the application
controller, which is then called in other inherited controllers with the
model
class as an argument.

HTH.

Surendra S.
http://ssinghi.kreeti.com, http://www.kreeti.com
Read my blog at: http://cuttingtheredtape.blogspot.com/
,----
| “All animals are equal, but some animals are more equal than others.”
| – Orwell, Animal Farm, 1945
`----


#3

I think I found an answer in the rails recipe book, the chapter “write
code that writes code”, but thanks for the pointers.


#4

There has been some talk on the Ruby-talk mailing list regarding getting
a
class from a name.

Thanx to Sergey V. for the class_by_name method.
Put this method into your application_controller.rb file to be able to
get a
class object from a string

def class_by_name name
name.split("::").inject(Object){ |c,n|
c.const_get(n)
}
end

Then in whatever controller you want put in the following method (the
argument is either string or symbol)

This follows the convention that you have shown in your post regarding
the
pluralisation of the model name in the params hash as a symbol.

def form_sort( model )
model = model.to_s
params[model.pluralize.downcase.to_sym].each_with_index do |id,index|
class_by_name( model.camelize ).update( id, :position => index )
end
end

This isn’t tested tho so fingers crossed.