jtable-rails 0.1.4 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (30) hide show
  1. data/Gemfile +1 -0
  2. data/Gemfile.lock +15 -0
  3. data/VERSION +1 -1
  4. data/jtable-rails.gemspec +34 -7
  5. data/lib/jtable-rails/action_controller.rb +3 -3
  6. data/lib/jtable-rails/active_record.rb +92 -33
  7. data/spec/controller_specs/{people_controller_spec.rb → basic_controller_spec.rb} +4 -4
  8. data/spec/fabricators/place.rb +5 -0
  9. data/spec/model_specs/basic_spec.rb +85 -0
  10. data/spec/model_specs/one_to_one_spec.rb +64 -0
  11. data/spec/support/rails_app/Gemfile.lock +1 -1
  12. data/spec/support/rails_app/app/controllers/places_controller.rb +83 -0
  13. data/spec/support/rails_app/app/helpers/places_helper.rb +2 -0
  14. data/spec/support/rails_app/app/models/person.rb +5 -1
  15. data/spec/support/rails_app/app/models/place.rb +3 -0
  16. data/spec/support/rails_app/app/views/places/_form.html.erb +29 -0
  17. data/spec/support/rails_app/app/views/places/edit.html.erb +6 -0
  18. data/spec/support/rails_app/app/views/places/index.html.erb +27 -0
  19. data/spec/support/rails_app/app/views/places/new.html.erb +5 -0
  20. data/spec/support/rails_app/app/views/places/show.html.erb +20 -0
  21. data/spec/support/rails_app/config/application.rb +3 -0
  22. data/spec/support/rails_app/config/routes.rb +2 -0
  23. data/spec/support/rails_app/db/migrate/20110302024653_create_places.rb +16 -0
  24. data/spec/support/rails_app/db/schema.rb +10 -1
  25. data/spec/support/rails_app/test/fixtures/places.yml +11 -0
  26. data/spec/support/rails_app/test/functional/places_controller_test.rb +49 -0
  27. data/spec/support/rails_app/test/unit/helpers/places_helper_test.rb +4 -0
  28. data/spec/support/rails_app/test/unit/place_test.rb +8 -0
  29. metadata +48 -11
  30. data/spec/model_specs/person_spec.rb +0 -85
data/Gemfile CHANGED
@@ -7,4 +7,5 @@ group :development do
7
7
  gem "jeweler", "~> 1.5.2"
8
8
  gem "rails", "3.0.5"
9
9
  gem 'sqlite3-ruby', :require => 'sqlite3'
10
+ gem 'ruby-debug19'
10
11
  end
@@ -28,8 +28,10 @@ GEM
28
28
  activemodel (= 3.0.5)
29
29
  activesupport (= 3.0.5)
30
30
  activesupport (3.0.5)
31
+ archive-tar-minitar (0.5.2)
31
32
  arel (2.0.9)
32
33
  builder (2.1.2)
34
+ columnize (0.3.2)
33
35
  diff-lcs (1.1.2)
34
36
  erubis (2.6.6)
35
37
  abstract (>= 1.0.0)
@@ -40,6 +42,8 @@ GEM
40
42
  bundler (~> 1.0.0)
41
43
  git (>= 1.2.5)
42
44
  rake
45
+ linecache19 (0.5.11)
46
+ ruby_core_source (>= 0.1.4)
43
47
  mail (2.2.15)
44
48
  activesupport (>= 2.3.6)
45
49
  i18n (>= 0.4.0)
@@ -74,6 +78,16 @@ GEM
74
78
  rspec-expectations (2.3.0)
75
79
  diff-lcs (~> 1.1.2)
76
80
  rspec-mocks (2.3.0)
81
+ ruby-debug-base19 (0.11.24)
82
+ columnize (>= 0.3.1)
83
+ linecache19 (>= 0.5.11)
84
+ ruby_core_source (>= 0.1.4)
85
+ ruby-debug19 (0.11.6)
86
+ columnize (>= 0.3.1)
87
+ linecache19 (>= 0.5.11)
88
+ ruby-debug-base19 (>= 0.11.19)
89
+ ruby_core_source (0.1.4)
90
+ archive-tar-minitar (>= 0.5.2)
77
91
  sqlite3 (1.3.3)
78
92
  sqlite3-ruby (1.3.3)
79
93
  sqlite3 (>= 1.3.3)
@@ -91,4 +105,5 @@ DEPENDENCIES
91
105
  jeweler (~> 1.5.2)
92
106
  rails (= 3.0.5)
93
107
  rspec
108
+ ruby-debug19
94
109
  sqlite3-ruby
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.4
1
+ 0.2.0
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{jtable-rails}
8
- s.version = "0.1.4"
8
+ s.version = "0.2.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Taylor Yelverton"]
12
- s.date = %q{2011-02-28}
12
+ s.date = %q{2011-04-08}
13
13
  s.description = %q{A Rails gem to power the jTable jQuery plugin}
14
14
  s.email = %q{tny5357@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -33,9 +33,11 @@ Gem::Specification.new do |s|
33
33
  "lib/jtable-rails/railtie.rb",
34
34
  "lib/jtable-rails/version.rb",
35
35
  "lib/tasks/jtable_rails.rake",
36
- "spec/controller_specs/people_controller_spec.rb",
36
+ "spec/controller_specs/basic_controller_spec.rb",
37
37
  "spec/fabricators/person.rb",
38
- "spec/model_specs/person_spec.rb",
38
+ "spec/fabricators/place.rb",
39
+ "spec/model_specs/basic_spec.rb",
40
+ "spec/model_specs/one_to_one_spec.rb",
39
41
  "spec/spec_helper.rb",
40
42
  "spec/support/rails_app/.DS_Store",
41
43
  "spec/support/rails_app/.gitignore",
@@ -44,15 +46,23 @@ Gem::Specification.new do |s|
44
46
  "spec/support/rails_app/Rakefile",
45
47
  "spec/support/rails_app/app/controllers/application_controller.rb",
46
48
  "spec/support/rails_app/app/controllers/people_controller.rb",
49
+ "spec/support/rails_app/app/controllers/places_controller.rb",
47
50
  "spec/support/rails_app/app/helpers/application_helper.rb",
48
51
  "spec/support/rails_app/app/helpers/people_helper.rb",
52
+ "spec/support/rails_app/app/helpers/places_helper.rb",
49
53
  "spec/support/rails_app/app/models/person.rb",
54
+ "spec/support/rails_app/app/models/place.rb",
50
55
  "spec/support/rails_app/app/views/layouts/application.html.erb",
51
56
  "spec/support/rails_app/app/views/people/_form.html.erb",
52
57
  "spec/support/rails_app/app/views/people/edit.html.erb",
53
58
  "spec/support/rails_app/app/views/people/index.html.erb",
54
59
  "spec/support/rails_app/app/views/people/new.html.erb",
55
60
  "spec/support/rails_app/app/views/people/show.html.erb",
61
+ "spec/support/rails_app/app/views/places/_form.html.erb",
62
+ "spec/support/rails_app/app/views/places/edit.html.erb",
63
+ "spec/support/rails_app/app/views/places/index.html.erb",
64
+ "spec/support/rails_app/app/views/places/new.html.erb",
65
+ "spec/support/rails_app/app/views/places/show.html.erb",
56
66
  "spec/support/rails_app/config.ru",
57
67
  "spec/support/rails_app/config/application.rb",
58
68
  "spec/support/rails_app/config/boot.rb",
@@ -69,6 +79,7 @@ Gem::Specification.new do |s|
69
79
  "spec/support/rails_app/config/locales/en.yml",
70
80
  "spec/support/rails_app/config/routes.rb",
71
81
  "spec/support/rails_app/db/migrate/20110207142623_create_people.rb",
82
+ "spec/support/rails_app/db/migrate/20110302024653_create_places.rb",
72
83
  "spec/support/rails_app/db/schema.rb",
73
84
  "spec/support/rails_app/db/seeds.rb",
74
85
  "spec/support/rails_app/public/images/jTable-icons.png",
@@ -77,6 +88,10 @@ Gem::Specification.new do |s|
77
88
  "spec/support/rails_app/public/stylesheets/jTable.css",
78
89
  "spec/support/rails_app/public/stylesheets/scaffold.css",
79
90
  "spec/support/rails_app/script/rails",
91
+ "spec/support/rails_app/test/fixtures/places.yml",
92
+ "spec/support/rails_app/test/functional/places_controller_test.rb",
93
+ "spec/support/rails_app/test/unit/helpers/places_helper_test.rb",
94
+ "spec/support/rails_app/test/unit/place_test.rb",
80
95
  "spec/support/rails_app/tmp/.DS_Store",
81
96
  "spec/support/rails_app/tmp/jtable.zip"
82
97
  ]
@@ -86,15 +101,20 @@ Gem::Specification.new do |s|
86
101
  s.rubygems_version = %q{1.3.7}
87
102
  s.summary = %q{A Rails gem to power the jTable jQuery plugin}
88
103
  s.test_files = [
89
- "spec/controller_specs/people_controller_spec.rb",
104
+ "spec/controller_specs/basic_controller_spec.rb",
90
105
  "spec/fabricators/person.rb",
91
- "spec/model_specs/person_spec.rb",
106
+ "spec/fabricators/place.rb",
107
+ "spec/model_specs/basic_spec.rb",
108
+ "spec/model_specs/one_to_one_spec.rb",
92
109
  "spec/spec_helper.rb",
93
110
  "spec/support/rails_app/app/controllers/application_controller.rb",
94
111
  "spec/support/rails_app/app/controllers/people_controller.rb",
112
+ "spec/support/rails_app/app/controllers/places_controller.rb",
95
113
  "spec/support/rails_app/app/helpers/application_helper.rb",
96
114
  "spec/support/rails_app/app/helpers/people_helper.rb",
115
+ "spec/support/rails_app/app/helpers/places_helper.rb",
97
116
  "spec/support/rails_app/app/models/person.rb",
117
+ "spec/support/rails_app/app/models/place.rb",
98
118
  "spec/support/rails_app/config/application.rb",
99
119
  "spec/support/rails_app/config/boot.rb",
100
120
  "spec/support/rails_app/config/environment.rb",
@@ -108,8 +128,12 @@ Gem::Specification.new do |s|
108
128
  "spec/support/rails_app/config/initializers/session_store.rb",
109
129
  "spec/support/rails_app/config/routes.rb",
110
130
  "spec/support/rails_app/db/migrate/20110207142623_create_people.rb",
131
+ "spec/support/rails_app/db/migrate/20110302024653_create_places.rb",
111
132
  "spec/support/rails_app/db/schema.rb",
112
- "spec/support/rails_app/db/seeds.rb"
133
+ "spec/support/rails_app/db/seeds.rb",
134
+ "spec/support/rails_app/test/functional/places_controller_test.rb",
135
+ "spec/support/rails_app/test/unit/helpers/places_helper_test.rb",
136
+ "spec/support/rails_app/test/unit/place_test.rb"
113
137
  ]
114
138
 
115
139
  if s.respond_to? :specification_version then
@@ -123,6 +147,7 @@ Gem::Specification.new do |s|
123
147
  s.add_development_dependency(%q<jeweler>, ["~> 1.5.2"])
124
148
  s.add_development_dependency(%q<rails>, ["= 3.0.5"])
125
149
  s.add_development_dependency(%q<sqlite3-ruby>, [">= 0"])
150
+ s.add_development_dependency(%q<ruby-debug19>, [">= 0"])
126
151
  s.add_runtime_dependency(%q<rails>, ["= 3.0.5"])
127
152
  else
128
153
  s.add_dependency(%q<bundler>, [">= 0"])
@@ -131,6 +156,7 @@ Gem::Specification.new do |s|
131
156
  s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
132
157
  s.add_dependency(%q<rails>, ["= 3.0.5"])
133
158
  s.add_dependency(%q<sqlite3-ruby>, [">= 0"])
159
+ s.add_dependency(%q<ruby-debug19>, [">= 0"])
134
160
  s.add_dependency(%q<rails>, ["= 3.0.5"])
135
161
  end
136
162
  else
@@ -140,6 +166,7 @@ Gem::Specification.new do |s|
140
166
  s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
141
167
  s.add_dependency(%q<rails>, ["= 3.0.5"])
142
168
  s.add_dependency(%q<sqlite3-ruby>, [">= 0"])
169
+ s.add_dependency(%q<ruby-debug19>, [">= 0"])
143
170
  s.add_dependency(%q<rails>, ["= 3.0.5"])
144
171
  end
145
172
  end
@@ -1,9 +1,9 @@
1
1
  module JTable
2
2
  module ActionController
3
- def jtable_for_json(rel, jtable_params)
3
+ def jtable_for_json(jtable_name, rel, jtable_params)
4
4
  jtable_params = HashWithIndifferentAccess.new(jtable_params)
5
- items = rel.jtable_paginate(jtable_params[:limit], jtable_params[:offset])
6
- {:total_items => rel.count, :items => items.collect(&:jtable_item)}
5
+ items = rel.send("jtable_#{jtable_name}_paginate",jtable_params[:limit], jtable_params[:offset])
6
+ {:total_items => rel.count, :items => items.collect(&("jtable_#{jtable_name}_item").to_sym)}
7
7
  end
8
8
  end
9
9
  end
@@ -2,59 +2,113 @@ module JTable
2
2
  module ActiveRecord
3
3
  extend ActiveSupport::Concern
4
4
  module ClassMethods
5
- def jtable(*fields)
5
+ def jtable(name, *fields)
6
6
  metaclass = class << self
7
7
  self
8
8
  end
9
+ join_associations = []
9
10
  fields.each do |field|
10
- metaclass.instance_eval do
11
- define_method "jtable_search_#{field}" do |term|
12
- unless [:date].include? arel_table[field.to_sym].column.type
13
- if [:integer, :boolean].include? arel_table[field.to_sym].column.type
14
- arel_table[field.to_sym].eq(term.to_i)
15
- else
16
- arel_table[field.to_sym].matches("%#{term}%")
11
+ if field.kind_of? Hash
12
+ field.each_pair do |assoc_name, assoc_fields|
13
+ join_associations << assoc_name
14
+ assoc_klass = assoc_name.to_s.camelcase.constantize
15
+ assoc_fields.each do |assoc_field|
16
+ metaclass.instance_eval do
17
+ define_method "jtable_#{name}_search_#{assoc_name}_#{assoc_field}" do |term|
18
+ unless [:date].include? assoc_klass.arel_table[assoc_field.to_sym].column.type
19
+ if [:integer, :boolean].include? assoc_klass.arel_table[assoc_field.to_sym].column.type
20
+ if term.to_i.to_s == term
21
+ assoc_klass.arel_table[assoc_field.to_sym].eq(term.to_i)
22
+ end
23
+ else
24
+ assoc_klass.arel_table[assoc_field.to_sym].matches("%#{term}%")
25
+ end
26
+ end
27
+ end
28
+
29
+ define_method "jtable_#{name}_order_#{assoc_name}_#{assoc_field}" do |direction|
30
+ %Q["#{assoc_name.to_s.camelcase.constantize.table_name}".#{assoc_field} #{direction}]
31
+ end
32
+ end
33
+
34
+ define_method "jtable_#{name}_attribute_#{assoc_name}_#{assoc_field}" do
35
+ if assoc_klass.arel_table[assoc_field.to_sym].column.type == :date
36
+ self.send(assoc_name).send(assoc_field).strftime("%m/%d/%Y %I:%M%P")
37
+ else
38
+ self.send(assoc_name).send(assoc_field)
39
+ end
17
40
  end
18
41
  end
19
42
  end
43
+ else
44
+ metaclass.instance_eval do
45
+ define_method "jtable_#{name}_search_#{field}" do |term|
46
+ unless [:date].include? arel_table[field.to_sym].column.type
47
+ if [:integer, :boolean].include? arel_table[field.to_sym].column.type
48
+ if term.to_i.to_s == term
49
+ arel_table[field.to_sym].eq(term.to_i)
50
+ end
51
+ else
52
+ arel_table[field.to_sym].matches("%#{term}%")
53
+ end
54
+ end
55
+ end
20
56
 
21
- define_method "jtable_order_#{field}" do |direction|
22
- "#{field} #{direction}"
57
+ define_method "jtable_#{name}_order_#{field}" do |direction|
58
+ "#{field} #{direction}"
59
+ end
23
60
  end
24
- end
25
- define_method "jtable_attribute_#{field}" do
26
- if self.class.arel_table[field.to_sym].column.type == :date
27
- self.send(field).strftime("%m/%d/%Y %I:%M%P")
28
- else
29
- self.send(field)
61
+ define_method "jtable_#{name}_attribute_#{field}" do
62
+ if self.class.arel_table[field.to_sym].column.type == :date
63
+ self.send(field).strftime("%m/%d/%Y %I:%M%P")
64
+ else
65
+ self.send(field)
66
+ end
30
67
  end
31
68
  end
32
69
  end
33
70
 
34
71
  metaclass.instance_eval do
35
- define_method "jtable_attributes" do
72
+ define_method "jtable_#{name}_attributes" do
36
73
  fields
37
74
  end
38
75
  end
39
76
 
40
- define_method "jtable_item" do
77
+ define_method "jtable_#{name}_item" do
41
78
  item_hash = {}
42
79
  item_hash[:id] = self.id
43
80
  fields.each do |field|
44
- item_hash[field.to_sym] = self.send("jtable_attribute_#{field}")
81
+ if field.kind_of? Hash
82
+ field.each_pair do |assoc_name, assoc_fields|
83
+ assoc_fields.each do |assoc_field|
84
+ item_hash["#{assoc_name}_#{assoc_field}".to_sym] = self.send("jtable_#{name}_attribute_#{assoc_name}_#{assoc_field}")
85
+ end
86
+ end
87
+ else
88
+ item_hash[field.to_sym] = self.send("jtable_#{name}_attribute_#{field}")
89
+ end
45
90
  end
46
91
  item_hash
47
92
  end
48
93
 
49
- scope :jtable_search, lambda { |jtable_params|
94
+ scope "jtable_#{name}_search", lambda { |jtable_params|
50
95
  search_terms = jtable_params[:search]
51
96
  unless search_terms.blank?
52
97
  wheres = []
53
98
  search_terms.split(" ").each do |term|
54
99
  where_query = []
55
100
  fields.each do |field|
56
- next unless jtable_params[:searchable_columns].include?(field.to_s)
57
- where_query << self.send("jtable_search_#{field}", term)
101
+ if field.kind_of? Hash
102
+ field.each_pair do |assoc_name, assoc_fields|
103
+ assoc_fields.each do |assoc_field|
104
+ next unless jtable_params[:searchable_columns].include?("#{assoc_name}_#{assoc_field}")
105
+ where_query << self.send("jtable_#{name}_search_#{assoc_name}_#{assoc_field}", term)
106
+ end
107
+ end
108
+ else
109
+ next unless jtable_params[:searchable_columns].include?(field.to_s)
110
+ where_query << self.send("jtable_#{name}_search_#{field}", term)
111
+ end
58
112
  end
59
113
  wheres << where(where_query.inject(&:or))
60
114
  end
@@ -62,41 +116,46 @@ module JTable
62
116
  end
63
117
  }
64
118
 
65
- scope :jtable_single_search, lambda {|column, search_terms|
119
+ scope "jtable_#{name}_single_search", lambda {|column, search_terms|
66
120
  unless column.blank? or search_terms.blank?
67
121
  wheres = []
68
122
  search_terms.split(" ").each do |term|
69
- wheres << where(self.send("jtable_search_#{column}", term))
123
+ wheres << where(self.send("jtable_#{name}_search_#{column}", term))
70
124
  end
71
125
  wheres.inject(&:merge)
72
126
  end
73
127
  }
74
128
 
75
- scope :jtable_order, lambda {|column, direction|
129
+ scope "jtable_#{name}_order", lambda {|column, direction|
76
130
  if !column.blank? and !direction.blank? and %w(asc desc).include?(direction.downcase)
77
- order(self.send("jtable_order_#{column}", direction))
131
+ order(self.send("jtable_#{name}_order_#{column}", direction))
78
132
  end
79
133
  }
80
134
 
81
- scope :jtable_paginate, lambda {|per_page, page_start|
135
+ scope "jtable_#{name}_paginate", lambda {|per_page, page_start|
82
136
  limit(per_page.to_i).offset(page_start.to_i)
83
137
  }
84
138
 
85
- scope :jtable_default, lambda {
139
+ scope "jtable_#{name}_default", lambda {
86
140
 
87
141
  }
88
142
 
89
- scope :jtable_query, lambda { |jtable_params|
143
+ scope "jtable_#{name}_query", lambda { |jtable_params|
90
144
  jtable_params = HashWithIndifferentAccess.new(jtable_params)
91
145
  queries = []
92
- queries << jtable_default()
93
- queries << jtable_search(jtable_params)
146
+ unless join_associations.empty?
147
+ join_associations.each do |join_association|
148
+ queries << joins(join_association)
149
+ end
150
+ end
151
+ queries << self.send("jtable_#{name}_default")
152
+ queries << self.send("jtable_#{name}_search", jtable_params)
94
153
  unless jtable_params[:column_search].blank?
95
154
  jtable_params[:column_search].each_pair do |column, search|
96
- queries << jtable_single_search(column, search)
155
+ queries << self.send("jtable_#{name}_single_search", column, search)
97
156
  end
98
157
  end
99
- queries << jtable_order(jtable_params[:sort_column], jtable_params[:sort_direction])
158
+ queries << self.send("jtable_#{name}_order", jtable_params[:sort_column], jtable_params[:sort_direction])
100
159
  queries.inject(&:merge)
101
160
  }
102
161
  end
@@ -1,6 +1,6 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
2
 
3
- describe "PeopleController" do
3
+ describe "BasicController" do
4
4
  before(:each) do
5
5
  @people_controller = PeopleController.new
6
6
 
@@ -24,8 +24,8 @@ describe "PeopleController" do
24
24
  end
25
25
 
26
26
  it "should return the appropriate json" do
27
- people = Person.jtable_query(@params)
28
- jtable_items = people.jtable_paginate(@params[:limit], @params[:offset]).collect(&:jtable_item)
29
- @people_controller.jtable_for_json(people, @params).to_json.should eql({:total_items => people.count, :items => jtable_items}.to_json)
27
+ people = Person.jtable_basic_query(@params)
28
+ jtable_items = people.jtable_basic_paginate(@params[:limit], @params[:offset]).collect(&:jtable_basic_item)
29
+ @people_controller.jtable_for_json(:basic, people, @params).to_json.should eql({:total_items => people.count, :items => jtable_items}.to_json)
30
30
  end
31
31
  end
@@ -0,0 +1,5 @@
1
+ Fabricator(:place) do
2
+ address {Fabricate.sequence(:place_address) {|i| "#{i} Some Rd."}}
3
+ city {Fabricate.sequence(:place_city) {|i| "City #{i}"}}
4
+ state {Fabricate.sequence(:place_state) {|i| "State #{i}"}}
5
+ end
@@ -0,0 +1,85 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe "Basic" do
4
+ before(:each) do
5
+ 10.times do |i|
6
+ Fabricate(:person)
7
+ end
8
+
9
+ @params = {
10
+ :searchable_columns => ["first_name", "last_name", "age", "date_of_birth", "gender"],
11
+ :column_search => {},
12
+ :limit => "5",
13
+ :offset => "0",
14
+ :search => "",
15
+ :sort_column => "",
16
+ :sort_direction => ""
17
+ }
18
+ end
19
+
20
+ it "should have the jtable scopes and methods" do
21
+ Person.respond_to?(:jtable).should be_true
22
+ Person.respond_to?(:jtable_basic_search).should be_true
23
+ Person.respond_to?(:jtable_basic_single_search).should be_true
24
+ Person.respond_to?(:jtable_basic_order).should be_true
25
+ Person.respond_to?(:jtable_basic_paginate).should be_true
26
+ Person.respond_to?(:jtable_basic_default).should be_true
27
+ Person.respond_to?(:jtable_basic_query).should be_true
28
+ [:first_name, :last_name, :age, :date_of_birth, :gender, :alive].each do |attribute|
29
+ Person.respond_to?("jtable_basic_search_#{attribute}").should be_true
30
+ Person.respond_to?("jtable_basic_order_#{attribute}").should be_true
31
+ Person.respond_to?("jtable_basic_attributes").should be_true
32
+ Person.new.respond_to?("jtable_basic_attribute_#{attribute}").should be_true
33
+ Person.new.respond_to?("jtable_basic_item").should be_true
34
+ end
35
+ end
36
+
37
+ it "should know jtables attributes" do
38
+ Person.jtable_basic_attributes.should eql([:first_name, :last_name, :age, :date_of_birth, :gender, :alive])
39
+ end
40
+
41
+ it "should load initial set" do
42
+ Person.jtable_basic_query(@params).to_json.should eql(Person.all.to_json)
43
+ end
44
+
45
+ it "should paginate" do
46
+ Person.jtable_basic_query(@params).jtable_basic_paginate(@params[:limit], @params[:offset]).to_json.should eql(Person.limit(5).offset(0).to_json)
47
+ @params[:offset] = 5
48
+ Person.jtable_basic_query(@params).jtable_basic_paginate(@params[:limit], @params[:offset]).to_json.should eql(Person.limit(5).offset(5).to_json)
49
+ end
50
+
51
+ it "should search" do
52
+ person_1 = Fabricate(:person)
53
+ person_2 = Fabricate(:person)
54
+ person_1.first_name = "John"
55
+ person_1.last_name = "Smith"
56
+ person_1.save
57
+ person_2.first_name = "Johnathan"
58
+ person_2.last_name = "Smith"
59
+ person_2.save
60
+ @params[:search] = "john smith"
61
+ Person.jtable_basic_query(@params).to_json.should eql(Person.where(['(first_name LIKE ? OR last_name LIKE ?) AND (first_name LIKE ? OR last_name LIKE ?)', "%john%", "%john%", "%smith%", "%smith%"]).to_json)
62
+ end
63
+
64
+ it "should single column search" do
65
+ person_1 = Fabricate(:person)
66
+ person_2 = Fabricate(:person)
67
+ person_1.first_name = "John"
68
+ person_1.save
69
+ person_2.first_name = "Johnathan"
70
+ person_2.save
71
+ @params[:column_search][:first_name] = "john"
72
+ Person.jtable_basic_query(@params).to_json.should eql(Person.where(['(first_name LIKE ?)', "%john%"]).to_json)
73
+ end
74
+
75
+ it "should order" do
76
+ @params[:sort_column] = "age"
77
+ @params[:sort_direction] = "ASC"
78
+ Person.jtable_basic_query(@params).to_json.should eql(Person.order('age ASC').to_json)
79
+ end
80
+
81
+ it "should format an item for jtable" do
82
+ person = Fabricate(:person, :first_name => "John", :last_name => "Smith", :age => 21, :date_of_birth => 21.years.ago, :gender => "Male", :alive => true)
83
+ person.jtable_basic_item.should eql({:id => person.id, :first_name => "John", :last_name => "Smith", :age => 21, :date_of_birth => 21.years.ago.strftime("%m/%d/%Y %I:%M%P"), :gender => "Male", :alive => true})
84
+ end
85
+ end
@@ -0,0 +1,64 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe "OneToOne" do
4
+ before(:each) do
5
+ 10.times do |i|
6
+ Fabricate(:person, :place => Fabricate(:place))
7
+ end
8
+
9
+ @params = {
10
+ :searchable_columns => ["first_name", "last_name", "age", "date_of_birth", "gender", "place_address", "place_city", "place_state"],
11
+ :column_search => {},
12
+ :limit => "5",
13
+ :offset => "0",
14
+ :search => "",
15
+ :sort_column => "",
16
+ :sort_direction => ""
17
+ }
18
+ end
19
+
20
+ it "Person should load initial set with nested Place" do
21
+ Person.jtable_one_to_one_query(@params).collect(&:jtable_one_to_one_item).to_json.should eql(Person.all.collect(&:jtable_one_to_one_item).to_json)
22
+ end
23
+
24
+ it "should paginate" do
25
+ Person.jtable_one_to_one_query(@params).jtable_one_to_one_paginate(@params[:limit], @params[:offset]).to_json.should eql(Person.limit(5).offset(0).to_json)
26
+ @params[:offset] = 5
27
+ Person.jtable_one_to_one_query(@params).jtable_one_to_one_paginate(@params[:limit], @params[:offset]).to_json.should eql(Person.limit(5).offset(5).to_json)
28
+ end
29
+
30
+ it "should search on top level object" do
31
+ person_1 = Fabricate(:person, :place => Fabricate(:place))
32
+ person_2 = Fabricate(:person, :place => Fabricate(:place))
33
+ person_1.first_name = "John"
34
+ person_1.last_name = "Smith"
35
+ person_1.save
36
+ person_2.first_name = "Johnathan"
37
+ person_2.last_name = "Smith"
38
+ person_2.save
39
+ @params[:search] = "john smith"
40
+ Person.jtable_one_to_one_query(@params).to_json.should eql(Person.where(['(first_name LIKE ? OR last_name LIKE ?) AND (first_name LIKE ? OR last_name LIKE ?)', "%john%", "%john%", "%smith%", "%smith%"]).to_json)
41
+ end
42
+
43
+ it "should search on nested object" do
44
+ place_1 = Person.first.place
45
+ place_2 = Person.last.place
46
+ place_1.state = "Alabama"
47
+ place_1.save
48
+ place_2.state = "Alaska"
49
+ place_2.save
50
+ @params[:search] = "ala"
51
+ Person.jtable_one_to_one_query(@params).to_json.should eql(Person.joins(:place).where(['"places".state LIKE ?', "%ala%"]).to_json)
52
+ end
53
+
54
+ it "should order on nested object" do
55
+ @params[:sort_column] = "place_state"
56
+ @params[:sort_direction] = "DESC"
57
+ Person.jtable_one_to_one_query(@params).to_json.should eql(Person.joins(:place).order('"places".state DESC').to_json)
58
+ end
59
+
60
+ it "Person should format an item for jtable with nested place" do
61
+ person = Fabricate(:person, :first_name => "John", :last_name => "Smith", :age => 21, :date_of_birth => 21.years.ago, :gender => "Male", :alive => true, :place => Fabricate(:place, :address => "100 Infinite Loop", :city => "Mountain View", :state => "California"))
62
+ person.jtable_one_to_one_item.should eql({:id => person.id, :first_name => "John", :last_name => "Smith", :age => 21, :date_of_birth => 21.years.ago.strftime("%m/%d/%Y %I:%M%P"), :gender => "Male", :alive => true, :place_address => "100 Infinite Loop", :place_city => "Mountain View", :place_state => "California"})
63
+ end
64
+ end
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: /Users/yelvert/projects/jtable/jtable-rails
3
3
  specs:
4
- jtable-rails (0.1.3)
4
+ jtable-rails (0.1.4)
5
5
  rails (= 3.0.5)
6
6
 
7
7
  GEM
@@ -0,0 +1,83 @@
1
+ class PlacesController < ApplicationController
2
+ # GET /places
3
+ # GET /places.xml
4
+ def index
5
+ @places = Place.all
6
+
7
+ respond_to do |format|
8
+ format.html # index.html.erb
9
+ format.xml { render :xml => @places }
10
+ end
11
+ end
12
+
13
+ # GET /places/1
14
+ # GET /places/1.xml
15
+ def show
16
+ @place = Place.find(params[:id])
17
+
18
+ respond_to do |format|
19
+ format.html # show.html.erb
20
+ format.xml { render :xml => @place }
21
+ end
22
+ end
23
+
24
+ # GET /places/new
25
+ # GET /places/new.xml
26
+ def new
27
+ @place = Place.new
28
+
29
+ respond_to do |format|
30
+ format.html # new.html.erb
31
+ format.xml { render :xml => @place }
32
+ end
33
+ end
34
+
35
+ # GET /places/1/edit
36
+ def edit
37
+ @place = Place.find(params[:id])
38
+ end
39
+
40
+ # POST /places
41
+ # POST /places.xml
42
+ def create
43
+ @place = Place.new(params[:place])
44
+
45
+ respond_to do |format|
46
+ if @place.save
47
+ format.html { redirect_to(@place, :notice => 'Place was successfully created.') }
48
+ format.xml { render :xml => @place, :status => :created, :location => @place }
49
+ else
50
+ format.html { render :action => "new" }
51
+ format.xml { render :xml => @place.errors, :status => :unprocessable_entity }
52
+ end
53
+ end
54
+ end
55
+
56
+ # PUT /places/1
57
+ # PUT /places/1.xml
58
+ def update
59
+ @place = Place.find(params[:id])
60
+
61
+ respond_to do |format|
62
+ if @place.update_attributes(params[:place])
63
+ format.html { redirect_to(@place, :notice => 'Place was successfully updated.') }
64
+ format.xml { head :ok }
65
+ else
66
+ format.html { render :action => "edit" }
67
+ format.xml { render :xml => @place.errors, :status => :unprocessable_entity }
68
+ end
69
+ end
70
+ end
71
+
72
+ # DELETE /places/1
73
+ # DELETE /places/1.xml
74
+ def destroy
75
+ @place = Place.find(params[:id])
76
+ @place.destroy
77
+
78
+ respond_to do |format|
79
+ format.html { redirect_to(places_url) }
80
+ format.xml { head :ok }
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,2 @@
1
+ module PlacesHelper
2
+ end
@@ -1,3 +1,7 @@
1
1
  class Person < ActiveRecord::Base
2
- jtable :first_name, :last_name, :age, :date_of_birth, :gender, :alive
2
+ has_one :place
3
+
4
+ jtable :basic, :first_name, :last_name, :age, :date_of_birth, :gender, :alive
5
+
6
+ jtable :one_to_one, :first_name, :last_name, :age, :date_of_birth, :gender, :alive, {:place => [:address, :city, :state]}
3
7
  end
@@ -0,0 +1,3 @@
1
+ class Place < ActiveRecord::Base
2
+ belongs_to :person
3
+ end
@@ -0,0 +1,29 @@
1
+ <%= form_for(@place) do |f| %>
2
+ <% if @place.errors.any? %>
3
+ <div id="error_explanation">
4
+ <h2><%= pluralize(@place.errors.count, "error") %> prohibited this place from being saved:</h2>
5
+
6
+ <ul>
7
+ <% @place.errors.full_messages.each do |msg| %>
8
+ <li><%= msg %></li>
9
+ <% end %>
10
+ </ul>
11
+ </div>
12
+ <% end %>
13
+
14
+ <div class="field">
15
+ <%= f.label :address %><br />
16
+ <%= f.text_field :address %>
17
+ </div>
18
+ <div class="field">
19
+ <%= f.label :city %><br />
20
+ <%= f.text_field :city %>
21
+ </div>
22
+ <div class="field">
23
+ <%= f.label :state %><br />
24
+ <%= f.text_field :state %>
25
+ </div>
26
+ <div class="actions">
27
+ <%= f.submit %>
28
+ </div>
29
+ <% end %>
@@ -0,0 +1,6 @@
1
+ <h1>Editing place</h1>
2
+
3
+ <%= render 'form' %>
4
+
5
+ <%= link_to 'Show', @place %> |
6
+ <%= link_to 'Back', places_path %>
@@ -0,0 +1,27 @@
1
+ <h1>Listing places</h1>
2
+
3
+ <table>
4
+ <tr>
5
+ <th>Address</th>
6
+ <th>City</th>
7
+ <th>State</th>
8
+ <th></th>
9
+ <th></th>
10
+ <th></th>
11
+ </tr>
12
+
13
+ <% @places.each do |place| %>
14
+ <tr>
15
+ <td><%= place.address %></td>
16
+ <td><%= place.city %></td>
17
+ <td><%= place.state %></td>
18
+ <td><%= link_to 'Show', place %></td>
19
+ <td><%= link_to 'Edit', edit_place_path(place) %></td>
20
+ <td><%= link_to 'Destroy', place, :confirm => 'Are you sure?', :method => :delete %></td>
21
+ </tr>
22
+ <% end %>
23
+ </table>
24
+
25
+ <br />
26
+
27
+ <%= link_to 'New Place', new_place_path %>
@@ -0,0 +1,5 @@
1
+ <h1>New place</h1>
2
+
3
+ <%= render 'form' %>
4
+
5
+ <%= link_to 'Back', places_path %>
@@ -0,0 +1,20 @@
1
+ <p id="notice"><%= notice %></p>
2
+
3
+ <p>
4
+ <b>Address:</b>
5
+ <%= @place.address %>
6
+ </p>
7
+
8
+ <p>
9
+ <b>City:</b>
10
+ <%= @place.city %>
11
+ </p>
12
+
13
+ <p>
14
+ <b>State:</b>
15
+ <%= @place.state %>
16
+ </p>
17
+
18
+
19
+ <%= link_to 'Edit', edit_place_path(@place) %> |
20
+ <%= link_to 'Back', places_path %>
@@ -38,5 +38,8 @@ module RailsApp
38
38
 
39
39
  # Configure sensitive parameters which will be filtered from the log file.
40
40
  config.filter_parameters += [:password]
41
+
42
+ ActiveRecord::Base.include_root_in_json = false
43
+ ActiveSupport.escape_html_entities_in_json = true
41
44
  end
42
45
  end
@@ -1,4 +1,6 @@
1
1
  RailsApp::Application.routes.draw do
2
+ resources :places
3
+
2
4
  resources :people
3
5
 
4
6
  # The priority is based upon order of creation:
@@ -0,0 +1,16 @@
1
+ class CreatePlaces < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :places do |t|
4
+ t.integer :person_id
5
+ t.string :address
6
+ t.string :city
7
+ t.string :state
8
+
9
+ t.timestamps
10
+ end
11
+ end
12
+
13
+ def self.down
14
+ drop_table :places
15
+ end
16
+ end
@@ -10,7 +10,7 @@
10
10
  #
11
11
  # It's strongly recommended to check this file into your version control system.
12
12
 
13
- ActiveRecord::Schema.define(:version => 20110207142623) do
13
+ ActiveRecord::Schema.define(:version => 20110302024653) do
14
14
 
15
15
  create_table "people", :force => true do |t|
16
16
  t.string "first_name"
@@ -23,4 +23,13 @@ ActiveRecord::Schema.define(:version => 20110207142623) do
23
23
  t.datetime "updated_at"
24
24
  end
25
25
 
26
+ create_table "places", :force => true do |t|
27
+ t.integer "person_id"
28
+ t.string "address"
29
+ t.string "city"
30
+ t.string "state"
31
+ t.datetime "created_at"
32
+ t.datetime "updated_at"
33
+ end
34
+
26
35
  end
@@ -0,0 +1,11 @@
1
+ # Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
2
+
3
+ one:
4
+ address: MyString
5
+ city: MyString
6
+ state: MyString
7
+
8
+ two:
9
+ address: MyString
10
+ city: MyString
11
+ state: MyString
@@ -0,0 +1,49 @@
1
+ require 'test_helper'
2
+
3
+ class PlacesControllerTest < ActionController::TestCase
4
+ setup do
5
+ @place = places(:one)
6
+ end
7
+
8
+ test "should get index" do
9
+ get :index
10
+ assert_response :success
11
+ assert_not_nil assigns(:places)
12
+ end
13
+
14
+ test "should get new" do
15
+ get :new
16
+ assert_response :success
17
+ end
18
+
19
+ test "should create place" do
20
+ assert_difference('Place.count') do
21
+ post :create, :place => @place.attributes
22
+ end
23
+
24
+ assert_redirected_to place_path(assigns(:place))
25
+ end
26
+
27
+ test "should show place" do
28
+ get :show, :id => @place.to_param
29
+ assert_response :success
30
+ end
31
+
32
+ test "should get edit" do
33
+ get :edit, :id => @place.to_param
34
+ assert_response :success
35
+ end
36
+
37
+ test "should update place" do
38
+ put :update, :id => @place.to_param, :place => @place.attributes
39
+ assert_redirected_to place_path(assigns(:place))
40
+ end
41
+
42
+ test "should destroy place" do
43
+ assert_difference('Place.count', -1) do
44
+ delete :destroy, :id => @place.to_param
45
+ end
46
+
47
+ assert_redirected_to places_path
48
+ end
49
+ end
@@ -0,0 +1,4 @@
1
+ require 'test_helper'
2
+
3
+ class PlacesHelperTest < ActionView::TestCase
4
+ end
@@ -0,0 +1,8 @@
1
+ require 'test_helper'
2
+
3
+ class PlaceTest < ActiveSupport::TestCase
4
+ # Replace this with your real tests.
5
+ test "the truth" do
6
+ assert true
7
+ end
8
+ end
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 1
8
- - 4
9
- version: 0.1.4
7
+ - 2
8
+ - 0
9
+ version: 0.2.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Taylor Yelverton
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2011-02-28 00:00:00 -06:00
17
+ date: 2011-04-08 00:00:00 -05:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -100,8 +100,21 @@ dependencies:
100
100
  prerelease: false
101
101
  version_requirements: *id006
102
102
  - !ruby/object:Gem::Dependency
103
- name: rails
103
+ name: ruby-debug19
104
104
  requirement: &id007 !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ segments:
110
+ - 0
111
+ version: "0"
112
+ type: :development
113
+ prerelease: false
114
+ version_requirements: *id007
115
+ - !ruby/object:Gem::Dependency
116
+ name: rails
117
+ requirement: &id008 !ruby/object:Gem::Requirement
105
118
  none: false
106
119
  requirements:
107
120
  - - "="
@@ -113,7 +126,7 @@ dependencies:
113
126
  version: 3.0.5
114
127
  type: :runtime
115
128
  prerelease: false
116
- version_requirements: *id007
129
+ version_requirements: *id008
117
130
  description: A Rails gem to power the jTable jQuery plugin
118
131
  email: tny5357@gmail.com
119
132
  executables: []
@@ -140,9 +153,11 @@ files:
140
153
  - lib/jtable-rails/railtie.rb
141
154
  - lib/jtable-rails/version.rb
142
155
  - lib/tasks/jtable_rails.rake
143
- - spec/controller_specs/people_controller_spec.rb
156
+ - spec/controller_specs/basic_controller_spec.rb
144
157
  - spec/fabricators/person.rb
145
- - spec/model_specs/person_spec.rb
158
+ - spec/fabricators/place.rb
159
+ - spec/model_specs/basic_spec.rb
160
+ - spec/model_specs/one_to_one_spec.rb
146
161
  - spec/spec_helper.rb
147
162
  - spec/support/rails_app/.DS_Store
148
163
  - spec/support/rails_app/.gitignore
@@ -151,15 +166,23 @@ files:
151
166
  - spec/support/rails_app/Rakefile
152
167
  - spec/support/rails_app/app/controllers/application_controller.rb
153
168
  - spec/support/rails_app/app/controllers/people_controller.rb
169
+ - spec/support/rails_app/app/controllers/places_controller.rb
154
170
  - spec/support/rails_app/app/helpers/application_helper.rb
155
171
  - spec/support/rails_app/app/helpers/people_helper.rb
172
+ - spec/support/rails_app/app/helpers/places_helper.rb
156
173
  - spec/support/rails_app/app/models/person.rb
174
+ - spec/support/rails_app/app/models/place.rb
157
175
  - spec/support/rails_app/app/views/layouts/application.html.erb
158
176
  - spec/support/rails_app/app/views/people/_form.html.erb
159
177
  - spec/support/rails_app/app/views/people/edit.html.erb
160
178
  - spec/support/rails_app/app/views/people/index.html.erb
161
179
  - spec/support/rails_app/app/views/people/new.html.erb
162
180
  - spec/support/rails_app/app/views/people/show.html.erb
181
+ - spec/support/rails_app/app/views/places/_form.html.erb
182
+ - spec/support/rails_app/app/views/places/edit.html.erb
183
+ - spec/support/rails_app/app/views/places/index.html.erb
184
+ - spec/support/rails_app/app/views/places/new.html.erb
185
+ - spec/support/rails_app/app/views/places/show.html.erb
163
186
  - spec/support/rails_app/config.ru
164
187
  - spec/support/rails_app/config/application.rb
165
188
  - spec/support/rails_app/config/boot.rb
@@ -176,6 +199,7 @@ files:
176
199
  - spec/support/rails_app/config/locales/en.yml
177
200
  - spec/support/rails_app/config/routes.rb
178
201
  - spec/support/rails_app/db/migrate/20110207142623_create_people.rb
202
+ - spec/support/rails_app/db/migrate/20110302024653_create_places.rb
179
203
  - spec/support/rails_app/db/schema.rb
180
204
  - spec/support/rails_app/db/seeds.rb
181
205
  - spec/support/rails_app/public/images/jTable-icons.png
@@ -184,6 +208,10 @@ files:
184
208
  - spec/support/rails_app/public/stylesheets/jTable.css
185
209
  - spec/support/rails_app/public/stylesheets/scaffold.css
186
210
  - spec/support/rails_app/script/rails
211
+ - spec/support/rails_app/test/fixtures/places.yml
212
+ - spec/support/rails_app/test/functional/places_controller_test.rb
213
+ - spec/support/rails_app/test/unit/helpers/places_helper_test.rb
214
+ - spec/support/rails_app/test/unit/place_test.rb
187
215
  - spec/support/rails_app/tmp/.DS_Store
188
216
  - spec/support/rails_app/tmp/jtable.zip
189
217
  has_rdoc: true
@@ -200,7 +228,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
200
228
  requirements:
201
229
  - - ">="
202
230
  - !ruby/object:Gem::Version
203
- hash: 2776022680366934975
231
+ hash: -183630829231020060
204
232
  segments:
205
233
  - 0
206
234
  version: "0"
@@ -220,15 +248,20 @@ signing_key:
220
248
  specification_version: 3
221
249
  summary: A Rails gem to power the jTable jQuery plugin
222
250
  test_files:
223
- - spec/controller_specs/people_controller_spec.rb
251
+ - spec/controller_specs/basic_controller_spec.rb
224
252
  - spec/fabricators/person.rb
225
- - spec/model_specs/person_spec.rb
253
+ - spec/fabricators/place.rb
254
+ - spec/model_specs/basic_spec.rb
255
+ - spec/model_specs/one_to_one_spec.rb
226
256
  - spec/spec_helper.rb
227
257
  - spec/support/rails_app/app/controllers/application_controller.rb
228
258
  - spec/support/rails_app/app/controllers/people_controller.rb
259
+ - spec/support/rails_app/app/controllers/places_controller.rb
229
260
  - spec/support/rails_app/app/helpers/application_helper.rb
230
261
  - spec/support/rails_app/app/helpers/people_helper.rb
262
+ - spec/support/rails_app/app/helpers/places_helper.rb
231
263
  - spec/support/rails_app/app/models/person.rb
264
+ - spec/support/rails_app/app/models/place.rb
232
265
  - spec/support/rails_app/config/application.rb
233
266
  - spec/support/rails_app/config/boot.rb
234
267
  - spec/support/rails_app/config/environment.rb
@@ -242,5 +275,9 @@ test_files:
242
275
  - spec/support/rails_app/config/initializers/session_store.rb
243
276
  - spec/support/rails_app/config/routes.rb
244
277
  - spec/support/rails_app/db/migrate/20110207142623_create_people.rb
278
+ - spec/support/rails_app/db/migrate/20110302024653_create_places.rb
245
279
  - spec/support/rails_app/db/schema.rb
246
280
  - spec/support/rails_app/db/seeds.rb
281
+ - spec/support/rails_app/test/functional/places_controller_test.rb
282
+ - spec/support/rails_app/test/unit/helpers/places_helper_test.rb
283
+ - spec/support/rails_app/test/unit/place_test.rb
@@ -1,85 +0,0 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
-
3
- describe "Person" do
4
- before(:each) do
5
- 10.times do |i|
6
- Fabricate(:person)
7
- end
8
-
9
- @params = {
10
- :searchable_columns => ["first_name", "last_name", "age", "date_of_birth", "gender"],
11
- :column_search => {},
12
- :limit => "5",
13
- :offset => "0",
14
- :search => "",
15
- :sort_column => "",
16
- :sort_direction => ""
17
- }
18
- end
19
-
20
- it "should have the jtable scopes and methods" do
21
- Person.respond_to?(:jtable).should be_true
22
- Person.respond_to?(:jtable_search).should be_true
23
- Person.respond_to?(:jtable_single_search).should be_true
24
- Person.respond_to?(:jtable_order).should be_true
25
- Person.respond_to?(:jtable_paginate).should be_true
26
- Person.respond_to?(:jtable_default).should be_true
27
- Person.respond_to?(:jtable_query).should be_true
28
- [:first_name, :last_name, :age, :date_of_birth, :gender, :alive].each do |attribute|
29
- Person.respond_to?("jtable_search_#{attribute}").should be_true
30
- Person.respond_to?("jtable_order_#{attribute}").should be_true
31
- Person.respond_to?("jtable_attributes").should be_true
32
- Person.new.respond_to?("jtable_attribute_#{attribute}").should be_true
33
- Person.new.respond_to?("jtable_item").should be_true
34
- end
35
- end
36
-
37
- it "should know jtables attributes" do
38
- Person.jtable_attributes.should eql([:first_name, :last_name, :age, :date_of_birth, :gender, :alive])
39
- end
40
-
41
- it "should load initial set" do
42
- Person.jtable_query(@params).to_json.should eql(Person.all.to_json)
43
- end
44
-
45
- it "should paginate" do
46
- Person.jtable_query(@params).jtable_paginate(@params[:limit], @params[:offset]).to_json.should eql(Person.limit(5).offset(0).to_json)
47
- @params[:offset] = 5
48
- Person.jtable_query(@params).jtable_paginate(@params[:limit], @params[:offset]).to_json.should eql(Person.limit(5).offset(5).to_json)
49
- end
50
-
51
- it "should search" do
52
- person_1 = Person.first
53
- person_2 = Person.last
54
- person_1.first_name = "John"
55
- person_1.last_name = "Smith"
56
- person_1.save
57
- person_2.first_name = "Johnathan"
58
- person_2.last_name = "Smith"
59
- person_2.save
60
- @params[:search] = "john smith"
61
- Person.jtable_query(@params).to_json.should eql(Person.where(['(first_name LIKE ? OR last_name LIKE ?) AND (first_name LIKE ? OR last_name LIKE ?)', "%john%", "%john%", "%smith%", "%smith%"]).to_json)
62
- end
63
-
64
- it "should single column search" do
65
- person_1 = Person.first
66
- person_2 = Person.last
67
- person_1.first_name = "John"
68
- person_1.save
69
- person_2.first_name = "Johnathan"
70
- person_2.save
71
- @params[:column_search][:first_name] = "john"
72
- Person.jtable_query(@params).to_json.should eql(Person.where(['(first_name LIKE ?)', "%john%"]).to_json)
73
- end
74
-
75
- it "should order" do
76
- @params[:sort_column] = "age"
77
- @params[:sort_direction] = "ASC"
78
- Person.jtable_query(@params).to_json.should eql(Person.order('age ASC').to_json)
79
- end
80
-
81
- it "should format an item for jtable" do
82
- person = Fabricate(:person, :first_name => "John", :last_name => "Smith", :age => 21, :date_of_birth => 21.years.ago, :gender => "Male", :alive => true)
83
- person.jtable_item.should eql({:id => person.id, :first_name => "John", :last_name => "Smith", :age => 21, :date_of_birth => 21.years.ago.strftime("%m/%d/%Y %I:%M%P"), :gender => "Male", :alive => true})
84
- end
85
- end