Module: Fera::BelongsToCustomer

Extended by:
ActiveSupport::Concern
Included in:
Media, Order, Review, Submission
Defined in:
lib/fera/models/concerns/belongs_to_customer.rb

Instance Method Summary collapse

Instance Method Details

#customerObject



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/fera/models/concerns/belongs_to_customer.rb', line 53

def customer
  if @customer.present?
    @customer
  elsif attributes.key?('customer') && attributes['customer'].present?
    Customer.new(attributes['customer'], true)
  elsif attributes.key?('customer_id') && customer_id.present?
    Customer.find(customer_id)
  elsif attributes.key?('external_customer_id') && external_customer_id.present?
    Customer.find(external_customer_id)
  end
end

#customer=(customer) ⇒ Object



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
# File 'lib/fera/models/concerns/belongs_to_customer.rb', line 11

def customer=(customer)
  if customer.is_a?(Customer)
    @customer                               = customer
    self.attributes['customer_id']          = customer.id
    self.attributes['external_customer_id'] = customer.try(:external_id)
    self.attributes.delete('customer')
  elsif customer.is_a?(Hash)
    customer_id = customer.with_indifferent_access[:id]

    if customer.with_indifferent_access.key?(:id) # Hash
      if customer_id =~ /^fcus_/
        self.attributes['customer_id'] = customer_id
      else
        self.attributes['external_customer_id'] = customer_id
      end
    end

    if customer.with_indifferent_access.key?(:external_id) # Hash
      self.attributes['external_customer_id'] = customer.with_indifferent_access[:external_id]
    end

    @customer = Customer.new(customer, customer_id.present?)
    self.attributes.delete('customer')
  end
end

#customer_id=(new_id) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/fera/models/concerns/belongs_to_customer.rb', line 37

def customer_id=(new_id)
  if @customer.present?
    @customer.id = new_id
  end

  self.attributes['customer_id'] = new_id
end

#external_customer_id=(new_external_id) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/fera/models/concerns/belongs_to_customer.rb', line 45

def external_customer_id=(new_external_id)
  if @customer.present?
    @customer.external_id = new_external_id
  end

  self.attributes['external_customer_id'] = new_external_id
end