obitum-rails_admin 0.0.3 → 0.0.4

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.
Files changed (43) hide show
  1. data/Gemfile +3 -0
  2. data/README.md +19 -69
  3. data/Rakefile +6 -1
  4. data/app/assets/javascripts/rails_admin/rails_admin.js.erb +10 -5
  5. data/app/controllers/rails_admin/main_controller.rb +9 -7
  6. data/config/initializers/mongoid_extensions.rb +4 -0
  7. data/lib/rails_admin/abstract_model.rb +55 -1
  8. data/lib/rails_admin/adapters/active_record.rb +20 -38
  9. data/lib/rails_admin/adapters/mongoid.rb +333 -0
  10. data/lib/rails_admin/adapters/mongoid/abstract_object.rb +32 -0
  11. data/lib/rails_admin/adapters/mongoid/extension.rb +27 -0
  12. data/lib/rails_admin/config/fields/base.rb +4 -4
  13. data/lib/rails_admin/config/fields/factories/belongs_to_association.rb +5 -2
  14. data/lib/rails_admin/config/fields/factories/serialized.rb +1 -1
  15. data/lib/rails_admin/config/fields/types.rb +2 -1
  16. data/lib/rails_admin/config/fields/types/all.rb +2 -0
  17. data/lib/rails_admin/config/fields/types/belongs_to_association.rb +1 -1
  18. data/lib/rails_admin/config/fields/types/bson_object_id.rb +42 -0
  19. data/lib/rails_admin/config/fields/types/enum.rb +2 -2
  20. data/lib/rails_admin/config/fields/types/mongoid_type.rb +25 -0
  21. data/lib/rails_admin/config/fields/types/polymorphic_association.rb +1 -1
  22. data/lib/rails_admin/config/fields/types/serialized.rb +1 -1
  23. data/lib/rails_admin/engine.rb +1 -0
  24. data/lib/rails_admin/version.rb +1 -1
  25. data/spec/dummy_app/Gemfile +2 -0
  26. data/spec/dummy_app/app/models/article.rb +9 -0
  27. data/spec/dummy_app/app/models/author.rb +6 -0
  28. data/spec/dummy_app/app/models/mongoid_field_test.rb +22 -0
  29. data/spec/dummy_app/app/models/tag.rb +7 -0
  30. data/spec/dummy_app/config/environments/development.rb +2 -2
  31. data/spec/dummy_app/config/mongoid.yml +17 -0
  32. data/spec/dummy_app/db/seeds.rb +7 -7
  33. data/spec/factories.rb +23 -0
  34. data/spec/integration/basic/create/rails_admin_basic_create_spec.rb +13 -0
  35. data/spec/integration/basic/update/rails_admin_basic_update_spec.rb +28 -0
  36. data/spec/spec_helper.rb +13 -0
  37. data/spec/support/tableless.rb +27 -0
  38. data/spec/unit/adapters/active_record_spec.rb +335 -37
  39. data/spec/unit/adapters/mongoid/abstract_object_spec.rb +30 -0
  40. data/spec/unit/adapters/mongoid_spec.rb +581 -0
  41. data/spec/unit/config/fields/base_spec.rb +9 -0
  42. metadata +280 -44
  43. data/app/assets/javascripts/rails_admin/jquery-ui-1.8.16.custom.js +0 -5271
@@ -0,0 +1,30 @@
1
+ require 'spec_helper'
2
+ require 'rails_admin/adapters/mongoid/abstract_object'
3
+
4
+ describe "Mongoid::AbstractObject" do
5
+ before(:each) do
6
+ @articles = FactoryGirl.create_list :article, 3
7
+ @author = RailsAdmin::Adapters::Mongoid::AbstractObject.new FactoryGirl.create :author
8
+ end
9
+
10
+ describe "references_many association" do
11
+ it "supports retrieval of ids through foo_ids" do
12
+ @author.article_ids.should == []
13
+ article = FactoryGirl.create :article, :author => @author
14
+ @author.article_ids.should == [article.id]
15
+ end
16
+
17
+ it "supports assignment of items through foo_ids=" do
18
+ @author.articles.should == []
19
+ @author.article_ids = @articles.map(&:id)
20
+ @author.reload
21
+ @author.articles.sort.should == @articles.sort
22
+ end
23
+
24
+ it "skips invalid id on assignment through foo_ids=" do
25
+ @author.article_ids = @articles.map{|item| item.id.to_s }.unshift('4f431021dcf2310db7000006')
26
+ @author.reload
27
+ @author.articles.sort.should == @articles.sort
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,581 @@
1
+ require 'spec_helper'
2
+ require 'timecop'
3
+ require 'rails_admin/adapters/mongoid'
4
+
5
+ describe RailsAdmin::Adapters::Mongoid do
6
+ describe '#associations' do
7
+ before :all do
8
+ RailsAdmin::AbstractModel.reset_polymorphic_parents
9
+
10
+ class MongoBlog
11
+ include Mongoid::Document
12
+ references_many :mongo_posts
13
+ references_many :mongo_comments, :as => :commentable
14
+ end
15
+
16
+ class MongoPost
17
+ include Mongoid::Document
18
+ referenced_in :mongo_blog
19
+ has_and_belongs_to_many :mongo_categories
20
+ references_many :mongo_comments, :as => :commentable
21
+ end
22
+
23
+ class MongoCategory
24
+ include Mongoid::Document
25
+ has_and_belongs_to_many :mongo_posts
26
+ end
27
+
28
+ class MongoUser
29
+ include Mongoid::Document
30
+ references_one :mongo_profile
31
+ field :name, :type => String
32
+ field :message, :type => String
33
+ field :short_text, :type => String
34
+
35
+ validates :short_text, :length => {:maximum => 255}
36
+ end
37
+
38
+ class MongoProfile
39
+ include Mongoid::Document
40
+ referenced_in :mongo_user
41
+ end
42
+
43
+ class MongoComment
44
+ include Mongoid::Document
45
+ referenced_in :commentable, :polymorphic => true
46
+ end
47
+
48
+ @blog = RailsAdmin::AbstractModel.new(MongoBlog)
49
+ @post = RailsAdmin::AbstractModel.new(MongoPost)
50
+ @category = RailsAdmin::AbstractModel.new(MongoCategory)
51
+ @user = RailsAdmin::AbstractModel.new(MongoUser)
52
+ @profile = RailsAdmin::AbstractModel.new(MongoProfile)
53
+ @comment = RailsAdmin::AbstractModel.new(MongoComment)
54
+ end
55
+
56
+ after :all do
57
+ RailsAdmin::AbstractModel.reset_polymorphic_parents
58
+ end
59
+
60
+ it 'lists associations' do
61
+ @post.associations.map{|a|a[:name].to_s}.sort.should == ['mongo_blog', 'mongo_categories', 'mongo_comments']
62
+ end
63
+
64
+ it 'reads correct and know types in [:belongs_to, :has_and_belongs_to_many, :has_many, :has_one]' do
65
+ (@post.associations + @blog.associations + @user.associations).map{|a|a[:type].to_s}.uniq.sort.should == ['belongs_to', 'has_and_belongs_to_many', 'has_many', 'has_one']
66
+ end
67
+
68
+ it "has correct parameter of belongs_to association" do
69
+ param = @post.associations.select{|a| a[:name] == :mongo_blog}.first
70
+ param.reject{|k, v| [:primary_key_proc, :model_proc].include? k }.should == {
71
+ :name=>:mongo_blog,
72
+ :pretty_name=>"Mongo blog",
73
+ :type=>:belongs_to,
74
+ :foreign_key=>:mongo_blog_id,
75
+ :foreign_type=>nil,
76
+ :as=>nil,
77
+ :polymorphic=>false,
78
+ :inverse_of=>nil,
79
+ :read_only=>nil,
80
+ :nested_form=>nil
81
+ }
82
+ param[:primary_key_proc].call.should == :_id
83
+ param[:model_proc].call.should == MongoBlog
84
+ end
85
+
86
+ it "has correct parameter of has_many association" do
87
+ param = @blog.associations.select{|a| a[:name] == :mongo_posts}.first
88
+ param.reject{|k, v| [:primary_key_proc, :model_proc].include? k }.should == {
89
+ :name=>:mongo_posts,
90
+ :pretty_name=>"Mongo posts",
91
+ :type=>:has_many,
92
+ :foreign_key=>:mongo_blog_id,
93
+ :foreign_type=>nil,
94
+ :as=>nil,
95
+ :polymorphic=>false,
96
+ :inverse_of=>nil,
97
+ :read_only=>nil,
98
+ :nested_form=>nil
99
+ }
100
+ param[:primary_key_proc].call.should == :_id
101
+ param[:model_proc].call.should == MongoPost
102
+ end
103
+
104
+ it "has correct parameter of has_and_belongs_to_many association" do
105
+ param = @post.associations.select{|a| a[:name] == :mongo_categories}.first
106
+ param.reject{|k, v| [:primary_key_proc, :model_proc].include? k }.should == {
107
+ :name=>:mongo_categories,
108
+ :pretty_name=>"Mongo categories",
109
+ :type=>:has_and_belongs_to_many,
110
+ :foreign_key=>:mongo_category_ids,
111
+ :foreign_type=>nil,
112
+ :as=>nil,
113
+ :polymorphic=>false,
114
+ :inverse_of=>nil,
115
+ :read_only=>nil,
116
+ :nested_form=>nil
117
+ }
118
+ param[:primary_key_proc].call.should == :_id
119
+ param[:model_proc].call.should == MongoCategory
120
+ end
121
+
122
+ it "has correct parameter of polymorphic belongs_to association" do
123
+ RailsAdmin::Config.stub!(:models_pool).and_return(["MongoBlog", "MongoPost", "MongoCategory", "MongoUser", "MongoProfile", "MongoComment"])
124
+ param = @comment.associations.select{|a| a[:name] == :commentable}.first
125
+ param.reject{|k, v| [:primary_key_proc, :model_proc].include? k }.should == {
126
+ :name=>:commentable,
127
+ :pretty_name=>"Commentable",
128
+ :type=>:belongs_to,
129
+ :foreign_key=>:commentable_id,
130
+ :foreign_type=>:commentable_type,
131
+ :as=>nil,
132
+ :polymorphic=>true,
133
+ :inverse_of=>nil,
134
+ :read_only=>nil,
135
+ :nested_form=>nil
136
+ }
137
+ param[:primary_key_proc].call.should == :_id
138
+ param[:model_proc].call.should == [MongoBlog, MongoPost]
139
+ end
140
+
141
+ it "has correct parameter of polymorphic inverse has_many association" do
142
+ RailsAdmin::Config.stub!(:models_pool).and_return(["MongoBlog", "MongoPost", "MongoCategory", "MongoUser", "MongoProfile", "MongoComment"])
143
+ param = @blog.associations.select{|a| a[:name] == :mongo_comments}.first
144
+ param.reject{|k, v| [:primary_key_proc, :model_proc].include? k }.should == {
145
+ :name=>:mongo_comments,
146
+ :pretty_name=>"Mongo comments",
147
+ :type=>:has_many,
148
+ :foreign_key=>:commentable_id,
149
+ :foreign_type=>nil,
150
+ :as=>:commentable,
151
+ :polymorphic=>false,
152
+ :inverse_of=>nil,
153
+ :read_only=>nil,
154
+ :nested_form=>nil
155
+ }
156
+ param[:primary_key_proc].call.should == :_id
157
+ param[:model_proc].call.should == MongoComment
158
+ end
159
+ end
160
+
161
+ describe "#properties" do
162
+ before :all do
163
+ @abstract_model = RailsAdmin::AbstractModel.new(MongoidFieldTest)
164
+ end
165
+
166
+ it "maps Mongoid column types to RA types" do
167
+ @abstract_model.properties.sort{|a,b| a[:name].to_s <=> b[:name].to_s }.should == [
168
+ { :name => :_id,
169
+ :pretty_name => "Id",
170
+ :nullable? => true,
171
+ :serial? => false,
172
+ :type => :bson_object_id,
173
+ :length => nil },
174
+ { :name => :_type,
175
+ :pretty_name => "Type",
176
+ :nullable? => true,
177
+ :serial? => false,
178
+ :type => :mongoid_type,
179
+ :length => 1024 },
180
+ { :name => :array_field,
181
+ :pretty_name => "Array field",
182
+ :nullable? => true,
183
+ :serial? => false,
184
+ :type => :serialized,
185
+ :length => nil },
186
+ { :name => :big_decimal_field,
187
+ :pretty_name => "Big decimal field",
188
+ :nullable? => true,
189
+ :serial? => false,
190
+ :type => :string,
191
+ :length => 1024 },
192
+ { :name => :boolean_field,
193
+ :pretty_name => "Boolean field",
194
+ :nullable? => true,
195
+ :serial? => false,
196
+ :type => :boolean,
197
+ :length => nil },
198
+ { :name => :bson_object_id_field,
199
+ :pretty_name => "Bson object id field",
200
+ :nullable? => true,
201
+ :serial? => false,
202
+ :type => :bson_object_id,
203
+ :length => nil },
204
+ { :name => :date_field,
205
+ :pretty_name => "Date field",
206
+ :nullable? => true,
207
+ :serial? => false,
208
+ :type => :date,
209
+ :length => nil },
210
+ { :name => :date_time_field,
211
+ :pretty_name => "Date time field",
212
+ :nullable? => true,
213
+ :serial? => false,
214
+ :type => :datetime,
215
+ :length => nil },
216
+ { :name => :description,
217
+ :pretty_name => "Description",
218
+ :nullable? => true,
219
+ :serial? => false,
220
+ :type => :text,
221
+ :length => nil },
222
+ { :name => :float_field,
223
+ :pretty_name => "Float field",
224
+ :nullable? => true,
225
+ :serial? => false,
226
+ :type => :float,
227
+ :length => nil },
228
+ { :name => :hash_field,
229
+ :pretty_name => "Hash field",
230
+ :nullable? => true,
231
+ :serial? => false,
232
+ :type => :serialized,
233
+ :length => nil },
234
+ { :name => :integer_field,
235
+ :pretty_name => "Integer field",
236
+ :nullable? => true,
237
+ :serial? => false,
238
+ :type => :integer,
239
+ :length => nil },
240
+ { :name => :name,
241
+ :pretty_name => "Name",
242
+ :nullable? => true,
243
+ :serial? => false,
244
+ :type => :string,
245
+ :length => 255 },
246
+ { :name => :object_field,
247
+ :pretty_name => "Object field",
248
+ :nullable? => true,
249
+ :serial? => false,
250
+ :type => :bson_object_id,
251
+ :length => nil },
252
+ { :name => :short_text,
253
+ :pretty_name => "Short text",
254
+ :nullable? => true,
255
+ :serial? => false,
256
+ :type => :string,
257
+ :length => 255 },
258
+ { :name => :subject,
259
+ :pretty_name => "Subject",
260
+ :nullable? => true,
261
+ :serial? => false,
262
+ :type => :string,
263
+ :length => 255 },
264
+ { :name => :time_field,
265
+ :pretty_name => "Time field",
266
+ :nullable? => true,
267
+ :serial? => false,
268
+ :type => :datetime,
269
+ :length => nil },
270
+ { :name => :title,
271
+ :pretty_name => "Title",
272
+ :nullable? => true,
273
+ :serial? => false,
274
+ :type => :string,
275
+ :length => 255 }
276
+ ]
277
+ end
278
+ end
279
+
280
+ describe "data access method" do
281
+ before do
282
+ @articles = FactoryGirl.create_list(:article, 3)
283
+ @abstract_model = RailsAdmin::AbstractModel.new('Article')
284
+ end
285
+
286
+ it "#new returns instance of AbstractObject" do
287
+ @abstract_model.new.object.should be_instance_of(Article)
288
+ end
289
+
290
+ it "#get returns instance of AbstractObject" do
291
+ @abstract_model.get(@articles.first.id.to_s).object.should == @articles.first
292
+ end
293
+
294
+ it "#get returns nil when id does not exist" do
295
+ @abstract_model.get('4f4f0824dcf2315093000000').should be_nil
296
+ end
297
+
298
+ it "#first returns first item" do
299
+ @abstract_model.first.should == @articles.first
300
+ end
301
+
302
+ it "#count returns count of items" do
303
+ @abstract_model.count.should == @articles.count
304
+ end
305
+
306
+ it "#destroy destroys multiple items" do
307
+ @abstract_model.destroy(@articles[0..1])
308
+ Article.all.should == @articles[2..2]
309
+ end
310
+
311
+ describe "#all" do
312
+ it "works without options" do
313
+ @abstract_model.all.sort.should == @articles.sort
314
+ end
315
+
316
+ it "supports eager loading" do
317
+ @abstract_model.all(:include => :author).inclusions.map{|i| i.class_name}.should == ["Author"]
318
+ end
319
+
320
+ it "supports limiting" do
321
+ @abstract_model.all(:limit => 2).to_a.count.should == 2
322
+ end
323
+
324
+ it "supports retrieval by bulk_ids" do
325
+ @abstract_model.all(:bulk_ids => @articles[0..1].map{|article| article.id.to_s }).
326
+ sort.should == @articles[0..1].sort
327
+ end
328
+
329
+ it "supports pagination" do
330
+ @abstract_model.all(:sort => :_id, :page => 2, :per => 1).to_a.should == @articles[1..1]
331
+ # To prevent RSpec matcher to call Mongoid::Criteria#== method,
332
+ # (we want to test equality of query result, not of Mongoid criteria)
333
+ # to_a is added to invoke Mongoid query
334
+ end
335
+
336
+ it "supports ordering" do
337
+ @abstract_model.all(:sort => :_id, :sort_reverse => false).to_a.should == @articles.sort
338
+ @abstract_model.all(:sort => :_id, :sort_reverse => true).to_a.should == @articles.sort.reverse
339
+ end
340
+
341
+ it "supports querying" do
342
+ @abstract_model.all(:query => @articles[1].title).should == @articles[1..1]
343
+ end
344
+
345
+ it "supports filtering" do
346
+ @abstract_model.all(:filters => {"title" => {"0000" => {:o=>"is", :v=>@articles[1].title}}}).should == @articles[1..1]
347
+ end
348
+
349
+ it "ignores non-existent field name on filtering" do
350
+ lambda{ @abstract_model.all(:filters => {"dummy" => {"0000" => {:o=>"is", :v=>@articles[1].title}}}) }.should_not raise_error
351
+ end
352
+ end
353
+ end
354
+
355
+ describe "searching on association" do
356
+ describe "whose type is belongs_to" do
357
+ before do
358
+ RailsAdmin.config Article do
359
+ field :author do
360
+ queryable true
361
+ end
362
+ end
363
+ @articles = FactoryGirl.create_list(:article, 3)
364
+ @author = FactoryGirl.create :author, :name => 'foobar'
365
+ @author.articles << @articles[1]
366
+ @abstract_model = RailsAdmin::AbstractModel.new('Article')
367
+ end
368
+
369
+ it "supports querying" do
370
+ @abstract_model.all(:query => 'foobar').to_a.should == @articles[1..1]
371
+ end
372
+
373
+ it "supports filtering" do
374
+ @abstract_model.all(:filters => {"author" => {"0000" => {:o=>"is", :v=>'foobar'}}}).to_a.should == @articles[1..1]
375
+ end
376
+ end
377
+
378
+ describe "whose type is has_many" do
379
+ before do
380
+ RailsAdmin.config Author do
381
+ field :articles do
382
+ queryable true
383
+ searchable :all
384
+ end
385
+ end
386
+ @authors = FactoryGirl.create_list(:author, 3)
387
+ @articles = [{:author => @authors[1]},
388
+ {:author => @authors[1], :title => 'foobar'},
389
+ {:author => @authors[2]}].map{|h| FactoryGirl.create :article, h}
390
+ @abstract_model = RailsAdmin::AbstractModel.new('Author')
391
+ end
392
+
393
+ it "supports querying" do
394
+ @abstract_model.all(:query => 'foobar').to_a.should == @authors[1..1]
395
+ end
396
+
397
+ it "supports filtering" do
398
+ @abstract_model.all(:filters => {"articles" => {"0000" => {:o=>"is", :v=>'foobar'}}}).to_a.should == @authors[1..1]
399
+ end
400
+ end
401
+
402
+ describe "whose type is has_and_belongs_to_many" do
403
+ before do
404
+ RailsAdmin.config Article do
405
+ field :tags do
406
+ queryable true
407
+ searchable :all
408
+ end
409
+ end
410
+ @articles = FactoryGirl.create_list(:article, 3)
411
+ @tags = [{}, {:name => 'foobar'}, {}].map{|h| FactoryGirl.create :tag, h}
412
+ @articles[1].tags = [@tags[0], @tags[1]]
413
+ @articles[2].tags << @tags[2]
414
+ @abstract_model = RailsAdmin::AbstractModel.new('Article')
415
+ end
416
+
417
+ it "supports querying" do
418
+ @abstract_model.all(:query => 'foobar').to_a.should == @articles[1..1]
419
+ end
420
+
421
+ it "supports filtering" do
422
+ @abstract_model.all(:filters => {"tags" => {"0000" => {:o=>"is", :v=>'foobar'}}}).to_a.should == @articles[1..1]
423
+ end
424
+ end
425
+ end
426
+
427
+ describe "#query_conditions" do
428
+ before do
429
+ @abstract_model = RailsAdmin::AbstractModel.new('Article')
430
+ @articles = [{}, {:title=>'Many foos'}, {:body=>'foo shortage'}].
431
+ map{|h| FactoryGirl.create :article, h}
432
+ end
433
+
434
+ it "makes conrrect query" do
435
+ @abstract_model.all(:query => "foo").sort.should == @articles[1..2]
436
+ end
437
+ end
438
+
439
+ describe "#filter_conditions" do
440
+ before do
441
+ @abstract_model = RailsAdmin::AbstractModel.new('Article')
442
+ @author = FactoryGirl.create :author, :name => 'king of bar'
443
+ @articles = [{}, {:author=>@author}, {:title=>'Many foos', :author=>@author}, {:title=>'Great foo'}].
444
+ map{|h| FactoryGirl.create :article, h}
445
+ end
446
+
447
+ it "makes conrrect query" do
448
+ @abstract_model.all(:filters =>
449
+ {"title" => {"0000" => {:o=>"like", :v=>"foo"}},
450
+ "author" => {"0001" => {:o=>"like", :v=>"bar"}}}
451
+ ).should == [@articles[2]]
452
+ end
453
+ end
454
+
455
+ describe "#build_statement" do
456
+ before do
457
+ @abstract_model = RailsAdmin::AbstractModel.new('MongoidFieldTest')
458
+ end
459
+
460
+ it "ignores '_discard' operator or value" do
461
+ [["_discard", ""], ["", "_discard"]].each do |value, operator|
462
+ @abstract_model.send(:build_statement, :name, :string, value, operator).should be_nil
463
+ end
464
+ end
465
+
466
+ it "supports '_blank' operator" do
467
+ [["_blank", ""], ["", "_blank"]].each do |value, operator|
468
+ @abstract_model.send(:build_statement, :name, :string, value, operator).should == {:name=>{"$in"=>[nil, ""]}}
469
+ end
470
+ end
471
+
472
+ it "supports '_present' operator" do
473
+ [["_present", ""], ["", "_present"]].each do |value, operator|
474
+ @abstract_model.send(:build_statement, :name, :string, value, operator).should == {:name=>{"$nin"=>[nil, ""]}}
475
+ end
476
+ end
477
+
478
+ it "supports '_null' operator" do
479
+ [["_null", ""], ["", "_null"]].each do |value, operator|
480
+ @abstract_model.send(:build_statement, :name, :string, value, operator).should == {:name=>nil}
481
+ end
482
+ end
483
+
484
+ it "supports '_not_null' operator" do
485
+ [["_not_null", ""], ["", "_not_null"]].each do |value, operator|
486
+ @abstract_model.send(:build_statement, :name, :string, value, operator).should == {:name=>{"$ne"=>nil}}
487
+ end
488
+ end
489
+
490
+ it "supports '_empty' operator" do
491
+ [["_empty", ""], ["", "_empty"]].each do |value, operator|
492
+ @abstract_model.send(:build_statement, :name, :string, value, operator).should == {:name=>""}
493
+ end
494
+ end
495
+
496
+ it "supports '_not_empty' operator" do
497
+ [["_not_empty", ""], ["", "_not_empty"]].each do |value, operator|
498
+ @abstract_model.send(:build_statement, :name, :string, value, operator).should == {:name=>{"$ne"=>""}}
499
+ end
500
+ end
501
+
502
+ it "supports boolean type query" do
503
+ ['false', 'f', '0'].each do |value|
504
+ @abstract_model.send(:build_statement, :field, :boolean, value, nil).should == {:field => false}
505
+ end
506
+ ['true', 't', '1'].each do |value|
507
+ @abstract_model.send(:build_statement, :field, :boolean, value, nil).should == {:field => true}
508
+ end
509
+ @abstract_model.send(:build_statement, :field, :boolean, 'word', nil).should be_nil
510
+ end
511
+
512
+ it "supports integer type query" do
513
+ @abstract_model.send(:build_statement, :field, :integer, "1", nil).should == {:field => 1}
514
+ @abstract_model.send(:build_statement, :field, :integer, 'word', nil).should be_nil
515
+ end
516
+
517
+ it "supports string type query" do
518
+ @abstract_model.send(:build_statement, :field, :string, "", nil).should be_nil
519
+ @abstract_model.send(:build_statement, :field, :string, "foo", "was").should be_nil
520
+ @abstract_model.send(:build_statement, :field, :string, "foo", "default").should == {:field=>/foo/}
521
+ @abstract_model.send(:build_statement, :field, :string, "foo", "like").should == {:field=>/foo/}
522
+ @abstract_model.send(:build_statement, :field, :string, "foo", "starts_with").should == {:field=>/^foo/}
523
+ @abstract_model.send(:build_statement, :field, :string, "foo", "ends_with").should == {:field=>/foo$/}
524
+ @abstract_model.send(:build_statement, :field, :string, "foo", "is").should == {:field=>'foo'}
525
+ end
526
+
527
+ context 'filters on dates' do
528
+ it 'lists elements within outbound limits' do
529
+ date_format = I18n.t("admin.misc.filter_date_format", :default => I18n.t("admin.misc.filter_date_format", :locale => :en)).gsub('dd', '%d').gsub('mm', '%m').gsub('yy', '%Y')
530
+
531
+ MongoidFieldTest.create!(:date_field => Date.strptime("01/01/2012", date_format))
532
+ MongoidFieldTest.create!(:date_field => Date.strptime("01/02/2012", date_format))
533
+ MongoidFieldTest.create!(:date_field => Date.strptime("01/03/2012", date_format))
534
+ MongoidFieldTest.create!(:date_field => Date.strptime("01/04/2012", date_format))
535
+ @abstract_model.all(:filters => { "date_field" => { "1" => { :v => ["", "01/02/2012", "01/03/2012"], :o => 'between' } } } ).count.should == 2
536
+ @abstract_model.all(:filters => { "date_field" => { "1" => { :v => ["", "01/02/2012", "01/02/2012"], :o => 'between' } } } ).count.should == 1
537
+ @abstract_model.all(:filters => { "date_field" => { "1" => { :v => ["", "01/03/2012", ""], :o => 'between' } } } ).count.should == 2
538
+ @abstract_model.all(:filters => { "date_field" => { "1" => { :v => ["", "", "01/02/2012"], :o => 'between' } } } ).count.should == 2
539
+ @abstract_model.all(:filters => { "date_field" => { "1" => { :v => ["01/02/2012"], :o => 'default' } } } ).count.should == 1
540
+
541
+ end
542
+ end
543
+
544
+ it "supports enum type query" do
545
+ @abstract_model.send(:build_statement, :field, :enum, "1", nil).should == {:field => {"$in" => ["1"]}}
546
+ end
547
+ end
548
+
549
+ describe "model attribute method" do
550
+ before do
551
+ @abstract_model = RailsAdmin::AbstractModel.new('Article')
552
+ end
553
+
554
+ it "#scoped returns relation object" do
555
+ @abstract_model.scoped.should be_instance_of(Mongoid::Criteria)
556
+ end
557
+
558
+ it "#table_name works" do
559
+ @abstract_model.table_name.should == 'articles'
560
+ end
561
+ end
562
+
563
+ describe "serialization" do
564
+ before do
565
+ @abstract_model = RailsAdmin::AbstractModel.new('MongoidFieldTest')
566
+ @controller = RailsAdmin::MainController.new
567
+ end
568
+
569
+ it "accepts array value" do
570
+ params = {:array_field => '[1, 3]'}
571
+ @controller.send(:sanitize_params_for!, 'create', @abstract_model.config, params)
572
+ params[:array_field].should == [1, 3]
573
+ end
574
+
575
+ it "accepts hash value" do
576
+ params = {:hash_field => '{a: 1, b: 3}'}
577
+ @controller.send(:sanitize_params_for!, 'create', @abstract_model.config, params)
578
+ params[:hash_field].should == {"a"=>1, "b"=>3}
579
+ end
580
+ end
581
+ end