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 is_null is_nil is_missing), "%s is null"], [%w(not_null not_nil not_missing is_not_null is_not_nil is_not_missing), "%s is not null"], [%w(blank not_present is_blank is_not_present), "%s is null OR %s = ''"], [%w(not_blank present is_not_blank is_present), "%s is not null AND %s <> ''"
- ORDERING_SCOPE_DEFINITIONS =
[ [/^ascend_by_/, 'asc'], [/^descend_by_/, 'desc'] ]
Instance Method Summary collapse
- #boolean_column_scopes(name, unscoped) ⇒ Object
- #equals_scope(name, unscoped) ⇒ Object
- #ordering_scopes(name, unscoped) ⇒ Object
- #scopes_with_value(name, unscoped) ⇒ Object
- #scopes_without_value(name, unscoped) ⇒ Object
Instance Method Details
#boolean_column_scopes(name, unscoped) ⇒ Object
124 125 126 127 128 |
# File 'lib/convenient_scopes.rb', line 124 def boolean_column_scopes name, unscoped str_name = name.to_s value = !str_name.gsub!(/^not_/, '') unscoped.where("#{table_name}.#{str_name}" => value) if boolean_column? str_name end |
#equals_scope(name, unscoped) ⇒ Object
119 120 121 122 |
# File 'lib/convenient_scopes.rb', line 119 def equals_scope name, unscoped return unless (column = match_suffix_and_column_name name, %w(equals eq is)) lambda {|value| unscoped.where("#{table_name}.#{column}" => value)} end |
#ordering_scopes(name, unscoped) ⇒ Object
113 114 115 116 117 |
# File 'lib/convenient_scopes.rb', line 113 def ordering_scopes name, unscoped ORDERING_SCOPE_DEFINITIONS.inject nil do |memo, definition| memo ||= match_ordering_scope name, unscoped, *definition end end |
#scopes_with_value(name, unscoped) ⇒ Object
101 102 103 104 105 |
# File 'lib/convenient_scopes.rb', line 101 def scopes_with_value name, unscoped SCOPE_DEFINITIONS.inject nil do |memo, definition| memo ||= match_and_define_scope name, unscoped, *definition end end |
#scopes_without_value(name, unscoped) ⇒ Object
107 108 109 110 111 |
# File 'lib/convenient_scopes.rb', line 107 def scopes_without_value name, unscoped SCOPE_WITHOUT_VALUE_DEFINITIONS.inject nil do |memo, definition| memo ||= match_and_define_scope_without_value name, unscoped, *definition end end |