Sha256: ed250158aaa20b54577613e61622929d058af12d13383ffe5db6f203ed4bd876

Contents?: true

Size: 1.46 KB

Versions: 1

Compression:

Stored size: 1.46 KB

Contents

# Party Relation 是参与者关系。

class Unidom::Party::PartyRelation < ActiveRecord::Base

  self.table_name = 'unidom_party_relations'

  include Unidom::Common::Concerns::ModelExtension

  validates :grade,    presence: true, numericality: { integer_only: true }
  validates :priority, presence: true, numericality: { integer_only: true }

  belongs_to :source_party, polymorphic: true
  belongs_to :target_party, polymorphic: true

  scope :source_party_is, ->(source_party) { where source_party: source_party }
  scope :target_party_is, ->(target_party) { where target_party: target_party }

  def self.relate(source_party, target_party, linkage_code: 'FRND', grade: 0, opened_at: Time.now, priority: 0, attributes: {})
    relation = source_party_is(source_party).target_party_is(target_party).linkage_coded_as(linkage_code).first_or_initialize grade: grade, priority: priority, opened_at: opened_at
    relation.assign_attributes attributes
    relation.save!
    relation
  end

  def self.relate!(source_party, target_party, linkage_code: 'FRND', grade: 0, opened_at: Time.now, priority: 0, attributes: {})
    relation = source_party_is(source_party).target_party_is(target_party).linkage_coded_as(linkage_code).first_or_initialize grade: grade, priority: priority, opened_at: opened_at
    relation.assign_attributes attributes
    relation.save!
  end

  class << self
    deprecate relate: :relate!, deprecator: ActiveSupport::Deprecation.new('1.0', 'unidom-party')
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
unidom-party-0.7 app/models/unidom/party/party_relation.rb