Sha256: aaf7a0c039f86fd8064a026f4af38776aee4098786e0371ad26e5053710e8561

Contents?: true

Size: 771 Bytes

Versions: 1

Compression:

Stored size: 771 Bytes

Contents

module Microscope
  class Scope < Struct.new(:model, :field)
    def initialize(*args)
      super

      @field_name = field.name
      @table_name = model.name.tableize
    end

    def quoted_field
      @quoted_field ||= "#{ActiveRecord::Base.connection.quote_table_name(@table_name)}.#{ActiveRecord::Base.connection.quote_column_name(@field_name)}"
    end

    def cropped_field
      @cropped_field ||= @field_name.gsub(@cropped_field_regex, '')
    end

    # Inject ActiveRecord scopes into a model
    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
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
microscope-0.6.2 lib/microscope/scope.rb