Add inverse_of to relationships that can't detect it automatically

This commit is contained in:
Tom Hughes 2022-02-23 19:23:51 +00:00
parent 172a71e3f2
commit 03fdc6d67f
20 changed files with 58 additions and 48 deletions

View file

@ -55,9 +55,6 @@ Rails/HasManyOrHasOneDependent:
Rails/HttpPositionalArguments:
Enabled: false
Rails/InverseOf:
Enabled: false
Rails/ReflectionClassName:
Enabled: false

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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 }

View file

@ -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?

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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) }

View file

@ -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

View file

@ -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 }

View file

@ -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

View file

@ -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