Module: ConvenientScopes::ScopeDefinitions

Included in:
ConvenientScopes
Defined in:
lib/convenient_scopes.rb

Constant Summary collapse

SCOPE_DEFINITIONS =
[ 
[%w(does_not_equal doesnt_equal ne is_not), "%s != ?"],
[%w(less_than lt before), "%s < ?"],
[%w(less_than_or_equal lte), "%s <= ?"],
[%w(greater_than gt after), "%s > ?"],
[%w(greater_than_or_equal gte), "%s >= ?"],
[%w(like matches contains includes), "%s like ?", "%%%s%%"],
[%w(not_like does_not_match doesnt_match does_not_contain doesnt_contain does_not_include doesnt_include), "%s not like ?", "%%%s%%"],
[%w(begins_with bw starts_with sw), "%s like ?", "%s%%"],
[%w(not_begin_with does_not_begin_with doesnt_begin_with does_not_start_with doesnt_start_with), "%s not like ?", "%s%%"],
[%w(ends_with ew), "%s like ?", "%%%s"],
[%w(not_end_with does_not_end_with doesnt_end_with), "%s not like ?", "%%%s"],
[%w(between), "%s >= ? AND %s < ?"
SCOPE_WITHOUT_VALUE_DEFINITIONS =
[
[%w(null nil missing), "%s is null"],
[%w(not_null not_nil not_missing), "%s is not null"],
[%w(blank not_present), "%s is null OR %s = ''"],
[%w(not_blank present), "%s is not null AND %s <> ''"
ORDERING_SCOPE_DEFINITIONS =
[
  [/^ascend_by_/, 'asc'],
  [/^descend_by_/, 'desc']
]

Instance Method Summary collapse

Instance Method Details

#boolean_column_scopes(name) ⇒ Object


101
102
103
104
105
# File 'lib/convenient_scopes.rb', line 101

def boolean_column_scopes name
  str_name = name.to_s
  value = !str_name.gsub!(/^not_/, '')
  unscoped.where(str_name => value) if boolean_column? str_name
end

#equals_scope(name) ⇒ Object


96
97
98
99
# File 'lib/convenient_scopes.rb', line 96

def equals_scope name
  return unless (column = match_suffix_and_column_name name, %w(equals eq is))
  lambda {|value| unscoped.where(column => value)}
end

#ordering_scopes(name) ⇒ Object


90
91
92
93
94
# File 'lib/convenient_scopes.rb', line 90

def ordering_scopes name
  ORDERING_SCOPE_DEFINITIONS.inject nil do |memo, definition|
    memo ||= match_ordering_scope name, *definition
  end
end

#scopes_with_value(name) ⇒ Object


78
79
80
81
82
# File 'lib/convenient_scopes.rb', line 78

def scopes_with_value name
  SCOPE_DEFINITIONS.inject nil do |memo, definition|
    memo ||= match_and_define_scope name, *definition
  end
end

#scopes_without_value(name) ⇒ Object


84
85
86
87
88
# File 'lib/convenient_scopes.rb', line 84

def scopes_without_value name
  SCOPE_WITHOUT_VALUE_DEFINITIONS.inject nil do |memo, definition|
    memo ||= match_and_define_scope_without_value name, *definition
  end
end