1 module ConsistencyValidations
2 extend ActiveSupport::Concern
4 # Generic checks that are run for the updates and deletes of
5 # node, ways and relations. This code is here to avoid duplication,
6 # and allow the extension of the checks without having to modify the
7 # code in 6 places for all the updates and deletes. Some of these tests are
8 # needed for creates, but are currently not run :-(
9 # This will throw an exception if there is an inconsistency
10 def check_update_element_consistency(old, new, user)
11 if new.id != old.id || new.id.nil? || old.id.nil?
12 raise OSM::APIPreconditionFailedError, "New and old IDs don't match on #{new.class}. #{new.id} != #{old.id}."
13 elsif new.version != old.version
14 raise OSM::APIVersionMismatchError.new(new.id, new.class.to_s, new.version, old.version)
17 check_changeset_consistency(new.changeset, user)
20 # This is similar to above, just some validations don't apply
21 def check_create_element_consistency(new, user)
22 check_changeset_consistency(new.changeset, user)
26 # subset of consistency checks which should be applied to almost
27 # all the changeset controller's writable methods.
28 def check_changeset_consistency(changeset, user)
29 # check user credentials - only the user who opened a changeset
32 raise OSM::APIChangesetMissingError
33 elsif user.id != changeset.user_id
34 raise OSM::APIUserChangesetMismatchError
35 elsif !changeset.open?
36 raise OSM::APIChangesetAlreadyClosedError, changeset