Fix some rubocop issues and extract ransack hash to reduce repetition

This commit is contained in:
luisramos0
2019-11-11 21:38:56 +00:00
parent 962779bba1
commit 88464d58c2
2 changed files with 32 additions and 37 deletions

View File

@@ -365,11 +365,10 @@ Metrics/AbcSize:
- app/controllers/spree/admin/overview_controller.rb
- app/controllers/spree/admin/payment_methods_controller.rb
- app/controllers/spree/admin/payments_controller.rb
- app/controllers/spree/admin/products_controller_decorator.rb
- app/controllers/spree/admin/products_controller.rb
- app/controllers/spree/admin/reports_controller.rb
- app/controllers/spree/admin/resource_controller.rb
- app/controllers/spree/admin/products_controller.rb
- app/controllers/spree/admin/search_controller_decorator.rb
- app/controllers/spree/admin/search_controller.rb
- app/controllers/spree/admin/taxons_controller.rb
- app/controllers/spree/admin/users_controller.rb
- app/controllers/spree/admin/variants_controller.rb
@@ -569,10 +568,9 @@ Metrics/MethodLength:
- app/controllers/spree/admin/orders_controller.rb
- app/controllers/spree/admin/payment_methods_controller.rb
- app/controllers/spree/admin/payments_controller.rb
- app/controllers/spree/admin/products_controller.rb
- app/controllers/spree/admin/reports_controller.rb
- app/controllers/spree/admin/resource_controller.rb
- app/controllers/spree/admin/products_controller.rb
- app/controllers/spree/admin/search_controller_decorator.rb
- app/controllers/spree/admin/tax_categories_controller.rb
- app/controllers/spree/admin/taxons_controller.rb
- app/controllers/spree/admin/users_controller.rb

View File

@@ -2,54 +2,51 @@ module Spree
module Admin
class SearchController < Spree::Admin::BaseController
# http://spreecommerce.com/blog/2010/11/02/json-hijacking-vulnerability/
before_filter :check_json_authenticity, :only => :index
before_filter :check_json_authenticity, only: :index
respond_to :json
# TODO: Clean this up by moving searching out to user_class_extensions
# And then JSON building with something like Active Model Serializers
def users
if params[:ids]
@users = Spree.user_class.where(:id => params[:ids].split(','))
else
@users = Spree.user_class.ransack({
:m => 'or',
:email_start => params[:q],
:ship_address_firstname_start => params[:q],
:ship_address_lastname_start => params[:q],
:bill_address_firstname_start => params[:q],
:bill_address_lastname_start => params[:q]
}).result.limit(10)
end
@users = if params[:ids]
Spree.user_class.where(id: params[:ids].split(','))
else
Spree.user_class.ransack(ransack_hash).result.limit(10)
end
render json: @users, each_serializer: Api::Admin::UserSerializer
render json: @users, each_serializer: ::Api::Admin::UserSerializer
end
def known_users
@users = if exact_match = Spree.user_class.find_by_email(params[:q])
[exact_match]
else
spree_current_user.known_users.ransack(
m: 'or',
email_start: params[:q],
ship_address_firstname_start: params[:q],
ship_address_lastname_start: params[:q],
bill_address_firstname_start: params[:q],
bill_address_lastname_start: params[:q]
).result.limit(10)
spree_current_user.known_users.ransack(ransack_hash).result.limit(10)
end
render json: @users, each_serializer: Api::Admin::UserSerializer
render json: @users, each_serializer: ::Api::Admin::UserSerializer
end
def customers
@customers = if spree_current_user.enterprises.pluck(:id).include? params[:distributor_id].to_i
Customer.ransack(m: 'or', email_start: params[:q], name_start: params[:q])
.result.where(enterprise_id: params[:distributor_id])
else
[]
end
@customers = []
if spree_current_user.enterprises.pluck(:id).include? params[:distributor_id].to_i
@customers = Customer.
ransack(m: 'or', email_start: params[:q], name_start: params[:q]).
result.
where(enterprise_id: params[:distributor_id])
end
render json: @customers, each_serializer: ::Api::Admin::CustomerSerializer
end
render json: @customers, each_serializer: Api::Admin::CustomerSerializer
private
def ransack_hash
{
m: 'or',
email_start: params[:q],
ship_address_firstname_start: params[:q],
ship_address_lastname_start: params[:q],
bill_address_firstname_start: params[:q],
bill_address_lastname_start: params[:q]
}
end
end
end