sample_models 1.2.6 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +6 -6
- data/Gemfile.lock +22 -7
- data/README.markdown +21 -42
- data/Rakefile +10 -14
- data/VERSION +1 -1
- data/lib/sample_models/attribute_sequence.rb +182 -0
- data/lib/sample_models/creation.rb +136 -89
- data/lib/sample_models/initializer.rb +48 -0
- data/lib/sample_models/model.rb +52 -143
- data/lib/sample_models/sampler.rb +64 -88
- data/lib/sample_models.rb +15 -95
- data/sample_models.gemspec +14 -18
- data/test/setup/models.rb +205 -0
- data/test/setup/schema.rb +108 -0
- data/test/test_helper.rb +42 -0
- data/test/unit/belongs_to_test.rb +143 -0
- data/test/unit/configuration_test.rb +85 -0
- data/test/unit/has_many_through_test.rb +46 -0
- data/test/unit/named_sample_test.rb +15 -0
- data/test/unit/polymorphic_belongs_to_test.rb +29 -0
- data/test/unit/sample_test.rb +134 -0
- metadata +16 -20
- data/lib/sample_models/finder.rb +0 -86
- data/spec/sample_models_spec.rb +0 -11
- data/spec_or_test/database.yml +0 -6
- data/spec_or_test/setup.rb +0 -288
- data/spec_or_test/specs_or_test_cases.rb +0 -586
- data/test/test_sample_models.rb +0 -28
- data/vendor/ar_query/MIT-LICENSE +0 -20
- data/vendor/ar_query/README +0 -0
- data/vendor/ar_query/ar_query.gemspec +0 -16
- data/vendor/ar_query/init.rb +0 -1
- data/vendor/ar_query/install.rb +0 -1
- data/vendor/ar_query/lib/ar_query.rb +0 -146
- data/vendor/ar_query/spec/ar_query_spec.rb +0 -318
- data/vendor/ar_query/tasks/ar_query_tasks.rake +0 -0
- data/vendor/ar_query/uninstall.rb +0 -1
@@ -1,146 +0,0 @@
|
|
1
|
-
class ARQuery
|
2
|
-
attr_reader :joins
|
3
|
-
|
4
|
-
def initialize(simple_values={})
|
5
|
-
@simple_values = simple_values
|
6
|
-
@base_condition = Condition.new self
|
7
|
-
@joins = UniqueArray.new simple_values[:joins]
|
8
|
-
end
|
9
|
-
|
10
|
-
def method_missing(sym, *args)
|
11
|
-
if sym == :total_entries=
|
12
|
-
@simple_values[:total_entries] = args.first
|
13
|
-
elsif [:has_conditions?, :boolean_join=].include?(sym)
|
14
|
-
@base_condition.send(sym, *args)
|
15
|
-
else
|
16
|
-
super
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
def add_condition(&block)
|
21
|
-
if @base_condition.has_conditions?
|
22
|
-
@base_condition.add_condition do |nested|
|
23
|
-
block.call nested
|
24
|
-
end
|
25
|
-
else
|
26
|
-
block.call @base_condition
|
27
|
-
end
|
28
|
-
end
|
29
|
-
|
30
|
-
def condition_bind_vars
|
31
|
-
@base_condition.bind_vars
|
32
|
-
end
|
33
|
-
|
34
|
-
def condition_bind_vars=(arg)
|
35
|
-
@base_condition.bind_vars = arg
|
36
|
-
end
|
37
|
-
|
38
|
-
def condition_sqls
|
39
|
-
@base_condition.sqls
|
40
|
-
end
|
41
|
-
|
42
|
-
def conditions
|
43
|
-
@base_condition
|
44
|
-
end
|
45
|
-
|
46
|
-
def to_hash
|
47
|
-
hash = @simple_values.dup
|
48
|
-
hash[:conditions] = @base_condition.to_conditions if has_conditions?
|
49
|
-
joins = @joins
|
50
|
-
if includes = @simple_values[:include]
|
51
|
-
if includes.is_a?(Array)
|
52
|
-
joins = joins.reject { |join|
|
53
|
-
includes.any? { |inc| inc.to_sym == join.to_sym }
|
54
|
-
}
|
55
|
-
else
|
56
|
-
joins = joins.reject { |join| includes.to_sym == join.to_sym }
|
57
|
-
end
|
58
|
-
end
|
59
|
-
hash[:joins] = joins unless joins.empty?
|
60
|
-
hash
|
61
|
-
end
|
62
|
-
|
63
|
-
class Condition
|
64
|
-
attr_accessor :bind_vars, :boolean_join
|
65
|
-
attr_reader :ar_query, :sqls
|
66
|
-
|
67
|
-
def initialize(ar_query)
|
68
|
-
@ar_query = ar_query
|
69
|
-
@bind_vars = []
|
70
|
-
@sqls = SQLs.new
|
71
|
-
@boolean_join = :and
|
72
|
-
@children = []
|
73
|
-
end
|
74
|
-
|
75
|
-
def []=(attr, value)
|
76
|
-
@sqls << "#{attr} = ?"
|
77
|
-
@bind_vars << value
|
78
|
-
end
|
79
|
-
|
80
|
-
def add_condition(&block)
|
81
|
-
if has_conditions?
|
82
|
-
@children << Condition.new(@ar_query)
|
83
|
-
yield @children.last
|
84
|
-
else
|
85
|
-
yield self
|
86
|
-
end
|
87
|
-
end
|
88
|
-
|
89
|
-
def has_conditions?
|
90
|
-
!@sqls.empty?
|
91
|
-
end
|
92
|
-
|
93
|
-
def to_conditions
|
94
|
-
join_str = @boolean_join == :and ? ' AND ' : ' OR '
|
95
|
-
binds = @bind_vars.dup || []
|
96
|
-
condition_sql_fragments = @sqls.map { |c_sql| "(#{c_sql})" }
|
97
|
-
@children.each do |child|
|
98
|
-
sub_conditions = child.to_conditions
|
99
|
-
if sub_conditions
|
100
|
-
if sub_conditions.is_a?(Array)
|
101
|
-
sql = sub_conditions.first
|
102
|
-
sub_binds = sub_conditions[1..-1]
|
103
|
-
condition_sql_fragments << "(#{sql})"
|
104
|
-
binds.concat sub_binds
|
105
|
-
else
|
106
|
-
condition_sql_fragments << "(#{sub_conditions})"
|
107
|
-
end
|
108
|
-
end
|
109
|
-
end
|
110
|
-
condition_sql = condition_sql_fragments.join(join_str)
|
111
|
-
if binds.empty?
|
112
|
-
condition_sql unless condition_sql == ''
|
113
|
-
else
|
114
|
-
[ condition_sql, *binds ]
|
115
|
-
end
|
116
|
-
end
|
117
|
-
|
118
|
-
class SQLs < Array
|
119
|
-
def <<(elt)
|
120
|
-
if elt.is_a?(String)
|
121
|
-
super
|
122
|
-
else
|
123
|
-
raise(
|
124
|
-
ArgumentError,
|
125
|
-
"Tried appending #{elt.inspect} to ARQuery::Condition::SQLs: Only strings are allowed"
|
126
|
-
)
|
127
|
-
end
|
128
|
-
end
|
129
|
-
end
|
130
|
-
end
|
131
|
-
|
132
|
-
class UniqueArray < Array
|
133
|
-
def initialize(values)
|
134
|
-
super()
|
135
|
-
if values
|
136
|
-
values = [values] unless values.is_a?(Array)
|
137
|
-
values.each do |value| self << value; end
|
138
|
-
end
|
139
|
-
end
|
140
|
-
|
141
|
-
def <<(value)
|
142
|
-
super
|
143
|
-
uniq!
|
144
|
-
end
|
145
|
-
end
|
146
|
-
end
|
@@ -1,318 +0,0 @@
|
|
1
|
-
require File.dirname(__FILE__) + '/../lib/ar_query'
|
2
|
-
|
3
|
-
describe ARQuery do
|
4
|
-
describe '#initialize with no values' do
|
5
|
-
before :all do
|
6
|
-
ar_query = ARQuery.new
|
7
|
-
@hash = ar_query.to_hash
|
8
|
-
end
|
9
|
-
|
10
|
-
it 'should not have conditions' do
|
11
|
-
@hash[:conditions].should be_nil
|
12
|
-
end
|
13
|
-
|
14
|
-
it 'should not have joins' do
|
15
|
-
@hash[:joins].should be_nil
|
16
|
-
end
|
17
|
-
|
18
|
-
it 'should not let you assign like a hash' do
|
19
|
-
lambda {
|
20
|
-
@ar_query[:conditions] = "foo = 'bar'"
|
21
|
-
}.should raise_error(NoMethodError)
|
22
|
-
lambda {
|
23
|
-
@ar_query[:joins] = "foo = 'bar'"
|
24
|
-
}.should raise_error(NoMethodError)
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
describe '#initialize with values' do
|
29
|
-
before :all do
|
30
|
-
@ar_query = ARQuery.new(:order => 'id desc', :limit => 25)
|
31
|
-
@hash = @ar_query.to_hash
|
32
|
-
end
|
33
|
-
|
34
|
-
it 'should have those values in the hash' do
|
35
|
-
@hash[:order].should == 'id desc'
|
36
|
-
@hash[:limit].should == 25
|
37
|
-
end
|
38
|
-
|
39
|
-
it 'should not have other values in the hash' do
|
40
|
-
@hash[:conditions].should be_nil
|
41
|
-
end
|
42
|
-
end
|
43
|
-
|
44
|
-
describe "#condition_sqls <<" do
|
45
|
-
before :all do
|
46
|
-
@ar_query = ARQuery.new
|
47
|
-
@ar_query.condition_sqls << "fname is not null"
|
48
|
-
@ar_query.condition_sqls << "lname is not null"
|
49
|
-
end
|
50
|
-
|
51
|
-
it 'should join the conditions with an AND' do
|
52
|
-
@ar_query.to_hash[:conditions].should ==
|
53
|
-
"(fname is not null) AND (lname is not null)"
|
54
|
-
end
|
55
|
-
|
56
|
-
it "should prevent you from appending nil" do
|
57
|
-
lambda { @ar_query.condition_sqls << nil }.should raise_error(
|
58
|
-
ArgumentError,
|
59
|
-
"Tried appending nil to ARQuery::Condition::SQLs: Only strings are allowed"
|
60
|
-
)
|
61
|
-
end
|
62
|
-
|
63
|
-
it "should prevent you from appending a value besides a string" do
|
64
|
-
lambda { @ar_query.condition_sqls << 55 }.should raise_error(
|
65
|
-
ArgumentError,
|
66
|
-
"Tried appending 55 to ARQuery::Condition::SQLs: Only strings are allowed"
|
67
|
-
)
|
68
|
-
end
|
69
|
-
end
|
70
|
-
|
71
|
-
describe '#condition_sqls << with OR as the boolean join' do
|
72
|
-
before :all do
|
73
|
-
@ar_query = ARQuery.new
|
74
|
-
@ar_query.boolean_join = :or
|
75
|
-
@ar_query.condition_sqls << "fname is not null"
|
76
|
-
@ar_query.condition_sqls << "lname is not null"
|
77
|
-
end
|
78
|
-
|
79
|
-
it 'should join the conditions with an OR' do
|
80
|
-
@ar_query.to_hash[:conditions].should ==
|
81
|
-
"(fname is not null) OR (lname is not null)"
|
82
|
-
end
|
83
|
-
end
|
84
|
-
|
85
|
-
describe '#conditions hash usage << with OR as the boolean join' do
|
86
|
-
before :all do
|
87
|
-
@ar_query = ARQuery.new
|
88
|
-
@ar_query.boolean_join = :or
|
89
|
-
@ar_query.conditions[:fname] = 'John'
|
90
|
-
@ar_query.conditions[:lname] = 'Doe'
|
91
|
-
end
|
92
|
-
|
93
|
-
it 'should join the conditions with an OR' do
|
94
|
-
@ar_query.to_hash[:conditions].should == [
|
95
|
-
"(fname = ?) OR (lname = ?)", 'John', 'Doe'
|
96
|
-
]
|
97
|
-
end
|
98
|
-
end
|
99
|
-
|
100
|
-
describe '[:conditions]' do
|
101
|
-
describe 'with bind vars' do
|
102
|
-
before :all do
|
103
|
-
@ar_query = ARQuery.new
|
104
|
-
@ar_query.condition_sqls << "fname = ?"
|
105
|
-
@ar_query.condition_sqls << "lname = ?"
|
106
|
-
end
|
107
|
-
|
108
|
-
describe 'using appends' do
|
109
|
-
before :all do
|
110
|
-
@ar_query.condition_bind_vars << 'Francis'
|
111
|
-
@ar_query.condition_bind_vars << 'Hwang'
|
112
|
-
end
|
113
|
-
|
114
|
-
it 'should put the bind_vars at the end of the conditions array' do
|
115
|
-
@ar_query.to_hash[:conditions].should ==
|
116
|
-
[ "(fname = ?) AND (lname = ?)", 'Francis', 'Hwang' ]
|
117
|
-
end
|
118
|
-
end
|
119
|
-
|
120
|
-
describe 'using assignment' do
|
121
|
-
before :all do
|
122
|
-
@ar_query.condition_bind_vars = %w( Francis Hwang )
|
123
|
-
end
|
124
|
-
|
125
|
-
it 'should put the bind_vars at the end of the conditions array' do
|
126
|
-
@ar_query.to_hash[:conditions].should ==
|
127
|
-
[ "(fname = ?) AND (lname = ?)", 'Francis', 'Hwang' ]
|
128
|
-
end
|
129
|
-
end
|
130
|
-
end
|
131
|
-
|
132
|
-
describe 'with hash setters' do
|
133
|
-
before :all do
|
134
|
-
@ar_query = ARQuery.new
|
135
|
-
@ar_query.conditions[:fname] = 'John'
|
136
|
-
@ar_query.conditions[:lname] = 'Doe'
|
137
|
-
end
|
138
|
-
|
139
|
-
it 'should return conditions as an array with bind vars' do
|
140
|
-
@ar_query.to_hash[:conditions].should == [
|
141
|
-
"(fname = ?) AND (lname = ?)", 'John', 'Doe'
|
142
|
-
]
|
143
|
-
end
|
144
|
-
end
|
145
|
-
|
146
|
-
describe 'with a combination of hash setters and bind vars' do
|
147
|
-
before :all do
|
148
|
-
@ar_query = ARQuery.new
|
149
|
-
@ar_query.conditions[:fname] = 'John'
|
150
|
-
@ar_query.condition_sqls << "lname = ?"
|
151
|
-
@ar_query.condition_bind_vars << 'Doe'
|
152
|
-
end
|
153
|
-
|
154
|
-
it 'should return conditions as an array with bind vars' do
|
155
|
-
@ar_query.to_hash[:conditions].should == [
|
156
|
-
"(fname = ?) AND (lname = ?)", 'John', 'Doe'
|
157
|
-
]
|
158
|
-
end
|
159
|
-
end
|
160
|
-
end
|
161
|
-
|
162
|
-
describe 'with a nested condition' do
|
163
|
-
before :all do
|
164
|
-
@ar_query = ARQuery.new
|
165
|
-
@ar_query.condition_sqls << "fname = ?"
|
166
|
-
@ar_query.condition_bind_vars << 'Francis'
|
167
|
-
@ar_query.add_condition do |cond|
|
168
|
-
cond.boolean_join = :or
|
169
|
-
cond.sqls << 'lname = ?'
|
170
|
-
cond.sqls << 'lname = ?'
|
171
|
-
cond.bind_vars << 'Hwang'
|
172
|
-
cond.bind_vars << 'Bacon'
|
173
|
-
cond.ar_query.should == @ar_query
|
174
|
-
end
|
175
|
-
end
|
176
|
-
|
177
|
-
it 'should generate nested conditions in SQL' do
|
178
|
-
@ar_query.to_hash[:conditions].should == [
|
179
|
-
"(fname = ?) AND ((lname = ?) OR (lname = ?))",
|
180
|
-
'Francis', 'Hwang', 'Bacon'
|
181
|
-
]
|
182
|
-
end
|
183
|
-
end
|
184
|
-
|
185
|
-
describe 'with a nested condition using hash setters' do
|
186
|
-
before :all do
|
187
|
-
@ar_query = ARQuery.new
|
188
|
-
@ar_query.condition_sqls << "fname = ?"
|
189
|
-
@ar_query.condition_bind_vars << 'Francis'
|
190
|
-
@ar_query.add_condition do |cond|
|
191
|
-
cond.boolean_join = :or
|
192
|
-
cond[:lname] = 'Hwang'
|
193
|
-
cond[:city] = 'Brooklyn'
|
194
|
-
cond.ar_query.should == @ar_query
|
195
|
-
end
|
196
|
-
end
|
197
|
-
|
198
|
-
it 'should generate nested conditions in SQL' do
|
199
|
-
@ar_query.to_hash[:conditions].should == [
|
200
|
-
"(fname = ?) AND ((lname = ?) OR (city = ?))",
|
201
|
-
'Francis', 'Hwang', 'Brooklyn'
|
202
|
-
]
|
203
|
-
end
|
204
|
-
end
|
205
|
-
|
206
|
-
describe "when using the nested condition syntax even though the query isn't nested" do
|
207
|
-
before :all do
|
208
|
-
@ar_query = ARQuery.new
|
209
|
-
@ar_query.add_condition do |cond|
|
210
|
-
cond.boolean_join = :or
|
211
|
-
cond.sqls << "fname = ?"
|
212
|
-
cond.bind_vars << 'Chunky'
|
213
|
-
end
|
214
|
-
end
|
215
|
-
|
216
|
-
it 'should generate the non-nested condition in SQL' do
|
217
|
-
@ar_query.to_hash[:conditions].should == ["(fname = ?)", 'Chunky']
|
218
|
-
end
|
219
|
-
end
|
220
|
-
|
221
|
-
describe 'when nesting the nested condition unnecessarily' do
|
222
|
-
before :all do
|
223
|
-
@ar_query = ARQuery.new
|
224
|
-
@ar_query.add_condition do |cond|
|
225
|
-
cond.add_condition do |subcond|
|
226
|
-
subcond.boolean_join = :or
|
227
|
-
subcond.sqls << "fname = ?"
|
228
|
-
subcond.bind_vars << 'Chunky'
|
229
|
-
end
|
230
|
-
end
|
231
|
-
end
|
232
|
-
|
233
|
-
it 'should generate the non-nested condition in SQL' do
|
234
|
-
@ar_query.to_hash[:conditions].should == ["(fname = ?)", 'Chunky']
|
235
|
-
end
|
236
|
-
end
|
237
|
-
|
238
|
-
describe 'when calling an empty #add_condition after already adding to condition_sqls' do
|
239
|
-
before :all do
|
240
|
-
@ar_query = ARQuery.new
|
241
|
-
@ar_query.condition_sqls << "published = true"
|
242
|
-
@ar_query.add_condition do |cond|
|
243
|
-
end
|
244
|
-
end
|
245
|
-
|
246
|
-
it 'should not append anything to the condition' do
|
247
|
-
@ar_query.to_hash[:conditions].should == '(published = true)'
|
248
|
-
end
|
249
|
-
end
|
250
|
-
|
251
|
-
describe '#joins <<' do
|
252
|
-
describe 'when there are no joins to start' do
|
253
|
-
before :all do
|
254
|
-
@ar_query = ARQuery.new
|
255
|
-
@ar_query.joins << :user
|
256
|
-
end
|
257
|
-
|
258
|
-
it 'should result in an array of 1 join' do
|
259
|
-
@ar_query.to_hash[:joins].should == [:user]
|
260
|
-
end
|
261
|
-
end
|
262
|
-
|
263
|
-
describe 'when it was initialized with one join' do
|
264
|
-
before :all do
|
265
|
-
@ar_query = ARQuery.new :joins => :user
|
266
|
-
@ar_query.joins << :tags
|
267
|
-
end
|
268
|
-
|
269
|
-
it 'should result in an array of 2 joins' do
|
270
|
-
@ar_query.to_hash[:joins].should == [:user, :tags]
|
271
|
-
end
|
272
|
-
end
|
273
|
-
|
274
|
-
describe 'when there are already two joins' do
|
275
|
-
before :all do
|
276
|
-
@ar_query = ARQuery.new :joins => [:user, :tags]
|
277
|
-
@ar_query.joins << :images
|
278
|
-
end
|
279
|
-
|
280
|
-
it 'should result in 3 joins' do
|
281
|
-
@ar_query.to_hash[:joins].should == [:user, :tags, :images]
|
282
|
-
end
|
283
|
-
end
|
284
|
-
|
285
|
-
describe 'when a duplicate join is being appended' do
|
286
|
-
before :all do
|
287
|
-
@ar_query = ARQuery.new :joins => [:user, :tags]
|
288
|
-
@ar_query.joins << :user
|
289
|
-
end
|
290
|
-
|
291
|
-
it 'should not keep the array of joins unique' do
|
292
|
-
@ar_query.to_hash[:joins].should == [:user, :tags]
|
293
|
-
end
|
294
|
-
end
|
295
|
-
|
296
|
-
describe 'when the same association has already been :included' do
|
297
|
-
before :all do
|
298
|
-
@ar_query = ARQuery.new :include => 'user'
|
299
|
-
@ar_query.joins << :user
|
300
|
-
end
|
301
|
-
|
302
|
-
it 'should not include the association in the join' do
|
303
|
-
@ar_query.to_hash[:joins].should be_nil
|
304
|
-
end
|
305
|
-
end
|
306
|
-
end
|
307
|
-
|
308
|
-
describe '#total_entries =' do
|
309
|
-
before :all do
|
310
|
-
@ar_query = ARQuery.new
|
311
|
-
@ar_query.total_entries = 25
|
312
|
-
end
|
313
|
-
|
314
|
-
it 'should set [:total_entries]' do
|
315
|
-
@ar_query.to_hash[:total_entries].should == 25
|
316
|
-
end
|
317
|
-
end
|
318
|
-
end
|
File without changes
|
@@ -1 +0,0 @@
|
|
1
|
-
# Uninstall hook code here
|