Sha256: 68dec71f475a69d2bf67c341bcf2dda0bb197825f24717f20770d62af7339865

Contents?: true

Size: 1.31 KB

Versions: 10

Compression:

Stored size: 1.31 KB

Contents

module Gaku
  class Contact < ActiveRecord::Base
    belongs_to :contact_type
    belongs_to :contactable, polymorphic: true, counter_cache: true

    validates :data, :contact_type, presence: true

    delegate :name, to: :contact_type, allow_nil: true

    def to_s
      data
    end

    def self.primary_email
      where(primary: true, contact_type: Gaku::ContactType.where(name: 'Email').first).first
    end

    def self.students
      where(contactable_type: Gaku::Student)
    end

    def self.teachers
      where(contactable_type: Gaku::Teacher)
    end

    def self.guardians
      where(contactable_type: Gaku::Guardian)
    end

    def make_primary
      contactable.contacts.where.not(id: id).update_all(primary: false)
      update_attribute(:primary, true)

      # if contactable.has_attribute?(:primary_contact)
      #   contactable.update_attribute(:primary_contact, contactable.contact_widget)
      # end
    end

    def primary?
      primary
    end

    def self.primary
      where(primary: true).first
    end

    def self.secondary
      where(primary: false)
    end

    private

    def increment_count
      contactable.class.increment_counter(:contacts_count, contactable.id)
    end

    def decrement_count
      contactable.class.decrement_counter(:contacts_count, contactable.id)
    end

  end
end

Version data entries

10 entries across 10 versions & 2 rubygems

Version Path
gaku-0.2.4 core/app/models/gaku/contact.rb
gaku_core-0.2.4 app/models/gaku/contact.rb
gaku-0.2.3 core/app/models/gaku/contact.rb
gaku_core-0.2.3 app/models/gaku/contact.rb
gaku-0.2.2 core/app/models/gaku/contact.rb
gaku_core-0.2.2 app/models/gaku/contact.rb
gaku-0.2.1 core/app/models/gaku/contact.rb
gaku_core-0.2.1 app/models/gaku/contact.rb
gaku-0.2.0 core/app/models/gaku/contact.rb
gaku_core-0.2.0 app/models/gaku/contact.rb