/api/app/controllers/spree/api/variants_controller.rb

https://github.com/joshnuss/spree-1 · Ruby · 72 lines · 63 code · 9 blank · 0 comment · 6 complexity · 49205f4278db62b77ec0986db903eeb3 MD5 · raw file

  1. module Spree
  2. module Api
  3. class VariantsController < Spree::Api::BaseController
  4. before_filter :product
  5. def create
  6. authorize! :create, Variant
  7. @variant = scope.new(params[:variant])
  8. if @variant.save
  9. respond_with(@variant, :status => 201, :default_template => :show)
  10. else
  11. invalid_resource!(@variant)
  12. end
  13. end
  14. def destroy
  15. @variant = scope.accessible_by(current_ability, :destroy).find(params[:id])
  16. @variant.destroy
  17. respond_with(@variant, :status => 204)
  18. end
  19. def index
  20. @variants = scope.includes(:option_values).ransack(params[:q]).result.
  21. page(params[:page]).per(params[:per_page])
  22. respond_with(@variants)
  23. end
  24. def new
  25. end
  26. def show
  27. @variant = scope.includes(:option_values).find(params[:id])
  28. respond_with(@variant)
  29. end
  30. def update
  31. @variant = scope.accessible_by(current_ability, :update).find(params[:id])
  32. if @variant.update_attributes(params[:variant])
  33. respond_with(@variant, :status => 200, :default_template => :show)
  34. else
  35. invalid_resource!(@product)
  36. end
  37. end
  38. private
  39. def product
  40. @product ||= Spree::Product.accessible_by(current_ability, :read).find_by_permalink(params[:product_id]) if params[:product_id]
  41. end
  42. def scope
  43. if @product
  44. unless current_api_user.has_spree_role?("admin") || params[:show_deleted]
  45. variants = @product.variants_including_master.accessible_by(current_ability, :read)
  46. else
  47. variants = @product.variants_including_master_and_deleted.accessible_by(current_ability, :read)
  48. end
  49. else
  50. variants = Variant.accessible_by(current_ability, :read)
  51. if current_api_user.has_spree_role?("admin")
  52. unless params[:show_deleted]
  53. variants = Variant.accessible_by(current_ability, :read).active
  54. end
  55. else
  56. variants = variants.active
  57. end
  58. end
  59. variants
  60. end
  61. end
  62. end
  63. end