Sha256: f03bb141f14d6888cd5e4d0a2eed4406bed87666ab024902aeb9915b94555d01

Contents?: true

Size: 1.79 KB

Versions: 17

Compression:

Stored size: 1.79 KB

Contents

module E9Crm
  ##
  # The User model associated with a Contact.  The idea is that a Contact can be 
  # associated to many different logins/users (e.g. bob@home.com, bob@work.com).
  #
  # In the default setup the included model is treated as simply an email, and given a
  # "type" such as Personal, Work, etc.
  #
  # One and only one of these models should be considered "primary".  The designation of
  # which is handled largely by the Contact class.
  #
  module Model
    extend ActiveSupport::Concern
    include E9Rails::ActiveRecord::InheritableOptions

    included do
      belongs_to :contact, :inverse_of => :users

      has_many :tracking_cookies, :inverse_of => :user
      has_many :page_views, :through => :tracking_cookies

      self.options_parameters = [:type, :primary]

      scope :primary, lambda { where(%Q{#{table_name}.options REGEXP "primary: [\\"']?true"}) }

      before_save :create_contact_if_missing!
      after_destroy :cleanup_contact
    end

    ##
    # Is this the Contact's primary model?
    #
    def primary?
      ["true", true].member? options.primary
    end

    def create_contact_if_missing!
      if contact.blank?
        # when creating a contact we should assume we're primary
        self.options.primary = true
        create_contact(create_contact_parameters)
      end
    end

    protected

      def create_contact_parameters
        { :first_name => self.first_name, :last_name => self.last_name }
      end

      def cleanup_contact
        # NOTE this is called after_delete, reload the contact to clear the deleted
        #      user from the contact's #users
        contact.reload.users.reset_primary! if self.primary? && contact.present?
      end

    module ClassMethods
      def email_types
        MenuOption.fetch_values('Email')
      end
    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
e9_crm-0.1.26 lib/e9_crm/model.rb
e9_crm-0.1.25 lib/e9_crm/model.rb
e9_crm-0.1.24 lib/e9_crm/model.rb
e9_crm-0.1.23 lib/e9_crm/model.rb
e9_crm-0.1.22 lib/e9_crm/model.rb
e9_crm-0.1.21 lib/e9_crm/model.rb
e9_crm-0.1.20 lib/e9_crm/model.rb
e9_crm-0.1.19 lib/e9_crm/model.rb
e9_crm-0.1.18 lib/e9_crm/model.rb
e9_crm-0.1.17 lib/e9_crm/model.rb
e9_crm-0.1.16 lib/e9_crm/model.rb
e9_crm-0.1.14 lib/e9_crm/model.rb
e9_crm-0.1.13 lib/e9_crm/model.rb
e9_crm-0.1.12 lib/e9_crm/model.rb
e9_crm-0.1.11 lib/e9_crm/model.rb
e9_crm-0.1.10 lib/e9_crm/model.rb
e9_crm-0.1.8 lib/e9_crm/model.rb