Added all associations to any object.
This commit is contained in:
parent
addd6633f0
commit
9841ce75e5
2 changed files with 33 additions and 6 deletions
|
@ -275,7 +275,7 @@ class ApplicationController < ActionController::Base
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
def model_create_render_item (generic_object)
|
def model_create_render_item (generic_object)
|
||||||
render :json => generic_object, :status => :created
|
render :json => generic_object.attributes_with_associations, :status => :created
|
||||||
end
|
end
|
||||||
|
|
||||||
def model_update_render (object, params)
|
def model_update_render (object, params)
|
||||||
|
@ -298,7 +298,7 @@ class ApplicationController < ActionController::Base
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
def model_update_render_item (generic_object)
|
def model_update_render_item (generic_object)
|
||||||
render :json => generic_object, :status => :ok
|
render :json => generic_object.attributes_with_associations, :status => :ok
|
||||||
end
|
end
|
||||||
|
|
||||||
def model_destory_render (object, params)
|
def model_destory_render (object, params)
|
||||||
|
@ -327,13 +327,13 @@ class ApplicationController < ActionController::Base
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
def model_show_render_item (generic_object)
|
def model_show_render_item (generic_object)
|
||||||
render :json => generic_object, :status => :ok
|
render :json => generic_object.attributes_with_associations, :status => :ok
|
||||||
end
|
end
|
||||||
|
|
||||||
def model_index_render (object, params)
|
def model_index_render (object, params)
|
||||||
begin
|
begin
|
||||||
generic_object = object.all
|
generic_objects = object.all
|
||||||
model_index_render_result( generic_object )
|
model_index_render_result( generic_objects )
|
||||||
rescue Exception => e
|
rescue Exception => e
|
||||||
logger.error e.message
|
logger.error e.message
|
||||||
logger.error e.backtrace.inspect
|
logger.error e.backtrace.inspect
|
||||||
|
|
|
@ -83,7 +83,8 @@ returns
|
||||||
|
|
||||||
set rellations of model based on params
|
set rellations of model based on params
|
||||||
|
|
||||||
result = Model.param_set_associations(params)
|
model = Model.find(1)
|
||||||
|
result = model.param_set_associations(params)
|
||||||
|
|
||||||
returns
|
returns
|
||||||
|
|
||||||
|
@ -112,6 +113,32 @@ returns
|
||||||
|
|
||||||
=begin
|
=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)
|
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)
|
result = Model.param_validation(params)
|
||||||
|
|
Loading…
Reference in a new issue