Module: SimpleColumn

Defined in:
lib/simple_column/scopes.rb,
lib/simple_column/scopes/version.rb

Overview

Purpose:
Create dynamic modules which define dynamic methods for scopes based on a dynamic array of column names

Library Usage:

class Monkey < ActiveRecord::Base

include SimpleColumn::Scopes.new(:for_user_id, :for_seller_id, … etc)
# => for_user_id, and for_seller_id scopes are added to the model,
and they query on the user_id and seller_id columns, respectively

Defined Under Namespace

Classes: Scopes

Constant Summary collapse

SCOPE_PREFIX =
"for_".freeze
SCOPE_PREFIX_REGEX =
Regexp.new("\\A#{SimpleColumn::SCOPE_PREFIX}")

Class Method Summary collapse

Class Method Details

.to_mod(scope_names_hash) ⇒ Module

Returns an anonymous (nameless) Module instance.

Parameters:

  • scope_names_hash (Hash{Symbol => String})

Returns:

  • (Module)

    an anonymous (nameless) Module instance



21
22
23
24
25
26
27
28
29
30
# File 'lib/simple_column/scopes.rb', line 21

def to_mod(scope_names_hash)
  Module.new do
    scope_names_hash.each do |scope_name, column_name|
      # methods are defined at the instance level, so will become class methods when this module extends a class.
      define_method(scope_name) do |value|
        where(column_name => value)
      end
    end
  end
end