spree_api 2.0.13 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (81) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +99 -1
  3. data/app/controllers/spree/api/addresses_controller.rb +5 -2
  4. data/app/controllers/spree/api/base_controller.rb +14 -21
  5. data/app/controllers/spree/api/checkouts_controller.rb +30 -27
  6. data/app/controllers/spree/api/config_controller.rb +6 -0
  7. data/app/controllers/spree/api/countries_controller.rb +7 -7
  8. data/app/controllers/spree/api/images_controller.rb +10 -8
  9. data/app/controllers/spree/api/inventory_units_controller.rb +7 -4
  10. data/app/controllers/spree/api/line_items_controller.rb +14 -14
  11. data/app/controllers/spree/api/option_types_controller.rb +14 -11
  12. data/app/controllers/spree/api/option_values_controller.rb +13 -11
  13. data/app/controllers/spree/api/orders_controller.rb +56 -53
  14. data/app/controllers/spree/api/payments_controller.rb +25 -33
  15. data/app/controllers/spree/api/product_properties_controller.rb +20 -13
  16. data/app/controllers/spree/api/products_controller.rb +11 -58
  17. data/app/controllers/spree/api/properties_controller.rb +19 -16
  18. data/app/controllers/spree/api/return_authorizations_controller.rb +30 -25
  19. data/app/controllers/spree/api/shipments_controller.rb +24 -19
  20. data/app/controllers/spree/api/states_controller.rb +9 -7
  21. data/app/controllers/spree/api/stock_items_controller.rb +9 -11
  22. data/app/controllers/spree/api/stock_locations_controller.rb +9 -7
  23. data/app/controllers/spree/api/stock_movements_controller.rb +7 -5
  24. data/app/controllers/spree/api/taxonomies_controller.rb +14 -8
  25. data/app/controllers/spree/api/taxons_controller.rb +25 -20
  26. data/app/controllers/spree/api/users_controller.rb +7 -5
  27. data/app/controllers/spree/api/variants_controller.rb +35 -33
  28. data/app/controllers/spree/api/zones_controller.rb +18 -17
  29. data/app/helpers/spree/api/api_helpers.rb +7 -6
  30. data/app/models/spree/option_value_decorator.rb +0 -4
  31. data/app/models/spree/order_decorator.rb +33 -54
  32. data/app/views/spree/api/adjustments/show.v1.rabl +2 -1
  33. data/app/views/spree/api/config/money.v1.rabl +6 -0
  34. data/app/views/spree/api/config/show.v1.rabl +2 -0
  35. data/app/views/spree/api/images/show.v1.rabl +0 -1
  36. data/app/views/spree/api/line_items/show.v1.rabl +3 -0
  37. data/app/views/spree/api/orders/index.v1.rabl +1 -1
  38. data/app/views/spree/api/orders/order.v1.rabl +6 -0
  39. data/app/views/spree/api/orders/payment.v1.rabl +1 -1
  40. data/app/views/spree/api/orders/show.v1.rabl +1 -1
  41. data/app/views/spree/api/payments/credit_over_limit.v1.rabl +1 -1
  42. data/app/views/spree/api/payments/new.v1.rabl +1 -0
  43. data/app/views/spree/api/products/index.v1.rabl +2 -2
  44. data/app/views/spree/api/products/show.v1.rabl +1 -0
  45. data/app/views/spree/api/shipments/show.v1.rabl +22 -5
  46. data/app/views/spree/api/shipping_rates/show.v1.rabl +2 -0
  47. data/app/views/spree/api/taxons/index.v1.rabl +2 -4
  48. data/app/views/spree/api/variants/index.v1.rabl +1 -1
  49. data/config/locales/en.yml +1 -3
  50. data/config/routes.rb +4 -1
  51. data/lib/spree/api/responders/rabl_template.rb +1 -1
  52. data/lib/spree/api/testing_support/helpers.rb +5 -0
  53. data/spec/controllers/spree/api/addresses_controller_spec.rb +2 -2
  54. data/spec/controllers/spree/api/checkouts_controller_spec.rb +32 -22
  55. data/spec/controllers/spree/api/config_controller_spec.rb +27 -0
  56. data/spec/controllers/spree/api/images_controller_spec.rb +2 -2
  57. data/spec/controllers/spree/api/inventory_units_controller_spec.rb +28 -24
  58. data/spec/controllers/spree/api/line_items_controller_spec.rb +25 -27
  59. data/spec/controllers/spree/api/option_types_controller_spec.rb +2 -2
  60. data/spec/controllers/spree/api/option_values_controller_spec.rb +2 -2
  61. data/spec/controllers/spree/api/orders_controller_spec.rb +130 -119
  62. data/spec/controllers/spree/api/payments_controller_spec.rb +77 -118
  63. data/spec/controllers/spree/api/products_controller_spec.rb +13 -143
  64. data/spec/controllers/spree/api/return_authorizations_controller_spec.rb +4 -19
  65. data/spec/controllers/spree/api/shipments_controller_spec.rb +3 -3
  66. data/spec/controllers/spree/api/states_controller_spec.rb +1 -1
  67. data/spec/controllers/spree/api/stock_items_controller_spec.rb +26 -62
  68. data/spec/controllers/spree/api/stock_locations_controller_spec.rb +29 -56
  69. data/spec/controllers/spree/api/stock_movements_controller_spec.rb +27 -51
  70. data/spec/controllers/spree/api/taxons_controller_spec.rb +18 -48
  71. data/spec/controllers/spree/api/users_controller_spec.rb +4 -6
  72. data/spec/controllers/spree/api/variants_controller_spec.rb +21 -38
  73. data/spec/controllers/spree/api/zones_controller_spec.rb +2 -2
  74. data/spec/models/spree/order_spec.rb +19 -111
  75. data/spec/spec_helper.rb +2 -26
  76. data/spec/support/controller_hacks.rb +1 -1
  77. data/spree_api.gemspec +1 -1
  78. metadata +19 -15
  79. data/app/models/spree/line_item_decorator.rb +0 -3
  80. data/app/views/spree/api/payments/update_forbidden.v1.rabl +0 -2
  81. data/db/migrate/20131017162334_add_index_to_user_spree_api_key.rb +0 -7
@@ -1,91 +1,94 @@
1
1
  module Spree
2
2
  module Api
3
3
  class OrdersController < Spree::Api::BaseController
4
- respond_to :json
5
4
 
6
- before_filter :find_and_authorize!, :except => [:index, :search, :create]
5
+ # Dynamically defines our stores checkout steps to ensure we check authorization on each step.
6
+ Order.checkout_steps.keys.each do |step|
7
+ define_method step do
8
+ find_order
9
+ authorize! :update, @order, params[:token]
10
+ end
11
+ end
12
+
13
+ def cancel
14
+ find_order
15
+ authorize! :update, @order, params[:token]
16
+ @order.cancel!
17
+ render :show
18
+ end
19
+
20
+ def create
21
+ authorize! :create, Order
22
+ @order = Order.build_from_api(current_api_user, order_params)
23
+ respond_with(@order, default_template: :show, status: 201)
24
+ end
25
+
26
+ def empty
27
+ find_order
28
+ @order.empty!
29
+ @order.update!
30
+ render text: nil, status: 200
31
+ end
7
32
 
8
33
  def index
9
- # should probably look at turning this into a CanCan step
10
- raise CanCan::AccessDenied unless current_api_user.has_spree_role?("admin")
34
+ authorize! :index, Order
11
35
  @orders = Order.ransack(params[:q]).result.page(params[:page]).per(params[:per_page])
12
36
  respond_with(@orders)
13
37
  end
14
38
 
15
39
  def show
40
+ find_order
41
+ method = "before_#{@order.state}"
42
+ send(method) if respond_to?(method, true)
16
43
  respond_with(@order)
17
44
  end
18
45
 
19
- def create
20
- nested_params[:line_items_attributes] = sanitize_line_items(nested_params[:line_items_attributes])
21
- @order = Order.build_from_api(current_api_user, nested_params)
22
- respond_with(@order, :default_template => :show, :status => 201)
23
- end
24
-
25
46
  def update
47
+ find_order
26
48
  # Parsing line items through as an update_attributes call in the API will result in
27
49
  # many line items for the same variant_id being created. We must be smarter about this,
28
50
  # hence the use of the update_line_items method, defined within order_decorator.rb.
29
- line_items_params = sanitize_line_items(nested_params.delete("line_items_attributes"))
30
- if @order.update_attributes(nested_params)
31
- @order.update_line_items(line_items_params)
51
+ order_params.delete("line_items_attributes")
52
+ if @order.update_attributes(order_params)
53
+ @order.update_line_items(params[:order][:line_items])
32
54
  @order.line_items.reload
33
55
  @order.update!
34
- respond_with(@order, :default_template => :show)
56
+ respond_with(@order, default_template: :show)
35
57
  else
36
58
  invalid_resource!(@order)
37
59
  end
38
60
  end
39
61
 
40
- def cancel
41
- @order.cancel!
42
- render :show
43
- end
44
-
45
- def empty
46
- @order.empty!
47
- @order.update!
48
- render :text => nil, :status => 200
49
- end
50
-
51
62
  private
52
63
 
53
- def nested_params
54
- @nested_params ||= map_nested_attributes_keys(Order, params[:order] || {})
55
- end
56
-
57
- def sanitize_line_items(line_item_attributes)
58
- return {} if line_item_attributes.blank?
59
- line_item_attributes = line_item_attributes.map do |id, attributes|
60
- attributes ||= id
64
+ def order_params
65
+ if params[:order]
66
+ params[:order][:line_items_attributes] = params[:order][:line_items]
67
+ params[:order][:ship_address_attributes] = params[:order][:ship_address] if params[:order][:ship_address]
68
+ params[:order][:bill_address_attributes] = params[:order][:bill_address] if params[:order][:bill_address]
69
+ params.require(:order).permit(permitted_order_attributes)
70
+ else
71
+ {}
72
+ end
73
+ end
61
74
 
62
- # Faux Strong-Parameters code to strip price if user isn't an admin
63
- if current_api_user.has_spree_role?("admin")
64
- [id, attributes.slice(*Spree::LineItem.attr_accessible[:api])]
75
+ def next!(options={})
76
+ if @order.valid? && @order.next
77
+ render :show, status: options[:status] || 200
65
78
  else
66
- [id, attributes.slice(*Spree::LineItem.attr_accessible[:default])]
79
+ render :could_not_transition, status: 422
67
80
  end
68
81
  end
69
- line_item_attributes = Hash[line_item_attributes].delete_if { |k,v| v.empty? }
70
- end
71
82
 
72
- def find_order(lock = false)
73
- @order = Spree::Order.lock(lock).find_by_number!(params[:id])
74
- authorize! :update, @order, params[:order_token]
75
- end
83
+ def find_order
84
+ @order = Spree::Order.find_by!(number: params[:id])
85
+ authorize! :update, @order, params[:order_token]
86
+ end
76
87
 
77
- def next!(options={})
78
- if @order.valid? && @order.next
79
- render :show, :status => options[:status] || 200
80
- else
81
- render :could_not_transition, :status => 422
88
+ def before_delivery
89
+ @order.create_proposed_shipments
82
90
  end
83
- end
84
91
 
85
- def find_and_authorize!
86
- find_order(true)
87
- authorize! :read, @order
88
- end
89
92
  end
90
93
  end
91
94
  end
@@ -1,10 +1,9 @@
1
1
  module Spree
2
2
  module Api
3
3
  class PaymentsController < Spree::Api::BaseController
4
- respond_to :json
5
4
 
6
5
  before_filter :find_order
7
- before_filter :find_payment, only: [:update, :show, :authorize, :purchase, :capture, :void, :credit]
6
+ before_filter :find_payment, only: [:show, :authorize, :purchase, :capture, :void, :credit]
8
7
 
9
8
  def index
10
9
  @payments = @order.payments.ransack(params[:q]).result.page(params[:page]).per(params[:per_page])
@@ -12,25 +11,14 @@ module Spree
12
11
  end
13
12
 
14
13
  def new
15
- @payment_methods = Spree::PaymentMethod.where(:environment => Rails.env)
14
+ @payment_methods = Spree::PaymentMethod.where(environment: Rails.env)
16
15
  respond_with(@payment_method)
17
16
  end
18
17
 
19
18
  def create
20
- @payment = @order.payments.build(params[:payment])
19
+ @payment = @order.payments.build(payment_params)
21
20
  if @payment.save
22
- respond_with(@payment, :status => 201, :default_template => :show)
23
- else
24
- invalid_resource!(@payment)
25
- end
26
- end
27
-
28
- def update
29
- authorize! params[:action], @payment
30
- if !@payment.pending?
31
- render 'update_forbidden', status: 403
32
- elsif @payment.update_attributes(params[:payment])
33
- respond_with(@payment, default_template: :show)
21
+ respond_with(@payment, status: 201, default_template: :show)
34
22
  else
35
23
  invalid_resource!(@payment)
36
24
  end
@@ -58,7 +46,7 @@ module Spree
58
46
 
59
47
  def credit
60
48
  if params[:amount].to_f > @payment.credit_allowed
61
- render 'credit_over_limit', status: 422
49
+ render 'spree/api/payments/credit_over_limit', status: 422
62
50
  else
63
51
  perform_payment_action(:credit, params[:amount])
64
52
  end
@@ -66,26 +54,30 @@ module Spree
66
54
 
67
55
  private
68
56
 
69
- def find_order
70
- @order = Order.find_by_number(params[:order_id])
71
- authorize! :read, @order
72
- end
57
+ def find_order
58
+ @order = Spree::Order.find_by(number: params[:order_id])
59
+ authorize! :read, @order
60
+ end
73
61
 
74
- def find_payment
75
- @payment = @order.payments.find(params[:id])
76
- end
62
+ def find_payment
63
+ @payment = @order.payments.find(params[:id])
64
+ end
77
65
 
78
- def perform_payment_action(action, *args)
79
- authorize! action, Payment
66
+ def perform_payment_action(action, *args)
67
+ authorize! action, Payment
80
68
 
81
- begin
82
- @payment.send("#{action}!", *args)
83
- respond_with(@payment, :default_template => :show)
84
- rescue Spree::Core::GatewayError => e
85
- @error = e.message
86
- render "spree/api/errors/gateway_error", :status => 422
69
+ begin
70
+ @payment.send("#{action}!", *args)
71
+ respond_with(@payment, :default_template => :show)
72
+ rescue Spree::Core::GatewayError => e
73
+ @error = e.message
74
+ render 'spree/api/errors/gateway_error', status: 422
75
+ end
76
+ end
77
+
78
+ def payment_params
79
+ params.require(:payment).permit(permitted_payment_attributes)
87
80
  end
88
- end
89
81
  end
90
82
  end
91
83
  end
@@ -1,13 +1,12 @@
1
1
  module Spree
2
2
  module Api
3
3
  class ProductPropertiesController < Spree::Api::BaseController
4
- respond_to :json
5
4
 
6
5
  before_filter :find_product
7
- before_filter :product_property, :only => [:show, :update, :destroy]
6
+ before_filter :product_property, only: [:show, :update, :destroy]
8
7
 
9
8
  def index
10
- @product_properties = @product.product_properties.
9
+ @product_properties = @product.product_properties.accessible_by(current_ability, :read).
11
10
  ransack(params[:q]).result.
12
11
  page(params[:page]).per(params[:per_page])
13
12
  respond_with(@product_properties)
@@ -22,44 +21,52 @@ module Spree
22
21
 
23
22
  def create
24
23
  authorize! :create, ProductProperty
25
- @product_property = @product.product_properties.new(params[:product_property])
24
+ @product_property = @product.product_properties.new(product_property_params)
26
25
  if @product_property.save
27
- respond_with(@product_property, :status => 201, :default_template => :show)
26
+ respond_with(@product_property, status: 201, default_template: :show)
28
27
  else
29
28
  invalid_resource!(@product_property)
30
29
  end
31
30
  end
32
31
 
33
32
  def update
34
- authorize! :update, ProductProperty
35
- if @product_property && @product_property.update_attributes(params[:product_property])
36
- respond_with(@product_property, :status => 200, :default_template => :show)
33
+ if @product_property
34
+ authorize! :update, @product_property
35
+ @product_property.update_attributes(product_property_params)
36
+ respond_with(@product_property, status: 200, default_template: :show)
37
37
  else
38
38
  invalid_resource!(@product_property)
39
39
  end
40
40
  end
41
41
 
42
42
  def destroy
43
- authorize! :delete, ProductProperty
44
- if(@product_property)
43
+ if @product_property
44
+ authorize! :destroy, @product_property
45
45
  @product_property.destroy
46
- respond_with(@product_property, :status => 204)
46
+ respond_with(@product_property, status: 204)
47
47
  else
48
48
  invalid_resource!(@product_property)
49
49
  end
50
50
  end
51
51
 
52
52
  private
53
+
53
54
  def find_product
54
55
  @product = super(params[:product_id])
56
+ authorize! :read, @product
55
57
  end
56
58
 
57
59
  def product_property
58
60
  if @product
59
- @product_property ||= @product.product_properties.find_by_id(params[:id])
60
- @product_property ||= @product.product_properties.joins(:property).where('spree_properties.name' => params[:id]).readonly(false).first
61
+ @product_property ||= @product.product_properties.find_by(id: params[:id])
62
+ @product_property ||= @product.product_properties.includes(:property).where(spree_properties: { name: params[:id] }).first
63
+ authorize! :read, @product_property
61
64
  end
62
65
  end
66
+
67
+ def product_property_params
68
+ params.require(:product_property).permit(permitted_product_properties_attributes)
69
+ end
63
70
  end
64
71
  end
65
72
  end
@@ -1,22 +1,20 @@
1
1
  module Spree
2
2
  module Api
3
3
  class ProductsController < Spree::Api::BaseController
4
- respond_to :json
5
4
 
6
5
  def index
7
6
  if params[:ids]
8
- @products = product_scope.where(:id => params[:ids].split(","))
7
+ @products = product_scope.where(:id => params[:ids])
9
8
  else
10
9
  @products = product_scope.ransack(params[:q]).result
11
10
  end
12
11
 
13
12
  @products = @products.page(params[:page]).per(params[:per_page])
14
-
15
- respond_with(@products)
16
13
  end
17
14
 
18
15
  def show
19
16
  @product = find_product(params[:id])
17
+ expires_in 3.minutes
20
18
  respond_with(@product)
21
19
  end
22
20
 
@@ -26,28 +24,9 @@ module Spree
26
24
  def create
27
25
  authorize! :create, Product
28
26
  params[:product][:available_on] ||= Time.now
29
-
30
- variants_attributes = params[:product].delete(:variants_attributes) || []
31
- option_type_attributes = params[:product].delete(:option_types) || []
32
- set_up_shipping_category
33
-
34
- @product = Product.new(params[:product])
35
27
  begin
28
+ @product = Product.new(product_params)
36
29
  if @product.save
37
- variants_attributes.each do |variant_attribute|
38
- variant = @product.variants.new
39
- variant.update_attributes(variant_attribute)
40
- end
41
-
42
- option_type_attributes.each do |name|
43
- option_type = OptionType.where(name: name).first_or_initialize do |option_type|
44
- option_type.presentation = name
45
- option_type.save!
46
- end
47
-
48
- @product.option_types << option_type unless @product.option_types.include?(option_type)
49
- end
50
-
51
30
  respond_with(@product, :status => 201, :default_template => :show)
52
31
  else
53
32
  invalid_resource!(@product)
@@ -56,36 +35,12 @@ module Spree
56
35
  @product.permalink = nil
57
36
  retry
58
37
  end
59
- end
38
+ end
60
39
 
61
40
  def update
62
- authorize! :update, Product
63
-
64
- variants_attributes = params[:product].delete(:variants_attributes) || []
65
- option_type_attributes = params[:product].delete(:option_types) || []
66
- set_up_shipping_category
67
-
68
41
  @product = find_product(params[:id])
69
- if @product.update_attributes(params[:product])
70
- variants_attributes.each do |variant_attribute|
71
- # update the variant if the id is present in the payload
72
- if variant_attribute['id'].present?
73
- @product.variants.find(variant_attribute['id'].to_i).update_attributes(variant_attribute)
74
- else
75
- variant = @product.variants.new
76
- variant.update_attributes(variant_attribute)
77
- end
78
- end
79
-
80
- option_type_attributes.each do |name|
81
- option_type = OptionType.where(name: name).first_or_initialize do |option_type|
82
- option_type.presentation = name
83
- option_type.save!
84
- end
85
-
86
- @product.option_types << option_type unless @product.option_types.include?(option_type)
87
- end
88
-
42
+ authorize! :update, @product
43
+ if @product.update_attributes(product_params)
89
44
  respond_with(@product, :status => 200, :default_template => :show)
90
45
  else
91
46
  invalid_resource!(@product)
@@ -93,18 +48,16 @@ module Spree
93
48
  end
94
49
 
95
50
  def destroy
96
- authorize! :delete, Product
97
51
  @product = find_product(params[:id])
98
- @product.destroy
52
+ authorize! :destroy, @product
53
+ @product.update_attribute(:deleted_at, Time.now)
54
+ @product.variants_including_master.update_all(:deleted_at => Time.now)
99
55
  respond_with(@product, :status => 204)
100
56
  end
101
57
 
102
58
  private
103
- def set_up_shipping_category
104
- if shipping_category = params[:product].delete(:shipping_category)
105
- id = ShippingCategory.find_or_create_by_name(shipping_category).id
106
- params[:product][:shipping_category_id] = id
107
- end
59
+ def product_params
60
+ params.require(:product).permit(permitted_product_attributes)
108
61
  end
109
62
  end
110
63
  end
@@ -1,12 +1,11 @@
1
1
  module Spree
2
2
  module Api
3
3
  class PropertiesController < Spree::Api::BaseController
4
- respond_to :json
5
4
 
6
- before_filter :find_property, :only => [:show, :update, :destroy]
5
+ before_filter :find_property, only: [:show, :update, :destroy]
7
6
 
8
7
  def index
9
- @properties = Spree::Property.
8
+ @properties = Spree::Property.accessible_by(current_ability, :read).
10
9
  ransack(params[:q]).result.
11
10
  page(params[:page]).per(params[:per_page])
12
11
  respond_with(@properties)
@@ -21,28 +20,29 @@ module Spree
21
20
 
22
21
  def create
23
22
  authorize! :create, Property
24
- @property = Spree::Property.new(params[:property])
23
+ @property = Spree::Property.new(property_params)
25
24
  if @property.save
26
- respond_with(@property, :status => 201, :default_template => :show)
25
+ respond_with(@property, status: 201, default_template: :show)
27
26
  else
28
27
  invalid_resource!(@property)
29
28
  end
30
29
  end
31
30
 
32
31
  def update
33
- authorize! :update, Property
34
- if @property && @property.update_attributes(params[:property])
35
- respond_with(@property, :status => 200, :default_template => :show)
32
+ if @property
33
+ authorize! :update, @property
34
+ @property.update_attributes(property_params)
35
+ respond_with(@property, status: 200, default_template: :show)
36
36
  else
37
37
  invalid_resource!(@property)
38
38
  end
39
39
  end
40
40
 
41
41
  def destroy
42
- authorize! :delete, Property
43
- if(@property)
42
+ if @property
43
+ authorize! :destroy, @property
44
44
  @property.destroy
45
- respond_with(@property, :status => 204)
45
+ respond_with(@property, status: 204)
46
46
  else
47
47
  invalid_resource!(@property)
48
48
  end
@@ -50,12 +50,15 @@ module Spree
50
50
 
51
51
  private
52
52
 
53
- def find_property
54
- @property = Spree::Property.find(params[:id])
55
- rescue ActiveRecord::RecordNotFound
56
- @property = Spree::Property.find_by_name!(params[:id])
57
- end
53
+ def find_property
54
+ @property = Spree::Property.accessible_by(current_ability, :read).find(params[:id])
55
+ rescue ActiveRecord::RecordNotFound
56
+ @property = Spree::Property.accessible_by(current_ability, :read).find_by!(name: params[:id])
57
+ end
58
58
 
59
+ def property_params
60
+ params.require(:property).permit(permitted_property_attributes)
61
+ end
59
62
  end
60
63
  end
61
64
  end
@@ -1,46 +1,50 @@
1
1
  module Spree
2
2
  module Api
3
3
  class ReturnAuthorizationsController < Spree::Api::BaseController
4
- respond_to :json
5
4
 
6
- before_filter :authorize_admin!
5
+ def create
6
+ authorize! :create, ReturnAuthorization
7
+ @return_authorization = order.return_authorizations.build(return_authorization_params)
8
+ if @return_authorization.save
9
+ respond_with(@return_authorization, status: 201, default_template: :show)
10
+ else
11
+ invalid_resource!(@return_authorization)
12
+ end
13
+ end
14
+
15
+ def destroy
16
+ @return_authorization = order.return_authorizations.accessible_by(current_ability, :destroy).find(params[:id])
17
+ @return_authorization.destroy
18
+ respond_with(@return_authorization, status: 204)
19
+ end
7
20
 
8
21
  def index
9
- @return_authorizations = order.return_authorizations.
22
+ authorize! :admin, ReturnAuthorization
23
+ @return_authorizations = order.return_authorizations.accessible_by(current_ability, :read).
10
24
  ransack(params[:q]).result.
11
25
  page(params[:page]).per(params[:per_page])
12
26
  respond_with(@return_authorizations)
13
27
  end
14
28
 
15
- def show
16
- @return_authorization = order.return_authorizations.find(params[:id])
17
- respond_with(@return_authorization)
29
+ def new
30
+ authorize! :admin, ReturnAuthorization
18
31
  end
19
32
 
20
- def create
21
- @return_authorization = order.return_authorizations.build(params[:return_authorization], :as => :api)
22
- if @return_authorization.save
23
- respond_with(@return_authorization, :status => 201, :default_template => :show)
24
- else
25
- invalid_resource!(@return_authorization)
26
- end
33
+ def show
34
+ authorize! :admin, ReturnAuthorization
35
+ @return_authorization = order.return_authorizations.accessible_by(current_ability, :read).find(params[:id])
36
+ respond_with(@return_authorization)
27
37
  end
28
38
 
29
39
  def update
30
- @return_authorization = order.return_authorizations.find(params[:id])
31
- if @return_authorization.update_attributes(params[:return_authorization])
32
- respond_with(@return_authorization, :default_template => :show)
40
+ @return_authorization = order.return_authorizations.accessible_by(current_ability, :update).find(params[:id])
41
+ if @return_authorization.update_attributes(return_authorization_params)
42
+ respond_with(@return_authorization, default_template: :show)
33
43
  else
34
44
  invalid_resource!(@return_authorization)
35
45
  end
36
46
  end
37
47
 
38
- def destroy
39
- @return_authorization = order.return_authorizations.find(params[:id])
40
- @return_authorization.destroy
41
- respond_with(@return_authorization, :status => 204)
42
- end
43
-
44
48
  def add
45
49
  @return_authorization = order.return_authorizations.accessible_by(current_ability, :update).find(params[:id])
46
50
  @return_authorization.add_variant params[:variant_id].to_i, params[:quantity].to_i
@@ -72,11 +76,12 @@ module Spree
72
76
  private
73
77
 
74
78
  def order
75
- @order ||= Order.find_by_number!(params[:order_id])
79
+ @order ||= Spree::Order.find_by!(number: params[:order_id])
80
+ authorize! :read, @order
76
81
  end
77
82
 
78
- def authorize_admin!
79
- authorize! :manage, Spree::ReturnAuthorization
83
+ def return_authorization_params
84
+ params.require(:return_authorization).permit(permitted_return_authorization_attributes)
80
85
  end
81
86
  end
82
87
  end