convenient_scopes 0.5.1 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/convenient_scopes.gemspec +3 -3
- data/lib/convenient_scopes.rb +16 -13
- data/test/test_conditions.rb +19 -14
- metadata +6 -6
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.6.0
|
data/convenient_scopes.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{convenient_scopes}
|
8
|
-
s.version = "0.
|
8
|
+
s.version = "0.6.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Ivan Schneider"]
|
12
|
-
s.date = %q{2011-03-
|
12
|
+
s.date = %q{2011-03-22}
|
13
13
|
s.description = %q{Dynamic scopes by convention for ActiveRecord 3}
|
14
14
|
s.email = %q{isc@massivebraingames.com}
|
15
15
|
s.extra_rdoc_files = [
|
@@ -32,7 +32,7 @@ Gem::Specification.new do |s|
|
|
32
32
|
]
|
33
33
|
s.homepage = %q{http://github.com/isc/convenient_scopes}
|
34
34
|
s.require_paths = ["lib"]
|
35
|
-
s.rubygems_version = %q{1.
|
35
|
+
s.rubygems_version = %q{1.5.2}
|
36
36
|
s.summary = %q{Dynamic scopes by convention for ActiveRecord 3}
|
37
37
|
s.test_files = [
|
38
38
|
"test/helper.rb",
|
data/lib/convenient_scopes.rb
CHANGED
@@ -70,6 +70,10 @@ module ConvenientScopes
|
|
70
70
|
match_and_define_scope_without_value name, %w(not_null not_nil not_missing), "%s is not null"
|
71
71
|
end
|
72
72
|
|
73
|
+
def between name
|
74
|
+
match_and_define_scope name, %w(between), "%s >= ? AND %s < ?"
|
75
|
+
end
|
76
|
+
|
73
77
|
def boolean_column_scope name
|
74
78
|
return unless column_names.include? name.to_s
|
75
79
|
return unless boolean_column? name
|
@@ -83,29 +87,28 @@ module ConvenientScopes
|
|
83
87
|
return unless boolean_column? str_name
|
84
88
|
unscoped.where(str_name => false)
|
85
89
|
end
|
86
|
-
|
87
90
|
end
|
88
91
|
|
89
92
|
include Conditions
|
90
93
|
|
91
94
|
module Ordering
|
92
|
-
|
95
|
+
|
93
96
|
def ascend_by_scope name
|
94
97
|
ordering_scope name, /^ascend_by_/, 'asc'
|
95
98
|
end
|
96
|
-
|
99
|
+
|
97
100
|
def descend_by_scope name
|
98
101
|
ordering_scope name, /^descend_by_/, 'desc'
|
99
102
|
end
|
100
|
-
|
103
|
+
|
101
104
|
end
|
102
|
-
|
105
|
+
|
103
106
|
def ordering_scope name, prefix, direction
|
104
107
|
str_name = name.to_s
|
105
108
|
return unless str_name.gsub!(prefix, '')
|
106
109
|
determine_order_scope_data str_name, direction
|
107
110
|
end
|
108
|
-
|
111
|
+
|
109
112
|
def determine_order_scope_data name, direction
|
110
113
|
if column_names.include? name.to_s
|
111
114
|
unscoped.order("#{quoted_table_name}.#{name} #{direction}")
|
@@ -113,9 +116,9 @@ module ConvenientScopes
|
|
113
116
|
next_scope = extract_next_scope name, assoc
|
114
117
|
scope_arg = assoc.klass.determine_order_scope_data next_scope, direction
|
115
118
|
scope_arg.is_a?(Array) ? [assoc.name] + scope_arg : [assoc.name, scope_arg] if scope_arg
|
116
|
-
end
|
119
|
+
end
|
117
120
|
end
|
118
|
-
|
121
|
+
|
119
122
|
include Ordering
|
120
123
|
|
121
124
|
def association_scope name
|
@@ -128,11 +131,11 @@ module ConvenientScopes
|
|
128
131
|
def possible_association_for_scope name
|
129
132
|
reflect_on_all_associations.detect {|assoc| name.to_s.starts_with? assoc.name.to_s}
|
130
133
|
end
|
131
|
-
|
134
|
+
|
132
135
|
def extract_next_scope name, assoc
|
133
136
|
name.to_s.split(/^#{assoc.name}_/).last.to_sym
|
134
137
|
end
|
135
|
-
|
138
|
+
|
136
139
|
def define_scope name
|
137
140
|
[Conditions, Ordering].map(&:instance_methods).flatten.each do |scope_type|
|
138
141
|
if scope_arg = (send scope_type.to_sym, name)
|
@@ -145,7 +148,7 @@ module ConvenientScopes
|
|
145
148
|
def match_and_define_scope name, suffixes, sql_format, value_format = nil
|
146
149
|
return unless (column = match_suffix_and_column_name name, suffixes)
|
147
150
|
sql = formatted_sql column, sql_format
|
148
|
-
lambda {
|
151
|
+
lambda {|*value| unscoped.where([sql, value_format ? (value_format % value) : value].flatten) }
|
149
152
|
end
|
150
153
|
|
151
154
|
def match_and_define_scope_without_value name, suffixes, sql_format
|
@@ -154,7 +157,7 @@ module ConvenientScopes
|
|
154
157
|
end
|
155
158
|
|
156
159
|
def formatted_sql column, sql_format
|
157
|
-
sql = sql_format % "#{quoted_table_name}.#{column}"
|
160
|
+
sql = sql_format % (["#{quoted_table_name}.#{column}"] * sql_format.each("%s").count)
|
158
161
|
end
|
159
162
|
|
160
163
|
def match_suffix_and_column_name name, suffixes
|
@@ -179,7 +182,7 @@ module ConvenientScopes
|
|
179
182
|
lambda {|*value| relation_or_proc.call(*value).joins joins_arg }
|
180
183
|
end
|
181
184
|
end
|
182
|
-
|
185
|
+
|
183
186
|
end
|
184
187
|
|
185
188
|
ActiveRecord::Base.extend ConvenientScopes
|
data/test/test_conditions.rb
CHANGED
@@ -8,7 +8,7 @@ class TestConditions < Test::Unit::TestCase
|
|
8
8
|
@slim = User.create :pseudo => 'Slim', :first_name => 'Angelo', :age => 12,
|
9
9
|
:activated_at => nil, :admin => false
|
10
10
|
end
|
11
|
-
|
11
|
+
|
12
12
|
should "equals scope" do
|
13
13
|
assert_raise NoMethodError do
|
14
14
|
User.blabla_eq('Bob')
|
@@ -18,60 +18,65 @@ class TestConditions < Test::Unit::TestCase
|
|
18
18
|
assert_equal [@slim], User.pseudo_is('Slim')
|
19
19
|
assert_equal [@bob], User.first_name_is('Robert')
|
20
20
|
end
|
21
|
-
|
21
|
+
|
22
22
|
should "equals scope on a relation" do
|
23
23
|
assert_equal [@bob], User.order('age').pseudo_eq('Bob')
|
24
24
|
end
|
25
|
-
|
25
|
+
|
26
26
|
should "not equals scope" do
|
27
27
|
assert_equal [@bob], User.pseudo_is_not('Slim')
|
28
28
|
assert_equal [@bob], User.first_name_does_not_equal('Angelo')
|
29
29
|
end
|
30
|
-
|
30
|
+
|
31
31
|
should "less than (or equal)? scope" do
|
32
32
|
assert_equal [@slim], User.age_lt(37)
|
33
33
|
assert_equal [@slim], User.age_less_than_or_equal(12)
|
34
34
|
end
|
35
|
-
|
35
|
+
|
36
36
|
should "greater than (or equal)? scope" do
|
37
37
|
assert_equal [@bob], User.age_greater_than(12)
|
38
38
|
assert_equal [@bob], User.age_gte(37)
|
39
39
|
assert_equal [@bob], User.activated_at_after(40.hours.ago)
|
40
40
|
end
|
41
|
-
|
41
|
+
|
42
42
|
should "like scope" do
|
43
43
|
assert_equal [@slim], User.first_name_matches('nge')
|
44
44
|
end
|
45
|
-
|
45
|
+
|
46
46
|
should "not like scope" do
|
47
47
|
assert_equal [@slim], User.pseudo_not_like('o')
|
48
48
|
end
|
49
|
-
|
49
|
+
|
50
50
|
should "begins / ends with scope" do
|
51
51
|
assert_equal [@bob], User.first_name_starts_with('Rob')
|
52
52
|
assert_equal [@slim], User.first_name_ends_with('o')
|
53
53
|
end
|
54
|
-
|
54
|
+
|
55
55
|
should "not begins / ends with scope" do
|
56
56
|
assert_equal [@slim], User.first_name_does_not_start_with('Rob')
|
57
|
-
assert_equal [@bob], User.first_name_doesnt_end_with('o')
|
57
|
+
assert_equal [@bob], User.first_name_doesnt_end_with('o')
|
58
58
|
end
|
59
|
-
|
59
|
+
|
60
60
|
should "(not)? null scope" do
|
61
61
|
assert_equal [@slim], User.activated_at_null
|
62
62
|
assert_equal [@bob], User.activated_at_not_nil
|
63
63
|
end
|
64
|
-
|
64
|
+
|
65
65
|
should "boolean columns" do
|
66
66
|
assert_equal [@bob], User.admin
|
67
67
|
assert_equal [@slim], User.not_admin
|
68
68
|
end
|
69
|
-
|
69
|
+
|
70
70
|
should "not mix up scopes" do
|
71
71
|
User.age_gt(0).pseudo_null
|
72
72
|
sql = User.pseudo_null.to_sql
|
73
73
|
assert_nil sql['age']
|
74
74
|
end
|
75
|
-
|
75
|
+
|
76
|
+
should "between" do
|
77
|
+
assert_equal [@bob], User.activated_at_between(40.hours.ago, 34.hours.ago)
|
78
|
+
assert_equal [], User.activated_at_between(40.hours.ago, 38.hours.ago)
|
79
|
+
end
|
80
|
+
|
76
81
|
end
|
77
82
|
end
|
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: 7
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
-
|
10
|
-
version: 0.
|
8
|
+
- 6
|
9
|
+
- 0
|
10
|
+
version: 0.6.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Ivan Schneider
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-03-
|
18
|
+
date: 2011-03-22 00:00:00 +01:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -114,7 +114,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
114
114
|
requirements: []
|
115
115
|
|
116
116
|
rubyforge_project:
|
117
|
-
rubygems_version: 1.
|
117
|
+
rubygems_version: 1.5.2
|
118
118
|
signing_key:
|
119
119
|
specification_version: 3
|
120
120
|
summary: Dynamic scopes by convention for ActiveRecord 3
|