Module: Fera::BelongsToProduct

Extended by:
ActiveSupport::Concern
Included in:
Media, Rating, Review
Defined in:
lib/fera/models/concerns/belongs_to_product.rb

Instance Method Summary collapse

Instance Method Details

#productObject



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/fera/models/concerns/belongs_to_product.rb', line 32

def product
  if @product.present?
    @product
  elsif attributes.key?('product') && attributes['product'].present?
    Product.new(attributes['product'], true)
  elsif attributes.key?('product_id') && product_id.present?
    Product.find(product_id)
  elsif attributes.key?('external_product_id') && external_product_id.present?
    Product.find(external_product_id)
  end
end

#product=(product) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/fera/models/concerns/belongs_to_product.rb', line 11

def product=(product)
  product_id          = if product.is_a?(Product)
                          product.id
                        else
                          product.try(:with_indifferent_access).try(:[], :id)
                        end
  external_product_id = if product.is_a?(Product)
                          product.external_id
                        else
                          product.try(:with_indifferent_access).try(:[], :external_id)
                        end
  @product            = if product.is_a?(Product)
                          product
                        else
                          Product.new(product, product_id.present?)
                        end
  self.attributes['product_id'] = product_id
  self.attributes['external_product_id'] = external_product_id
  self.attributes.delete('product')
end