module TheSchemaIs::Common
Attributes
model[R]
Public Class Methods
included(cls)
click to toggle source
Calls superclass method
# File lib/the_schema_is/cops.rb, line 26 def self.included(cls) cls.define_singleton_method(:badge) do RuboCop::Cop::Badge.for("TheSchemaIs::#{name.split('::').last}") end super end
Public Instance Methods
external_dependency_checksum()
click to toggle source
We need this method to tell Rubocop that EVEN if app/models/user.rb haven't changed, and .rubocop.yml haven't changed, we STILL may need to rerun the cop if schema.rb have changed.
# File lib/the_schema_is/cops.rb, line 43 def external_dependency_checksum return unless schema_path Digest::SHA1.hexdigest(File.read(schema_path)) end
on_class(node)
click to toggle source
# File lib/the_schema_is/cops.rb, line 33 def on_class(node) @model = Cops::Parser.model(node, base_classes: cop_config.fetch('BaseClass'), table_prefix: cop_config['TablePrefix']) or return register_offense(node) end
Private Instance Methods
model_columns()
click to toggle source
# File lib/the_schema_is/cops.rb, line 65 def model_columns statements = model.schema.ast_search('(block (send nil? :the_schema_is _?) _ $...)') .last.last Cops::Parser.columns(statements).to_h { |col| [col.name, col] } end
register_offense(node)
click to toggle source
# File lib/the_schema_is/cops.rb, line 53 def register_offense(node) fail NotImplementedError end
schema()
click to toggle source
# File lib/the_schema_is/cops.rb, line 61 def schema Cops.schema_cache.dig(schema_path, model.table_name) end
schema_columns()
click to toggle source
# File lib/the_schema_is/cops.rb, line 72 def schema_columns Cops::Parser.columns(schema).to_h { |col| [col.name, col] } end
schema_path()
click to toggle source
# File lib/the_schema_is/cops.rb, line 57 def schema_path cop_config.fetch('Schema') end