Skip to content

Commit a297e4a

Browse files
author
M. Saiqul Haq
committed
fix AjaxDatabaseRails::base#search_condition for namespaced model
convert rspec syntax to latest using transpec gem
1 parent f7744b6 commit a297e4a

File tree

9 files changed

+104
-39
lines changed

9 files changed

+104
-39
lines changed

ajax-datatables-rails.gemspec

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@ Gem::Specification.new do |gem|
1919
gem.require_path = "lib"
2020

2121
gem.add_dependency 'railties', '>= 3.1'
22-
22+
2323
gem.add_development_dependency "rspec"
2424
gem.add_development_dependency "generator_spec"
2525
gem.add_development_dependency "pry"
2626
gem.add_development_dependency "rake"
27-
27+
gem.add_development_dependency "activerecord", "~> 4.l.6"
28+
gem.add_development_dependency "sqlite3"
29+
2830
if RUBY_VERSION == '1.9.2'
2931
gem.add_development_dependency "rails", "3.1.0"
3032
else

lib/ajax-datatables-rails/base.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,11 @@ def build_conditions_for(query)
104104

105105
def search_condition(column, value)
106106
model, column = column.split('.')
107-
model = model.singularize.titleize.gsub( / /, '' ).constantize
107+
if model.scan("_").any?
108+
model = model.singularize.titleize.gsub( / /, '::' ).constantize
109+
else
110+
model = model.singularize.titleize.gsub( / /, '' ).constantize
111+
end
108112

109113
casted_column = ::Arel::Nodes::NamedFunction.new('CAST', [model.arel_table[column.to_sym].as(typecast)])
110114
casted_column.matches("%#{value}%")
@@ -139,12 +143,12 @@ def per_page
139143
end
140144

141145
def sort_column(item)
142-
sortable_columns[item['column'].to_i]
146+
sortable_columns[item[:column].to_i]
143147
end
144148

145149
def sort_direction(item)
146150
options = %w(desc asc)
147-
options.include?(item['dir']) ? item['dir'].upcase : 'ASC'
151+
options.include?(item[:dir]) ? item[:dir].upcase : 'ASC'
148152
end
149153
end
150154
end

spec/ajax-datatables-rails/ajax_datatables_rails_spec.rb

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,6 @@
11
require 'spec_helper'
22

33
describe AjaxDatatablesRails::Base do
4-
class Column
5-
def matches(query)
6-
[]
7-
end
8-
end
9-
10-
class User
11-
def self.arel_table
12-
{ :foo => Column.new }
13-
end
14-
end
15-
16-
class UserData
17-
def self.arel_table
18-
{ :bar => Column.new }
19-
end
20-
end
214

225
params = {
236
:draw => '5',
@@ -103,9 +86,9 @@ def self.arel_table
10386
}
10487
)
10588
datatable = AjaxDatatablesRails::Base.new(sort_view)
106-
datatable.stub(:sortable_columns) { ['foo', 'bar', 'baz'] }
89+
allow(datatable).to receive(:sortable_columns) { ['foo', 'bar', 'baz'] }
10790

108-
expect(datatable.send(:sort_column)).to eq('bar')
91+
expect(datatable.send(:sort_column, sort_view.params[:order]["0"])).to eq('bar')
10992
end
11093
end
11194

@@ -120,7 +103,7 @@ def self.arel_table
120103
}
121104
)
122105
datatable = AjaxDatatablesRails::Base.new(sorting_view)
123-
expect(datatable.send(:sort_direction)).to eq('DESC')
106+
expect(datatable.send(:sort_direction, sorting_view.params[:order]["0"])).to eq('DESC')
124107
end
125108

126109
it 'can only be one option from ASC or DESC' do
@@ -133,7 +116,29 @@ def self.arel_table
133116
}
134117
)
135118
datatable = AjaxDatatablesRails::Base.new(sorting_view)
136-
expect(datatable.send(:sort_direction)).to eq('ASC')
119+
expect(datatable.send(:sort_direction, sorting_view.params[:order]["0"])).to eq('ASC')
120+
end
121+
end
122+
123+
describe "#search_condition" do
124+
let(:datatable) { AjaxDatatablesRails::Base.new(view) }
125+
126+
context "normal model" do
127+
it "should return arel object" do
128+
expect(datatable.send(:search_condition, 'users.bar', 'bar').class).to eq(Arel::Nodes::Matches)
129+
end
130+
end
131+
132+
context "namespaced model" do
133+
it "should return arel object" do
134+
expect(datatable.send(:search_condition, 'statistics_sessions.bar', 'bar').class).to eq(Arel::Nodes::Matches)
135+
end
136+
end
137+
138+
it "should raise uninitialized constant if column not exist" do
139+
expect {
140+
datatable.send(:search_condition, 'asdusers.bar', 'bar').class
141+
}.to raise_error(/uninitialized constant/)
137142
end
138143
end
139144

@@ -167,20 +172,21 @@ def self.arel_table
167172

168173
describe '#sort_records' do
169174
it 'calls #order on a collection' do
170-
results.should_receive(:order)
175+
expect(results).to receive(:order)
171176
datatable.send(:sort_records, results)
172177
end
173178
end
174179

175180
describe '#filter_records' do
176-
let(:records) { double('User', :where => []) }
181+
let(:records) { double('User', :where => []) }
177182
let(:search_view) { double('view', :params => params) }
178183

179184
it 'applies search like functionality on a collection' do
180185
datatable = AjaxDatatablesRails::Base.new(search_view)
181-
datatable.stub(:searchable_columns) { ['users.foo'] }
186+
allow(datatable).to receive(:searchable_columns) { ['users.foo'] }
182187

183-
records.should_receive(:where)
188+
expect(records).to receive(:where)
189+
records.where
184190
datatable.send(:filter_records, records)
185191
end
186192
end
@@ -191,9 +197,10 @@ def self.arel_table
191197

192198
it 'applies search like functionality on a collection' do
193199
datatable = AjaxDatatablesRails::Base.new(search_view)
194-
datatable.stub(:searchable_columns) { ['user_datas.bar'] }
200+
allow(datatable).to receive(:searchable_columns) { ['user_datas.bar'] }
195201

196-
records.should_receive(:where)
202+
expect(records).to receive(:where)
203+
records.where
197204
datatable.send(:filter_records, records)
198205
end
199206
end

spec/ajax-datatables-rails/kaminari_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@ class KaminariDatatable < AjaxDatatablesRails::Base
1818
let(:records) { users_database.all }
1919

2020
it 'calls #page on passed record collection' do
21-
records.should_receive(:page)
21+
expect(records).to receive(:page)
2222
datatable.send(:paginate_records, records)
2323
end
2424

2525
it 'calls #per_page on passed record collection' do
2626
arry = double('Array', :per => [])
27-
records.stub(:page).and_return(arry)
28-
arry.should_receive(:per)
27+
allow(records).to receive(:page).and_return(arry)
28+
expect(arry).to receive(:per)
2929
datatable.send(:paginate_records, records)
3030
end
3131
end

spec/ajax-datatables-rails/simple_paginator_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@ class SimplePaginateDatatable < AjaxDatatablesRails::Base
1818
let(:records) { users_database.all }
1919

2020
it 'calls #offset on passed record collection' do
21-
records.should_receive(:offset)
21+
expect(records).to receive(:offset)
2222
datatable.send(:paginate_records, records)
2323
end
2424

2525
it 'calls #limit on passed record collection' do
2626
arry = double('Array', :limit => [])
27-
records.stub(:offset).and_return(arry)
28-
arry.should_receive(:limit)
27+
allow(records).to receive(:offset).and_return(arry)
28+
expect(arry).to receive(:limit)
2929
datatable.send(:paginate_records, records)
3030
end
3131
end

spec/ajax-datatables-rails/will_paginate_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class WillPaginateDatatable < AjaxDatatablesRails::Base
1818
let(:records) { users_database.all }
1919

2020
it 'calls #page and #per_page on passed record collection' do
21-
records.should_receive(:paginate).with(:page=>1, :per_page=>10)
21+
expect(records).to receive(:paginate).with(:page=>1, :per_page=>10)
2222
datatable.send(:paginate_records, records)
2323
end
2424
end

spec/models.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class User < ActiveRecord::Base
2+
end
3+
4+
class UserData < ActiveRecord::Base
5+
end
6+
7+
module Statistics
8+
def self.table_name_prefix
9+
"statistics_"
10+
end
11+
end
12+
13+
class Statistics::Request < ActiveRecord::Base
14+
end
15+
16+
class Statistics::Session < ActiveRecord::Base
17+
end

spec/schema.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
ActiveRecord::Schema.define do
2+
self.verbose = false
3+
4+
create_table :users, :force => true do |t|
5+
t.string :username
6+
7+
t.timestamps
8+
end
9+
10+
create_table :user_data, :force => true do |t|
11+
t.string :address
12+
13+
t.timestamps
14+
end
15+
16+
17+
create_table :statistics_requests, :force => true do |t|
18+
t.string :baz
19+
20+
t.timestamps
21+
end
22+
23+
create_table :statistics_sessions, :force => true do |t|
24+
t.string :foo
25+
t.integer :bar
26+
27+
t.timestamps
28+
end
29+
end

spec/spec_helper.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
11
require 'pry'
22
require 'rails'
3+
require 'active_record'
34
require 'ajax-datatables-rails'
5+
6+
ActiveRecord::Base.establish_connection adapter: "sqlite3", database: ":memory:"
7+
8+
load File.dirname(__FILE__) + '/schema.rb'
9+
require File.dirname(__FILE__) + '/models.rb'

0 commit comments

Comments
 (0)