From 03fdc6d67f1826b1a202eb50c284fa9202af24af Mon Sep 17 00:00:00 2001 From: Tom Hughes Date: Wed, 23 Feb 2022 19:23:51 +0000 Subject: [PATCH] Add inverse_of to relationships that can't detect it automatically --- .rubocop.yml | 3 --- .rubocop_todo.yml | 33 +++++++++++++++++++++---------- app/models/diary_entry.rb | 4 ++-- app/models/language.rb | 2 +- app/models/node.rb | 2 +- app/models/note.rb | 2 +- app/models/old_node.rb | 4 ++-- app/models/old_node_tag.rb | 2 +- app/models/old_relation.rb | 6 +++--- app/models/old_relation_member.rb | 2 +- app/models/old_relation_tag.rb | 2 +- app/models/old_way.rb | 6 +++--- app/models/old_way_node.rb | 2 +- app/models/old_way_tag.rb | 2 +- app/models/relation.rb | 4 ++-- app/models/trace.rb | 4 ++-- app/models/tracepoint.rb | 2 +- app/models/tracetag.rb | 2 +- app/models/user.rb | 18 ++++++++--------- app/models/way.rb | 4 ++-- 20 files changed, 58 insertions(+), 48 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index aa60df305..65db8a6a5 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -55,9 +55,6 @@ Rails/HasManyOrHasOneDependent: Rails/HttpPositionalArguments: Enabled: false -Rails/InverseOf: - Enabled: false - Rails/ReflectionClassName: Enabled: false diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 8c0827ab7..2527192f9 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -1,6 +1,6 @@ # This configuration was generated by # `rubocop --auto-gen-config` -# on 2021-12-28 19:44:27 UTC using RuboCop version 1.24.0. +# on 2022-02-23 19:11:04 UTC using RuboCop version 1.25.1. # The point is for the user to remove these configuration records # one by one as the offenses are removed from the code base. # Note that changes in the inspected code, or installation of new @@ -14,7 +14,7 @@ require: - rubocop-rails - rubocop-rake -# Offense count: 544 +# Offense count: 550 # Cop supports --auto-correct. # Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns. # URISchemes: http, https @@ -63,12 +63,12 @@ Lint/DuplicateBranch: - 'app/controllers/geocoder_controller.rb' - 'app/helpers/browse_tags_helper.rb' -# Offense count: 663 +# Offense count: 665 # Configuration parameters: IgnoredMethods, CountRepeatedAttributes. Metrics/AbcSize: Max: 189 -# Offense count: 72 +# Offense count: 73 # Configuration parameters: CountComments, CountAsOne, ExcludedMethods, IgnoredMethods. # IgnoredMethods: refine Metrics/BlockLength: @@ -84,12 +84,12 @@ Metrics/BlockNesting: Metrics/ClassLength: Max: 313 -# Offense count: 57 +# Offense count: 58 # Configuration parameters: IgnoredMethods. Metrics/CyclomaticComplexity: Max: 25 -# Offense count: 739 +# Offense count: 742 # Configuration parameters: CountComments, CountAsOne, ExcludedMethods, IgnoredMethods. Metrics/MethodLength: Max: 179 @@ -104,7 +104,7 @@ Metrics/ParameterLists: Metrics/PerceivedComplexity: Max: 26 -# Offense count: 549 +# Offense count: 550 Minitest/MultipleAssertions: Max: 52 @@ -155,7 +155,20 @@ Rails/HelperInstanceVariable: Exclude: - 'app/helpers/title_helper.rb' -# Offense count: 1 +# Offense count: 16 +# Configuration parameters: IgnoreScopes, Include. +# Include: app/models/**/*.rb +Rails/InverseOf: + Exclude: + - 'app/models/changeset.rb' + - 'app/models/diary_entry.rb' + - 'app/models/friendship.rb' + - 'app/models/issue.rb' + - 'app/models/message.rb' + - 'app/models/note.rb' + - 'app/models/user.rb' + +# Offense count: 2 # Configuration parameters: Include. # Include: app/controllers/**/*.rb Rails/LexicallyScopedActionFilter: @@ -183,7 +196,7 @@ Rails/OutputSafety: - 'lib/rich_text.rb' - 'test/helpers/application_helper_test.rb' -# Offense count: 23 +# Offense count: 26 # Cop supports --auto-correct. Rails/RedundantPresenceValidationOnBelongsTo: Enabled: false @@ -204,7 +217,7 @@ Rake/Desc: - 'lib/tasks/subscribe_diary_authors.rake' - 'lib/tasks/subscribe_old_changesets.rake' -# Offense count: 609 +# Offense count: 617 # Cop supports --auto-correct. # Configuration parameters: EnforcedStyle. # SupportedStyles: always, always_true, never diff --git a/app/models/diary_entry.rb b/app/models/diary_entry.rb index 4ff1eeb35..e83dfb9ee 100644 --- a/app/models/diary_entry.rb +++ b/app/models/diary_entry.rb @@ -28,9 +28,9 @@ class DiaryEntry < ApplicationRecord belongs_to :user, :counter_cache => true - belongs_to :language, :foreign_key => "language_code" + belongs_to :language, :foreign_key => "language_code", :inverse_of => :diary_entries - has_many :comments, -> { order(:id).preload(:user) }, :class_name => "DiaryComment" + has_many :comments, -> { order(:id).preload(:user) }, :class_name => "DiaryComment", :inverse_of => :diary_entry has_many :visible_comments, -> { joins(:user).where(:visible => true, :users => { :status => %w[active confirmed] }).order(:id) }, :class_name => "DiaryComment" has_many :subscriptions, :class_name => "DiaryEntrySubscription" has_many :subscribers, :through => :subscriptions, :source => :user diff --git a/app/models/language.rb b/app/models/language.rb index cdf85a52b..1158721e8 100644 --- a/app/models/language.rb +++ b/app/models/language.rb @@ -10,7 +10,7 @@ class Language < ApplicationRecord self.primary_key = "code" - has_many :diary_entries, :foreign_key => "language" + has_many :diary_entries, :foreign_key => "language", :inverse_of => :language def self.load(file) Language.transaction do diff --git a/app/models/node.rb b/app/models/node.rb index 3214564c8..f6f0f258e 100644 --- a/app/models/node.rb +++ b/app/models/node.rb @@ -32,7 +32,7 @@ class Node < ApplicationRecord belongs_to :changeset - has_many :old_nodes, -> { order(:version) } + has_many :old_nodes, -> { order(:version) }, :inverse_of => :current_node has_many :way_nodes has_many :ways, :through => :way_nodes diff --git a/app/models/note.rb b/app/models/note.rb index ea88599a4..24ba6854d 100644 --- a/app/models/note.rb +++ b/app/models/note.rb @@ -22,7 +22,7 @@ class Note < ApplicationRecord include GeoRecord has_many :comments, -> { left_joins(:author).where(:visible => true, :users => { :status => [nil, "active", "confirmed"] }).order(:created_at) }, :class_name => "NoteComment", :foreign_key => :note_id - has_many :all_comments, -> { left_joins(:author).order(:created_at) }, :class_name => "NoteComment", :foreign_key => :note_id + has_many :all_comments, -> { left_joins(:author).order(:created_at) }, :class_name => "NoteComment", :foreign_key => :note_id, :inverse_of => :note validates :id, :uniqueness => true, :presence => { :on => :update }, :numericality => { :on => :update, :only_integer => true } diff --git a/app/models/old_node.rb b/app/models/old_node.rb index 3caf0442b..1324c8139 100644 --- a/app/models/old_node.rb +++ b/app/models/old_node.rb @@ -47,9 +47,9 @@ class OldNode < ApplicationRecord belongs_to :changeset belongs_to :redaction - belongs_to :current_node, :class_name => "Node", :foreign_key => "node_id" + belongs_to :current_node, :class_name => "Node", :foreign_key => "node_id", :inverse_of => :old_nodes - has_many :old_tags, :class_name => "OldNodeTag", :foreign_key => [:node_id, :version] + has_many :old_tags, :class_name => "OldNodeTag", :foreign_key => [:node_id, :version], :inverse_of => :old_node def validate_position errors.add(:base, "Node is not in the world") unless in_world? diff --git a/app/models/old_node_tag.rb b/app/models/old_node_tag.rb index f2cba896c..11a2efd5a 100644 --- a/app/models/old_node_tag.rb +++ b/app/models/old_node_tag.rb @@ -16,7 +16,7 @@ class OldNodeTag < ApplicationRecord self.table_name = "node_tags" self.primary_keys = "node_id", "version", "k" - belongs_to :old_node, :foreign_key => [:node_id, :version] + belongs_to :old_node, :foreign_key => [:node_id, :version], :inverse_of => :old_tags validates :old_node, :presence => true, :associated => true validates :k, :v, :allow_blank => true, :length => { :maximum => 255 }, :characters => true diff --git a/app/models/old_relation.rb b/app/models/old_relation.rb index 29edb90ad..71bd4b73d 100644 --- a/app/models/old_relation.rb +++ b/app/models/old_relation.rb @@ -32,10 +32,10 @@ class OldRelation < ApplicationRecord belongs_to :changeset belongs_to :redaction - belongs_to :current_relation, :class_name => "Relation", :foreign_key => "relation_id" + belongs_to :current_relation, :class_name => "Relation", :foreign_key => "relation_id", :inverse_of => :old_relations - 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] + has_many :old_members, -> { order(:sequence_id) }, :class_name => "OldRelationMember", :foreign_key => [:relation_id, :version], :inverse_of => :old_relation + has_many :old_tags, :class_name => "OldRelationTag", :foreign_key => [:relation_id, :version], :inverse_of => :old_relation validates :changeset, :presence => true, :associated => true validates :timestamp, :presence => true diff --git a/app/models/old_relation_member.rb b/app/models/old_relation_member.rb index 1714f3c85..5bc2e22e0 100644 --- a/app/models/old_relation_member.rb +++ b/app/models/old_relation_member.rb @@ -22,7 +22,7 @@ class OldRelationMember < ApplicationRecord self.table_name = "relation_members" self.primary_keys = "relation_id", "version", "sequence_id" - belongs_to :old_relation, :foreign_key => [:relation_id, :version] + belongs_to :old_relation, :foreign_key => [:relation_id, :version], :inverse_of => :old_members # A bit messy, referring to the current tables, should do for the data browser for now belongs_to :member, :polymorphic => true end diff --git a/app/models/old_relation_tag.rb b/app/models/old_relation_tag.rb index d6e6e19c2..1fccdf7a8 100644 --- a/app/models/old_relation_tag.rb +++ b/app/models/old_relation_tag.rb @@ -16,7 +16,7 @@ class OldRelationTag < ApplicationRecord self.table_name = "relation_tags" self.primary_keys = "relation_id", "version", "k" - belongs_to :old_relation, :foreign_key => [:relation_id, :version] + belongs_to :old_relation, :foreign_key => [:relation_id, :version], :inverse_of => :old_tags validates :old_relation, :presence => true, :associated => true validates :k, :v, :allow_blank => true, :length => { :maximum => 255 }, :characters => true diff --git a/app/models/old_way.rb b/app/models/old_way.rb index 7af8906b1..1f00c46ba 100644 --- a/app/models/old_way.rb +++ b/app/models/old_way.rb @@ -32,10 +32,10 @@ class OldWay < ApplicationRecord belongs_to :changeset belongs_to :redaction - belongs_to :current_way, :class_name => "Way", :foreign_key => "way_id" + belongs_to :current_way, :class_name => "Way", :foreign_key => "way_id", :inverse_of => :old_ways - has_many :old_nodes, :class_name => "OldWayNode", :foreign_key => [:way_id, :version] - has_many :old_tags, :class_name => "OldWayTag", :foreign_key => [:way_id, :version] + has_many :old_nodes, :class_name => "OldWayNode", :foreign_key => [:way_id, :version], :inverse_of => :old_way + has_many :old_tags, :class_name => "OldWayTag", :foreign_key => [:way_id, :version], :inverse_of => :old_way validates :changeset, :presence => true, :associated => true validates :timestamp, :presence => true diff --git a/app/models/old_way_node.rb b/app/models/old_way_node.rb index d89227936..9c8c0c6f3 100644 --- a/app/models/old_way_node.rb +++ b/app/models/old_way_node.rb @@ -20,7 +20,7 @@ class OldWayNode < ApplicationRecord self.table_name = "way_nodes" self.primary_keys = "way_id", "version", "sequence_id" - belongs_to :old_way, :foreign_key => [:way_id, :version] + belongs_to :old_way, :foreign_key => [:way_id, :version], :inverse_of => :old_nodes # A bit messy, referring to current nodes and ways, should do for the data browser for now belongs_to :node belongs_to :way diff --git a/app/models/old_way_tag.rb b/app/models/old_way_tag.rb index 90bf704b6..e684199bf 100644 --- a/app/models/old_way_tag.rb +++ b/app/models/old_way_tag.rb @@ -16,7 +16,7 @@ class OldWayTag < ApplicationRecord self.table_name = "way_tags" self.primary_keys = "way_id", "version", "k" - belongs_to :old_way, :foreign_key => [:way_id, :version] + belongs_to :old_way, :foreign_key => [:way_id, :version], :inverse_of => :old_tags validates :old_way, :presence => true, :associated => true validates :k, :v, :allow_blank => true, :length => { :maximum => 255 }, :characters => true diff --git a/app/models/relation.rb b/app/models/relation.rb index 4200a08dd..61ae45c99 100644 --- a/app/models/relation.rb +++ b/app/models/relation.rb @@ -27,9 +27,9 @@ class Relation < ApplicationRecord belongs_to :changeset - has_many :old_relations, -> { order(:version) } + has_many :old_relations, -> { order(:version) }, :inverse_of => :current_relation - has_many :relation_members, -> { order(:sequence_id) } + has_many :relation_members, -> { order(:sequence_id) }, :inverse_of => :relation has_many :relation_tags has_many :containing_relation_members, :class_name => "RelationMember", :as => :member diff --git a/app/models/trace.rb b/app/models/trace.rb index bdafdd9d5..feefc0773 100644 --- a/app/models/trace.rb +++ b/app/models/trace.rb @@ -31,8 +31,8 @@ class Trace < ApplicationRecord self.table_name = "gpx_files" belongs_to :user, :counter_cache => true - has_many :tags, :class_name => "Tracetag", :foreign_key => "gpx_id", :dependent => :delete_all - has_many :points, :class_name => "Tracepoint", :foreign_key => "gpx_id", :dependent => :delete_all + has_many :tags, :class_name => "Tracetag", :foreign_key => "gpx_id", :dependent => :delete_all, :inverse_of => :trace + has_many :points, :class_name => "Tracepoint", :foreign_key => "gpx_id", :dependent => :delete_all, :inverse_of => :trace scope :visible, -> { where(:visible => true) } scope :visible_to, ->(u) { visible.where("visibility IN ('public', 'identifiable') OR user_id = ?", u) } diff --git a/app/models/tracepoint.rb b/app/models/tracepoint.rb index b3c37430d..d36ceb8c3 100644 --- a/app/models/tracepoint.rb +++ b/app/models/tracepoint.rb @@ -30,5 +30,5 @@ class Tracepoint < ApplicationRecord validates :trace, :associated => true validates :timestamp, :presence => true - belongs_to :trace, :foreign_key => "gpx_id" + belongs_to :trace, :foreign_key => "gpx_id", :inverse_of => :points end diff --git a/app/models/tracetag.rb b/app/models/tracetag.rb index f13f7269f..70296cd54 100644 --- a/app/models/tracetag.rb +++ b/app/models/tracetag.rb @@ -19,7 +19,7 @@ class Tracetag < ApplicationRecord self.table_name = "gpx_file_tags" - belongs_to :trace, :foreign_key => "gpx_id" + belongs_to :trace, :foreign_key => "gpx_id", :inverse_of => :tags validates :trace, :associated => true validates :tag, :length => 1..255, :characters => { :url_safe => true } diff --git a/app/models/user.rb b/app/models/user.rb index 7a0d06992..e7e077a9b 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -48,8 +48,8 @@ class User < ApplicationRecord include AASM has_many :traces, -> { where(:visible => true) } - has_many :diary_entries, -> { order(:created_at => :desc) } - has_many :diary_comments, -> { order(:created_at => :desc) } + has_many :diary_entries, -> { order(:created_at => :desc) }, :inverse_of => :user + has_many :diary_comments, -> { order(:created_at => :desc) }, :inverse_of => :user has_many :diary_entry_subscriptions, :class_name => "DiaryEntrySubscription" has_many :diary_subscriptions, :through => :diary_entry_subscriptions, :source => :diary_entry has_many :messages, -> { where(:to_user_visible => true).order(:sent_on => :desc).preload(:sender, :recipient) }, :foreign_key => :to_user_id @@ -59,26 +59,26 @@ class User < ApplicationRecord has_many :friends, :through => :friendships, :source => :befriendee has_many :tokens, :class_name => "UserToken", :dependent => :destroy has_many :preferences, :class_name => "UserPreference" - has_many :changesets, -> { order(:created_at => :desc) } - has_many :changeset_comments, :foreign_key => :author_id + has_many :changesets, -> { order(:created_at => :desc) }, :inverse_of => :user + has_many :changeset_comments, :foreign_key => :author_id, :inverse_of => :author has_and_belongs_to_many :changeset_subscriptions, :class_name => "Changeset", :join_table => "changesets_subscribers", :foreign_key => "subscriber_id" - has_many :note_comments, :foreign_key => :author_id + has_many :note_comments, :foreign_key => :author_id, :inverse_of => :author has_many :notes, :through => :note_comments has_many :client_applications - has_many :oauth_tokens, -> { order(:authorized_at => :desc).preload(:client_application) }, :class_name => "OauthToken" + has_many :oauth_tokens, -> { order(:authorized_at => :desc).preload(:client_application) }, :class_name => "OauthToken", :inverse_of => :user has_many :oauth2_applications, :class_name => Doorkeeper.config.application_model.name, :as => :owner has_many :access_grants, :class_name => Doorkeeper.config.access_grant_model.name, :foreign_key => :resource_owner_id has_many :access_tokens, :class_name => Doorkeeper.config.access_token_model.name, :foreign_key => :resource_owner_id has_many :blocks, :class_name => "UserBlock" - has_many :blocks_created, :class_name => "UserBlock", :foreign_key => :creator_id - has_many :blocks_revoked, :class_name => "UserBlock", :foreign_key => :revoker_id + has_many :blocks_created, :class_name => "UserBlock", :foreign_key => :creator_id, :inverse_of => :creator + has_many :blocks_revoked, :class_name => "UserBlock", :foreign_key => :revoker_id, :inverse_of => :revoker has_many :roles, :class_name => "UserRole" - has_many :issues, :class_name => "Issue", :foreign_key => :reported_user_id + has_many :issues, :class_name => "Issue", :foreign_key => :reported_user_id, :inverse_of => :reported_user has_many :issue_comments has_many :reports diff --git a/app/models/way.rb b/app/models/way.rb index 933cb7c8b..0c6e645b7 100644 --- a/app/models/way.rb +++ b/app/models/way.rb @@ -27,9 +27,9 @@ class Way < ApplicationRecord belongs_to :changeset - has_many :old_ways, -> { order(:version) } + has_many :old_ways, -> { order(:version) }, :inverse_of => :current_way - has_many :way_nodes, -> { order(:sequence_id) } + has_many :way_nodes, -> { order(:sequence_id) }, :inverse_of => :way has_many :nodes, :through => :way_nodes has_many :way_tags -- 2.43.2