1 # == Schema Information
3 # Table name: relations
5 # relation_id :bigint(8) default(0), not null, primary key
6 # changeset_id :bigint(8) not null
7 # timestamp :datetime not null
8 # version :bigint(8) not null, primary key
9 # visible :boolean default(TRUE), not null
10 # redaction_id :integer
14 # relations_changeset_id_idx (changeset_id)
15 # relations_timestamp_idx (timestamp)
19 # relations_changeset_id_fkey (changeset_id => changesets.id)
20 # relations_redaction_id_fkey (redaction_id => redactions.id)
23 class OldRelation < ApplicationRecord
24 include ConsistencyValidations
26 self.table_name = "relations"
27 self.primary_keys = "relation_id", "version"
29 # NOTE: this needs to be included after the table name changes, or
30 # the queries generated by Redactable will use the wrong table name.
34 belongs_to :redaction, :optional => true
35 belongs_to :current_relation, :class_name => "Relation", :foreign_key => "relation_id", :inverse_of => :old_relations
37 has_many :old_members, -> { order(:sequence_id) }, :class_name => "OldRelationMember", :foreign_key => [:relation_id, :version], :inverse_of => :old_relation
38 has_many :old_tags, :class_name => "OldRelationTag", :foreign_key => [:relation_id, :version], :inverse_of => :old_relation
40 validates :changeset, :associated => true
41 validates :timestamp, :presence => true
42 validates :visible, :inclusion => [true, false]
44 def self.from_relation(relation)
45 old_relation = OldRelation.new
46 old_relation.visible = relation.visible
47 old_relation.changeset_id = relation.changeset_id
48 old_relation.timestamp = relation.timestamp
49 old_relation.relation_id = relation.id
50 old_relation.version = relation.version
51 old_relation.members = relation.members
52 old_relation.tags = relation.tags
56 def save_with_dependencies!
60 tag = OldRelationTag.new
63 tag.relation_id = relation_id
68 members.each_with_index do |m, i|
69 member = OldRelationMember.new
70 member.id = [relation_id, version, i]
71 member.member_type = m[0].classify
72 member.member_id = m[1]
73 member.member_role = m[2]
79 @members ||= old_members.collect do |member|
80 [member.member_type, member.member_id, member.member_role]
85 @tags ||= old_tags.to_h { |t| [t.k, t.v] }
88 attr_writer :members, :tags
90 # Temporary method to match interface to relations
95 # Pretend we're not in any relations
96 def containing_relation_members
100 # check whether this element is the latest version - that is,
101 # has the same version as its "current" counterpart.
103 current_relation.version == version