mongoid-data_table 0.0.2 → 1.0.0

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/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --colour
2
+ --format documentation
3
+ --fail-fast
data/.watchr ADDED
@@ -0,0 +1,24 @@
1
+ # vim:set filetype=ruby:
2
+ def run(cmd)
3
+ puts cmd
4
+ system cmd
5
+ end
6
+
7
+ def spec(file)
8
+ if File.exists?(file)
9
+ run("bundle exec rspec #{file}")
10
+ else
11
+ puts("Spec: #{file} does not exist.")
12
+ end
13
+ end
14
+
15
+ watch("spec/.*/*_spec\.rb") do |match|
16
+ puts(match[0])
17
+ spec(match[0])
18
+ end
19
+
20
+ watch("lib/(.*/.*)\.rb") do |match|
21
+ puts(match[1])
22
+ spec("spec/unit/#{match[1]}_spec.rb")
23
+ spec("spec/functional/#{match[1]}_spec.rb")
24
+ end
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- mongoid-data_table (0.0.1)
4
+ mongoid-data_table (1.0.0)
5
5
  rails (~> 3.0.0)
6
6
  will_paginate (~> 3.0.pre2)
7
7
 
@@ -49,6 +49,7 @@ GEM
49
49
  mime-types (~> 1.16)
50
50
  treetop (~> 1.4.8)
51
51
  mime-types (1.16)
52
+ mocha (0.9.12)
52
53
  mongo (1.3.1)
53
54
  bson (>= 1.3.1)
54
55
  mongoid (2.0.1)
@@ -76,20 +77,20 @@ GEM
76
77
  rake (>= 0.8.7)
77
78
  thor (~> 0.14.4)
78
79
  rake (0.8.7)
79
- rr (1.0.2)
80
- rspec (2.5.0)
81
- rspec-core (~> 2.5.0)
82
- rspec-expectations (~> 2.5.0)
83
- rspec-mocks (~> 2.5.0)
84
- rspec-core (2.5.2)
85
- rspec-expectations (2.5.0)
80
+ rspec (2.6.0)
81
+ rspec-core (~> 2.6.0)
82
+ rspec-expectations (~> 2.6.0)
83
+ rspec-mocks (~> 2.6.0)
84
+ rspec-core (2.6.0)
85
+ rspec-expectations (2.6.0)
86
86
  diff-lcs (~> 1.1.2)
87
- rspec-mocks (2.5.0)
87
+ rspec-mocks (2.6.0)
88
88
  shoulda (2.11.3)
89
89
  thor (0.14.6)
90
90
  treetop (1.4.9)
91
91
  polyglot (>= 0.3.1)
92
92
  tzinfo (0.3.27)
93
+ watchr (0.7)
93
94
  will_paginate (3.0.pre2)
94
95
 
95
96
  PLATFORMS
@@ -97,8 +98,9 @@ PLATFORMS
97
98
 
98
99
  DEPENDENCIES
99
100
  bson_ext (~> 1.3.0)
101
+ mocha (~> 0.9.12)
100
102
  mongoid (~> 2.0.1)
101
103
  mongoid-data_table!
102
- rr (~> 1.0.2)
103
- rspec (~> 2.5.0)
104
+ rspec (~> 2.6.0)
104
105
  shoulda (~> 2.11.3)
106
+ watchr (~> 0.7)
data/LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2010 Jason Dew
1
+ Copyright (c) 2010-2011 Jason Dew, Andrew Bennett
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
data/Rakefile CHANGED
@@ -5,4 +5,6 @@ require 'rspec/core/rake_task'
5
5
  Bundler::GemHelper.install_tasks
6
6
 
7
7
  desc "Run all specs in spec directory"
8
- RSpec::Core::RakeTask.new(:spec)
8
+ RSpec::Core::RakeTask.new(:spec) do |spec|
9
+ spec.pattern = "spec/**/*_spec.rb"
10
+ end
@@ -2,7 +2,7 @@ module Mongoid
2
2
  module DataTable
3
3
  class Proxy < ::Mongoid::Relations::Proxy
4
4
 
5
- attr_reader :klass, :controller, :options, :block, :params, :criteria, :unscoped, :fields
5
+ attr_reader :klass, :controller, :options, :block, :params, :criteria, :unscoped, :fields, :aliases
6
6
 
7
7
  def initialize(klass, controller, options = {}, &block)
8
8
  @klass = klass
@@ -14,6 +14,7 @@ module Mongoid
14
14
  @criteria = options[:criteria] || klass.scoped
15
15
  @unscoped = options[:unscoped] || klass.unscoped
16
16
  @fields = options[:fields] || klass.data_table_fields
17
+ @aliases = options[:aliases] || @fields
17
18
  end
18
19
 
19
20
  def collection(force = false)
@@ -83,7 +84,22 @@ module Mongoid
83
84
  def filter_field_conditions
84
85
  order_params = params.select { |k,v| k =~ /sSearch_\d+/ }.inject({}) do |h,(k,v)|
85
86
  i = /sSearch_(\d+)/.match(k)[1]
86
- h[fields[i.to_i]] = Regexp.new(params["sSearch_#{i}"]) if params["sSearch_#{i}"].present?
87
+
88
+ field_name = fields[i.to_i]
89
+ field = klass.fields[field_name]
90
+ field_type = field.respond_to?(:type) ? field.type : String
91
+
92
+ query = params["sSearch_#{i}"]
93
+
94
+ h[field_name] = (if [ Array, String, Symbol ].include?(field_type)
95
+ begin
96
+ Regexp.new(query)
97
+ rescue RegexpError
98
+ Regexp.new(Regexp.escape(query))
99
+ end
100
+ else
101
+ query
102
+ end) if query.present?
87
103
  h
88
104
  end
89
105
  end
@@ -92,7 +108,7 @@ module Mongoid
92
108
 
93
109
  def default_data_table_block
94
110
  lambda do |object|
95
- Hash[fields.map { |c| [ fields.index(c), object.send(c) ] }].merge(:DT_RowId => object._id)
111
+ Hash[aliases.map { |c| [ aliases.index(c), object.send(c) ] }].merge(:DT_RowId => object._id)
96
112
  end
97
113
  end
98
114
 
@@ -1,5 +1,5 @@
1
1
  module Mongoid
2
2
  module DataTable
3
- VERSION = "0.0.2"
3
+ VERSION = "1.0.0"
4
4
  end
5
5
  end
@@ -1,10 +1,12 @@
1
- require 'rails'
2
1
  require 'active_support/core_ext/object/blank'
3
2
  require 'active_support/core_ext/object/to_json'
4
3
  require 'active_support/json/encoding'
5
4
  require 'active_support/core_ext/string/output_safety'
6
5
  require 'active_support/core_ext/string/inflections'
7
6
  require 'active_support/core_ext/class/inheritable_attributes'
7
+ require 'active_support/concern'
8
+
9
+ require 'mongoid/relations/proxy'
8
10
 
9
11
  require 'mongoid/data_table/proxy'
10
12
  require 'mongoid/data_table/version'
@@ -22,7 +24,7 @@ module Mongoid
22
24
  module ClassMethods
23
25
 
24
26
  def data_table_fields
25
- self.data_table_options[:fields] ||= []
27
+ self.data_table_options[:fields] ||= self.fields.dup.stringify_keys.keys.reject { |k| Mongoid.destructive_fields.include?(k) }
26
28
  end
27
29
 
28
30
  def data_table_searchable_fields
@@ -36,7 +38,7 @@ module Mongoid
36
38
  def to_data_table(controller, options = {}, explicit_block = nil, &implicit_block) #fields, search_fields=nil, explicit_block=nil, &implicit_block)
37
39
  block = (explicit_block or implicit_block)
38
40
 
39
- DataTable::Proxy.new(self, controller, options, &block)
41
+ ::Mongoid::DataTable::Proxy.new(self, controller, options, &block)
40
42
  end
41
43
 
42
44
  end
@@ -21,7 +21,8 @@ Gem::Specification.new do |s|
21
21
 
22
22
  s.add_development_dependency "bson_ext", "~>1.3.0"
23
23
  s.add_development_dependency "mongoid", "~>2.0.1"
24
- s.add_development_dependency "rspec", "~>2.5.0"
24
+ s.add_development_dependency "mocha", "~>0.9.12"
25
+ s.add_development_dependency "rspec", "~>2.6.0"
25
26
  s.add_development_dependency "shoulda", "~>2.11.3"
26
- s.add_development_dependency "rr", "~>1.0.2"
27
+ s.add_development_dependency "watchr", "~>0.7"
27
28
  end
@@ -0,0 +1,7 @@
1
+ class Person
2
+ include Mongoid::Document
3
+ include Mongoid::DataTable
4
+
5
+ field :name
6
+ field :_type # destrustive_field
7
+ end
@@ -0,0 +1,12 @@
1
+ class UserAgent
2
+ include Mongoid::Document
3
+ include Mongoid::DataTable
4
+
5
+ field :engine
6
+ field :name
7
+ field :platforms, :type => Array
8
+ field :version, :type => Float
9
+ field :grade
10
+
11
+ validates_inclusion_of :grade, :in => ('A'..'Z')
12
+ end
data/spec/spec_helper.rb CHANGED
@@ -1,3 +1,32 @@
1
- require "data_table"
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), "..", "lib"))
2
3
 
3
- RSpec.configure {|config| config.mock_with :rr }
4
+ MODELS = File.join(File.dirname(__FILE__), "models")
5
+ SUPPORT = File.join(File.dirname(__FILE__), "support")
6
+ $LOAD_PATH.unshift(MODELS)
7
+ $LOAD_PATH.unshift(SUPPORT)
8
+
9
+ require 'rails'
10
+ require 'action_controller'
11
+ require 'mongoid'
12
+ require 'mongoid-data_table'
13
+ require 'mocha'
14
+ require 'rspec'
15
+
16
+ LOGGER = ActiveSupport::BufferedLogger.new($stdout)
17
+
18
+ Mongoid.configure do |config|
19
+ name = "mongoid_data_table_test"
20
+ config.master = Mongo::Connection.new.db(name)
21
+ config.logger = nil
22
+ end
23
+
24
+ Dir[ File.join(MODELS, "*.rb") ].sort.each { |file| require File.basename(file) }
25
+ Dir[ File.join(SUPPORT, "*.rb") ].each { |file| require File.basename(file) }
26
+
27
+ RSpec.configure do |config|
28
+ config.mock_with(:mocha)
29
+ config.after(:suite) do
30
+ Mongoid.master.collections.select {|c| c.name !~ /system/ }.each(&:drop)
31
+ end
32
+ end
@@ -0,0 +1,69 @@
1
+ require 'spec_helper'
2
+
3
+ describe Mongoid::DataTable::Proxy do
4
+
5
+ let(:controller) do
6
+ Class.new(ActionController::Base).new.tap do |c|
7
+ c.stubs(:params).returns({})
8
+ end
9
+ end
10
+
11
+ describe "#new" do
12
+
13
+ it "creates a new Mongoid::DataTable::Proxy object" do
14
+ proxy = Mongoid::DataTable::Proxy.new(Person, controller)
15
+ proxy.__metaclass__.superclass.should == Mongoid::DataTable::Proxy
16
+ end
17
+
18
+ end
19
+
20
+ context "with default settings" do
21
+
22
+ let(:proxy) do
23
+ bob
24
+ Mongoid::DataTable::Proxy.new(Person, controller)
25
+ end
26
+
27
+ let(:bob) do
28
+ Person.find_or_create_by(:name => 'Bob')
29
+ end
30
+
31
+ let(:sam) do
32
+ Person.find_or_initialize_by(:name => 'Sam')
33
+ end
34
+
35
+ describe "#collection" do
36
+
37
+ it "should return WillPaginate::Collection" do
38
+ proxy.collection.should be_a(WillPaginate::Collection)
39
+ end
40
+
41
+ it "should reload when passed true" do
42
+ proxy.collection.should include(bob)
43
+ proxy.collection.should_not include(sam)
44
+ sam.save
45
+ proxy.collection(true).should include(sam)
46
+ sam.destroy
47
+ end
48
+
49
+ end
50
+
51
+ describe "#current_page" do
52
+
53
+ it "should default to 1" do
54
+ proxy.current_page.should be(1)
55
+ end
56
+
57
+ end
58
+
59
+ describe "#per_page" do
60
+
61
+ it "should default to 10" do
62
+ proxy.per_page.should be(10)
63
+ end
64
+
65
+ end
66
+
67
+ end
68
+
69
+ end
@@ -0,0 +1,150 @@
1
+ require 'spec_helper'
2
+
3
+ describe Mongoid::DataTable do
4
+
5
+ before do
6
+ Person.delete_all
7
+ end
8
+
9
+ context "on a default document" do
10
+
11
+ describe ".data_table_options" do
12
+
13
+ it "defaults to a hash" do
14
+ Person.data_table_options.should be_a(Hash)
15
+ end
16
+
17
+ end
18
+
19
+ describe ".data_table_options" do
20
+
21
+ it "defaults to a hash" do
22
+ Person.data_table_options.should be_a(Hash)
23
+ end
24
+
25
+ end
26
+
27
+ describe ".data_table_fields" do
28
+
29
+ it "defaults to already defined fields" do
30
+ (Person.fields.keys & Person.data_table_fields).should have(Person.data_table_fields.length).items
31
+ end
32
+
33
+ it "should not include Mongoid.destructive_fields" do
34
+ Person.data_table_fields.should_not include(*Mongoid.destructive_fields)
35
+ end
36
+
37
+ end
38
+
39
+ describe ".data_table_searchable_fields" do
40
+
41
+ it "defaults to .data_table_fields" do
42
+ Person.data_table_searchable_fields.should == Person.data_table_fields
43
+ end
44
+
45
+ end
46
+
47
+ describe ".data_table_sortable_fields" do
48
+
49
+ it "defaults to .data_table_fields" do
50
+ Person.data_table_sortable_fields.should == Person.data_table_fields
51
+ end
52
+
53
+ end
54
+
55
+ describe ".to_data_table" do
56
+
57
+ before do
58
+ Rails.stubs(:logger).returns(LOGGER)
59
+ end
60
+
61
+ let(:bob) do
62
+ Person.find_or_create_by(:name => 'Bob')
63
+ end
64
+
65
+ let(:controller) do
66
+ Class.new(ActionController::Base).new.tap do |c|
67
+ c.stubs(:params).returns({})
68
+ end
69
+ end
70
+
71
+ it "should create a Mongoid::DataTable::Proxy object" do
72
+ Person.to_data_table(controller).__metaclass__.superclass.should == ::Mongoid::DataTable::Proxy
73
+ end
74
+
75
+ describe "#to_hash" do
76
+
77
+ let(:dt) do
78
+ bob
79
+ Person.to_data_table(controller)
80
+ end
81
+
82
+ it "should be a Hash" do
83
+ dt.to_hash.should be_a(Hash)
84
+ end
85
+
86
+ it "should be formatted for DataTables" do
87
+ dt.to_hash.keys.should include(:sEcho, :iTotalRecords, :iTotalDisplayRecords, :aaData)
88
+ end
89
+
90
+ it "should include DT_RowId" do
91
+ result = dt.to_hash[:aaData].first
92
+ result.keys.should include('DT_RowId')
93
+ end
94
+
95
+ context "with inline block" do
96
+
97
+ it "should run inline block (Array)" do
98
+ result = dt.to_hash do |person|
99
+ [ person.name ]
100
+ end[:aaData].first
101
+ result.should be_a(Array)
102
+ result.first.should == bob.name
103
+ end
104
+
105
+ it "should run inline block (Hash)" do
106
+ result = dt.to_hash do |person|
107
+ { :name => person.name }
108
+ end[:aaData].first
109
+ result.should be_a(Hash)
110
+ result['name'].should == bob.name
111
+ end
112
+
113
+ end
114
+
115
+ end
116
+
117
+ context "with inline block (Array)" do
118
+
119
+ let(:dt) do
120
+ bob
121
+ Person.to_data_table(controller) do |person|
122
+ [ person.name ]
123
+ end
124
+ end
125
+
126
+ describe "#to_hash" do
127
+
128
+ it "should be a Hash" do
129
+ dt.to_hash.should be_a(Hash)
130
+ end
131
+
132
+ it "should be formatted for DataTables" do
133
+ dt.to_hash.keys.should include(:sEcho, :iTotalRecords, :iTotalDisplayRecords, :aaData)
134
+ end
135
+
136
+ it "should run inline block" do
137
+ result = dt.to_hash[:aaData].first
138
+ result.should be_a(Array)
139
+ result.first.should == bob.name
140
+ end
141
+
142
+ end
143
+
144
+ end
145
+
146
+ end
147
+
148
+ end
149
+
150
+ end
@@ -0,0 +1,2 @@
1
+ require 'spec_helper'
2
+
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: mongoid-data_table
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.2
5
+ version: 1.0.0
6
6
  platform: ruby
7
7
  authors:
8
8
  - Jason Dew
@@ -11,11 +11,10 @@ autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
13
 
14
- date: 2011-05-16 00:00:00 Z
14
+ date: 2011-05-17 00:00:00 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: rails
18
- prerelease: false
19
18
  requirement: &id001 !ruby/object:Gem::Requirement
20
19
  none: false
21
20
  requirements:
@@ -23,10 +22,10 @@ dependencies:
23
22
  - !ruby/object:Gem::Version
24
23
  version: 3.0.0
25
24
  type: :runtime
25
+ prerelease: false
26
26
  version_requirements: *id001
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: will_paginate
29
- prerelease: false
30
29
  requirement: &id002 !ruby/object:Gem::Requirement
31
30
  none: false
32
31
  requirements:
@@ -34,10 +33,10 @@ dependencies:
34
33
  - !ruby/object:Gem::Version
35
34
  version: 3.0.pre2
36
35
  type: :runtime
36
+ prerelease: false
37
37
  version_requirements: *id002
38
38
  - !ruby/object:Gem::Dependency
39
39
  name: bson_ext
40
- prerelease: false
41
40
  requirement: &id003 !ruby/object:Gem::Requirement
42
41
  none: false
43
42
  requirements:
@@ -45,10 +44,10 @@ dependencies:
45
44
  - !ruby/object:Gem::Version
46
45
  version: 1.3.0
47
46
  type: :development
47
+ prerelease: false
48
48
  version_requirements: *id003
49
49
  - !ruby/object:Gem::Dependency
50
50
  name: mongoid
51
- prerelease: false
52
51
  requirement: &id004 !ruby/object:Gem::Requirement
53
52
  none: false
54
53
  requirements:
@@ -56,40 +55,52 @@ dependencies:
56
55
  - !ruby/object:Gem::Version
57
56
  version: 2.0.1
58
57
  type: :development
58
+ prerelease: false
59
59
  version_requirements: *id004
60
60
  - !ruby/object:Gem::Dependency
61
- name: rspec
62
- prerelease: false
61
+ name: mocha
63
62
  requirement: &id005 !ruby/object:Gem::Requirement
64
63
  none: false
65
64
  requirements:
66
65
  - - ~>
67
66
  - !ruby/object:Gem::Version
68
- version: 2.5.0
67
+ version: 0.9.12
69
68
  type: :development
69
+ prerelease: false
70
70
  version_requirements: *id005
71
71
  - !ruby/object:Gem::Dependency
72
- name: shoulda
73
- prerelease: false
72
+ name: rspec
74
73
  requirement: &id006 !ruby/object:Gem::Requirement
75
74
  none: false
76
75
  requirements:
77
76
  - - ~>
78
77
  - !ruby/object:Gem::Version
79
- version: 2.11.3
78
+ version: 2.6.0
80
79
  type: :development
80
+ prerelease: false
81
81
  version_requirements: *id006
82
82
  - !ruby/object:Gem::Dependency
83
- name: rr
84
- prerelease: false
83
+ name: shoulda
85
84
  requirement: &id007 !ruby/object:Gem::Requirement
86
85
  none: false
87
86
  requirements:
88
87
  - - ~>
89
88
  - !ruby/object:Gem::Version
90
- version: 1.0.2
89
+ version: 2.11.3
91
90
  type: :development
91
+ prerelease: false
92
92
  version_requirements: *id007
93
+ - !ruby/object:Gem::Dependency
94
+ name: watchr
95
+ requirement: &id008 !ruby/object:Gem::Requirement
96
+ none: false
97
+ requirements:
98
+ - - ~>
99
+ - !ruby/object:Gem::Version
100
+ version: "0.7"
101
+ type: :development
102
+ prerelease: false
103
+ version_requirements: *id008
93
104
  description: Simple data preparation from Mongoid to the jQuery DataTables plugin
94
105
  email:
95
106
  - jason.dew@gmail.com
@@ -102,6 +113,8 @@ extra_rdoc_files: []
102
113
 
103
114
  files:
104
115
  - .gitignore
116
+ - .rspec
117
+ - .watchr
105
118
  - Gemfile
106
119
  - Gemfile.lock
107
120
  - LICENSE
@@ -113,10 +126,12 @@ files:
113
126
  - lib/mongoid/data_table/proxy.rb
114
127
  - lib/mongoid/data_table/version.rb
115
128
  - mongoid-data_table.gemspec
116
- - spec/active_record_data_table_spec.rb
117
- - spec/data_table_spec.rb
118
- - spec/mongoid_data_table_spec.rb
129
+ - spec/models/person.rb
130
+ - spec/models/user_agent.rb
119
131
  - spec/spec_helper.rb
132
+ - spec/unit/mongoid-data_table_spec.rb
133
+ - spec/unit/mongoid/data_table/proxy_spec.rb
134
+ - spec/unit/mongoid/data_table_spec.rb
120
135
  homepage: http://rubygems.org/gems/mongoid-data_table
121
136
  licenses: []
122
137
 
@@ -130,12 +145,18 @@ required_ruby_version: !ruby/object:Gem::Requirement
130
145
  requirements:
131
146
  - - ">="
132
147
  - !ruby/object:Gem::Version
148
+ hash: 1445524431539221366
149
+ segments:
150
+ - 0
133
151
  version: "0"
134
152
  required_rubygems_version: !ruby/object:Gem::Requirement
135
153
  none: false
136
154
  requirements:
137
155
  - - ">="
138
156
  - !ruby/object:Gem::Version
157
+ hash: 1445524431539221366
158
+ segments:
159
+ - 0
139
160
  version: "0"
140
161
  requirements: []
141
162
 
@@ -145,8 +166,9 @@ signing_key:
145
166
  specification_version: 3
146
167
  summary: Simple data preparation from Mongoid to the jQuery DataTables plugin
147
168
  test_files:
148
- - spec/active_record_data_table_spec.rb
149
- - spec/data_table_spec.rb
150
- - spec/mongoid_data_table_spec.rb
169
+ - spec/models/person.rb
170
+ - spec/models/user_agent.rb
151
171
  - spec/spec_helper.rb
152
- has_rdoc:
172
+ - spec/unit/mongoid-data_table_spec.rb
173
+ - spec/unit/mongoid/data_table/proxy_spec.rb
174
+ - spec/unit/mongoid/data_table_spec.rb
@@ -1,52 +0,0 @@
1
- require "spec_helper"
2
-
3
- describe DataTable do
4
-
5
- include DataTable::ActiveRecord::ClassMethods
6
-
7
- context "#_find_objects" do
8
-
9
- it "should find the objects required based on the params" do
10
- params = {:sSearch => "answer", :iSortCol_0 => "0", :sSortDir_0 => "desc", :iDisplayLength => 10, :sEcho => 1}
11
-
12
- mock(self)._discover_joins(%w(foo bar baz)) { [] }
13
- mock(self)._where_conditions("answer", %w(foo bar)) { "where clause" }
14
- mock(self)._order_fields(params, %w(foo bar baz)) { "order" }
15
-
16
- mock(self).where("where clause") { mock!.includes([]) { mock!.order("order") { mock!.paginate({:page => :page, :per_page => :per_page}) { :answer } } } }
17
- mock(self)._page(params) { :page }
18
- mock(self)._per_page(params) { :per_page }
19
-
20
- _find_objects(params, %w(foo bar baz), %w(foo bar)).should == :answer
21
- end
22
-
23
- end
24
-
25
- context "#_where_conditions" do
26
-
27
- it "should return nil if the query is blank" do
28
- send(:_where_conditions, "", %w(foo bar baz)).should == nil
29
- end
30
-
31
- it "should return an AR array with an entry for each search field" do
32
- send(:_where_conditions, "query", %w(foo bar)).should == ["UPPER(foo) LIKE ? OR UPPER(bar) LIKE ?", "%QUERY%", "%QUERY%"]
33
- end
34
-
35
- end
36
-
37
- context "#_discover_joins" do
38
-
39
- it "should return the joins on the fields" do
40
- _discover_joins(%w(foo.bar foz.ber baz)).to_a.should == [:foo, :foz]
41
- end
42
-
43
- end
44
-
45
- context "#_order_fields" do
46
-
47
- it "should find the field name and pass the sort direction" do
48
- send(:_order_fields, {:iSortCol_0 => "1", :sSortDir_0 => "asc"}, %w(foo bar baz)).should == "bar ASC"
49
- end
50
-
51
- end
52
- end
@@ -1,82 +0,0 @@
1
- require "spec_helper"
2
-
3
- describe DataTable do
4
-
5
- include DataTable::Base::ClassMethods
6
-
7
- context "on being included" do
8
- it "should extend ClassMethods" do
9
- klass = Class.new
10
- klass.instance_eval %{include DataTable::Base}
11
- (class << klass; self.included_modules; end).should include(DataTable::Base::ClassMethods)
12
- end
13
- end
14
-
15
- context "#for_data_table" do
16
-
17
- it "should produce JSON for the datatables plugin to consume" do
18
- params = {:sSearch => "answer", :iSortCol_0 => "0", :sSortDir_0 => "desc", :iDisplayLength => "10", :sEcho => "1"}
19
- controller = mock!.params { params }.subject
20
-
21
- fields = %w(foo bar baz)
22
- search_fields = %w(foo bar)
23
- block = :block
24
-
25
- mock(self).count { 42 }
26
- objects = mock!.total_entries { 10 }.subject
27
- mock(self)._find_objects(params, fields, search_fields) { objects }
28
- mock(self)._yield_and_render_array(controller, objects, block) { :results }
29
-
30
- result = for_data_table(controller, fields, search_fields, block)
31
- result.should == {:sEcho => 1, :iTotalRecords => 42, :iTotalDisplayRecords => 10, :aaData => :results}.to_json.html_safe
32
- end
33
-
34
- end
35
-
36
- context "#_yield_and_render_array" do
37
-
38
- it "should walk through the array and render it, passing in the appropriate local name" do
39
- block = lambda {|x| mock!.map { [42] }.subject }
40
-
41
- result = _yield_and_render_array Object.new, [:foo], block
42
- result.should == [[42]]
43
- end
44
-
45
- end
46
-
47
- context "#_page" do
48
-
49
- context "with a display length of 10" do
50
- it "should return 1 when start is blank" do
51
- send(:_page, {:iDisplayStart => "", :iDisplayLength => "10"}).should == 1
52
- end
53
-
54
- it "should return 1 when start is 0" do
55
- send(:_page, {:iDisplayStart => "0", :iDisplayLength => "10"}).should == 1
56
- end
57
-
58
- it "should return 2 when start is 10" do
59
- send(:_page, {:iDisplayStart => "10", :iDisplayLength => "10"}).should == 2
60
- end
61
- end
62
-
63
- end
64
-
65
- context "#_per_page" do
66
-
67
- it "should return 10 given an iDisplayLength of 10" do
68
- send(:_per_page, {:iDisplayLength => "10"}).should == 10
69
- end
70
-
71
- it "should return a default of 25 given an invalid iDisplayLength" do
72
- send(:_per_page, {:iDisplayLength => "foobar"}).should == 25
73
- end
74
-
75
- it "should return self.count given an iDisplayLength of -1" do
76
- mock(self).count { :all }
77
- send(:_per_page, {:iDisplayLength => "-1"}).should == :all
78
- end
79
-
80
- end
81
-
82
- end
@@ -1,47 +0,0 @@
1
- require "spec_helper"
2
-
3
- describe DataTable do
4
-
5
- include DataTable::Mongoid::ClassMethods
6
-
7
- context "#_find_objects" do
8
- it "should find the objects required based on the params" do
9
- params = {:sSearch => "answer", :iSortCol_0 => "0", :sSortDir_0 => "desc", :iDisplayLength => 10, :sEcho => 1}
10
-
11
- mock(self)._where_conditions("answer", %w(foo bar)) { "where clause" }
12
- mock(self)._order_by_fields(params, %w(foo bar baz)) { "order by" }
13
-
14
- mock(self).where("where clause") { mock!.order_by("order by") { mock!.paginate({:page => :page, :per_page => :per_page}) { :answer } } }
15
- mock(self)._page(params) { :page }
16
- mock(self)._per_page(params) { :per_page }
17
-
18
- _find_objects(params, %w(foo bar baz), %w(foo bar)).should == :answer
19
- end
20
- end
21
-
22
- context "#_where_conditions" do
23
-
24
- it "should return nil if the query is blank" do
25
- send(:_where_conditions, "", %w(foo bar baz)).should == nil
26
- end
27
-
28
- it "should strip out slashes" do
29
- send(:_where_conditions, "//", %w(foo bar baz)).should == nil
30
- end
31
-
32
- it "should return a mongoid $or hash with an entry for each search field" do
33
- send(:_where_conditions, "q", %w(foo bar)).should == {"$or" => [{"foo" => /q/i}, {"bar" => /q/i}]}
34
- end
35
-
36
- end
37
-
38
- context "#_order_by_fields" do
39
-
40
- it "should find the field name and pass the sort direction" do
41
- send(:_order_by_fields,
42
- {:iSortCol_0 => "1", :sSortDir_0 => "asc"},
43
- %w(foo bar baz)).should == ["bar", "asc"]
44
- end
45
-
46
- end
47
- end