]> git.openstreetmap.org Git - rails.git/blob - app/models/concerns/consistency_validations.rb
Merge remote-tracking branch 'upstream/pull/4728'
[rails.git] / app / models / concerns / consistency_validations.rb
1 module ConsistencyValidations
2   extend ActiveSupport::Concern
3
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.
8   # This will throw an exception if there is an inconsistency
9   def check_update_element_consistency(old, new, user)
10     if new.id != old.id || new.id.nil? || old.id.nil?
11       raise OSM::APIPreconditionFailedError, "New and old IDs don't match on #{new.class}. #{new.id} != #{old.id}."
12     elsif new.version != old.version
13       raise OSM::APIVersionMismatchError.new(new.id, new.class.to_s, new.version, old.version)
14     end
15
16     check_changeset_consistency(new.changeset, user)
17   end
18
19   # This is similar to above, just some validations don't apply
20   def check_create_element_consistency(new, user)
21     check_changeset_consistency(new.changeset, user)
22   end
23
24   ##
25   # subset of consistency checks which should be applied to almost
26   # all the changeset controller's writable methods.
27   def check_changeset_consistency(changeset, user)
28     # check user credentials - only the user who opened a changeset
29     # may alter it.
30     if changeset.nil?
31       raise OSM::APIChangesetMissingError
32     elsif user.id != changeset.user_id
33       raise OSM::APIUserChangesetMismatchError
34     elsif !changeset.open?
35       raise OSM::APIChangesetAlreadyClosedError, changeset
36     end
37   end
38 end