Module: RuboCop::Cop::ActiveRecordHelper
- Extended by:
- NodePattern::Macros
- Included in:
- Rails::PluckId, Rails::PluckInWhere, Rails::UniqueValidationWithoutIndex
- Defined in:
- lib/rubocop/cop/mixin/active_record_helper.rb
Overview
A mixin to extend cops for Active Record features
Constant Summary collapse
- WHERE_METHODS =
%i[where rewhere].freeze
Instance Method Summary collapse
- #external_dependency_checksum ⇒ Object
- #foreign_key_of(belongs_to) ⇒ Object
- #in_where?(node) ⇒ Boolean
-
#resolve_relation_into_column(name:, class_node:, table:) ⇒ String?
Resolve relation into column name.
- #schema ⇒ Object
- #table_name(class_node) ⇒ Object
Instance Method Details
#external_dependency_checksum ⇒ Object
19 20 21 22 23 24 25 26 27 28 |
# File 'lib/rubocop/cop/mixin/active_record_helper.rb', line 19 def external_dependency_checksum return @external_dependency_checksum if defined?(@external_dependency_checksum) schema_path = RuboCop::Rails::SchemaLoader.db_schema_path return nil if schema_path.nil? schema_code = File.read(schema_path) @external_dependency_checksum ||= Digest::SHA1.hexdigest(schema_code) end |
#foreign_key_of(belongs_to) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/rubocop/cop/mixin/active_record_helper.rb', line 66 def foreign_key_of(belongs_to) = belongs_to.last_argument return unless .hash_type? .each_pair.find do |pair| next unless pair.key.sym_type? && pair.key.value == :foreign_key next unless pair.value.sym_type? || pair.value.str_type? break pair.value.value.to_s end end |
#in_where?(node) ⇒ Boolean
78 79 80 81 |
# File 'lib/rubocop/cop/mixin/active_record_helper.rb', line 78 def in_where?(node) send_node = node.each_ancestor(:send).first send_node && WHERE_METHODS.include?(send_node.method_name) end |
#resolve_relation_into_column(name:, class_node:, table:) ⇒ String?
Resolve relation into column name. It just returns column_name if the column exists. Or it tries to resolve column_name as a relation. It returns ‘nil` if it can’t resolve.
54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/rubocop/cop/mixin/active_record_helper.rb', line 54 def resolve_relation_into_column(name:, class_node:, table:) return name if table.with_column?(name: name) find_belongs_to(class_node) do |belongs_to| next unless belongs_to.first_argument.value.to_s == name fk = foreign_key_of(belongs_to) || "#{name}_id" return fk if table.with_column?(name: fk) end nil end |
#schema ⇒ Object
30 31 32 |
# File 'lib/rubocop/cop/mixin/active_record_helper.rb', line 30 def schema RuboCop::Rails::SchemaLoader.load(target_ruby_version) end |
#table_name(class_node) ⇒ Object
34 35 36 37 38 39 40 41 42 43 |
# File 'lib/rubocop/cop/mixin/active_record_helper.rb', line 34 def table_name(class_node) table_name = find_set_table_name(class_node).to_a.last&.first_argument return table_name.value.to_s if table_name namespaces = class_node.each_ancestor(:class, :module) [class_node, *namespaces] .reverse .map { |klass| klass.identifier.children[1] }.join('_') .tableize end |