1 # == Schema Information
3 # Table name: changesets
5 # id :bigint(8) not null, primary key
6 # user_id :bigint(8) not null
7 # created_at :datetime not null
12 # closed_at :datetime not null
13 # num_changes :integer default(0), not null
17 # changesets_bbox_idx (min_lat,max_lat,min_lon,max_lon) USING gist
18 # changesets_closed_at_idx (closed_at)
19 # changesets_created_at_idx (created_at)
20 # changesets_user_id_created_at_idx (user_id,created_at)
21 # changesets_user_id_id_idx (user_id,id)
25 # changesets_user_id_fkey (user_id => users.id)
28 class Changeset < ApplicationRecord
31 belongs_to :user, :counter_cache => true
33 has_many :changeset_tags
40 has_many :old_relations
42 has_many :comments, -> { where(:visible => true).order(:created_at) }, :class_name => "ChangesetComment"
43 has_and_belongs_to_many :subscribers, :class_name => "User", :join_table => "changesets_subscribers", :association_foreign_key => "subscriber_id"
45 validates :id, :uniqueness => true, :presence => { :on => :update },
46 :numericality => { :on => :update, :only_integer => true }
47 validates :num_changes, :presence => true,
48 :numericality => { :only_integer => true,
49 :greater_than_or_equal_to => 0 }
50 validates :created_at, :closed_at, :presence => true
51 validates :min_lat, :max_lat, :min_lon, :max_lat, :allow_nil => true,
52 :numericality => { :only_integer => true }
54 before_save :update_closed_at
56 # maximum number of elements allowed in a changeset
59 # maximum time a changeset is allowed to be open for.
62 # idle timeout increment, one hour seems reasonable.
65 # Use a method like this, so that we can easily change how we
66 # determine whether a changeset is open, without breaking code in at
69 # a changeset is open (that is, it will accept further changes) when
70 # it has not yet run out of time and its capacity is small enough.
71 # note that this may not be a hard limit - due to timing changes and
72 # concurrency it is possible that some changesets may be slightly
73 # longer than strictly allowed or have slightly more changes in them.
74 ((closed_at > Time.now.utc) && (num_changes <= MAX_ELEMENTS))
77 def set_closed_time_now
78 self.closed_at = Time.now.utc if is_open?
81 def self.from_xml(xml, create: false)
82 p = XML::Parser.string(xml, :options => XML::Parser::Options::NOERROR)
84 pt = doc.find_first("//osm/changeset")
87 Changeset.from_xml_node(pt, :create => create)
89 raise OSM::APIBadXMLError.new("changeset", xml, "XML doesn't contain an osm/changeset element.")
91 rescue LibXML::XML::Error, ArgumentError => e
92 raise OSM::APIBadXMLError.new("changeset", xml, e.message)
95 def self.from_xml_node(pt, create: false)
98 cs.created_at = Time.now.utc
99 # initial close time is 1h ahead, but will be increased on each
101 cs.closed_at = cs.created_at + IDLE_TIMEOUT
102 # initially we have no changes in a changeset
106 pt.find("tag").each do |tag|
107 raise OSM::APIBadXMLError.new("changeset", pt, "tag is missing key") if tag["k"].nil?
108 raise OSM::APIBadXMLError.new("changeset", pt, "tag is missing value") if tag["v"].nil?
110 cs.add_tag_keyval(tag["k"], tag["v"])
117 # returns the bounding box of the changeset. it is possible that some
118 # or all of the values will be nil, indicating that they are undefined.
120 @bbox ||= BoundingBox.new(min_lon, min_lat, max_lon, max_lat)
128 # expand the bounding box to include the given bounding box.
129 def update_bbox!(bbox_update)
130 bbox.expand!(bbox_update)
132 # update active record. rails 2.1's dirty handling should take care of
133 # whether this object needs saving or not.
134 self.min_lon, self.min_lat, self.max_lon, self.max_lat = @bbox.to_a.collect(&:round) if bbox.complete?
138 # the number of elements is also passed in so that we can ensure that
139 # a single changeset doesn't contain too many elements.
140 def add_changes!(elements)
141 self.num_changes += elements
147 changeset_tags.each do |tag|
156 def add_tag_keyval(k, v)
159 # duplicate tags are now forbidden, so we can't allow values
160 # in the hash to be overwritten.
161 raise OSM::APIDuplicateTagsError.new("changeset", id, k) if @tags.include? k
167 # do the changeset update and the changeset tags update in the
168 # same transaction to ensure consistency.
169 Changeset.transaction do
173 ChangesetTag.where(:changeset_id => id).delete_all
176 tag = ChangesetTag.new
177 tag.changeset_id = id
186 # set the auto-close time to be one hour in the future unless
187 # that would make it more than 24h long, in which case clip to
188 # 24h, as this has been decided is a reasonable time limit.
191 self.closed_at = if (closed_at - created_at) > (MAX_TIME_OPEN - IDLE_TIMEOUT)
192 created_at + MAX_TIME_OPEN
194 Time.now.utc + IDLE_TIMEOUT
200 # update this instance from another instance given and the user who is
201 # doing the updating. note that this method is not for updating the
202 # bounding box, only the tags of the changeset.
203 def update_from(other, user)
204 # ensure that only the user who opened the changeset may modify it.
205 raise OSM::APIUserChangesetMismatchError unless user.id == user_id
207 # can't change a closed changeset
208 raise OSM::APIChangesetAlreadyClosedError, self unless is_open?
210 # copy the other's tags
211 self.tags = other.tags