PageRenderTime 2027ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/core/app/controllers/spree/admin/users_controller.rb

https://github.com/andrewmp1/spree
Ruby | 68 lines | 53 code | 10 blank | 5 comment | 3 complexity | e6faedc95b5e40bd0b3576f3301cfffa MD5 | raw file
  1. module Spree
  2. module Admin
  3. class UsersController < ResourceController
  4. # http://spreecommerce.com/blog/2010/11/02/json-hijacking-vulnerability/
  5. before_filter :check_json_authenticity, :only => :index
  6. def index
  7. respond_with(@collection) do |format|
  8. format.html
  9. format.json { render :json => json_data }
  10. end
  11. end
  12. def dismiss_banner
  13. if request.xhr? and params[:banner_id]
  14. current_user.dismiss_banner(params[:banner_id])
  15. render :nothing => true
  16. end
  17. end
  18. protected
  19. def collection
  20. return @collection if @collection.present?
  21. unless request.xhr?
  22. @search = Spree::User.registered.ransack(params[:q])
  23. @collection = @search.result.page(params[:page]).per(Spree::Config[:admin_products_per_page])
  24. else
  25. #disabling proper nested include here due to rails 3.1 bug
  26. #@collection = User.includes(:bill_address => [:state, :country], :ship_address => [:state, :country]).
  27. @collection = Spree::User.includes(:bill_address, :ship_address).
  28. where("spree_users.email #{LIKE} :search
  29. OR (spree_addresses.firstname #{LIKE} :search AND spree_addresses.id = spree_users.bill_address_id)
  30. OR (spree_addresses.lastname #{LIKE} :search AND spree_addresses.id = spree_users.bill_address_id)
  31. OR (spree_addresses.firstname #{LIKE} :search AND spree_addresses.id = spree_users.ship_address_id)
  32. OR (spree_addresses.lastname #{LIKE} :search AND spree_addresses.id = spree_users.ship_address_id)",
  33. { :search => "#{params[:q].strip}%" }).
  34. limit(params[:limit] || 100)
  35. end
  36. end
  37. private
  38. # handling raise from Admin::ResourceController#destroy
  39. def user_destroy_with_orders_error
  40. invoke_callbacks(:destroy, :fails)
  41. render :status => :forbidden, :text => t(:error_user_destroy_with_orders)
  42. end
  43. # Allow different formats of json data to suit different ajax calls
  44. def json_data
  45. json_format = params[:json_format] or 'default'
  46. case json_format
  47. when 'basic'
  48. collection.map { |u| { 'id' => u.id, 'name' => u.email } }.to_json
  49. else
  50. address_fields = [:firstname, :lastname, :address1, :address2, :city, :zipcode, :phone, :state_name, :state_id, :country_id]
  51. includes = { :only => address_fields , :include => { :state => { :only => :name }, :country => { :only => :name } } }
  52. collection.to_json(:only => [:id, :email], :include =>
  53. { :bill_address => includes, :ship_address => includes })
  54. end
  55. end
  56. end
  57. end
  58. end