Module: SolidusProductAssembly::Spree::ProductDecorator

Defined in:
app/decorators/models/solidus_product_assembly/spree/product_decorator.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.prepended(base) ⇒ Object



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
# File 'app/decorators/models/solidus_product_assembly/spree/product_decorator.rb', line 6

def self.prepended(base)
  base.class_eval do
    has_and_belongs_to_many :parts, class_name: "Spree::Variant",
                                    join_table: "spree_assemblies_parts",
                                    foreign_key: "assembly_id", association_foreign_key: "part_id"

    has_many :assemblies_parts, class_name: "Spree::AssembliesPart",
                                foreign_key: "assembly_id"

    scope :individual_saled, -> { where(individual_sale: true) }

    if defined?(SolidusGlobalize)
      scope :search_can_be_part, ->(query){
        not_deleted.available.joins(:master)
          .joins(:translations)
          .where(
            ::Spree::Product::Translation.arel_table["name"].matches(query)
              .or(::Spree::Variant.arel_table["sku"].matches(query))
          )
          .where(can_be_part: true)
          .limit(30)
      }
    else
      scope :search_can_be_part, ->(query){
        not_deleted.available.joins(:master)
          .where(arel_table["name"].matches(query).or(::Spree::Variant.arel_table["sku"].matches(query)))
          .where(can_be_part: true)
          .limit(30)
      }
    end

    validate :assembly_cannot_be_part, if: :assembly?
  end
end

Instance Method Details

#add_part(variant, count = 1) ⇒ Object



41
42
43
# File 'app/decorators/models/solidus_product_assembly/spree/product_decorator.rb', line 41

def add_part(variant, count = 1)
  set_part_count(variant, count_of(variant) + count)
end

#assembly?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'app/decorators/models/solidus_product_assembly/spree/product_decorator.rb', line 60

def assembly?
  parts.present?
end

#assembly_cannot_be_partObject



70
71
72
# File 'app/decorators/models/solidus_product_assembly/spree/product_decorator.rb', line 70

def assembly_cannot_be_part
  errors.add(:can_be_part, I18n.t('spree.assembly_cannot_be_part')) if can_be_part
end

#count_of(variant) ⇒ Object



64
65
66
67
68
# File 'app/decorators/models/solidus_product_assembly/spree/product_decorator.rb', line 64

def count_of(variant)
  ap = assemblies_part(variant)
  # This checks persisted because the default count is 1
  ap.persisted? ? ap.count : 0
end

#remove_part(variant) ⇒ Object



45
46
47
# File 'app/decorators/models/solidus_product_assembly/spree/product_decorator.rb', line 45

def remove_part(variant)
  set_part_count(variant, 0)
end

#set_part_count(variant, count) ⇒ Object



49
50
51
52
53
54
55
56
57
58
# File 'app/decorators/models/solidus_product_assembly/spree/product_decorator.rb', line 49

def set_part_count(variant, count)
  ap = assemblies_part(variant)
  if count > 0
    ap.count = count
    ap.save
  else
    ap.destroy
  end
  reload
end