1 # == Schema Information
3 # Table name: relations
5 # relation_id :bigint(8) 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 self.table_name = "relations"
26 # NOTE: this needs to be included after the table name changes, or
27 # the queries generated by Redactable will use the wrong table name.
31 belongs_to :redaction, :optional => true
32 belongs_to :current_relation, :class_name => "Relation", :foreign_key => "relation_id", :inverse_of => :old_relations
34 has_many :old_members, -> { order(:sequence_id) }, :class_name => "OldRelationMember", :query_constraints => [:relation_id, :version], :inverse_of => :old_relation
35 has_many :old_tags, :class_name => "OldRelationTag", :query_constraints => [:relation_id, :version], :inverse_of => :old_relation
37 validates :changeset, :associated => true
38 validates :timestamp, :presence => true
39 validates :visible, :inclusion => [true, false]
41 def self.from_relation(relation)
42 old_relation = OldRelation.new
43 old_relation.visible = relation.visible
44 old_relation.changeset_id = relation.changeset_id
45 old_relation.timestamp = relation.timestamp
46 old_relation.relation_id = relation.id
47 old_relation.version = relation.version
48 old_relation.members = relation.members
49 old_relation.tags = relation.tags
53 def save_with_dependencies!
57 tag = OldRelationTag.new
60 tag.relation_id = relation_id
65 members.each_with_index do |m, i|
66 member = OldRelationMember.new
67 member.id = [relation_id, version, i]
68 member.member_type = m[0].classify
69 member.member_id = m[1]
70 member.member_role = m[2]
76 @members ||= old_members.collect do |member|
77 [member.member_type, member.member_id, member.member_role]
82 @tags ||= old_tags.to_h { |t| [t.k, t.v] }
85 attr_writer :members, :tags
87 # Temporary method to match interface to relations
92 # Pretend we're not in any relations
93 def containing_relation_members
97 # check whether this element is the latest version - that is,
98 # has the same version as its "current" counterpart.
100 current_relation.version == version