]> git.openstreetmap.org Git - rails.git/commitdiff
Fix predicate method names in the user model
authorTom Hughes <tom@compton.nu>
Tue, 15 Aug 2023 17:53:14 +0000 (18:53 +0100)
committerTom Hughes <tom@compton.nu>
Tue, 15 Aug 2023 17:53:14 +0000 (18:53 +0100)
14 files changed:
.rubocop_todo.yml
app/controllers/diary_entries_controller.rb
app/controllers/issue_comments_controller.rb
app/controllers/user_roles_controller.rb
app/helpers/application_helper.rb
app/helpers/user_roles_helper.rb
app/models/user.rb
app/views/api/users/_user.json.jbuilder
app/views/api/users/_user.xml.builder
app/views/dashboards/_contact.html.erb
app/views/dashboards/show.html.erb
app/views/layouts/map.html.erb
app/views/profiles/edit.html.erb
test/models/user_test.rb

index 527c793545f61d6307aedff2f35df3f69cf307cc..db94a610b8fe20aecc9ebc4851cba172c0afc05f 100644 (file)
@@ -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:
index 018343d7c457dbc3d0565e106f9e6008a78fd0c9..6981ed797f92112956e2097bf7a11a1e54d9465b 100644 (file)
@@ -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
index 7edef184e782cf560a5c31d5a68b9c1550f1b88c..5bf4d023784a587613645f03891f5e2f22ae2cae 100644 (file)
@@ -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")
index fe4c855e3d827e9696e4233b2f86c57ccd4c7b16..cf5b4de9e35e2c8771673e4bd09ac7d011aeeb73 100644 (file)
@@ -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
index 4b3a22cd3955c4984011fab89dc2b9f937f49be9..28c97e485f372242369c444f12057aa405c64926 100644 (file)
@@ -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]
index a8bc06b6e699c286eed1ae5a29ddd6953e296403..c9f8e2e6549e06429b23b024fd45717c927cac10 100644 (file)
@@ -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}")
index 41f249d4e484b628ca236e7e01af2be5a4cb386e..5c21736b0f6a9cbe031056cf4ef3ebaeae297cc1 100644 (file)
@@ -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
index b8491601055207ac3609b6dcf3ba9c120b27d5f5..1a339f62805137307cdadc8a1db45a8bba0a6e80 100644 (file)
@@ -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
index 23ec1d34d6c0af80fc9b6899b84f3aed8b731973..9fb74826a017b87170ca3f0f0ac771bb433e0d97 100644 (file)
@@ -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
index aba6094b2c9a159815917d87c929a983160d48f0..0c012cec90040c9921abb64cb9805ea990c12dae 100644 (file)
@@ -11,7 +11,7 @@
   <div class="col">
     <p class='text-muted mb-0'>
       <%= 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 %>)
index eb3ceef6c528e35174acaf7c3053694f6a2741df..744661dfdbeb7bbd53a5ff812bfcf2a81b209030 100644 (file)
@@ -5,7 +5,7 @@
 <div class="row">
   <% if current_user and @user.id == current_user.id %>
     <div class="col-md order-md-last">
-      <% if !@user.has_home? %>
+      <% if !@user.home_location? %>
         <div id="map" class="content_map border border-grey">
           <p class="m-3"><%= t(".no_home_location_html", :edit_profile_link => link_to(t(".edit_your_profile"), edit_profile_path)) %></p>
         </div>
index 40f06a8990f41878c51033a95c35b277525cc5d1..377f961499cb7154f07a712701330ad867268cf3 100644 (file)
@@ -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"),
                 "#",
index cac657ff1a91cf500c9ee469ef9d8b276082fe55..bf53832c8802b6709bd15348ce0ec30d3b0685f7 100644 (file)
 
   <fieldset>
     <legend><%= t ".home location" -%></legend>
-    <p id="home_message" class="text-muted m-0<% if current_user.has_home? %> invisible<% end %>"><%= t ".no home location" %></p>
+    <p id="home_message" class="text-muted m-0<% if current_user.home_location? %> invisible<% end %>"><%= t ".no home location" %></p>
     <div class="row">
       <%= 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" %>
       <div class="col-sm-4 pt-2 align-self-end">
-        <button type="button" id="home_show" class="btn btn-outline-primary"<% unless current_user.has_home? %> hidden<% end %> disabled><%= t ".show" %></button>
-        <button type="button" id="home_delete" class="btn btn-outline-primary"<% unless current_user.has_home? %> hidden<% end %>><%= t ".delete" %></button>
+        <button type="button" id="home_show" class="btn btn-outline-primary"<% unless current_user.home_location? %> hidden<% end %> disabled><%= t ".show" %></button>
+        <button type="button" id="home_delete" class="btn btn-outline-primary"<% unless current_user.home_location? %> hidden<% end %>><%= t ".delete" %></button>
         <button type="button" id="home_undelete" class="btn btn-outline-primary" hidden><%= t ".undelete" %></button>
       </div>
     </div>
     <div class="form-check">
-      <input class="form-check-input" type="checkbox" name="updatehome" value="1" <% unless current_user.has_home? %> checked <% end %> id="updatehome" />
+      <input class="form-check-input" type="checkbox" name="updatehome" value="1" <% unless current_user.home_location? %> checked <% end %> id="updatehome" />
       <label class="form-check-label" for="updatehome"><%= t ".update home location on click" %></label>
     </div>
     <%= tag.div "", :id => "map", :class => "content_map set_location border border-grey rounded" %>
index 8329b19627942a57e2e251c3ba13c584062ae957..a4ed07e09eac0079a610f8e03762b0b89aeda105 100644 (file)
@@ -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