Sha256: bba6037e6f7f545fbf9dec038d7514e3edc3d3ff8bc1bb7cdac936367ae87f82

Contents?: true

Size: 812 Bytes

Versions: 14

Compression:

Stored size: 812 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"

        if Microscope::Scope.const_defined?(scope)
          "Microscope::Scope::#{scope}".constantize.new(model, field).apply
        end
      end
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
microscope-0.6.1 lib/microscope/scope.rb
microscope-0.6 lib/microscope/scope.rb
microscope-0.5.10 lib/microscope/scope.rb
microscope-0.5.9 lib/microscope/scope.rb
microscope-0.5.8 lib/microscope/scope.rb
microscope-0.5.7 lib/microscope/scope.rb
microscope-0.5.6.1 lib/microscope/scope.rb
microscope-0.5.6 lib/microscope/scope.rb
microscope-0.5.5 lib/microscope/scope.rb
microscope-0.5.4 lib/microscope/scope.rb
microscope-0.5.3 lib/microscope/scope.rb
microscope-0.5.2 lib/microscope/scope.rb
microscope-0.5.1 lib/microscope/scope.rb
microscope-0.5 lib/microscope/scope.rb