Skip to content

Instantly share code, notes, and snippets.

@remogatto
Forked from sub/gist:839014
Created February 22, 2011 17:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save remogatto/839055 to your computer and use it in GitHub Desktop.
Save remogatto/839055 to your computer and use it in GitHub Desktop.
def create
offer = Offer.find(params[:offer_id])
if params[:offer_id]
task = JSON.parse(params[:tasks])
unless task.nil?
# seleziona i parametri rispondenti alla RE active_(.*)
# in questo modo otterrai una lista "elems" di al max 3 elementi del tipo
# elems == ['active_gift', 'active_encoding', ...]
elems.each do |elem|
klass_name = elem.split('_')[1].capitalize
klass = eval klass_name
# a questo punto hai la classe klass che equivale a Gift, Encoding, etc.
task, elem_instance = klass.separate(task)
if task.class == Hash and !task["id"].empty?
task_id = task["id"]
new_task = Task.find(task_id)
new_task.update_attributes(task)
if task["active_#{elem}"]
delete_elem = klass.find_by_task_id(task_id)
delete_elem.delete unless delete_elem.nil?
new_task.send(elem, klass.new(elem_instance))
new_task.save
else
...
elsif task.class == Array
...
else
...
if task["active_#{elem}"]
...
else
...
end
end
end
else
render :json => @service.to_ext_json(:success => @service.update_attributes(params[:service]))
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment