Added all associations to any object.

This commit is contained in:
Martin Edenhofer 2014-05-16 15:09:17 +02:00
parent addd6633f0
commit 9841ce75e5
2 changed files with 33 additions and 6 deletions

View file

@ -275,7 +275,7 @@ class ApplicationController < ActionController::Base
end
end
def model_create_render_item (generic_object)
render :json => generic_object, :status => :created
render :json => generic_object.attributes_with_associations, :status => :created
end
def model_update_render (object, params)
@ -298,7 +298,7 @@ class ApplicationController < ActionController::Base
end
end
def model_update_render_item (generic_object)
render :json => generic_object, :status => :ok
render :json => generic_object.attributes_with_associations, :status => :ok
end
def model_destory_render (object, params)
@ -327,13 +327,13 @@ class ApplicationController < ActionController::Base
end
end
def model_show_render_item (generic_object)
render :json => generic_object, :status => :ok
render :json => generic_object.attributes_with_associations, :status => :ok
end
def model_index_render (object, params)
begin
generic_object = object.all
model_index_render_result( generic_object )
generic_objects = object.all
model_index_render_result( generic_objects )
rescue Exception => e
logger.error e.message
logger.error e.backtrace.inspect

View file

@ -83,7 +83,8 @@ returns
set rellations of model based on params
result = Model.param_set_associations(params)
model = Model.find(1)
result = model.param_set_associations(params)
returns
@ -112,6 +113,32 @@ returns
=begin
get rellations of model based on params
model = Model.find(1)
attributes = model.attributes_with_associations
returns
hash with attributes and association ids
=end
def attributes_with_associations
# set relations
attributes = self.attributes
self.class.reflect_on_all_associations.map { |assoc|
real_key = assoc.name.to_s[0,assoc.name.to_s.length-1] + '_ids'
if self.respond_to?( real_key )
attributes[ real_key ] = self.send( real_key )
end
}
attributes
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)