jtable-rails 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/jtable-rails.gemspec +1 -1
- data/lib/jtable-rails/action_controller.rb +2 -1
- data/lib/jtable-rails/active_record.rb +24 -2
- data/spec/controller_specs/people_controller_spec.rb +2 -1
- data/spec/model_specs/person_spec.rb +12 -0
- data/spec/support/rails_app/Gemfile.lock +1 -1
- metadata +3 -3
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.3
|
data/jtable-rails.gemspec
CHANGED
@@ -2,7 +2,8 @@ module JTable
|
|
2
2
|
module ActionController
|
3
3
|
def jtable_for_json(rel, jtable_params)
|
4
4
|
jtable_params = HashWithIndifferentAccess.new(jtable_params)
|
5
|
-
|
5
|
+
items = rel.jtable_paginate(jtable_params[:limit], jtable_params[:offset])
|
6
|
+
{:total_items => rel.count, :items => items.collect(&:jtable_item)}
|
6
7
|
end
|
7
8
|
end
|
8
9
|
end
|
@@ -7,7 +7,7 @@ module JTable
|
|
7
7
|
self
|
8
8
|
end
|
9
9
|
fields.each do |field|
|
10
|
-
metaclass.instance_eval do
|
10
|
+
metaclass.instance_eval do
|
11
11
|
define_method "jtable_search_#{field}" do |term|
|
12
12
|
unless [:date].include? arel_table[field.to_sym].column.type
|
13
13
|
if [:integer, :boolean].include? arel_table[field.to_sym].column.type
|
@@ -17,11 +17,33 @@ module JTable
|
|
17
17
|
end
|
18
18
|
end
|
19
19
|
end
|
20
|
-
|
20
|
+
|
21
21
|
define_method "jtable_order_#{field}" do |direction|
|
22
22
|
"#{field} #{direction}"
|
23
23
|
end
|
24
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)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
metaclass.instance_eval do
|
35
|
+
define_method "jtable_attributes" do
|
36
|
+
fields
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
define_method "jtable_item" do
|
41
|
+
item_hash = {}
|
42
|
+
item_hash[:id] = self.id
|
43
|
+
fields.each do |field|
|
44
|
+
item_hash[field.to_sym] = self.send("jtable_attribute_#{field}")
|
45
|
+
end
|
46
|
+
item_hash
|
25
47
|
end
|
26
48
|
|
27
49
|
scope :jtable_search, lambda { |jtable_params|
|
@@ -25,6 +25,7 @@ describe "PeopleController" do
|
|
25
25
|
|
26
26
|
it "should return the appropriate json" do
|
27
27
|
people = Person.jtable_query(@params)
|
28
|
-
|
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)
|
29
30
|
end
|
30
31
|
end
|
@@ -28,9 +28,16 @@ describe "Person" do
|
|
28
28
|
[:first_name, :last_name, :age, :date_of_birth, :gender, :alive].each do |attribute|
|
29
29
|
Person.respond_to?("jtable_search_#{attribute}").should be_true
|
30
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
|
31
34
|
end
|
32
35
|
end
|
33
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
|
+
|
34
41
|
it "should load initial set" do
|
35
42
|
Person.jtable_query(@params).to_json.should eql(Person.all.to_json)
|
36
43
|
end
|
@@ -70,4 +77,9 @@ describe "Person" do
|
|
70
77
|
@params[:sort_direction] = "ASC"
|
71
78
|
Person.jtable_query(@params).to_json.should eql(Person.order('age ASC').to_json)
|
72
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
|
73
85
|
end
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 1
|
8
|
-
-
|
9
|
-
version: 0.1.
|
8
|
+
- 3
|
9
|
+
version: 0.1.3
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Taylor Yelverton
|
@@ -190,7 +190,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
190
190
|
requirements:
|
191
191
|
- - ">="
|
192
192
|
- !ruby/object:Gem::Version
|
193
|
-
hash:
|
193
|
+
hash: 626560608574340471
|
194
194
|
segments:
|
195
195
|
- 0
|
196
196
|
version: "0"
|