Просмотр исходного кода

Introduced REST expand=true/false/1/0, full=true/false/1/0 and all=true/false/1/0 options. Improved controller tests. @hanneshal

Martin Edenhofer 7 лет назад
Родитель
Сommit
e460c99cad

+ 4 - 2
.gitlab-ci.yml

@@ -310,7 +310,8 @@ test:integration:es_mysql:
     - ruby -I test/ test/controllers/search_controller_test.rb
     - ruby -I test/ test/integration/report_test.rb
     - ruby -I test/ test/controllers/form_controller_test.rb
-    - ruby -I test/ test/controllers/user_organization_controller_test.rb
+    - ruby -I test/ test/controllers/user_controller_test.rb
+    - ruby -I test/ test/controllers/organization_controller_test.rb
     - rake db:drop
 
 test:integration:es_postgresql:
@@ -328,7 +329,8 @@ test:integration:es_postgresql:
     - ruby -I test/ test/controllers/search_controller_test.rb
     - ruby -I test/ test/integration/report_test.rb
     - ruby -I test/ test/controllers/form_controller_test.rb
-    - ruby -I test/ test/controllers/user_organization_controller_test.rb
+    - ruby -I test/ test/controllers/user_controller_test.rb
+    - ruby -I test/ test/controllers/organization_controller_test.rb
     - rake db:drop
 
 test:integration:zendesk_mysql:

+ 2 - 2
app/assets/javascripts/app/controllers/users.coffee

@@ -145,7 +145,7 @@ class Index extends App.ControllerSubContent
         query: @query
         limit: 140
         role_ids: role_ids
-        full:  1
+        full:  true
       processData: true,
       success: (data, status, xhr) =>
         App.Collection.loadAssets(data.assets)
@@ -167,7 +167,7 @@ class Index extends App.ControllerSubContent
       data:
         limit: 50
         role_ids: role_ids
-        full: 1
+        full: true
       processData: true
       success: (data, status, xhr) =>
         App.Collection.loadAssets(data.assets)

+ 1 - 0
app/controllers/application_controller.rb

@@ -10,6 +10,7 @@ class ApplicationController < ActionController::Base
   include ApplicationController::ChecksMaintainance
   include ApplicationController::RendersModels
   include ApplicationController::HasUser
+  include ApplicationController::HasResponseExtentions
   include ApplicationController::PreventsCsrf
   include ApplicationController::LogsHttpAccess
   include ApplicationController::ChecksAccess

+ 31 - 0
app/controllers/application_controller/has_response_extentions.rb

@@ -0,0 +1,31 @@
+module ApplicationController::HasResponseExtentions
+  extend ActiveSupport::Concern
+
+  private
+
+  def response_expand?
+    return true if params[:expand] == true
+    return true if params[:expand] == 'true'
+    return true if params[:expand] == 1
+    return true if params[:expand] == '1'
+
+    false
+  end
+
+  def response_full?
+    return true if params[:full] == true
+    return true if params[:full] == 'true'
+    return true if params[:full] == 1
+    return true if params[:full] == '1'
+    false
+  end
+
+  def response_all?
+    return true if params[:all] == true
+    return true if params[:all] == 'true'
+    return true if params[:all] == 1
+    return true if params[:all] == '1'
+    false
+  end
+
+end

+ 18 - 10
app/controllers/application_controller/renders_models.rb

@@ -18,11 +18,16 @@ module ApplicationController::RendersModels
     # set relations
     generic_object.associations_from_param(params)
 
-    if params[:expand]
+    if response_expand?
       render json: generic_object.attributes_with_association_names, status: :created
       return
     end
 
+    if response_full?
+      render json: generic_object.class.full(generic_object.id), status: :created
+      return
+    end
+
     model_create_render_item(generic_object)
   end
 
@@ -47,11 +52,16 @@ module ApplicationController::RendersModels
       generic_object.associations_from_param(params)
     end
 
-    if params[:expand]
+    if response_expand?
       render json: generic_object.attributes_with_association_names, status: :ok
       return
     end
 
+    if response_full?
+      render json: generic_object.class.full(generic_object.id), status: :ok
+      return
+    end
+
     model_update_render_item(generic_object)
   end
 
@@ -71,20 +81,18 @@ module ApplicationController::RendersModels
 
   def model_show_render(object, params)
 
-    if params[:expand]
+    if response_expand?
       generic_object = object.find(params[:id])
       render json: generic_object.attributes_with_association_names, status: :ok
       return
     end
 
-    if params[:full]
-      generic_object_full = object.full(params[:id])
-      render json: generic_object_full, status: :ok
+    if response_full?
+      render json: object.full(params[:id]), status: :ok
       return
     end
 
-    generic_object = object.find(params[:id])
-    model_show_render_item(generic_object)
+    model_show_render_item(object.find(params[:id]))
   end
 
   def model_show_render_item(generic_object)
@@ -109,7 +117,7 @@ module ApplicationController::RendersModels
                         object.all.order(id: 'ASC').offset(offset).limit(limit)
                       end
 
-    if params[:expand]
+    if response_expand?
       list = []
       generic_objects.each do |generic_object|
         list.push generic_object.attributes_with_association_names
@@ -118,7 +126,7 @@ module ApplicationController::RendersModels
       return
     end
 
-    if params[:full]
+    if response_full?
       assets = {}
       item_ids = []
       generic_objects.each do |item|

+ 1 - 1
app/controllers/applications_controller.rb

@@ -5,7 +5,7 @@ class ApplicationsController < ApplicationController
 
   def index
     all = Doorkeeper::Application.all
-    if params[:full]
+    if response_full?
       assets = {}
       item_ids = []
       all.each do |item|

+ 2 - 2
app/controllers/online_notifications_controller.rb

@@ -47,7 +47,7 @@ curl http://localhost/api/v1/online_notifications.json -v -u #{login}:#{password
 =end
 
   def index
-    if params[:full]
+    if response_full?
       render json: OnlineNotification.list_full(current_user, 200)
       return
     end
@@ -149,7 +149,7 @@ curl http://localhost/api/v1/online_notifications/mark_all_as_read -v -u #{login
     notifications = OnlineNotification.list(current_user, 200)
     notifications.each do |notification|
       if !notification['seen']
-        OnlineNotification.seen( id: notification['id'] )
+        OnlineNotification.seen(id: notification['id'])
       end
     end
     render json: {}, status: :ok

+ 8 - 7
app/controllers/organizations_controller.rb

@@ -69,7 +69,7 @@ curl http://localhost/api/v1/organizations -v -u #{login}:#{password}
       organizations = Organization.all.order(id: 'ASC').offset(offset).limit(per_page)
     end
 
-    if params[:expand]
+    if response_expand?
       list = []
       organizations.each do |organization|
         list.push organization.attributes_with_association_names
@@ -78,7 +78,7 @@ curl http://localhost/api/v1/organizations -v -u #{login}:#{password}
       return
     end
 
-    if params[:full]
+    if response_full?
       assets = {}
       item_ids = []
       organizations.each do |item|
@@ -91,6 +91,7 @@ curl http://localhost/api/v1/organizations -v -u #{login}:#{password}
       }, status: :ok
       return
     end
+
     list = []
     organizations.each do |organization|
       list.push organization.attributes_with_association_ids
@@ -126,15 +127,15 @@ curl http://localhost/api/v1/organizations/#{id} -v -u #{login}:#{password}
       raise Exceptions::NotAuthorized if params[:id].to_i != current_user.organization_id
     end
 
-    if params[:expand]
+    if response_expand?
       organization = Organization.find(params[:id]).attributes_with_association_names
       render json: organization, status: :ok
       return
     end
 
-    if params[:full]
+    if response_full?
       full = Organization.full(params[:id])
-      render json: full
+      render json: full, status: :ok
       return
     end
 
@@ -259,7 +260,7 @@ curl http://localhost/api/v1/organization/{id} -v -u #{login}:#{password} -H "Co
       organization_all = organization_all[offset, params[:per_page].to_i] || []
     end
 
-    if params[:expand]
+    if response_expand?
       list = []
       organization_all.each do |organization|
         list.push organization.attributes_with_association_names
@@ -281,7 +282,7 @@ curl http://localhost/api/v1/organization/{id} -v -u #{login}:#{password} -H "Co
       return
     end
 
-    if params[:full]
+    if response_full?
       organization_ids = []
       assets = {}
       organization_all.each do |organization|

+ 1 - 1
app/controllers/slas_controller.rb

@@ -48,7 +48,7 @@ curl http://localhost/api/v1/slas.json -v -u #{login}:#{password}
 
   def index
 
-    if params[:full]
+    if response_full?
 
       # calendars
       assets = {}

+ 10 - 10
app/controllers/ticket_articles_controller.rb

@@ -17,13 +17,13 @@ class TicketArticlesController < ApplicationController
     article = Ticket::Article.find(params[:id])
     access!(article, 'read')
 
-    if params[:expand]
+    if response_expand?
       result = article.attributes_with_association_names
       render json: result, status: :ok
       return
     end
 
-    if params[:full]
+    if response_full?
       full = Ticket::Article.full(params[:id])
       render json: full
       return
@@ -39,7 +39,7 @@ class TicketArticlesController < ApplicationController
 
     articles = []
 
-    if params[:expand]
+    if response_expand?
       ticket.articles.each do |article|
 
         # ignore internal article if customer is requesting
@@ -52,7 +52,7 @@ class TicketArticlesController < ApplicationController
       return
     end
 
-    if params[:full]
+    if response_full?
       assets = {}
       record_ids = []
       ticket.articles.each do |article|
@@ -66,7 +66,7 @@ class TicketArticlesController < ApplicationController
       render json: {
         record_ids: record_ids,
         assets: assets,
-      }
+      }, status: :ok
       return
     end
 
@@ -76,7 +76,7 @@ class TicketArticlesController < ApplicationController
       next if article.internal == true && current_user.permissions?('ticket.customer')
       articles.push article.attributes_with_association_names
     end
-    render json: articles
+    render json: articles, status: :ok
   end
 
   # POST /articles
@@ -85,13 +85,13 @@ class TicketArticlesController < ApplicationController
     access!(ticket, 'create')
     article = article_create(ticket, params)
 
-    if params[:expand]
+    if response_expand?
       result = article.attributes_with_association_names
       render json: result, status: :created
       return
     end
 
-    if params[:full]
+    if response_full?
       full = Ticket::Article.full(params[:id])
       render json: full, status: :created
       return
@@ -114,13 +114,13 @@ class TicketArticlesController < ApplicationController
 
     article.update!(clean_params)
 
-    if params[:expand]
+    if response_expand?
       result = article.attributes_with_association_names
       render json: result, status: :ok
       return
     end
 
-    if params[:full]
+    if response_full?
       full = Ticket::Article.full(params[:id])
       render json: full, status: :ok
       return

Некоторые файлы не были показаны из-за большого количества измененных файлов