Module: CheddargetterClientRails

Included in:
ActiveRecord::Base
Defined in:
lib/cheddargetter_client_rails.rb,
lib/cheddargetter_client_rails/subscription.rb

Defined Under Namespace

Modules: ClassMethods Classes: Subscription

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/cheddargetter_client_rails.rb', line 8

def self.included(base)
  attr_accessor :has_subscription_options
  base.extend ClassMethods
  
  def subscription
    @subscription ||= CheddargetterClientRails::Subscription.new
  end
  
  def subscription=(value)
    @subscription = value
  end
  
  def validate_subscription
     supplement_subscription_fields
     
     if !skip_cheddargetter && new_record? && !subscription.valid?
       errors.add(:subscription, 'problem')
     end
  end
  
  def supplement_subscription_fields
    if subscription.is_a?(ActiveSupport::HashWithIndifferentAccess)
      self.subscription = CheddargetterClientRails::Subscription.new(subscription)
    end

    self.class.shared_columns.each do |subscription_column, user_attribute|
      if(subscription_column == :planCode && user_attribute.is_a?(String)) #user can specify planCode as a string
        subscription.send(subscription_column.to_s + '=', user_attribute)
      else
        subscription.send(subscription_column.to_s + '=', send(user_attribute))
      end
    end
  end
  
  def create_subscription
    raise ArgumentError, 'Customer code is not set on record.' if !customer_code_column_value && !subscription.customerCode
    subscription.customerCode = customer_code_column_value if !subscription.customerCode
    subscription.create unless skip_cheddargetter
  end
      
  def current_subscription
    @current_subscription ||= CheddargetterClientRails::Subscription.get(customer_code_column_value) if customer_code_column_value
  end
  
  def destroy_subscription
    current_subscription.try(:destroy)
  end

  def customer_code_column_value
    if self.class.send(:customer_code_column)
      value = send(self.class.send(:customer_code_column))
      value.to_s if value.try(:to_s).present?
    end 
  end
  
  def build_subscription(attributes_hash)
    # set attributes from current cheddargetter subscription, then
    # replaces any values with supplied data
    new_subscription = CheddargetterClientRails::Subscription.new
    if old_subscription = current_subscription
      old_subscription.instance_variables_hash.each do |key, value|
        new_subscription.send(key.to_s + '=', value)
      end
    end
    
    attributes_hash.each do |key, value|
      new_subscription.send(key.to_s + '=', value)
    end
    
    self.subscription = new_subscription
    new_subscription
  end
  
  def save_subscription(attributes_hash)
    build_subscription(attributes_hash)
    subscription.save
  end
  
  def update_subscription
    if !new_record?
      subscription.customerCode = customer_code_column_value if subscription.customerCode.blank? and customer_code_column_value.present?
      if shared_attributes_have_changed? || subscription.fields_present?
        subscription.update
      end
    end
  end
  
  def shared_attributes_have_changed?
    self.class.shared_columns.collect do |cgkey, column|
      self.send(column.to_s + '_changed?')
    end.include?(true)      
  end
end

Instance Method Details

#build_subscription(attributes_hash) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/cheddargetter_client_rails.rb', line 63

def build_subscription(attributes_hash)
  # set attributes from current cheddargetter subscription, then
  # replaces any values with supplied data
  new_subscription = CheddargetterClientRails::Subscription.new
  if old_subscription = current_subscription
    old_subscription.instance_variables_hash.each do |key, value|
      new_subscription.send(key.to_s + '=', value)
    end
  end
  
  attributes_hash.each do |key, value|
    new_subscription.send(key.to_s + '=', value)
  end
  
  self.subscription = new_subscription
  new_subscription
end

#create_subscriptionObject

Raises:

  • (ArgumentError)


42
43
44
45
46
# File 'lib/cheddargetter_client_rails.rb', line 42

def create_subscription
  raise ArgumentError, 'Customer code is not set on record.' if !customer_code_column_value && !subscription.customerCode
  subscription.customerCode = customer_code_column_value if !subscription.customerCode
  subscription.create unless skip_cheddargetter
end

#current_subscriptionObject



48
49
50
# File 'lib/cheddargetter_client_rails.rb', line 48

def current_subscription
  @current_subscription ||= CheddargetterClientRails::Subscription.get(customer_code_column_value) if customer_code_column_value
end

#customer_code_column_valueObject



56
57
58
59
60
61
# File 'lib/cheddargetter_client_rails.rb', line 56

def customer_code_column_value
  if self.class.send(:customer_code_column)
    value = send(self.class.send(:customer_code_column))
    value.to_s if value.try(:to_s).present?
  end 
end

#destroy_subscriptionObject



52
53
54
# File 'lib/cheddargetter_client_rails.rb', line 52

def destroy_subscription
  current_subscription.try(:destroy)
end

#save_subscription(attributes_hash) ⇒ Object



81
82
83
84
# File 'lib/cheddargetter_client_rails.rb', line 81

def save_subscription(attributes_hash)
  build_subscription(attributes_hash)
  subscription.save
end

#shared_attributes_have_changed?Boolean

Returns:

  • (Boolean)


95
96
97
98
99
# File 'lib/cheddargetter_client_rails.rb', line 95

def shared_attributes_have_changed?
  self.class.shared_columns.collect do |cgkey, column|
    self.send(column.to_s + '_changed?')
  end.include?(true)      
end

#subscriptionObject



12
13
14
# File 'lib/cheddargetter_client_rails.rb', line 12

def subscription
  @subscription ||= CheddargetterClientRails::Subscription.new
end

#subscription=(value) ⇒ Object



16
17
18
# File 'lib/cheddargetter_client_rails.rb', line 16

def subscription=(value)
  @subscription = value
end

#supplement_subscription_fieldsObject



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/cheddargetter_client_rails.rb', line 28

def supplement_subscription_fields
  if subscription.is_a?(ActiveSupport::HashWithIndifferentAccess)
    self.subscription = CheddargetterClientRails::Subscription.new(subscription)
  end

  self.class.shared_columns.each do |subscription_column, user_attribute|
    if(subscription_column == :planCode && user_attribute.is_a?(String)) #user can specify planCode as a string
      subscription.send(subscription_column.to_s + '=', user_attribute)
    else
      subscription.send(subscription_column.to_s + '=', send(user_attribute))
    end
  end
end

#update_subscriptionObject



86
87
88
89
90
91
92
93
# File 'lib/cheddargetter_client_rails.rb', line 86

def update_subscription
  if !new_record?
    subscription.customerCode = customer_code_column_value if subscription.customerCode.blank? and customer_code_column_value.present?
    if shared_attributes_have_changed? || subscription.fields_present?
      subscription.update
    end
  end
end

#validate_subscriptionObject



20
21
22
23
24
25
26
# File 'lib/cheddargetter_client_rails.rb', line 20

def validate_subscription
   supplement_subscription_fields
   
   if !skip_cheddargetter && new_record? && !subscription.valid?
     errors.add(:subscription, 'problem')
   end
end