Browse Source

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

Martin Edenhofer 8 years ago
parent
commit
88e3d1fecb

+ 20 - 1
app/controllers/application_controller.rb

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

+ 23 - 1
app/controllers/organizations_controller.rb

@@ -65,6 +65,15 @@ curl http://localhost/api/v1/organizations -v -u #{login}:#{password}
       organizations = Organization.all.offset(offset).limit(per_page)
     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]
       assets = {}
       item_ids = []
@@ -112,11 +121,19 @@ curl http://localhost/api/v1/organizations/#{id} -v -u #{login}:#{password}
         return
       end
     end
+
+    if params[:expand]
+      organization = Organization.find(params[:id]).attributes_with_relation_names
+      render json: organization, status: :ok
+      return
+    end
+
     if params[:full]
       full = Organization.full(params[:id])
       render json: full
       return
     end
+
     model_show_render(Organization, params)
   end
 
@@ -200,6 +217,7 @@ curl http://localhost/api/v1/organization/{id} -v -u #{login}:#{password} -H "Co
     model_destory_render(Organization, params)
   end
 
+  # GET /api/v1/organizations/search
   def search
 
     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
 
     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
     end
 

+ 37 - 0
app/controllers/tickets_controller.rb

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

+ 34 - 2
app/controllers/users_controller.rb

@@ -27,6 +27,15 @@ class UsersController < ApplicationController
               User.all.offset(offset).limit(per_page)
             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]
       assets = {}
       item_ids = []
@@ -65,6 +74,12 @@ class UsersController < ApplicationController
     # access deny
     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]
       full = User.full(params[:id])
       render json: full
@@ -203,6 +218,13 @@ class UsersController < ApplicationController
           objects: result,
         )
       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.delete('password')
       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] })
       end
 
+      if params[:expand]
+        user = User.find(user.id).attributes_with_relation_names
+        render json: user, status: :ok
+        return
+      end
+
       # 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')
       render json: user_new, status: :ok
     rescue => e
@@ -327,7 +355,11 @@ class UsersController < ApplicationController
     end
 
     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
     end
 

+ 64 - 1
app/models/application_model.rb

@@ -175,7 +175,7 @@ returns
 
 =begin
 
-get rellations of model based on params
+get relations of model based on params
 
   model = Model.find(1)
   attributes = model.attributes_with_associations
@@ -200,6 +200,69 @@ returns
 
 =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)
 
   result = Model.param_validation(params)

+ 12 - 12
app/models/user.rb

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