Added expand format for data rest api (deliver records with filled reverences for "group_id: 123" add automatically "group: 'some name'").

This commit is contained in:
Martin Edenhofer 2016-06-08 06:56:05 +02:00
parent 09be713014
commit 88e3d1fecb
6 changed files with 190 additions and 17 deletions

View file

@ -400,6 +400,11 @@ class ApplicationController < ActionController::Base
# set relations # set relations
generic_object.param_set_associations(params) generic_object.param_set_associations(params)
if params[:expand]
render json: generic_object.attributes_with_relation_names, status: :created
return
end
model_create_render_item(generic_object) model_create_render_item(generic_object)
rescue => e rescue => e
logger.error e.message logger.error e.message
@ -425,6 +430,11 @@ class ApplicationController < ActionController::Base
# set relations # set relations
generic_object.param_set_associations(params) generic_object.param_set_associations(params)
if params[:expand]
render json: generic_object.attributes_with_relation_names, status: :ok
return
end
model_update_render_item(generic_object) model_update_render_item(generic_object)
rescue => e rescue => e
logger.error e.message logger.error e.message
@ -454,7 +464,7 @@ class ApplicationController < ActionController::Base
if params[:expand] if params[:expand]
generic_object = object.find(params[:id]) generic_object = object.find(params[:id])
model_show_render_item(generic_object) render json: generic_object.attributes_with_relation_names, status: :ok
return return
end end
@ -489,6 +499,15 @@ class ApplicationController < ActionController::Base
object.all.offset(offset).limit(limit) object.all.offset(offset).limit(limit)
end end
if params[:expand]
list = []
generic_objects.each {|generic_object|
list.push generic_object.attributes_with_relation_names
}
render json: list, status: :ok
return
end
if params[:full] if params[:full]
assets = {} assets = {}
item_ids = [] item_ids = []

View file

@ -65,6 +65,15 @@ curl http://localhost/api/v1/organizations -v -u #{login}:#{password}
organizations = Organization.all.offset(offset).limit(per_page) organizations = Organization.all.offset(offset).limit(per_page)
end end
if params[:expand]
list = []
organizations.each {|organization|
list.push organization.attributes_with_relation_names
}
render json: list, status: :ok
return
end
if params[:full] if params[:full]
assets = {} assets = {}
item_ids = [] item_ids = []
@ -112,11 +121,19 @@ curl http://localhost/api/v1/organizations/#{id} -v -u #{login}:#{password}
return return
end end
end end
if params[:expand]
organization = Organization.find(params[:id]).attributes_with_relation_names
render json: organization, status: :ok
return
end
if params[:full] if params[:full]
full = Organization.full(params[:id]) full = Organization.full(params[:id])
render json: full render json: full
return return
end end
model_show_render(Organization, params) model_show_render(Organization, params)
end end
@ -200,6 +217,7 @@ curl http://localhost/api/v1/organization/{id} -v -u #{login}:#{password} -H "Co
model_destory_render(Organization, params) model_destory_render(Organization, params)
end end
# GET /api/v1/organizations/search
def search def search
if role?(Z_ROLENAME_CUSTOMER) && !role?(Z_ROLENAME_ADMIN) && !role?(Z_ROLENAME_AGENT) if role?(Z_ROLENAME_CUSTOMER) && !role?(Z_ROLENAME_ADMIN) && !role?(Z_ROLENAME_AGENT)
@ -231,7 +249,11 @@ curl http://localhost/api/v1/organization/{id} -v -u #{login}:#{password} -H "Co
end end
if params[:expand] if params[:expand]
render json: organization_all list = []
organization_all.each {|organization|
list.push organization.attributes_with_relation_names
}
render json: list, status: :ok
return return
end end

View file

@ -16,6 +16,15 @@ class TicketsController < ApplicationController
access_condition = Ticket.access_condition(current_user) access_condition = Ticket.access_condition(current_user)
tickets = Ticket.where(access_condition).offset(offset).limit(per_page) tickets = Ticket.where(access_condition).offset(offset).limit(per_page)
if params[:expand]
list = []
tickets.each {|ticket|
list.push ticket.attributes_with_relation_names
}
render json: list, status: :ok
return
end
if params[:full] if params[:full]
assets = {} assets = {}
item_ids = [] item_ids = []
@ -40,6 +49,12 @@ class TicketsController < ApplicationController
ticket = Ticket.find(params[:id]) ticket = Ticket.find(params[:id])
return if !ticket_permission(ticket) return if !ticket_permission(ticket)
if params[:expand]
result = ticket.attributes_with_relation_names
render json: result, status: :ok
return
end
if params[:full] if params[:full]
full = Ticket.full(params[:id]) full = Ticket.full(params[:id])
render json: full render json: full
@ -90,6 +105,12 @@ class TicketsController < ApplicationController
article_create(ticket, params[:article]) article_create(ticket, params[:article])
end end
if params[:expand]
result = ticket.attributes_with_relation_names
render json: result, status: :created
return
end
render json: ticket, status: :created render json: ticket, status: :created
end end
@ -109,6 +130,12 @@ class TicketsController < ApplicationController
article_create(ticket, params[:article]) article_create(ticket, params[:article])
end end
if params[:expand]
result = ticket.attributes_with_relation_names
render json: result, status: :ok
return
end
render json: ticket, status: :ok render json: ticket, status: :ok
else else
render json: ticket.errors, status: :unprocessable_entity render json: ticket.errors, status: :unprocessable_entity
@ -290,6 +317,16 @@ class TicketsController < ApplicationController
condition: params[:condition], condition: params[:condition],
current_user: current_user, current_user: current_user,
) )
if params[:expand]
list = []
tickets.each {|ticket|
list.push ticket.attributes_with_relation_names
}
render json: list, status: :ok
return
end
assets = {} assets = {}
ticket_result = [] ticket_result = []
tickets.each do |ticket| tickets.each do |ticket|

View file

@ -27,6 +27,15 @@ class UsersController < ApplicationController
User.all.offset(offset).limit(per_page) User.all.offset(offset).limit(per_page)
end end
if params[:expand]
list = []
users.each {|user|
list.push user.attributes_with_relation_names
}
render json: list, status: :ok
return
end
if params[:full] if params[:full]
assets = {} assets = {}
item_ids = [] item_ids = []
@ -65,6 +74,12 @@ class UsersController < ApplicationController
# access deny # access deny
return if !permission_check return if !permission_check
if params[:expand]
user = User.find(params[:id]).attributes_with_relation_names
render json: user, status: :ok
return
end
if params[:full] if params[:full]
full = User.full(params[:id]) full = User.full(params[:id])
render json: full render json: full
@ -203,6 +218,13 @@ class UsersController < ApplicationController
objects: result, objects: result,
) )
end end
if params[:expand]
user = User.find(user.id).attributes_with_relation_names
render json: user, status: :created
return
end
user_new = User.find(user.id).attributes_with_associations user_new = User.find(user.id).attributes_with_associations
user_new.delete('password') user_new.delete('password')
render json: user_new, status: :created render json: user_new, status: :created
@ -253,8 +275,14 @@ class UsersController < ApplicationController
user.param_set_associations({ organization_ids: params[:organization_ids], organizations: params[:organizations] }) user.param_set_associations({ organization_ids: params[:organization_ids], organizations: params[:organizations] })
end end
if params[:expand]
user = User.find(user.id).attributes_with_relation_names
render json: user, status: :ok
return
end
# get new data # get new data
user_new = User.find(params[:id]).attributes_with_associations user_new = User.find(user.id).attributes_with_associations
user_new.delete('password') user_new.delete('password')
render json: user_new, status: :ok render json: user_new, status: :ok
rescue => e rescue => e
@ -327,7 +355,11 @@ class UsersController < ApplicationController
end end
if params[:expand] if params[:expand]
render json: user_all list = []
user_all.each {|user|
list.push user.attributes_with_relation_names
}
render json: list, status: :ok
return return
end end

View file

@ -175,7 +175,7 @@ returns
=begin =begin
get rellations of model based on params get relations of model based on params
model = Model.find(1) model = Model.find(1)
attributes = model.attributes_with_associations attributes = model.attributes_with_associations
@ -200,6 +200,69 @@ returns
=begin =begin
get relation name of model based on params
model = Model.find(1)
attributes = model.attributes_with_relation_names
returns
hash with attributes, association ids, association names and relation name
=end
def attributes_with_relation_names
# get relations
attributes = attributes_with_associations
self.class.reflect_on_all_associations.map { |assoc|
next if !respond_to?(assoc.name)
ref = send(assoc.name)
next if !ref
if ref.respond_to?(:first)
attributes[assoc.name.to_s] = []
ref.each {|item|
if item[:login]
attributes[assoc.name.to_s].push item[:login]
next
end
next if !item[:name]
attributes[assoc.name.to_s].push item[:name]
}
if ref.count > 0 && attributes[assoc.name.to_s].empty?
attributes.delete(assoc.name.to_s)
end
next
end
if ref[:login]
attributes[assoc.name.to_s] = ref[:login]
next
end
next if !ref[:name]
attributes[assoc.name.to_s] = ref[:name]
}
# fill created_by/updated_by
{
'created_by_id' => 'created_by',
'updated_by_id' => 'updated_by',
}.each {|source, destination|
next if !attributes[source]
user = User.lookup(id: attributes[source])
next if !user
attributes[destination] = user.login
}
# remove forbitten attributes
%w(password token tokens token_ids).each {|item|
attributes.delete(item)
}
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)

View file

@ -174,20 +174,20 @@ returns
result = [ result = [
{ {
:id => 2, id: 2,
:o_id => 2, o_id: 2,
:created_by_id => 3, created_by_id: 3,
:created_at => '2013-09-28 00:57:21', created_at: '2013-09-28 00:57:21',
:object => "User", object: "User",
:type => "created", type: "created",
}, },
{ {
:id => 2, id: 2,
:o_id => 2, o_id: 2,
:created_by_id => 3, created_by_id: 3,
:created_at => '2013-09-28 00:59:21', created_at: '2013-09-28 00:59:21',
:object => "User", object: "User",
:type => "updated", type: "updated",
}, },
] ]