class Microscope::Scope

Attributes

field[R]
model[R]

Public Class Methods

inject_scopes(model, fields, _options) click to toggle source

Inject ActiveRecord scopes into a model

# File lib/microscope/scope.rb, line 19
def self.inject_scopes(model, fields, _options)
  fields.each do |field|
    scope = "#{field.type.to_s.camelize}Scope"
    "Microscope::Scope::#{scope}".constantize.new(model, field).apply if const_defined?(scope)
  end
end
new(model, field) click to toggle source
# File lib/microscope/scope.rb, line 5
def initialize(model, field)
  @model = model
  @field = field
end

Public Instance Methods

cropped_field() click to toggle source
# File lib/microscope/scope.rb, line 14
def cropped_field
  @cropped_field ||= @field.name.gsub(@cropped_field_regex, '')
end
quoted_field() click to toggle source
# File lib/microscope/scope.rb, line 10
def quoted_field
  @quoted_field ||= "#{ActiveRecord::Base.connection.quote_table_name(@model.table_name)}.#{ActiveRecord::Base.connection.quote_column_name(@field.name)}"
end

Protected Instance Methods

blacklisted_fields() click to toggle source
# File lib/microscope/scope.rb, line 28
def blacklisted_fields
  return [] unless defined? ActiveRecord::AttributeMethods::BLACKLISTED_CLASS_METHODS

  ActiveRecord::AttributeMethods::BLACKLISTED_CLASS_METHODS
end
validate_field_name!(cropped_field_name, field_name) click to toggle source
# File lib/microscope/scope.rb, line 34
def validate_field_name!(cropped_field_name, field_name)
  raise Microscope::BlacklistedColumnsError, Microscope::BlacklistedColumnsErrorMessage % field_name if blacklisted_fields.include?(cropped_field_name)
end