diff --git a/.rubocop_manual_todo.yml b/.rubocop_manual_todo.yml index 8587ac7850..02a6c340f9 100644 --- a/.rubocop_manual_todo.yml +++ b/.rubocop_manual_todo.yml @@ -88,7 +88,6 @@ Layout/LineLength: - app/models/spree/tax_rate_decorator.rb - app/models/spree/taxon_decorator.rb - app/models/spree/user.rb - - app/models/spree/variant_decorator.rb - app/models/subscription.rb - app/models/variant_override.rb - app/models/variant_override_set.rb diff --git a/app/models/spree/variant_decorator.rb b/app/models/spree/variant_decorator.rb index 566394d946..c23ab202cd 100644 --- a/app/models/spree/variant_decorator.rb +++ b/app/models/spree/variant_decorator.rb @@ -16,7 +16,8 @@ Spree::Variant.class_eval do has_many :variant_overrides has_many :inventory_items - attr_accessible :unit_value, :unit_description, :images_attributes, :display_as, :display_name, :import_date + attr_accessible :unit_value, :unit_description, :images_attributes, + :display_as, :display_name, :import_date accepts_nested_attributes_for :images validates :unit_value, presence: true, if: ->(variant) { @@ -53,13 +54,23 @@ Spree::Variant.class_eval do } scope :visible_for, lambda { |enterprise| - joins(:inventory_items).where('inventory_items.enterprise_id = (?) AND inventory_items.visible = (?)', enterprise, true) + joins(:inventory_items). + where( + 'inventory_items.enterprise_id = (?) AND inventory_items.visible = (?)', + enterprise, + true + ) } scope :not_hidden_for, lambda { |enterprise| return where("1=0") if enterprise.blank? - joins("LEFT OUTER JOIN (SELECT * from inventory_items WHERE enterprise_id = #{sanitize enterprise.andand.id}) AS o_inventory_items ON o_inventory_items.variant_id = spree_variants.id") + joins(" + LEFT OUTER JOIN (SELECT * + FROM inventory_items + WHERE enterprise_id = #{sanitize enterprise.andand.id}) + AS o_inventory_items + ON o_inventory_items.variant_id = spree_variants.id") .where("o_inventory_items.id IS NULL OR o_inventory_items.visible = (?)", true) } @@ -68,7 +79,8 @@ Spree::Variant.class_eval do scope :stockable_by, lambda { |enterprise| return where("1=0") if enterprise.blank? - joins(:product).where(spree_products: { id: Spree::Product.stockable_by(enterprise).pluck(:id) }) + joins(:product). + where(spree_products: { id: Spree::Product.stockable_by(enterprise).pluck(:id) }) } # Define sope as class method to allow chaining with other scopes filtering id. @@ -85,7 +97,19 @@ Spree::Variant.class_eval do ] end - # We override in_stock? to avoid depending on the non-overridable method Spree::Stock::Quantifier.can_supply? + def self.active(currency = nil) + # "where(id:" is necessary so that the returned relation has no includes + # The relation without includes will not be readonly and allow updates on it + where("spree_variants.id in (?)", joins(:prices). + where(deleted_at: nil). + where('spree_prices.currency' => + currency || Spree::Config[:currency]). + where('spree_prices.amount IS NOT NULL'). + select("spree_variants.id")) + end + + # We override in_stock? to avoid depending + # on the non-overridable method Spree::Stock::Quantifier.can_supply? # VariantStock implements can_supply? itself which depends on overridable methods def in_stock?(quantity = 1) can_supply?(quantity)