From 6982903ae791702453bfa3f3a1cb9bfcf342fa2d Mon Sep 17 00:00:00 2001 From: Tom Hughes Date: Tue, 15 Aug 2023 18:53:14 +0100 Subject: [PATCH] Fix predicate method names in the user model --- .rubocop_todo.yml | 10 ---------- app/controllers/diary_entries_controller.rb | 2 +- app/controllers/issue_comments_controller.rb | 2 +- app/controllers/user_roles_controller.rb | 4 ++-- app/helpers/application_helper.rb | 2 +- app/helpers/user_roles_helper.rb | 4 ++-- app/models/user.rb | 12 ++++++------ app/views/api/users/_user.json.jbuilder | 2 +- app/views/api/users/_user.xml.builder | 2 +- app/views/dashboards/_contact.html.erb | 2 +- app/views/dashboards/show.html.erb | 2 +- app/views/layouts/map.html.erb | 2 +- app/views/profiles/edit.html.erb | 8 ++++---- test/models/user_test.rb | 10 +++++----- 14 files changed, 27 insertions(+), 37 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 527c79354..db94a610b 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -92,16 +92,6 @@ Minitest/EmptyLineBeforeAssertionMethods: Minitest/MultipleAssertions: Max: 54 -# Offense count: 1 -# Configuration parameters: NamePrefix, ForbiddenPrefixes, AllowedMethods, MethodDefinitionMacros. -# NamePrefix: is_, has_, have_ -# ForbiddenPrefixes: is_, has_, have_ -# AllowedMethods: is_a? -# MethodDefinitionMacros: define_method, define_singleton_method -Naming/PredicateName: - Exclude: - - 'app/models/user.rb' - # Offense count: 1 # This cop supports unsafe autocorrection (--autocorrect-all). Rails/ActionControllerFlashBeforeRender: diff --git a/app/controllers/diary_entries_controller.rb b/app/controllers/diary_entries_controller.rb index 018343d7c..6981ed797 100644 --- a/app/controllers/diary_entries_controller.rb +++ b/app/controllers/diary_entries_controller.rb @@ -280,7 +280,7 @@ class DiaryEntriesController < ApplicationController @lon = @diary_entry.longitude @lat = @diary_entry.latitude @zoom = 12 - elsif !current_user.has_home? + elsif !current_user.home_location? @lon = params[:lon] || -0.1 @lat = params[:lat] || 51.5 @zoom = params[:zoom] || 4 diff --git a/app/controllers/issue_comments_controller.rb b/app/controllers/issue_comments_controller.rb index 7edef184e..5bf4d0237 100644 --- a/app/controllers/issue_comments_controller.rb +++ b/app/controllers/issue_comments_controller.rb @@ -19,7 +19,7 @@ class IssueCommentsController < ApplicationController reassign_issue(@issue) flash[:notice] = t ".issue_reassigned" - if current_user.has_role? @issue.assigned_role + if current_user.role? @issue.assigned_role redirect_to @issue else redirect_to issues_path(:status => "open") diff --git a/app/controllers/user_roles_controller.rb b/app/controllers/user_roles_controller.rb index fe4c855e3..cf5b4de9e 100644 --- a/app/controllers/user_roles_controller.rb +++ b/app/controllers/user_roles_controller.rb @@ -41,7 +41,7 @@ class UserRolesController < ApplicationController ## # checks that the user doesn't already have this role def not_in_role - if @user.has_role? @role + if @user.role? @role flash[:error] = t("user_role.filter.already_has_role", :role => @role) redirect_to user_path(@user) end @@ -50,7 +50,7 @@ class UserRolesController < ApplicationController ## # checks that the user already has this role def in_role - unless @user.has_role? @role + unless @user.role? @role flash[:error] = t("user_role.filter.doesnt_have_role", :role => @role) redirect_to user_path(@user) end diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 4b3a22cd3..28c97e485 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -54,7 +54,7 @@ module ApplicationHelper if current_user data[:user] = current_user.id.to_json - data[:user_home] = { :lat => current_user.home_lat, :lon => current_user.home_lon } if current_user.has_home? + data[:user_home] = { :lat => current_user.home_lat, :lon => current_user.home_lon } if current_user.home_location? end data[:location] = session[:location] if session[:location] diff --git a/app/helpers/user_roles_helper.rb b/app/helpers/user_roles_helper.rb index a8bc06b6e..c9f8e2e65 100644 --- a/app/helpers/user_roles_helper.rb +++ b/app/helpers/user_roles_helper.rb @@ -5,7 +5,7 @@ module UserRolesHelper def role_icon(user, role) if current_user&.administrator? - if user.has_role?(role) + if user.role?(role) image = "roles/#{role}" alt = t("users.show.role.revoke.#{role}") title = t("users.show.role.revoke.#{role}") @@ -18,7 +18,7 @@ module UserRolesHelper url = grant_role_path(:display_name => user.display_name, :role => role) confirm = t("user_role.grant.are_you_sure", :name => user.display_name, :role => role) end - elsif user.has_role?(role) + elsif user.role?(role) image = "roles/#{role}" alt = t("users.show.role.#{role}") title = t("users.show.role.#{role}") diff --git a/app/models/user.rb b/app/models/user.rb index 41f249d4e..5c21736b0 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -238,12 +238,12 @@ class User < ApplicationRecord @preferred_languages ||= Locale.list(languages) end - def has_home? + def home_location? home_lat && home_lon end def nearby(radius = Settings.nearby_radius, num = Settings.nearby_users) - if has_home? + if home_location? gc = OSM::GreatCircle.new(home_lat, home_lon) sql_for_area = QuadTile.sql_for_area(gc.bounds(radius), "home_") sql_for_distance = gc.sql_for_distance("home_lat", "home_lon") @@ -282,18 +282,18 @@ class User < ApplicationRecord ## # returns true if the user has the moderator role, false otherwise def moderator? - has_role? "moderator" + role? "moderator" end ## # returns true if the user has the administrator role, false otherwise def administrator? - has_role? "administrator" + role? "administrator" end ## # returns true if the user has the requested role - def has_role?(role) + def role?(role) roles.any? { |r| r.role == role } end @@ -405,6 +405,6 @@ class User < ApplicationRecord end def update_tile - self.home_tile = QuadTile.tile_for_point(home_lat, home_lon) if has_home? + self.home_tile = QuadTile.tile_for_point(home_lat, home_lon) if home_location? end end diff --git a/app/views/api/users/_user.json.jbuilder b/app/views/api/users/_user.json.jbuilder index b84916010..1a339f628 100644 --- a/app/views/api/users/_user.json.jbuilder +++ b/app/views/api/users/_user.json.jbuilder @@ -46,7 +46,7 @@ json.user do end if current_user && current_user == user && can?(:details, User) - if user.has_home? + if user.home_location? json.home do json.lat user.home_lat json.lon user.home_lon diff --git a/app/views/api/users/_user.xml.builder b/app/views/api/users/_user.xml.builder index 23ec1d34d..9fb74826a 100644 --- a/app/views/api/users/_user.xml.builder +++ b/app/views/api/users/_user.xml.builder @@ -25,7 +25,7 @@ xml.tag! "user", :id => user.id, end end if current_user && current_user == user && can?(:details, User) - if user.has_home? + if user.home_location? xml.tag! "home", :lat => user.home_lat, :lon => user.home_lon, :zoom => user.home_zoom diff --git a/app/views/dashboards/_contact.html.erb b/app/views/dashboards/_contact.html.erb index aba6094b2..0c012cec9 100644 --- a/app/views/dashboards/_contact.html.erb +++ b/app/views/dashboards/_contact.html.erb @@ -11,7 +11,7 @@

<%= link_to contact.display_name, user_path(contact) %> - <% if @user.has_home? and contact.has_home? %> + <% if @user.home_location? and contact.home_location? %> <% distance = @user.distance(contact) %> <% if distance < 1 %> (<%= t ".m away", :count => (distance * 1000).round %>) diff --git a/app/views/dashboards/show.html.erb b/app/views/dashboards/show.html.erb index eb3ceef6c..744661dfd 100644 --- a/app/views/dashboards/show.html.erb +++ b/app/views/dashboards/show.html.erb @@ -5,7 +5,7 @@

<% if current_user and @user.id == current_user.id %>
- <% if !@user.has_home? %> + <% if !@user.home_location? %>

<%= t(".no_home_location_html", :edit_profile_link => link_to(t(".edit_your_profile"), edit_profile_path)) %>

diff --git a/app/views/layouts/map.html.erb b/app/views/layouts/map.html.erb index 40f06a899..377f96149 100644 --- a/app/views/layouts/map.html.erb +++ b/app/views/layouts/map.html.erb @@ -4,7 +4,7 @@ <% content_for(:body_class) { "map-layout" } %> -<% if current_user&.has_home? %> +<% if current_user&.home_location? %> <% content_for :greeting do %> <%= link_to t("layouts.home"), "#", diff --git a/app/views/profiles/edit.html.erb b/app/views/profiles/edit.html.erb index cac657ff1..bf53832c8 100644 --- a/app/views/profiles/edit.html.erb +++ b/app/views/profiles/edit.html.erb @@ -42,18 +42,18 @@
<%= t ".home location" -%> -

<%= t ".no home location" %>

+

<%= t ".no home location" %>

<%= f.text_field :home_lat, :wrapper_class => "col-sm-4", :id => "home_lat" %> <%= f.text_field :home_lon, :wrapper_class => "col-sm-4", :id => "home_lon" %>
- - + +
- checked <% end %> id="updatehome" /> + checked <% end %> id="updatehome" />
<%= tag.div "", :id => "map", :class => "content_map set_location border border-grey rounded" %> diff --git a/test/models/user_test.rb b/test/models/user_test.rb index 8329b1962..a4ed07e09 100644 --- a/test/models/user_test.rb +++ b/test/models/user_test.rb @@ -239,11 +239,11 @@ class UserTest < ActiveSupport::TestCase assert_predicate create(:administrator_user), :administrator? end - def test_has_role? - assert_not create(:user).has_role?("administrator") - assert_not create(:user).has_role?("moderator") - assert create(:administrator_user).has_role?("administrator") - assert create(:moderator_user).has_role?("moderator") + def test_role? + assert_not create(:user).role?("administrator") + assert_not create(:user).role?("moderator") + assert create(:administrator_user).role?("administrator") + assert create(:moderator_user).role?("moderator") end def test_soft_destroy -- 2.39.5