Sha256: 1e0f54ece1f20e0fac92cc79ade9390819913f508bfa712c928324e3f4bd523b

Contents?: true

Size: 1.81 KB

Versions: 8

Compression:

Stored size: 1.81 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!
      return true unless contact.blank?

      # when creating a contact we should assume we're primary
      self.options.primary = true
      build_contact(create_contact_parameters)
      save
    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

8 entries across 8 versions & 1 rubygems

Version Path
e9_crm-0.1.34 lib/e9_crm/model.rb
e9_crm-0.1.33 lib/e9_crm/model.rb
e9_crm-0.1.32 lib/e9_crm/model.rb
e9_crm-0.1.31 lib/e9_crm/model.rb
e9_crm-0.1.30 lib/e9_crm/model.rb
e9_crm-0.1.29 lib/e9_crm/model.rb
e9_crm-0.1.28 lib/e9_crm/model.rb
e9_crm-0.1.27 lib/e9_crm/model.rb