Sha256: ada1f85a40840481858f0af310ffa1e8c428fde4200885f9db71a07cf7e336d8

Contents?: true

Size: 1.33 KB

Versions: 7

Compression:

Stored size: 1.33 KB

Contents

class Relationship < ActiveRecord::Base
  attr_accessible :person, :relation, :other, :starred, :inverse, :relation_id, :other_id

  belongs_to :person
  belongs_to :other, :class_name => 'Person'
  belongs_to :inverse, :class_name => 'Relationship', :dependent => :destroy, :foreign_key => :inverse_id
  has_one :inverse_relationship, :class_name => 'Relationship', :dependent => :destroy, :foreign_key => :inverse_id
  belongs_to :relation

  validates_with RelationshipValidator

  after_create :assign_company_name

  def assign_company_name
    return unless relation.description == 'employed by'
    person.company_name = other.company_name unless person.company_name.present?
    person.save!
  end

  def self.starred
    where(:starred => true)
  end

  def self.unstarred
    where(:starred => false)
  end

  def self.of_relation(relation)
    where(:relation => {:description => relation})
  end

  def unstar!
    Relationship.transaction do
      self[:starred] = false
      inverse[:starred] = false
      save!
      inverse.save!
    end
  end

  def star!
    Relationship.transaction do
      self[:starred] = true
      inverse[:starred] = true
      save!
      inverse.save!
    end
  end

  def ensure_inverse
    create_inverse(:relation => relation.inverse, :person => other, :other => person, :inverse => self)
    save
  end

end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
artfully_ose-1.2.0 app/models/relationship.rb
artfully_ose-1.2.0.beta.1 app/models/relationship.rb
artfully_ose-1.2.0.alpha.2 app/models/relationship.rb
artfully_ose-1.2.0.alpha.1 app/models/relationship.rb
artfully_ose-1.2.0.pre.27 app/models/relationship.rb
artfully_ose-1.2.0.pre.26 app/models/relationship.rb
artfully_ose-1.2.0.pre.24 app/models/relationship.rb