From dbe165bbb3d6eda37d9579f1ac767599f9055607 Mon Sep 17 00:00:00 2001 From: Tom Hughes Date: Wed, 25 Feb 2015 18:49:21 +0000 Subject: [PATCH] Fix some rubocop rails style issues --- .rubocop_todo.yml | 17 -------- app/controllers/amf_controller.rb | 4 +- app/controllers/api_controller.rb | 10 ++--- app/controllers/application_controller.rb | 2 +- app/controllers/browse_controller.rb | 10 ++--- app/controllers/changeset_controller.rb | 26 ++++++------ app/controllers/diary_entry_controller.rb | 14 +++---- app/controllers/directions_controller.rb | 6 +-- app/controllers/export_controller.rb | 4 +- app/controllers/geocoder_controller.rb | 6 +-- app/controllers/message_controller.rb | 12 +++--- app/controllers/node_controller.rb | 16 ++++---- app/controllers/notes_controller.rb | 20 ++++----- app/controllers/oauth_clients_controller.rb | 6 +-- app/controllers/old_controller.rb | 22 +++++----- app/controllers/redactions_controller.rb | 14 +++---- app/controllers/relation_controller.rb | 16 ++++---- app/controllers/search_controller.rb | 4 +- app/controllers/site_controller.rb | 12 +++--- app/controllers/swf_controller.rb | 4 +- app/controllers/trace_controller.rb | 28 ++++++------- app/controllers/user_blocks_controller.rb | 18 ++++---- app/controllers/user_controller.rb | 34 +++++++-------- app/controllers/user_preference_controller.rb | 10 ++--- app/controllers/user_roles_controller.rb | 14 +++---- app/controllers/way_controller.rb | 16 ++++---- app/models/access_token.rb | 2 +- app/models/changeset.rb | 17 ++++---- app/models/changeset_comment.rb | 14 +++---- app/models/changeset_tag.rb | 7 ++-- app/models/client_application.rb | 9 ++-- app/models/diary_comment.rb | 6 +-- app/models/diary_entry.rb | 19 +++++---- app/models/message.rb | 8 ++-- app/models/node.rb | 21 ++++++---- app/models/node_tag.rb | 7 ++-- app/models/note.rb | 12 +++--- app/models/note_comment.rb | 17 ++++---- app/models/oauth2_verifier.rb | 3 +- app/models/oauth_nonce.rb | 4 +- app/models/oauth_token.rb | 5 ++- app/models/old_node.rb | 12 ++++-- app/models/old_node_tag.rb | 7 ++-- app/models/old_relation.rb | 4 +- app/models/old_relation_tag.rb | 7 ++-- app/models/old_way.rb | 4 +- app/models/old_way_tag.rb | 7 ++-- app/models/redaction.rb | 2 +- app/models/relation.rb | 16 ++++---- app/models/relation_tag.rb | 7 ++-- app/models/request_token.rb | 2 +- app/models/trace.rb | 12 +++--- app/models/tracepoint.rb | 9 ++-- app/models/tracetag.rb | 6 +-- app/models/user.rb | 41 ++++++++++--------- app/models/user_block.rb | 2 +- app/models/user_preference.rb | 4 +- app/models/user_role.rb | 3 +- app/models/way.rb | 16 ++++---- app/models/way_tag.rb | 7 ++-- lib/validators.rb | 20 ++++----- 61 files changed, 336 insertions(+), 348 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index c4eb88ac4..57b992971 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -62,28 +62,11 @@ Metrics/ParameterLists: Metrics/PerceivedComplexity: Max: 24 -# Offense count: 164 -# Cop supports --auto-correct. -# Configuration parameters: EnforcedStyle, SupportedStyles, Include. -Rails/ActionFilter: - Enabled: false - # Offense count: 2 # Configuration parameters: Include. Rails/HasAndBelongsToMany: Enabled: false -# Offense count: 17 -# Cop supports --auto-correct. -# Configuration parameters: Include. -Rails/ReadWriteAttribute: - Enabled: false - -# Offense count: 123 -# Configuration parameters: Include. -Rails/Validation: - Enabled: false - # Offense count: 5 Style/AccessorMethodName: Enabled: false diff --git a/app/controllers/amf_controller.rb b/app/controllers/amf_controller.rb index 6c3e2c5cf..d7364d106 100644 --- a/app/controllers/amf_controller.rb +++ b/app/controllers/amf_controller.rb @@ -38,8 +38,8 @@ class AmfController < ApplicationController include Potlatch - skip_before_filter :verify_authenticity_token - before_filter :check_api_writable + skip_before_action :verify_authenticity_token + before_action :check_api_writable # Main AMF handlers: process the raw AMF string (using AMF library) and # calls each action (private method) accordingly. diff --git a/app/controllers/api_controller.rb b/app/controllers/api_controller.rb index 6bbbee1ce..7bf8f0499 100644 --- a/app/controllers/api_controller.rb +++ b/app/controllers/api_controller.rb @@ -1,9 +1,9 @@ class ApiController < ApplicationController - skip_before_filter :verify_authenticity_token - before_filter :check_api_readable, :except => [:capabilities] - before_filter :setup_user_auth, :only => [:permissions] - after_filter :compress_output - around_filter :api_call_handle_error, :api_call_timeout + skip_before_action :verify_authenticity_token + before_action :check_api_readable, :except => [:capabilities] + before_action :setup_user_auth, :only => [:permissions] + after_action :compress_output + around_action :api_call_handle_error, :api_call_timeout # Get an XML response containing a list of tracepoints that have been uploaded # within the specified bounding box, and in the specified page. diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 72de936d7..8322201ff 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -3,7 +3,7 @@ class ApplicationController < ActionController::Base protect_from_forgery - before_filter :fetch_body + before_action :fetch_body def authorize_web if session[:user] diff --git a/app/controllers/browse_controller.rb b/app/controllers/browse_controller.rb index a529b393e..c7f8b0e81 100644 --- a/app/controllers/browse_controller.rb +++ b/app/controllers/browse_controller.rb @@ -1,11 +1,11 @@ class BrowseController < ApplicationController layout :map_layout - before_filter :authorize_web - before_filter :set_locale - before_filter :except => [:query] { |c| c.check_database_readable(true) } - before_filter :require_oauth - around_filter :web_timeout + before_action :authorize_web + before_action :set_locale + before_action :except => [:query] { |c| c.check_database_readable(true) } + before_action :require_oauth + around_action :web_timeout def relation @type = "relation" diff --git a/app/controllers/changeset_controller.rb b/app/controllers/changeset_controller.rb index eefe5b55f..d2740f7f3 100644 --- a/app/controllers/changeset_controller.rb +++ b/app/controllers/changeset_controller.rb @@ -4,19 +4,19 @@ class ChangesetController < ApplicationController layout "site" require "xml/libxml" - skip_before_filter :verify_authenticity_token, :except => [:list] - before_filter :authorize_web, :only => [:list, :feed, :comments_feed] - before_filter :set_locale, :only => [:list, :feed, :comments_feed] - before_filter :authorize, :only => [:create, :update, :delete, :upload, :include, :close, :comment, :subscribe, :unsubscribe, :hide_comment, :unhide_comment] - before_filter :require_moderator, :only => [:hide_comment, :unhide_comment] - before_filter :require_allow_write_api, :only => [:create, :update, :delete, :upload, :include, :close, :comment, :subscribe, :unsubscribe, :hide_comment, :unhide_comment] - before_filter :require_public_data, :only => [:create, :update, :delete, :upload, :include, :close, :comment, :subscribe, :unsubscribe] - before_filter :check_api_writable, :only => [:create, :update, :delete, :upload, :include, :comment, :subscribe, :unsubscribe, :hide_comment, :unhide_comment] - before_filter :check_api_readable, :except => [:create, :update, :delete, :upload, :download, :query, :list, :feed, :comment, :subscribe, :unsubscribe, :comments_feed] - before_filter(:only => [:list, :feed, :comments_feed]) { |c| c.check_database_readable(true) } - after_filter :compress_output - around_filter :api_call_handle_error, :except => [:list, :feed, :comments_feed] - around_filter :web_timeout, :only => [:list, :feed, :comments_feed] + skip_before_action :verify_authenticity_token, :except => [:list] + before_action :authorize_web, :only => [:list, :feed, :comments_feed] + before_action :set_locale, :only => [:list, :feed, :comments_feed] + before_action :authorize, :only => [:create, :update, :delete, :upload, :include, :close, :comment, :subscribe, :unsubscribe, :hide_comment, :unhide_comment] + before_action :require_moderator, :only => [:hide_comment, :unhide_comment] + before_action :require_allow_write_api, :only => [:create, :update, :delete, :upload, :include, :close, :comment, :subscribe, :unsubscribe, :hide_comment, :unhide_comment] + before_action :require_public_data, :only => [:create, :update, :delete, :upload, :include, :close, :comment, :subscribe, :unsubscribe] + before_action :check_api_writable, :only => [:create, :update, :delete, :upload, :include, :comment, :subscribe, :unsubscribe, :hide_comment, :unhide_comment] + before_action :check_api_readable, :except => [:create, :update, :delete, :upload, :download, :query, :list, :feed, :comment, :subscribe, :unsubscribe, :comments_feed] + before_action(:only => [:list, :feed, :comments_feed]) { |c| c.check_database_readable(true) } + after_action :compress_output + around_action :api_call_handle_error, :except => [:list, :feed, :comments_feed] + around_action :web_timeout, :only => [:list, :feed, :comments_feed] # Helper methods for checking consistency include ConsistencyValidations diff --git a/app/controllers/diary_entry_controller.rb b/app/controllers/diary_entry_controller.rb index 7c38e58a8..ee998ba8a 100644 --- a/app/controllers/diary_entry_controller.rb +++ b/app/controllers/diary_entry_controller.rb @@ -1,13 +1,13 @@ class DiaryEntryController < ApplicationController layout "site", :except => :rss - before_filter :authorize_web - before_filter :set_locale - before_filter :require_user, :only => [:new, :edit, :comment, :hide, :hidecomment] - before_filter :lookup_this_user, :only => [:view, :comments] - before_filter :check_database_readable - before_filter :check_database_writable, :only => [:new, :edit] - before_filter :require_administrator, :only => [:hide, :hidecomment] + before_action :authorize_web + before_action :set_locale + before_action :require_user, :only => [:new, :edit, :comment, :hide, :hidecomment] + before_action :lookup_this_user, :only => [:view, :comments] + before_action :check_database_readable + before_action :check_database_writable, :only => [:new, :edit] + before_action :require_administrator, :only => [:hide, :hidecomment] def new @title = t "diary_entry.new.title" diff --git a/app/controllers/directions_controller.rb b/app/controllers/directions_controller.rb index d153f0320..b04db6b6f 100644 --- a/app/controllers/directions_controller.rb +++ b/app/controllers/directions_controller.rb @@ -1,7 +1,7 @@ class DirectionsController < ApplicationController - before_filter :authorize_web - before_filter :set_locale - before_filter :require_oauth, :only => [:search] + before_action :authorize_web + before_action :set_locale + before_action :require_oauth, :only => [:search] def search render :layout => map_layout diff --git a/app/controllers/export_controller.rb b/app/controllers/export_controller.rb index 6a8348fb5..bd020f29b 100644 --- a/app/controllers/export_controller.rb +++ b/app/controllers/export_controller.rb @@ -1,6 +1,6 @@ class ExportController < ApplicationController - before_filter :authorize_web - before_filter :set_locale + before_action :authorize_web + before_action :set_locale caches_page :embed diff --git a/app/controllers/geocoder_controller.rb b/app/controllers/geocoder_controller.rb index e889228f3..be4f0570c 100644 --- a/app/controllers/geocoder_controller.rb +++ b/app/controllers/geocoder_controller.rb @@ -6,9 +6,9 @@ class GeocoderController < ApplicationController require "net/http" require "rexml/document" - before_filter :authorize_web - before_filter :set_locale - before_filter :require_oauth, :only => [:search] + before_action :authorize_web + before_action :set_locale + before_action :require_oauth, :only => [:search] def search normalize_params diff --git a/app/controllers/message_controller.rb b/app/controllers/message_controller.rb index 32f2f8780..a22802e63 100644 --- a/app/controllers/message_controller.rb +++ b/app/controllers/message_controller.rb @@ -1,12 +1,12 @@ class MessageController < ApplicationController layout "site" - before_filter :authorize_web - before_filter :set_locale - before_filter :require_user - before_filter :lookup_this_user, :only => [:new] - before_filter :check_database_readable - before_filter :check_database_writable, :only => [:new, :reply, :mark] + before_action :authorize_web + before_action :set_locale + before_action :require_user + before_action :lookup_this_user, :only => [:new] + before_action :check_database_readable + before_action :check_database_writable, :only => [:new, :reply, :mark] # Allow the user to write a new message to another user. This action also # deals with the sending of that message to the other user when the user diff --git a/app/controllers/node_controller.rb b/app/controllers/node_controller.rb index 8c57ad8f9..9d3825679 100644 --- a/app/controllers/node_controller.rb +++ b/app/controllers/node_controller.rb @@ -3,14 +3,14 @@ class NodeController < ApplicationController require "xml/libxml" - skip_before_filter :verify_authenticity_token - before_filter :authorize, :only => [:create, :update, :delete] - before_filter :require_allow_write_api, :only => [:create, :update, :delete] - before_filter :require_public_data, :only => [:create, :update, :delete] - before_filter :check_api_writable, :only => [:create, :update, :delete] - before_filter :check_api_readable, :except => [:create, :update, :delete] - after_filter :compress_output - around_filter :api_call_handle_error, :api_call_timeout + skip_before_action :verify_authenticity_token + before_action :authorize, :only => [:create, :update, :delete] + before_action :require_allow_write_api, :only => [:create, :update, :delete] + before_action :require_public_data, :only => [:create, :update, :delete] + before_action :check_api_writable, :only => [:create, :update, :delete] + before_action :check_api_readable, :except => [:create, :update, :delete] + after_action :compress_output + around_action :api_call_handle_error, :api_call_timeout # Create a node from XML. def create diff --git a/app/controllers/notes_controller.rb b/app/controllers/notes_controller.rb index 5bdf28862..5ae136f29 100644 --- a/app/controllers/notes_controller.rb +++ b/app/controllers/notes_controller.rb @@ -1,16 +1,16 @@ class NotesController < ApplicationController layout "site", :only => [:mine] - before_filter :check_api_readable - before_filter :authorize_web, :only => [:mine] - before_filter :setup_user_auth, :only => [:create, :comment] - before_filter :authorize, :only => [:close, :reopen, :destroy] - before_filter :require_moderator, :only => [:destroy] - before_filter :check_api_writable, :only => [:create, :comment, :close, :reopen, :destroy] - before_filter :require_allow_write_notes, :only => [:create, :comment, :close, :reopen, :destroy] - before_filter :set_locale - after_filter :compress_output - around_filter :api_call_handle_error, :api_call_timeout + before_action :check_api_readable + before_action :authorize_web, :only => [:mine] + before_action :setup_user_auth, :only => [:create, :comment] + before_action :authorize, :only => [:close, :reopen, :destroy] + before_action :require_moderator, :only => [:destroy] + before_action :check_api_writable, :only => [:create, :comment, :close, :reopen, :destroy] + before_action :require_allow_write_notes, :only => [:create, :comment, :close, :reopen, :destroy] + before_action :set_locale + after_action :compress_output + around_action :api_call_handle_error, :api_call_timeout ## # Return a list of notes in a given area diff --git a/app/controllers/oauth_clients_controller.rb b/app/controllers/oauth_clients_controller.rb index bf55679e8..1c1877ad4 100644 --- a/app/controllers/oauth_clients_controller.rb +++ b/app/controllers/oauth_clients_controller.rb @@ -1,9 +1,9 @@ class OauthClientsController < ApplicationController layout "site" - before_filter :authorize_web - before_filter :set_locale - before_filter :require_user + before_action :authorize_web + before_action :set_locale + before_action :require_user def index @client_applications = @user.client_applications diff --git a/app/controllers/old_controller.rb b/app/controllers/old_controller.rb index ff86407f5..b52c4efa4 100644 --- a/app/controllers/old_controller.rb +++ b/app/controllers/old_controller.rb @@ -4,17 +4,17 @@ class OldController < ApplicationController require "xml/libxml" - skip_before_filter :verify_authenticity_token - before_filter :setup_user_auth, :only => [:history, :version] - before_filter :authorize, :only => [:redact] - before_filter :authorize_moderator, :only => [:redact] - before_filter :require_allow_write_api, :only => [:redact] - before_filter :check_api_readable - before_filter :check_api_writable, :only => [:redact] - after_filter :compress_output - around_filter :api_call_handle_error, :api_call_timeout - before_filter :lookup_old_element, :except => [:history] - before_filter :lookup_old_element_versions, :only => [:history] + skip_before_action :verify_authenticity_token + before_action :setup_user_auth, :only => [:history, :version] + before_action :authorize, :only => [:redact] + before_action :authorize_moderator, :only => [:redact] + before_action :require_allow_write_api, :only => [:redact] + before_action :check_api_readable + before_action :check_api_writable, :only => [:redact] + after_action :compress_output + around_action :api_call_handle_error, :api_call_timeout + before_action :lookup_old_element, :except => [:history] + before_action :lookup_old_element_versions, :only => [:history] def history # the .where() method used in the lookup_old_element_versions diff --git a/app/controllers/redactions_controller.rb b/app/controllers/redactions_controller.rb index beff596a0..31dd7273f 100644 --- a/app/controllers/redactions_controller.rb +++ b/app/controllers/redactions_controller.rb @@ -1,13 +1,13 @@ class RedactionsController < ApplicationController layout "site" - before_filter :authorize_web - before_filter :set_locale - before_filter :require_user, :only => [:new, :create, :edit, :update, :destroy] - before_filter :require_moderator, :only => [:new, :create, :edit, :update, :destroy] - before_filter :lookup_redaction, :only => [:show, :edit, :update, :destroy] - before_filter :check_database_readable - before_filter :check_database_writable, :only => [:create, :update, :destroy] + before_action :authorize_web + before_action :set_locale + before_action :require_user, :only => [:new, :create, :edit, :update, :destroy] + before_action :require_moderator, :only => [:new, :create, :edit, :update, :destroy] + before_action :lookup_redaction, :only => [:show, :edit, :update, :destroy] + before_action :check_database_readable + before_action :check_database_writable, :only => [:create, :update, :destroy] def index @redactions = Redaction.order(:id) diff --git a/app/controllers/relation_controller.rb b/app/controllers/relation_controller.rb index 22a8d2831..63fdafbde 100644 --- a/app/controllers/relation_controller.rb +++ b/app/controllers/relation_controller.rb @@ -1,14 +1,14 @@ class RelationController < ApplicationController require "xml/libxml" - skip_before_filter :verify_authenticity_token - before_filter :authorize, :only => [:create, :update, :delete] - before_filter :require_allow_write_api, :only => [:create, :update, :delete] - before_filter :require_public_data, :only => [:create, :update, :delete] - before_filter :check_api_writable, :only => [:create, :update, :delete] - before_filter :check_api_readable, :except => [:create, :update, :delete] - after_filter :compress_output - around_filter :api_call_handle_error, :api_call_timeout + skip_before_action :verify_authenticity_token + before_action :authorize, :only => [:create, :update, :delete] + before_action :require_allow_write_api, :only => [:create, :update, :delete] + before_action :require_public_data, :only => [:create, :update, :delete] + before_action :check_api_writable, :only => [:create, :update, :delete] + before_action :check_api_readable, :except => [:create, :update, :delete] + after_action :compress_output + around_action :api_call_handle_error, :api_call_timeout def create assert_method :put diff --git a/app/controllers/search_controller.rb b/app/controllers/search_controller.rb index 0217bb29e..6e9130a52 100644 --- a/app/controllers/search_controller.rb +++ b/app/controllers/search_controller.rb @@ -2,8 +2,8 @@ class SearchController < ApplicationController # Support searching for nodes, ways, or all # Can search by tag k, v, or both (type->k,value->v) # Can search by name (k=name,v=....) - skip_before_filter :verify_authenticity_token - after_filter :compress_output + skip_before_action :verify_authenticity_token + after_action :compress_output def search_all do_search(true, true, true) diff --git a/app/controllers/site_controller.rb b/app/controllers/site_controller.rb index 4034950b7..5131629ac 100644 --- a/app/controllers/site_controller.rb +++ b/app/controllers/site_controller.rb @@ -2,12 +2,12 @@ class SiteController < ApplicationController layout "site" layout :map_layout, :only => [:index, :export] - before_filter :authorize_web - before_filter :set_locale - before_filter :redirect_browse_params, :only => :index - before_filter :redirect_map_params, :only => [:index, :edit, :export] - before_filter :require_user, :only => [:welcome] - before_filter :require_oauth, :only => [:index] + before_action :authorize_web + before_action :set_locale + before_action :redirect_browse_params, :only => :index + before_action :redirect_map_params, :only => [:index, :edit, :export] + before_action :require_user, :only => [:welcome] + before_action :require_oauth, :only => [:index] def index unless STATUS == :database_readonly || STATUS == :database_offline diff --git a/app/controllers/swf_controller.rb b/app/controllers/swf_controller.rb index 8d907d850..b7ec2714e 100644 --- a/app/controllers/swf_controller.rb +++ b/app/controllers/swf_controller.rb @@ -1,6 +1,6 @@ class SwfController < ApplicationController - skip_before_filter :verify_authenticity_token - before_filter :check_api_readable + skip_before_action :verify_authenticity_token + before_action :check_api_readable # to log: # RAILS_DEFAULT_LOGGER.error("Args: #{args[0]}, #{args[1]}, #{args[2]}, #{args[3]}") diff --git a/app/controllers/trace_controller.rb b/app/controllers/trace_controller.rb index 9d6dad600..64ecd5a7f 100644 --- a/app/controllers/trace_controller.rb +++ b/app/controllers/trace_controller.rb @@ -1,20 +1,20 @@ class TraceController < ApplicationController layout "site" - skip_before_filter :verify_authenticity_token, :only => [:api_create, :api_read, :api_update, :api_delete, :api_data] - before_filter :authorize_web - before_filter :set_locale - before_filter :require_user, :only => [:mine, :create, :edit, :delete] - before_filter :authorize, :only => [:api_create, :api_read, :api_update, :api_delete, :api_data] - before_filter :check_database_readable, :except => [:api_read, :api_data] - before_filter :check_database_writable, :only => [:create, :edit, :delete, :api_create, :api_update, :api_delete] - before_filter :check_api_readable, :only => [:api_read, :api_data] - before_filter :check_api_writable, :only => [:api_create, :api_update, :api_delete] - before_filter :require_allow_read_gpx, :only => [:api_read, :api_data] - before_filter :require_allow_write_gpx, :only => [:api_create, :api_update, :api_delete] - before_filter :offline_warning, :only => [:mine, :view] - before_filter :offline_redirect, :only => [:create, :edit, :delete, :data, :api_create, :api_delete, :api_data] - around_filter :api_call_handle_error, :only => [:api_create, :api_read, :api_update, :api_delete, :api_data] + skip_before_action :verify_authenticity_token, :only => [:api_create, :api_read, :api_update, :api_delete, :api_data] + before_action :authorize_web + before_action :set_locale + before_action :require_user, :only => [:mine, :create, :edit, :delete] + before_action :authorize, :only => [:api_create, :api_read, :api_update, :api_delete, :api_data] + before_action :check_database_readable, :except => [:api_read, :api_data] + before_action :check_database_writable, :only => [:create, :edit, :delete, :api_create, :api_update, :api_delete] + before_action :check_api_readable, :only => [:api_read, :api_data] + before_action :check_api_writable, :only => [:api_create, :api_update, :api_delete] + before_action :require_allow_read_gpx, :only => [:api_read, :api_data] + before_action :require_allow_write_gpx, :only => [:api_create, :api_update, :api_delete] + before_action :offline_warning, :only => [:mine, :view] + before_action :offline_redirect, :only => [:create, :edit, :delete, :data, :api_create, :api_delete, :api_data] + around_action :api_call_handle_error, :only => [:api_create, :api_read, :api_update, :api_delete, :api_data] # Counts and selects pages of GPX traces for various criteria (by user, tags, public etc.). # target_user - if set, specifies the user to fetch traces for. if not set will fetch all traces diff --git a/app/controllers/user_blocks_controller.rb b/app/controllers/user_blocks_controller.rb index b0ac27dd4..7a8c7a29e 100644 --- a/app/controllers/user_blocks_controller.rb +++ b/app/controllers/user_blocks_controller.rb @@ -1,15 +1,15 @@ class UserBlocksController < ApplicationController layout "site" - before_filter :authorize_web - before_filter :set_locale - before_filter :require_user, :only => [:new, :create, :edit, :update, :revoke] - before_filter :require_moderator, :only => [:new, :create, :edit, :update, :revoke] - before_filter :lookup_this_user, :only => [:new, :create, :blocks_on, :blocks_by] - before_filter :lookup_user_block, :only => [:show, :edit, :update, :revoke] - before_filter :require_valid_params, :only => [:create, :update] - before_filter :check_database_readable - before_filter :check_database_writable, :only => [:create, :update, :revoke] + before_action :authorize_web + before_action :set_locale + before_action :require_user, :only => [:new, :create, :edit, :update, :revoke] + before_action :require_moderator, :only => [:new, :create, :edit, :update, :revoke] + before_action :lookup_this_user, :only => [:new, :create, :blocks_on, :blocks_by] + before_action :lookup_user_block, :only => [:show, :edit, :update, :revoke] + before_action :require_valid_params, :only => [:create, :update] + before_action :check_database_readable + before_action :check_database_writable, :only => [:create, :update, :revoke] def index @user_blocks_pages, @user_blocks = paginate(:user_blocks, diff --git a/app/controllers/user_controller.rb b/app/controllers/user_controller.rb index a62e58b0e..7c20c5111 100644 --- a/app/controllers/user_controller.rb +++ b/app/controllers/user_controller.rb @@ -1,23 +1,23 @@ class UserController < ApplicationController layout "site", :except => [:api_details] - skip_before_filter :verify_authenticity_token, :only => [:api_read, :api_details, :api_gpx_files, :auth_success] - before_filter :disable_terms_redirect, :only => [:terms, :save, :logout, :api_details] - before_filter :authorize, :only => [:api_details, :api_gpx_files] - before_filter :authorize_web, :except => [:api_read, :api_details, :api_gpx_files] - before_filter :set_locale, :except => [:api_read, :api_details, :api_gpx_files] - before_filter :require_user, :only => [:account, :go_public, :make_friend, :remove_friend] - before_filter :require_self, :only => [:account] - before_filter :check_database_readable, :except => [:login, :api_read, :api_details, :api_gpx_files] - before_filter :check_database_writable, :only => [:new, :account, :confirm, :confirm_email, :lost_password, :reset_password, :go_public, :make_friend, :remove_friend] - before_filter :check_api_readable, :only => [:api_read, :api_details, :api_gpx_files] - before_filter :require_allow_read_prefs, :only => [:api_details] - before_filter :require_allow_read_gpx, :only => [:api_gpx_files] - before_filter :require_cookies, :only => [:new, :login, :confirm] - before_filter :require_administrator, :only => [:set_status, :delete, :list] - around_filter :api_call_handle_error, :only => [:api_read, :api_details, :api_gpx_files] - before_filter :lookup_user_by_id, :only => [:api_read] - before_filter :lookup_user_by_name, :only => [:set_status, :delete] + skip_before_action :verify_authenticity_token, :only => [:api_read, :api_details, :api_gpx_files, :auth_success] + before_action :disable_terms_redirect, :only => [:terms, :save, :logout, :api_details] + before_action :authorize, :only => [:api_details, :api_gpx_files] + before_action :authorize_web, :except => [:api_read, :api_details, :api_gpx_files] + before_action :set_locale, :except => [:api_read, :api_details, :api_gpx_files] + before_action :require_user, :only => [:account, :go_public, :make_friend, :remove_friend] + before_action :require_self, :only => [:account] + before_action :check_database_readable, :except => [:login, :api_read, :api_details, :api_gpx_files] + before_action :check_database_writable, :only => [:new, :account, :confirm, :confirm_email, :lost_password, :reset_password, :go_public, :make_friend, :remove_friend] + before_action :check_api_readable, :only => [:api_read, :api_details, :api_gpx_files] + before_action :require_allow_read_prefs, :only => [:api_details] + before_action :require_allow_read_gpx, :only => [:api_gpx_files] + before_action :require_cookies, :only => [:new, :login, :confirm] + before_action :require_administrator, :only => [:set_status, :delete, :list] + around_action :api_call_handle_error, :only => [:api_read, :api_details, :api_gpx_files] + before_action :lookup_user_by_id, :only => [:api_read] + before_action :lookup_user_by_name, :only => [:set_status, :delete] def terms @legale = params[:legale] || OSM.ip_to_country(request.remote_ip) || DEFAULT_LEGALE diff --git a/app/controllers/user_preference_controller.rb b/app/controllers/user_preference_controller.rb index 78f310ce5..e9eed12e3 100644 --- a/app/controllers/user_preference_controller.rb +++ b/app/controllers/user_preference_controller.rb @@ -1,10 +1,10 @@ # Update and read user preferences, which are arbitrayr key/val pairs class UserPreferenceController < ApplicationController - skip_before_filter :verify_authenticity_token - before_filter :authorize - before_filter :require_allow_read_prefs, :only => [:read_one, :read] - before_filter :require_allow_write_prefs, :except => [:read_one, :read] - around_filter :api_call_handle_error + skip_before_action :verify_authenticity_token + before_action :authorize + before_action :require_allow_read_prefs, :only => [:read_one, :read] + before_action :require_allow_write_prefs, :except => [:read_one, :read] + around_action :api_call_handle_error ## # return all the preferences as an XML document diff --git a/app/controllers/user_roles_controller.rb b/app/controllers/user_roles_controller.rb index 8b1c2e8c0..1cfbaf977 100644 --- a/app/controllers/user_roles_controller.rb +++ b/app/controllers/user_roles_controller.rb @@ -1,13 +1,13 @@ class UserRolesController < ApplicationController layout "site" - before_filter :authorize_web - before_filter :require_user - before_filter :lookup_this_user - before_filter :require_administrator - before_filter :require_valid_role - before_filter :not_in_role, :only => [:grant] - before_filter :in_role, :only => [:revoke] + before_action :authorize_web + before_action :require_user + before_action :lookup_this_user + before_action :require_administrator + before_action :require_valid_role + before_action :not_in_role, :only => [:grant] + before_action :in_role, :only => [:revoke] def grant @this_user.roles.create(:role => @role, :granter_id => @user.id) diff --git a/app/controllers/way_controller.rb b/app/controllers/way_controller.rb index b13c91b08..0e98854ef 100644 --- a/app/controllers/way_controller.rb +++ b/app/controllers/way_controller.rb @@ -1,14 +1,14 @@ class WayController < ApplicationController require "xml/libxml" - skip_before_filter :verify_authenticity_token - before_filter :authorize, :only => [:create, :update, :delete] - before_filter :require_allow_write_api, :only => [:create, :update, :delete] - before_filter :require_public_data, :only => [:create, :update, :delete] - before_filter :check_api_writable, :only => [:create, :update, :delete] - before_filter :check_api_readable, :except => [:create, :update, :delete] - after_filter :compress_output - around_filter :api_call_handle_error, :api_call_timeout + skip_before_action :verify_authenticity_token + before_action :authorize, :only => [:create, :update, :delete] + before_action :require_allow_write_api, :only => [:create, :update, :delete] + before_action :require_public_data, :only => [:create, :update, :delete] + before_action :check_api_writable, :only => [:create, :update, :delete] + before_action :check_api_readable, :except => [:create, :update, :delete] + after_action :compress_output + around_action :api_call_handle_error, :api_call_timeout def create assert_method :put diff --git a/app/models/access_token.rb b/app/models/access_token.rb index 2bf226784..f690c9754 100644 --- a/app/models/access_token.rb +++ b/app/models/access_token.rb @@ -4,7 +4,7 @@ class AccessToken < OauthToken scope :valid, -> { where(:invalidated_at => nil) } - validates_presence_of :user, :secret + validates :user, :secret, :presence => true before_create :set_authorized_at diff --git a/app/models/changeset.rb b/app/models/changeset.rb index 11a098137..cede7daa1 100644 --- a/app/models/changeset.rb +++ b/app/models/changeset.rb @@ -15,13 +15,16 @@ class Changeset < ActiveRecord::Base has_many :comments, -> { where(:visible => true).order(:created_at) }, :class_name => "ChangesetComment" has_and_belongs_to_many :subscribers, :class_name => "User", :join_table => "changesets_subscribers", :association_foreign_key => "subscriber_id" - validates_presence_of :id, :on => :update - validates_presence_of :user_id, :created_at, :closed_at, :num_changes - validates_uniqueness_of :id - validates_numericality_of :id, :on => :update, :integer_only => true - validates_numericality_of :min_lat, :max_lat, :min_lon, :max_lat, :allow_nil => true, :integer_only => true - validates_numericality_of :user_id, :integer_only => true - validates_numericality_of :num_changes, :integer_only => true, :greater_than_or_equal_to => 0 + validates :id, :uniqueness => true, :presence => { :on => :update }, + :numericality => { :on => :update, :integer_only => true } + validates :user_id, :presence => true, + :numericality => { :integer_only => true } + validates :num_changes, :presence => true, + :numericality => { :integer_only => true, + :greater_than_or_equal_to => 0 } + validates :created_at, :closed_at, :presence => true + validates :min_lat, :max_lat, :min_lon, :max_lat, :allow_nil => true, + :numericality => { :integer_only => true } before_save :update_closed_at diff --git a/app/models/changeset_comment.rb b/app/models/changeset_comment.rb index 02f68a2fa..ec563d6c7 100644 --- a/app/models/changeset_comment.rb +++ b/app/models/changeset_comment.rb @@ -2,16 +2,14 @@ class ChangesetComment < ActiveRecord::Base belongs_to :changeset belongs_to :author, :class_name => "User" - validates_presence_of :id, :on => :update # is it necessary? - validates_uniqueness_of :id - validates_presence_of :changeset - validates_associated :changeset - validates_presence_of :author - validates_associated :author - validates :visible, :inclusion => { :in => [true, false] } + validates :id, :uniqueness => true, :presence => { :on => :update }, + :numericality => { :on => :update, :integer_only => true } + validates :changeset, :presence => true, :associated => true + validates :author, :presence => true, :associated => true + validates :visible, :inclusion => [true, false] # Return the comment text def body - RichText.new("text", read_attribute(:body)) + RichText.new("text", self[:body]) end end diff --git a/app/models/changeset_tag.rb b/app/models/changeset_tag.rb index 0425fa6b4..7ffba0dc3 100644 --- a/app/models/changeset_tag.rb +++ b/app/models/changeset_tag.rb @@ -3,8 +3,7 @@ class ChangesetTag < ActiveRecord::Base belongs_to :changeset - validates_presence_of :changeset - validates_length_of :k, :maximum => 255, :allow_blank => true - validates_uniqueness_of :k, :scope => :changeset_id - validates_length_of :v, :maximum => 255, :allow_blank => true + validates :changeset, :presence => true, :associated => true + validates :k, :v, :allow_blank => true, :length => { :maximum => 255 } + validates :k, :uniqueness => { :scope => :changeset_id } end diff --git a/app/models/client_application.rb b/app/models/client_application.rb index e1358028d..e37629d17 100644 --- a/app/models/client_application.rb +++ b/app/models/client_application.rb @@ -7,11 +7,10 @@ class ClientApplication < ActiveRecord::Base has_many :oauth2_verifiers has_many :oauth_tokens - validates_presence_of :name, :url, :key, :secret - validates_uniqueness_of :key - validates_format_of :url, :with => %r{\Ahttp(s?)://(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(/|/([\w#!:.?+=&%@!\-/]))?}i - validates_format_of :support_url, :with => %r{\Ahttp(s?)://(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(/|/([\w#!:.?+=&%@!\-/]))?}i, :allow_blank => true - validates_format_of :callback_url, :with => %r{\A[a-z][a-z0-9.+-]*://(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(/|/([\w#!:.?+=&%@!\-/]))?}i, :allow_blank => true + validates :key, :presence => true, :uniqueness => true + validates :name, :url, :secret, :presence => true + validates :url, :format => %r{\Ahttp(s?)://(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(/|/([\w#!:.?+=&%@!\-/]))?}i + validates :support_url, :callback_url, :allow_blank => true, :format => %r{\Ahttp(s?)://(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(/|/([\w#!:.?+=&%@!\-/]))?}i before_validation :generate_keys, :on => :create diff --git a/app/models/diary_comment.rb b/app/models/diary_comment.rb index b38240db0..2796eb612 100644 --- a/app/models/diary_comment.rb +++ b/app/models/diary_comment.rb @@ -2,13 +2,13 @@ class DiaryComment < ActiveRecord::Base belongs_to :user belongs_to :diary_entry - validates_presence_of :body - validates_associated :diary_entry + validates :body, :presence => true + validates :diary_entry, :user, :associated => true after_save :spam_check def body - RichText.new(read_attribute(:body_format), read_attribute(:body)) + RichText.new(self[:body_format], self[:body]) end def digest diff --git a/app/models/diary_entry.rb b/app/models/diary_entry.rb index fa3c4c848..368ee3aca 100644 --- a/app/models/diary_entry.rb +++ b/app/models/diary_entry.rb @@ -7,19 +7,20 @@ class DiaryEntry < ActiveRecord::Base scope :visible, -> { where(:visible => true) } - validates_presence_of :title, :body - validates_length_of :title, :within => 1..255 - # validates_length_of :language, :within => 2..5, :allow_nil => false - validates_numericality_of :latitude, :allow_nil => true, - :greater_than_or_equal_to => -90, :less_than_or_equal_to => 90 - validates_numericality_of :longitude, :allow_nil => true, - :greater_than_or_equal_to => -180, :less_than_or_equal_to => 180 - validates_associated :language + validates :title, :body, :presence => true + validates :title, :length => 1..255 + validates :latitude, :allow_nil => true, + :numericality => { :greater_than_or_equal_to => -90, + :less_than_or_equal_to => 90 } + validates :longitude, :allow_nil => true, + :numericality => { :greater_than_or_equal_to => -180, + :less_than_or_equal_to => 180 } + validates :language, :user, :associated => true after_save :spam_check def body - RichText.new(read_attribute(:body_format), read_attribute(:body)) + RichText.new(self[:body_format], self[:body]) end private diff --git a/app/models/message.rb b/app/models/message.rb index 10b6bda8a..2a0cea4a1 100644 --- a/app/models/message.rb +++ b/app/models/message.rb @@ -4,10 +4,8 @@ class Message < ActiveRecord::Base belongs_to :sender, :class_name => "User", :foreign_key => :from_user_id belongs_to :recipient, :class_name => "User", :foreign_key => :to_user_id - validates_presence_of :title, :body, :sent_on, :sender, :recipient - validates_length_of :title, :within => 1..255 - validates_inclusion_of :message_read, :in => [true, false] - validates_as_utf8 :title + validates :title, :presence => true, :utf8 => true, :length => 1..255 + validates :body, :sent_on, :sender, :recipient, :presence => true def self.from_mail(mail, from, to) if mail.multipart? @@ -33,7 +31,7 @@ class Message < ActiveRecord::Base end def body - RichText.new(read_attribute(:body_format), read_attribute(:body)) + RichText.new(self[:body_format], self[:body]) end def digest diff --git a/app/models/node.rb b/app/models/node.rb index ec899fa89..f1a89e0e2 100644 --- a/app/models/node.rb +++ b/app/models/node.rb @@ -23,14 +23,21 @@ class Node < ActiveRecord::Base has_many :containing_relation_members, :class_name => "RelationMember", :as => :member has_many :containing_relations, :class_name => "Relation", :through => :containing_relation_members, :source => :relation - validates_presence_of :id, :on => :update - validates_presence_of :timestamp, :version, :changeset_id - validates_uniqueness_of :id - validates_inclusion_of :visible, :in => [true, false] - validates_numericality_of :latitude, :longitude, :changeset_id, :version, :integer_only => true - validates_numericality_of :id, :on => :update, :integer_only => true + validates :id, :uniqueness => true, :presence => { :on => :update }, + :numericality => { :on => :update, :integer_only => true } + validates :version, :presence => true, + :numericality => { :integer_only => true } + validates :changeset_id, :presence => true, + :numericality => { :integer_only => true } + validates :latitude, :presence => true, + :numericality => { :integer_only => true } + validates :longitude, :presence => true, + :numericality => { :integer_only => true } + validates :timestamp, :presence => true + validates :changeset, :associated => true + validates :visible, :inclusion => [true, false] + validate :validate_position - validates_associated :changeset scope :visible, -> { where(:visible => true) } scope :invisible, -> { where(:visible => false) } diff --git a/app/models/node_tag.rb b/app/models/node_tag.rb index cb906402c..5f2f9efc9 100644 --- a/app/models/node_tag.rb +++ b/app/models/node_tag.rb @@ -4,8 +4,7 @@ class NodeTag < ActiveRecord::Base belongs_to :node - validates_presence_of :node - validates_length_of :k, :maximum => 255, :allow_blank => true - validates_uniqueness_of :k, :scope => :node_id - validates_length_of :v, :maximum => 255, :allow_blank => true + validates :node, :presence => true, :associated => true + validates :k, :v, :allow_blank => true, :length => { :maximum => 255 } + validates :k, :uniqueness => { :scope => :node_id } end diff --git a/app/models/note.rb b/app/models/note.rb index f70e4f3bf..31056be49 100644 --- a/app/models/note.rb +++ b/app/models/note.rb @@ -3,12 +3,12 @@ class Note < ActiveRecord::Base has_many :comments, -> { where(:visible => true).order(:created_at) }, :class_name => "NoteComment", :foreign_key => :note_id - validates_presence_of :id, :on => :update - validates_uniqueness_of :id - validates_numericality_of :latitude, :only_integer => true - validates_numericality_of :longitude, :only_integer => true - validates_presence_of :closed_at if :status == "closed" - validates_inclusion_of :status, :in => %w(open closed hidden) + validates :id, :uniqueness => true, :presence => { :on => :update }, + :numericality => { :on => :update, :integer_only => true } + validates :latitude, :longitude, :numericality => { :only_integer => true } + validates :closed_at, :presence => true, :if => proc { :status == "closed" } + validates :status, :inclusion => %w(open closed hidden) + validate :validate_position scope :visible, -> { where("status != 'hidden'") } diff --git a/app/models/note_comment.rb b/app/models/note_comment.rb index 192064864..31ee6f99b 100644 --- a/app/models/note_comment.rb +++ b/app/models/note_comment.rb @@ -2,17 +2,16 @@ class NoteComment < ActiveRecord::Base belongs_to :note, :foreign_key => :note_id, :touch => true belongs_to :author, :class_name => "User", :foreign_key => :author_id - validates_presence_of :id, :on => :update - validates_uniqueness_of :id - validates_presence_of :note_id - validates_associated :note - validates_presence_of :visible - validates_associated :author - validates_inclusion_of :event, :in => %w(opened closed reopened commented hidden) - validates_format_of :body, :with => /\A[^\x00-\x08\x0b-\x0c\x0e-\x1f\x7f\ufffe\uffff]*\z/ + validates :id, :uniqueness => true, :presence => { :on => :update }, + :numericality => { :on => :update, :integer_only => true } + validates :note, :presence => true, :associated => true + validates :visible, :inclusion => [true, false] + validates :author, :associated => true + validates :event, :inclusion => %w(opened closed reopened commented hidden) + validates :body, :format => /\A[^\x00-\x08\x0b-\x0c\x0e-\x1f\x7f\ufffe\uffff]*\z/ # Return the comment text def body - RichText.new("text", read_attribute(:body)) + RichText.new("text", self[:body]) end end diff --git a/app/models/oauth2_verifier.rb b/app/models/oauth2_verifier.rb index 4dcce5588..a404d0c7a 100644 --- a/app/models/oauth2_verifier.rb +++ b/app/models/oauth2_verifier.rb @@ -1,5 +1,6 @@ class Oauth2Verifier < OauthToken - validates_presence_of :user + validates :user, :presence => true, :associated => true + attr_accessor :state def exchange!(_params = {}) diff --git a/app/models/oauth_nonce.rb b/app/models/oauth_nonce.rb index 84211e31d..4d615dd5d 100644 --- a/app/models/oauth_nonce.rb +++ b/app/models/oauth_nonce.rb @@ -1,8 +1,8 @@ # Simple store of nonces. The OAuth Spec requires that any given pair of nonce and timestamps are unique. # Thus you can use the same nonce with a different timestamp and viceversa. class OauthNonce < ActiveRecord::Base - validates_presence_of :nonce, :timestamp - validates_uniqueness_of :nonce, :scope => :timestamp + validates :timestamp, :presence => true + validates :nonce, :presence => true, :uniqueness => { :scope => :timestamp } # Remembers a nonce and it's associated timestamp. It returns false if it has already been used def self.remember(nonce, timestamp) diff --git a/app/models/oauth_token.rb b/app/models/oauth_token.rb index f2dd31df0..9a3e7de9e 100644 --- a/app/models/oauth_token.rb +++ b/app/models/oauth_token.rb @@ -4,8 +4,9 @@ class OauthToken < ActiveRecord::Base scope :authorized, -> { where("authorized_at IS NOT NULL and invalidated_at IS NULL") } - validates_uniqueness_of :token - validates_presence_of :client_application, :token + validates :token, :presence => true, :uniqueness => true + validates :user, :associated => true + validates :client_application, :presence => true before_validation :generate_keys, :on => :create diff --git a/app/models/old_node.rb b/app/models/old_node.rb index 5b5739ff2..84899d7fb 100644 --- a/app/models/old_node.rb +++ b/app/models/old_node.rb @@ -10,11 +10,15 @@ class OldNode < ActiveRecord::Base # the queries generated by Redactable will use the wrong table name. include Redactable - validates_presence_of :changeset_id, :timestamp - validates_inclusion_of :visible, :in => [true, false] - validates_numericality_of :latitude, :longitude + validates :changeset, :presence => true, :associated => true + validates :latitude, :presence => true, + :numericality => { :integer_only => true } + validates :longitude, :presence => true, + :numericality => { :integer_only => true } + validates :timestamp, :presence => true + validates :visible, :inclusion => [true, false] + validate :validate_position - validates_associated :changeset belongs_to :changeset belongs_to :redaction diff --git a/app/models/old_node_tag.rb b/app/models/old_node_tag.rb index eef1e02d5..f1df02c84 100644 --- a/app/models/old_node_tag.rb +++ b/app/models/old_node_tag.rb @@ -4,8 +4,7 @@ class OldNodeTag < ActiveRecord::Base belongs_to :old_node, :foreign_key => [:node_id, :version] - validates_presence_of :old_node - validates_length_of :k, :maximum => 255, :allow_blank => true - validates_uniqueness_of :k, :scope => [:node_id, :version] - validates_length_of :v, :maximum => 255, :allow_blank => true + validates :old_node, :presence => true, :associated => true + validates :k, :v, :allow_blank => true, :length => { :maximum => 255 } + validates :k, :uniqueness => { :scope => [:node_id, :version] } end diff --git a/app/models/old_relation.rb b/app/models/old_relation.rb index deba6ecb6..af49ddefb 100644 --- a/app/models/old_relation.rb +++ b/app/models/old_relation.rb @@ -16,7 +16,9 @@ class OldRelation < ActiveRecord::Base has_many :old_members, -> { order(:sequence_id) }, :class_name => "OldRelationMember", :foreign_key => [:relation_id, :version] has_many :old_tags, :class_name => "OldRelationTag", :foreign_key => [:relation_id, :version] - validates_associated :changeset + validates :changeset, :presence => true, :associated => true + validates :timestamp, :presence => true + validates :visible, :inclusion => [true, false] def self.from_relation(relation) old_relation = OldRelation.new diff --git a/app/models/old_relation_tag.rb b/app/models/old_relation_tag.rb index 952c51fa5..ad3b72734 100644 --- a/app/models/old_relation_tag.rb +++ b/app/models/old_relation_tag.rb @@ -4,8 +4,7 @@ class OldRelationTag < ActiveRecord::Base belongs_to :old_relation, :foreign_key => [:relation_id, :version] - validates_presence_of :old_relation - validates_length_of :k, :maximum => 255, :allow_blank => true - validates_uniqueness_of :k, :scope => [:relation_id, :version] - validates_length_of :v, :maximum => 255, :allow_blank => true + validates :old_relation, :presence => true, :associated => true + validates :k, :v, :allow_blank => true, :length => { :maximum => 255 } + validates :k, :uniqueness => { :scope => [:relation_id, :version] } end diff --git a/app/models/old_way.rb b/app/models/old_way.rb index ed1f867f6..c86590ed9 100644 --- a/app/models/old_way.rb +++ b/app/models/old_way.rb @@ -16,7 +16,9 @@ class OldWay < ActiveRecord::Base has_many :old_nodes, :class_name => "OldWayNode", :foreign_key => [:way_id, :version] has_many :old_tags, :class_name => "OldWayTag", :foreign_key => [:way_id, :version] - validates_associated :changeset + validates :changeset, :presence => true, :associated => true + validates :timestamp, :presence => true + validates :visible, :inclusion => [true, false] def self.from_way(way) old_way = OldWay.new diff --git a/app/models/old_way_tag.rb b/app/models/old_way_tag.rb index 9926db144..69bd2b186 100644 --- a/app/models/old_way_tag.rb +++ b/app/models/old_way_tag.rb @@ -4,8 +4,7 @@ class OldWayTag < ActiveRecord::Base belongs_to :old_way, :foreign_key => [:way_id, :version] - validates_presence_of :old_way - validates_length_of :k, :maximum => 255, :allow_blank => true - validates_uniqueness_of :k, :scope => [:way_id, :version] - validates_length_of :v, :maximum => 255, :allow_blank => true + validates :old_way, :presence => true, :associated => true + validates :k, :v, :allow_blank => true, :length => { :maximum => 255 } + validates :k, :uniqueness => { :scope => [:way_id, :version] } end diff --git a/app/models/redaction.rb b/app/models/redaction.rb index b3c5ec5ba..fda59f33b 100644 --- a/app/models/redaction.rb +++ b/app/models/redaction.rb @@ -17,6 +17,6 @@ class Redaction < ActiveRecord::Base # this method overrides the AR default to provide the rich # text object for the description field. def description - RichText.new(read_attribute(:description_format), read_attribute(:description)) + RichText.new(self[:description_format], self[:description]) end end diff --git a/app/models/relation.rb b/app/models/relation.rb index 97241f437..d39fd2796 100644 --- a/app/models/relation.rb +++ b/app/models/relation.rb @@ -17,13 +17,15 @@ class Relation < ActiveRecord::Base has_many :containing_relation_members, :class_name => "RelationMember", :as => :member has_many :containing_relations, :class_name => "Relation", :through => :containing_relation_members, :source => :relation - validates_presence_of :id, :on => :update - validates_presence_of :timestamp, :version, :changeset_id - validates_uniqueness_of :id - validates_inclusion_of :visible, :in => [true, false] - validates_numericality_of :id, :on => :update, :integer_only => true - validates_numericality_of :changeset_id, :version, :integer_only => true - validates_associated :changeset + validates :id, :uniqueness => true, :presence => { :on => :update }, + :numericality => { :on => :update, :integer_only => true } + validates :version, :presence => true, + :numericality => { :integer_only => true } + validates :changeset_id, :presence => true, + :numericality => { :integer_only => true } + validates :timestamp, :presence => true + validates :changeset, :associated => true + validates :visible, :inclusion => [true, false] scope :visible, -> { where(:visible => true) } scope :invisible, -> { where(:visible => false) } diff --git a/app/models/relation_tag.rb b/app/models/relation_tag.rb index c665cfef6..382606795 100644 --- a/app/models/relation_tag.rb +++ b/app/models/relation_tag.rb @@ -4,8 +4,7 @@ class RelationTag < ActiveRecord::Base belongs_to :relation - validates_presence_of :relation - validates_length_of :k, :maximum => 255, :allow_blank => true - validates_uniqueness_of :k, :scope => :relation_id - validates_length_of :v, :maximum => 255, :allow_blank => true + validates :relation, :presence => true, :associated => true + validates :k, :v, :allow_blank => true, :length => { :maximum => 255 } + validates :k, :uniqueness => { :scope => :relation_id } end diff --git a/app/models/request_token.rb b/app/models/request_token.rb index 02f168346..a1b30abb3 100644 --- a/app/models/request_token.rb +++ b/app/models/request_token.rb @@ -17,7 +17,7 @@ class RequestToken < OauthToken params = { :user => user, :client_application => client_application } # copy the permissions from the authorised request token to the access token client_application.permissions.each do |p| - params[p] = read_attribute(p) + params[p] = self[p] end access_token = AccessToken.create(params) diff --git a/app/models/trace.rb b/app/models/trace.rb index 157b63f69..994bafcf1 100644 --- a/app/models/trace.rb +++ b/app/models/trace.rb @@ -10,13 +10,11 @@ class Trace < ActiveRecord::Base scope :visible_to_all, -> { where(:visibility => %w(public identifiable)) } scope :tagged, ->(t) { joins(:tags).where(:gpx_file_tags => { :tag => t }) } - validates_presence_of :user_id, :name, :timestamp - validates_presence_of :description, :on => :create - validates_length_of :name, :maximum => 255 - validates_length_of :description, :maximum => 255 - # validates_numericality_of :latitude, :longitude - validates_inclusion_of :inserted, :in => [true, false] - validates_inclusion_of :visibility, :in => %w(private public trackable identifiable) + validates :user, :presence => true, :associated => true + validates :name, :presence => true, :length => 1..255 + validates :description, :presence => { :on => :create }, :length => 1..255 + validates :timestamp, :presence => true + validates :visibility, :inclusion => %w(private public trackable identifiable) def destroy super diff --git a/app/models/tracepoint.rb b/app/models/tracepoint.rb index 398a849d0..9485b8a14 100644 --- a/app/models/tracepoint.rb +++ b/app/models/tracepoint.rb @@ -3,11 +3,10 @@ class Tracepoint < ActiveRecord::Base self.table_name = "gps_points" - validates_numericality_of :trackid, :only_integer => true - validates_numericality_of :latitude, :only_integer => true - validates_numericality_of :longitude, :only_integer => true - validates_associated :trace - validates_presence_of :timestamp + validates :trackid, :numericality => { :only_integer => true } + validates :latitude, :longitude, :numericality => { :only_integer => true } + validates :trace, :associated => true + validates :timestamp, :presence => true belongs_to :trace, :foreign_key => "gpx_id" diff --git a/app/models/tracetag.rb b/app/models/tracetag.rb index 2614e5971..355137831 100644 --- a/app/models/tracetag.rb +++ b/app/models/tracetag.rb @@ -1,8 +1,8 @@ class Tracetag < ActiveRecord::Base self.table_name = "gpx_file_tags" - validates_format_of :tag, :with => /\A[^\/;.,?]*\z/ - validates_length_of :tag, :within => 1..255 - belongs_to :trace, :foreign_key => "gpx_id" + + validates :trace, :presence => true, :associated => true + validates :tag, :length => 1..255, :format => /\A[^\/;.,?]*\z/ end diff --git a/app/models/user.rb b/app/models/user.rb index ac293a0bd..d8c5153b1 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -34,24 +34,27 @@ class User < ActiveRecord::Base :default_url => "/assets/:class/:attachment/:style.png", :styles => { :large => "100x100>", :small => "50x50>" } - validates_presence_of :email, :display_name - validates_confirmation_of :email # , :message => ' addresses must match' - validates_confirmation_of :pass_crypt # , :message => ' must match the confirmation password' - validates_uniqueness_of :display_name, :allow_nil => true, :case_sensitive => false, :if => proc { |u| u.display_name_changed? } - validates_uniqueness_of :email, :case_sensitive => false, :if => proc { |u| u.email_changed? } - validates_length_of :pass_crypt, :within => 8..255 - validates_length_of :display_name, :within => 3..255, :allow_nil => true + validates :display_name, :presence => true, :allow_nil => true, :length => 3..255, + :exclusion => %w(new terms save confirm confirm-email go_public reset-password forgot-password suspended) + validates :display_name, :if => proc { |u| u.display_name_changed? }, + :uniqueness => { :case_sensitive => false } + validates :display_name, :if => proc { |u| u.display_name_changed? }, + :format => { :with => /\A[^\x00-\x1f\x7f\ufffe\uffff\/;.,?%#]*\z/ } + validates :display_name, :if => proc { |u| u.display_name_changed? }, + :format => { :with => /\A\S/, :message => "has leading whitespace" } + validates :display_name, :if => proc { |u| u.display_name_changed? }, + :format => { :with => /\S\z/, :message => "has trailing whitespace" } + validates :email, :presence => true, :confirmation => true + validates :email, :if => proc { |u| u.email_changed? }, + :uniqueness => { :case_sensitive => false } + validates :pass_crypt, :confirmation => true, :length => 8..255 + validates :home_lat, :home_lon, :allow_nil => true, :numericality => true + validates :home_zoom, :allow_nil => true, :numericality => { :only_integer => true } + validates :preferred_editor, :inclusion => Editors::ALL_EDITORS, :allow_nil => true + validates :image, :attachment_content_type => { :content_type => /\Aimage\/.*\Z/ } + validates_email_format_of :email, :if => proc { |u| u.email_changed? } validates_email_format_of :new_email, :allow_blank => true, :if => proc { |u| u.new_email_changed? } - validates_format_of :display_name, :with => /\A[^\x00-\x1f\x7f\ufffe\uffff\/;.,?%#]*\z/, :if => proc { |u| u.display_name_changed? } - validates_format_of :display_name, :with => /\A\S/, :message => "has leading whitespace", :if => proc { |u| u.display_name_changed? } - validates_format_of :display_name, :with => /\S\z/, :message => "has trailing whitespace", :if => proc { |u| u.display_name_changed? } - validates_exclusion_of :display_name, :in => %w(new terms save confirm confirm-email go_public reset-password forgot-password suspended) - validates_numericality_of :home_lat, :allow_nil => true - validates_numericality_of :home_lon, :allow_nil => true - validates_numericality_of :home_zoom, :only_integer => true, :allow_nil => true - validates_inclusion_of :preferred_editor, :in => Editors::ALL_EDITORS, :allow_nil => true - validates_attachment_content_type :image, :content_type => /\Aimage\/.*\Z/ after_initialize :set_defaults before_save :encrypt_password @@ -113,15 +116,15 @@ class User < ActiveRecord::Base end def description - RichText.new(read_attribute(:description_format), read_attribute(:description)) + RichText.new(self[:description_format], self[:description]) end def languages - attribute_present?(:languages) ? read_attribute(:languages).split(/ *, */) : [] + attribute_present?(:languages) ? self[:languages].split(/ *, */) : [] end def languages=(languages) - write_attribute(:languages, languages.join(",")) + self[:languages] = languages.join(",") end def preferred_language diff --git a/app/models/user_block.rb b/app/models/user_block.rb index 3759afd69..de14dcaa9 100644 --- a/app/models/user_block.rb +++ b/app/models/user_block.rb @@ -16,7 +16,7 @@ class UserBlock < ActiveRecord::Base ## # return a renderable version of the reason text. def reason - RichText.new(read_attribute(:reason_format), read_attribute(:reason)) + RichText.new(self[:reason_format], self[:reason]) end ## diff --git a/app/models/user_preference.rb b/app/models/user_preference.rb index 0533ccad2..7c65a5899 100644 --- a/app/models/user_preference.rb +++ b/app/models/user_preference.rb @@ -3,8 +3,8 @@ class UserPreference < ActiveRecord::Base belongs_to :user - validates_length_of :k, :within => 1..255 - validates_length_of :v, :within => 1..255 + validates :user, :presence => true, :associated => true + validates :k, :v, :length => 1..255 # Turn this Node in to an XML Node without the wrapper. def to_xml_node diff --git a/app/models/user_role.rb b/app/models/user_role.rb index c4d703530..9686f7250 100644 --- a/app/models/user_role.rb +++ b/app/models/user_role.rb @@ -3,6 +3,5 @@ class UserRole < ActiveRecord::Base ALL_ROLES = %w(administrator moderator) - validates_inclusion_of :role, :in => ALL_ROLES - validates_uniqueness_of :role, :scope => :user_id + validates :role, :inclusion => ALL_ROLES, :uniqueness => { :scope => :user_id } end diff --git a/app/models/way.rb b/app/models/way.rb index 9b1765d12..f0b35a88a 100644 --- a/app/models/way.rb +++ b/app/models/way.rb @@ -19,13 +19,15 @@ class Way < ActiveRecord::Base has_many :containing_relation_members, :class_name => "RelationMember", :as => :member has_many :containing_relations, :class_name => "Relation", :through => :containing_relation_members, :source => :relation - validates_presence_of :id, :on => :update - validates_presence_of :changeset_id, :version, :timestamp - validates_uniqueness_of :id - validates_inclusion_of :visible, :in => [true, false] - validates_numericality_of :changeset_id, :version, :integer_only => true - validates_numericality_of :id, :on => :update, :integer_only => true - validates_associated :changeset + validates :id, :uniqueness => true, :presence => { :on => :update }, + :numericality => { :on => :update, :integer_only => true } + validates :version, :presence => true, + :numericality => { :integer_only => true } + validates :changeset_id, :presence => true, + :numericality => { :integer_only => true } + validates :timestamp, :presence => true + validates :changeset, :associated => true + validates :visible, :inclusion => [true, false] scope :visible, -> { where(:visible => true) } scope :invisible, -> { where(:visible => false) } diff --git a/app/models/way_tag.rb b/app/models/way_tag.rb index 6204af9a5..99d435d33 100644 --- a/app/models/way_tag.rb +++ b/app/models/way_tag.rb @@ -4,8 +4,7 @@ class WayTag < ActiveRecord::Base belongs_to :way - validates_presence_of :way - validates_length_of :k, :maximum => 255, :allow_blank => true - validates_uniqueness_of :k, :scope => :way_id - validates_length_of :v, :maximum => 255, :allow_blank => true + validates :way, :presence => true, :associated => true + validates :k, :v, :allow_blank => true, :length => { :maximum => 255 } + validates :k, :uniqueness => { :scope => :way_id } end diff --git a/lib/validators.rb b/lib/validators.rb index 69cabb942..0c6b3f594 100644 --- a/lib/validators.rb +++ b/lib/validators.rb @@ -1,15 +1,9 @@ -module ActiveRecord - module Validations - module ClassMethods - ## - # validation method to be included like any other validations methods - # in the models definitions. this one checks that the named attribute - # is a valid UTF-8 format string. - def validates_as_utf8(*attrs) - validates_each(attrs) do |record, attr, value| - record.errors.add(attr, " is invalid UTF-8") unless UTF8.valid? value - end - end - end +## +# validation method to be included like any other validations methods +# in the models definitions. this one checks that the named attribute +# is a valid UTF-8 format string. +class Utf8Validator < ActiveModel::EachValidator + def validate_each(record, attribute, value) + record.errors.add(attribute, " is invalid UTF-8") unless UTF8.valid? value end end -- 2.39.5