From 5511b0594c5d7d2a8a3bf09a6e839bef64e110a6 Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Tue, 1 Oct 2013 01:37:45 +0200 Subject: [PATCH] Added param_set_associations to auto set associations. --- app/controllers/application_controller.rb | 10 ++++++++- app/models/application_model.rb | 27 +++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 8c96d274f..98ac39e96 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -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 diff --git a/app/models/application_model.rb b/app/models/application_model.rb index 42f692a93..8b8b82018 100644 --- a/app/models/application_model.rb +++ b/app/models/application_model.rb @@ -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)