]> git.openstreetmap.org Git - rails.git/blob - lib/consistency_validations.rb
Update leaflet polyline plugin
[rails.git] / lib / consistency_validations.rb
1 module ConsistencyValidations
2   # Generic checks that are run for the updates and deletes of
3   # node, ways and relations. This code is here to avoid duplication,
4   # and allow the extention of the checks without having to modify the
5   # code in 6 places for all the updates and deletes. Some of these tests are
6   # needed for creates, but are currently not run :-(
7   # This will throw an exception if there is an inconsistency
8   def check_consistency(old, new, user)
9     if new.id != old.id || new.id.nil? || old.id.nil?
10       fail OSM::APIPreconditionFailedError.new("New and old IDs don't match on #{new.class}. #{new.id} != #{old.id}.")
11     elsif new.version != old.version
12       fail OSM::APIVersionMismatchError.new(new.id, new.class.to_s, new.version, old.version)
13     elsif new.changeset.nil?
14       fail OSM::APIChangesetMissingError.new
15     elsif new.changeset.user_id != user.id
16       fail OSM::APIUserChangesetMismatchError.new
17     elsif !new.changeset.is_open?
18       fail OSM::APIChangesetAlreadyClosedError.new(new.changeset)
19     end
20   end
21
22   # This is similar to above, just some validations don't apply
23   def check_create_consistency(new, user)
24     if new.changeset.nil?
25       fail OSM::APIChangesetMissingError.new
26     elsif new.changeset.user_id != user.id
27       fail OSM::APIUserChangesetMismatchError.new
28     elsif !new.changeset.is_open?
29       fail OSM::APIChangesetAlreadyClosedError.new(new.changeset)
30     end
31   end
32
33   ##
34   # subset of consistency checks which should be applied to almost
35   # all the changeset controller's writable methods.
36   def check_changeset_consistency(changeset, user)
37     # check user credentials - only the user who opened a changeset
38     # may alter it.
39     if changeset.nil?
40       fail OSM::APIChangesetMissingError.new
41     elsif user.id != changeset.user_id
42       fail OSM::APIUserChangesetMismatchError.new
43     elsif !changeset.is_open?
44       fail OSM::APIChangesetAlreadyClosedError.new(changeset)
45     end
46   end
47 end