Class: EventRule

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/event_rule.rb

Instance Method Summary collapse

Instance Method Details

#add_tag(lead, match_data) ⇒ Object



70
71
72
73
74
# File 'app/models/event_rule.rb', line 70

def add_tag(lead, match_data)
  lead.tag_list << tag
  save_lead_without_versioning_or_observers(lead)
  lead.versions.create! :event => "Rule for #{human_event_label}: Added tag '#{tag}'"
end

#add_to_mailing_list_group(lead, match_data) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'app/models/event_rule.rb', line 82

def add_to_mailing_list_group(lead, match_data)
  # Send a Cloudfuji event which results in a call to the Mailchimp API,
  # and adds InterestGroup to lead.
  event = {
    :category => :mailing_list_group,
    :name     => :added,
    :data     => {
      :email                 => lead.email,
      :customer_ido_id       => lead.ido_id,
      :user_ido_id           => user_ido_id,
      :mailing_list          => mailing_list,
      :mailing_list_grouping => mailing_list_grouping,
      :mailing_list_group    => mailing_list_group
    }
  }

  puts "Publishing Cloudfuji Event: #{event.inspect}"
  ::Cloudfuji::Event.publish(event)
end

#change_lead_score(lead, match_data) ⇒ Object

Actions




45
46
47
48
49
50
51
# File 'app/models/event_rule.rb', line 45

def change_lead_score(lead, match_data)
  lead.without_versioning do
    lead.update_attribute :score, lead.score + change_score_by
  end
  # Add history event to lead, to record change of score
  lead.versions.create! :event => "Rule for #{human_event_label}: Score changed by #{change_score_by} points. (New total: #{lead.score})"
end

#process(lead, params = {}) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'app/models/event_rule.rb', line 20

def process(lead, params = {})
  # How many times this rule has been applied to a given Lead
  count = LeadEventRuleCount.find_by_lead_id_and_event_rule_id(lead, self) ||
          LeadEventRuleCount.new(:lead => lead, :event_rule => self)
  # Don't apply this rule more than limit_per_lead, if set
  unless limit_per_lead.present? && count.count > limit_per_lead
    # If :match is present, only apply the rule if data matches string
    if match.blank? || event_matches?(params)
      if (app_id.blank? && page_name.blank?) || page_and_app_matches?(params)
        # Run the action method if defined
        if respond_to?(action)
          send(action, lead, params)
          # Increment and save count of rule/lead applications
          count.count += 1
          count.save
        else
          raise "Do not know how to process '#{action}' action."
        end
      end
    end
  end
end

#remove_tag(lead, match_data) ⇒ Object



76
77
78
79
80
# File 'app/models/event_rule.rb', line 76

def remove_tag(lead, match_data)
  lead.tag_list -= [tag]
  save_lead_without_versioning_or_observers(lead)
  lead.versions.create! :event => "Rule for #{human_event_label}: Removed tag '#{tag}'"
end

#send_notification(lead, match_data) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'app/models/event_rule.rb', line 53

def send_notification(lead, match_data)
  if ::Cloudfuji::Platform.on_cloudfuji?
    # Fire a Cloudfuji event
    message = case event_category
    when 'cloudfuji_event_received'
      "Cloudfuji event was received - '#{cloudfuji_event}'"
    when 'lead_attribute_changed'
      "Lead \"#{lead.full_name}\" was updated - #{lead_attribute} was changed from '#{match_data['old_value']}' to '#{match_data['new_value']}'."
    end

    User.all.each do |user|
      user.notify(lead.full_name, message, "crm") if !user.ido_id.nil?
    end

  end
end