forked from solidusio/solidus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stock_items_controller.rb
69 lines (58 loc) · 2.53 KB
/
stock_items_controller.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# frozen_string_literal: true
module Spree
module Admin
class StockItemsController < ResourceController
class_attribute :variant_display_attributes
self.variant_display_attributes = [
{ translation_key: :sku, attr_name: :sku },
{ translation_key: :name, attr_name: :name }
]
update.before :determine_backorderable
before_action :load_product, :load_stock_management_data, only: :index
private
def build_resource
variant = Spree::Variant.accessible_by(current_ability, :show).find(params[:variant_id])
stock_location = Spree::StockLocation.accessible_by(current_ability, :show).find(params[:stock_location_id])
stock_location.stock_movements.build(stock_movement_params).tap do |stock_movement|
stock_movement.originator = try_spree_current_user
stock_movement.stock_item = stock_location.set_up_stock_item(variant)
end
end
def permitted_resource_params
{}
end
def stock_movement_params
params.require(:stock_movement).permit(permitted_stock_movement_attributes)
end
def determine_backorderable
@stock_item.backorderable = params[:stock_item].present? && params[:stock_item][:backorderable].present?
end
def load_product
@product = Spree::Product.accessible_by(current_ability, :show).friendly.find(params[:product_slug]) if params[:product_slug]
end
def load_stock_management_data
@stock_locations = Spree::StockLocation.accessible_by(current_ability)
@stock_item_stock_locations = params[:stock_location_id].present? ? @stock_locations.where(id: params[:stock_location_id]) : @stock_locations
@variant_display_attributes = self.class.variant_display_attributes
@variants = Spree::Config.variant_search_class.new(params[:variant_search_term], scope: variant_scope).results
@variants = @variants.includes(:images, stock_items: :stock_location, product: :variant_images)
@variants = @variants.includes(option_values: :option_type)
@variants = @variants.order(id: :desc).page(params[:page]).per(params[:per_page] || Spree::Config[:orders_per_page])
end
def variant_scope
scope = Spree::Variant.accessible_by(current_ability)
if @product
scope = scope.where(
product: @product,
is_master: [email protected]_variants?
)
end
scope = scope.order(:sku)
scope
end
def collection
[]
end
end
end
end