Added param_set_associations to auto set associations.

This commit is contained in:
Martin Edenhofer 2013-10-01 01:37:45 +02:00
parent 3b2baa8c9d
commit 5511b0594c
2 changed files with 36 additions and 1 deletions

View file

@ -63,6 +63,7 @@ class ApplicationController < ActionController::Base
@_current_user = User.find( session[:user_id] )
end
def current_user_set(user)
session[:user_id] = user.id
@_current_user = user
set_user
end
@ -262,6 +263,9 @@ class ApplicationController < ActionController::Base
# save object
generic_object.save!
# set relations
generic_object.param_set_associations( params )
model_create_render_item(generic_object)
rescue Exception => e
puts e.message.inspect
@ -282,7 +286,11 @@ class ApplicationController < ActionController::Base
# save object
generic_object.update_attributes!( object.param_cleanup( params[object.to_app_model_url] ) )
model_update_render_item(generic_object)
# set relations
generic_object.param_set_associations( params )
model_update_render_item( generic_object )
rescue Exception => e
logger.error e.message
logger.error e.backtrace.inspect

View file

@ -73,6 +73,33 @@ returns
=begin
set rellations of model based on params
result = Model.param_set_associations(params)
returns
result = true|false
=end
def param_set_associations(params)
# set relations
self.class.reflect_on_all_associations.map { |assoc|
real_key = assoc.name.to_s[0,assoc.name.to_s.length-1] + '_ids'
if params.has_key?( real_key.to_sym )
list = []
params[ real_key.to_sym ].each {|item|
list.push( assoc.klass.find(item) )
}
self.send( assoc.name.to_s + '=', list )
end
}
end
=begin
remove all not used params of object (per default :updated_at, :created_at, :updated_by_id and :created_by_id)
result = Model.param_validation(params)