]> git.openstreetmap.org Git - rails.git/blob - app/models/concerns/consistency_validations.rb
Add frozen_string_literal comments to ruby files
[rails.git] / app / models / concerns / consistency_validations.rb
1 # frozen_string_literal: true
2
3 module ConsistencyValidations
4   extend ActiveSupport::Concern
5
6   # Generic checks that are run for the updates and deletes of
7   # node, ways and relations. This code is here to avoid duplication,
8   # and allow the extension of the checks without having to modify the
9   # code in 6 places for all the updates and deletes.
10   # This will throw an exception if there is an inconsistency
11   def check_update_element_consistency(old, new, user)
12     if new.id != old.id || new.id.nil? || old.id.nil?
13       raise OSM::APIPreconditionFailedError, "New and old IDs don't match on #{new.class}. #{new.id} != #{old.id}."
14     elsif new.version != old.version
15       raise OSM::APIVersionMismatchError.new(new.id, new.class.to_s, new.version, old.version)
16     end
17
18     check_changeset_consistency(new.changeset, user)
19   end
20
21   # This is similar to above, just some validations don't apply
22   def check_create_element_consistency(new, user)
23     check_changeset_consistency(new.changeset, user)
24   end
25
26   ##
27   # subset of consistency checks which should be applied to almost
28   # all the changeset controller's writable methods.
29   def check_changeset_consistency(changeset, user)
30     # check user credentials - only the user who opened a changeset
31     # may alter it.
32     if changeset.nil?
33       raise OSM::APIChangesetMissingError
34     elsif user.id != changeset.user_id
35       raise OSM::APIUserChangesetMismatchError
36     elsif !changeset.open?
37       raise OSM::APIChangesetAlreadyClosedError, changeset
38     end
39   end
40 end