event.rb 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. module Ahoy
  2. class Event < ActiveRecord::Base
  3. self.table_name = "ahoy_events"
  4. belongs_to :visit
  5. belongs_to :user
  6. belongs_to :action_page
  7. scope :actions, -> { where(name: "Action") }
  8. scope :views, -> { where(name: "View") }
  9. scope :emails, -> { where("properties ->> 'actionType' = 'email'") }
  10. scope :congress_messages, -> { where("properties ->> 'actionType' = 'congress_message'") }
  11. scope :calls, -> { where("properties ->> 'actionType' = 'call'") }
  12. scope :signatures, -> { where("properties ->> 'actionType' = 'signature'") }
  13. scope :tweets, -> { where("properties ->> 'actionType' = 'tweet'") }
  14. scope :on_page, -> (id) { where(action_page_id: id) }
  15. before_save :user_opt_out
  16. before_save :anonymize_views
  17. after_create :record_civicrm
  18. def user_opt_out
  19. if user
  20. user_id = nil unless user.record_activity?
  21. end
  22. end
  23. def record_civicrm
  24. if name == "Action" && user && action_page_id
  25. user.add_civicrm_activity! action_page_id
  26. end
  27. end
  28. def anonymize_views
  29. self.user_id = nil if name == "View"
  30. end
  31. end
  32. end