PageRenderTime 1321ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/app/controllers/spree/admin/wholesalers_controller.rb

https://github.com/citrus/spree_wholesale
Ruby | 82 lines | 68 code | 14 blank | 0 comment | 4 complexity | 80b1437a51f01602834c6164be59628f MD5 | raw file
  1. class Spree::Admin::WholesalersController < Spree::Admin::ResourceController
  2. respond_to :html, :xml
  3. before_filter :approval_setup, :only => [ :approve, :reject ]
  4. def index
  5. end
  6. def show
  7. @wholesaler = Spree::Wholesaler.find(params[:id])
  8. respond_with(@wholesaler)
  9. end
  10. def new
  11. @wholesaler = Spree::Wholesaler.new
  12. @wholesaler.build_user
  13. @wholesaler.bill_address = Spree::Address.default
  14. @wholesaler.ship_address = Spree::Address.default
  15. respond_with(@wholesaler)
  16. end
  17. def create
  18. @wholesaler = Spree::Wholesaler.new(params[:wholesaler])
  19. if @wholesaler.save
  20. flash[:notice] = I18n.t('spree.admin.wholesaler.success')
  21. redirect_to spree.admin_wholesalers_path
  22. else
  23. flash[:error] = I18n.t('spree.admin.wholesaler.failed')
  24. render :action => "new"
  25. end
  26. end
  27. def edit
  28. @wholesaler = Spree::Wholesaler.find(params[:id])
  29. respond_with(@wholesaler)
  30. end
  31. def update
  32. @wholesaler = Spree::Wholesaler.find(params[:id])
  33. if @wholesaler.update_attributes(params[:wholesaler])
  34. flash[:notice] = I18n.t('spree.admin.wholesaler.update_success')
  35. else
  36. flash[:error] = I18n.t('spree.admin.wholesaler.update_failed')
  37. end
  38. respond_with(@wholesaler)
  39. end
  40. def destroy
  41. @wholesaler = Spree::Wholesaler.find(params[:id])
  42. @wholesaler.destroy
  43. flash[:notice] = I18n.t('spree.admin.wholesaler.destroy_success')
  44. respond_with(@wholesaler)
  45. end
  46. def approve
  47. return redirect_to request.referer, :flash => { :error => "Wholesaler is already active." } if @wholesaler.active?
  48. @wholesaler.activate!
  49. redirect_to request.referer, :flash => { :notice => "Wholesaler was successfully approved." }
  50. end
  51. def reject
  52. return redirect_to request.referer, :flash => { :error => "Wholesaler is already rejected." } unless @wholesaler.active?
  53. @wholesaler.deactivate!
  54. redirect_to request.referer, :flash => { :notice => "Wholesaler was successfully rejected." }
  55. end
  56. private
  57. def approval_setup
  58. @wholesaler = Spree::Wholesaler.find(params[:id])
  59. @role = Spree::Role.find_or_create_by_name("wholesaler")
  60. end
  61. def collection
  62. return @collection if @collection.present?
  63. params[:search] ||= {}
  64. params[:search][:meta_sort] ||= "company.asc"
  65. @search = Spree::Wholesaler.ransack(params[:q])
  66. @collection = @search.result.page(params[:page]).per(Spree::Config[:admin_products_per_page])
  67. end
  68. end