convenient_scopes 0.7.0 → 0.7.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/VERSION +1 -1
- data/convenient_scopes.gemspec +1 -1
- data/lib/convenient_scopes.rb +36 -39
- metadata +3 -3
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.7.
|
1
|
+
0.7.1
|
data/convenient_scopes.gemspec
CHANGED
data/lib/convenient_scopes.rb
CHANGED
@@ -3,11 +3,17 @@ module ConvenientScopes
|
|
3
3
|
def method_missing(name, *args, &block)
|
4
4
|
if scope_data = (define_scope name)
|
5
5
|
scope name, convert_to_scope_arg(scope_data)
|
6
|
-
|
6
|
+
send name, *args, &block
|
7
7
|
else
|
8
8
|
super
|
9
9
|
end
|
10
10
|
end
|
11
|
+
|
12
|
+
def define_scope name
|
13
|
+
([Conditions, Ordering].map(&:instance_methods).flatten.inject nil do |memo, scope_type|
|
14
|
+
memo ||= send scope_type.to_sym, name
|
15
|
+
end) || (association_scope name)
|
16
|
+
end
|
11
17
|
|
12
18
|
def search search_scopes
|
13
19
|
res = unscoped
|
@@ -21,16 +27,22 @@ module ConvenientScopes
|
|
21
27
|
res
|
22
28
|
end
|
23
29
|
|
24
|
-
|
30
|
+
def determine_order_scope_data name, direction
|
31
|
+
if column_names.include? name.to_s
|
32
|
+
unscoped.order("#{quoted_table_name}.#{name} #{direction}")
|
33
|
+
elsif assoc = (possible_association_for_scope name)
|
34
|
+
next_scope = extract_next_scope name, assoc
|
35
|
+
scope_arg = assoc.klass.determine_order_scope_data next_scope, direction
|
36
|
+
scope_arg.is_a?(Array) ? [assoc.name] + scope_arg : [assoc.name, scope_arg] if scope_arg
|
37
|
+
end
|
25
38
|
end
|
39
|
+
|
40
|
+
private
|
41
|
+
|
42
|
+
class InvalidScopes < Exception ; end
|
26
43
|
|
27
44
|
module Conditions
|
28
45
|
|
29
|
-
def equals_scope name
|
30
|
-
return unless (column = match_suffix_and_column_name name, %w(equals eq is))
|
31
|
-
lambda {|value| unscoped.where(column => value)}
|
32
|
-
end
|
33
|
-
|
34
46
|
SCOPE_DEFINITIONS = [
|
35
47
|
[%w(does_not_equal doesnt_equal ne is_not), "%s != ?"],
|
36
48
|
[%w(less_than lt before), "%s < ?"],
|
@@ -45,22 +57,27 @@ module ConvenientScopes
|
|
45
57
|
[%w(not_end_with does_not_end_with doesnt_end_with), "%s not like ?", "%%%s"],
|
46
58
|
[%w(between), "%s >= ? AND %s < ?"]
|
47
59
|
]
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
60
|
+
|
61
|
+
SCOPE_WITHOUT_VALUE_DEFINITIONS = [
|
62
|
+
[%w(null nil missing), "%s is null"],
|
63
|
+
[%w(not_null not_nil not_missing), "%s is not null"]
|
64
|
+
]
|
65
|
+
|
66
|
+
def scopes_with_value name
|
67
|
+
SCOPE_DEFINITIONS.inject nil do |memo, definition|
|
68
|
+
memo ||= match_and_define_scope name, *definition
|
54
69
|
end
|
55
|
-
nil
|
56
70
|
end
|
57
71
|
|
58
|
-
def
|
59
|
-
|
72
|
+
def scopes_without_value name
|
73
|
+
SCOPE_WITHOUT_VALUE_DEFINITIONS.inject nil do |memo, definition|
|
74
|
+
memo ||= match_and_define_scope_without_value name, *definition
|
75
|
+
end
|
60
76
|
end
|
61
77
|
|
62
|
-
def
|
63
|
-
|
78
|
+
def equals_scope name
|
79
|
+
return unless (column = match_suffix_and_column_name name, %w(equals eq is))
|
80
|
+
lambda {|value| unscoped.where(column => value)}
|
64
81
|
end
|
65
82
|
|
66
83
|
def boolean_column_scope name
|
@@ -70,8 +87,7 @@ module ConvenientScopes
|
|
70
87
|
|
71
88
|
def negative_boolean_column_scope name
|
72
89
|
str_name = name.to_s
|
73
|
-
return unless str_name.gsub!(/^not_/, '')
|
74
|
-
return unless boolean_column? str_name
|
90
|
+
return unless str_name.gsub!(/^not_/, '') && boolean_column?(str_name)
|
75
91
|
unscoped.where(str_name => false)
|
76
92
|
end
|
77
93
|
end
|
@@ -96,16 +112,6 @@ module ConvenientScopes
|
|
96
112
|
determine_order_scope_data str_name, direction
|
97
113
|
end
|
98
114
|
|
99
|
-
def determine_order_scope_data name, direction
|
100
|
-
if column_names.include? name.to_s
|
101
|
-
unscoped.order("#{quoted_table_name}.#{name} #{direction}")
|
102
|
-
elsif assoc = (possible_association_for_scope name)
|
103
|
-
next_scope = extract_next_scope name, assoc
|
104
|
-
scope_arg = assoc.klass.determine_order_scope_data next_scope, direction
|
105
|
-
scope_arg.is_a?(Array) ? [assoc.name] + scope_arg : [assoc.name, scope_arg] if scope_arg
|
106
|
-
end
|
107
|
-
end
|
108
|
-
|
109
115
|
include Ordering
|
110
116
|
|
111
117
|
def association_scope name
|
@@ -123,15 +129,6 @@ module ConvenientScopes
|
|
123
129
|
name.to_s.split(/^#{assoc.name}_/).last.to_sym
|
124
130
|
end
|
125
131
|
|
126
|
-
def define_scope name
|
127
|
-
[Conditions, Ordering].map(&:instance_methods).flatten.each do |scope_type|
|
128
|
-
if scope_arg = (send scope_type.to_sym, name)
|
129
|
-
return scope_arg
|
130
|
-
end
|
131
|
-
end
|
132
|
-
association_scope name
|
133
|
-
end
|
134
|
-
|
135
132
|
def match_and_define_scope name, suffixes, sql_format, value_format = nil
|
136
133
|
return unless (column = match_suffix_and_column_name name, suffixes)
|
137
134
|
sql = formatted_sql column, sql_format
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: convenient_scopes
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 1
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 7
|
9
|
-
-
|
10
|
-
version: 0.7.
|
9
|
+
- 1
|
10
|
+
version: 0.7.1
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Ivan Schneider
|