module RuboCop::Cop::ActiveRecordHelper
def external_dependency_checksum
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
def foreign_key_of(belongs_to)
def foreign_key_of(belongs_to) options = belongs_to.last_argument return unless options.hash_type? options.each_pair.find do |pair| next unless pair.key.sym_type? && pair.key.value == :foreign_key next unless pair.value.type?(:sym, :str) break pair.value.value.to_s end end
def in_where?(node)
def in_where?(node) send_node = node.each_ancestor(:call).first return false unless send_node return true if WHERE_METHODS.include?(send_node.method_name) receiver = send_node.receiver return false unless receiver&.send_type? send_node.method?(:not) && WHERE_METHODS.include?(receiver.method_name) end
def inherit_active_record_base?(node)
def inherit_active_record_base?(node) node.each_ancestor(:class).any? { |class_node| active_record?(class_node.parent_class) } end
def polymorphic?(belongs_to)
def polymorphic?(belongs_to) options = belongs_to.last_argument return false unless options.hash_type? options.each_pair.any? do |pair| pair.key.sym_type? && pair.key.value == :polymorphic && pair.value.true_type? end end
def resolve_relation_into_column(name:, class_node:, table:)
-
(Array, String, nil)
-
Parameters:
-
table
(RuboCop::Rails::SchemaLoader::Table
) -- -
class_node
(RuboCop::AST::Node
) -- -
name
(String
) --
def resolve_relation_into_column(name:, class_node:, table:) return unless 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" next unless table.with_column?(name: fk) return polymorphic?(belongs_to) ? [fk, "#{name}_type"] : fk end nil end
def schema
def schema # For compatibility with RuboCop 1.61.0 or lower. if respond_to?(:parser_engine) RuboCop::Rails::SchemaLoader.load(target_ruby_version, parser_engine) else RuboCop::Rails::SchemaLoader.load(target_ruby_version, :parser_whitequark) end end
def table_name(class_node)
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 class_nodes = class_node.defined_module.each_node namespaces = class_node.each_ancestor(:class, :module).map(&:identifier) [*class_nodes, *namespaces].reverse.map { |node| node.children[1] }.join('_').tableize end