roar-extensions 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -27,3 +27,8 @@ TODO: Write usage instructions here
27
27
  3. Commit your changes (`git commit -am 'Added some feature'`)
28
28
  4. Push to the branch (`git push origin my-new-feature`)
29
29
  5. Create new Pull Request
30
+
31
+ ## License
32
+
33
+ `roar-extensions` is released under the [MIT
34
+ License](http://www.opensource.org/licenses/MIT).
@@ -0,0 +1,23 @@
1
+ module RoarExtensions
2
+ class KaminariShim < SimpleDelegator
3
+ def per_page
4
+ limit_value
5
+ end
6
+
7
+ def next_page
8
+ last_page? ? nil : current_page + 1
9
+ end
10
+
11
+ def previous_page
12
+ first_page? ? nil : current_page - 1
13
+ end
14
+
15
+ def total_pages
16
+ num_pages
17
+ end
18
+
19
+ def total_entries
20
+ total_count
21
+ end
22
+ end
23
+ end
@@ -1,3 +1,4 @@
1
+ require 'roar_extensions/kaminari_shim'
1
2
  module RoarExtensions
2
3
  class PaginatedCollectionPresenter
3
4
  include RoarExtensions::Presenter
@@ -16,6 +17,9 @@ module RoarExtensions
16
17
  link(:rel => "previous_page") { page_link(record.previous_page) }
17
18
 
18
19
  def initialize(record, base_path)
20
+ unless record.respond_to?(:next_page)
21
+ record = KaminariShim.new(record)
22
+ end
19
23
  super(record)
20
24
  @base_path = base_path
21
25
  end
@@ -1,3 +1,3 @@
1
1
  module RoarExtensions
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -29,148 +29,172 @@ module RoarExtensions
29
29
  let(:entries) {[
30
30
  TestEntry.new("Bob", 41)
31
31
  ]}
32
- let(:paginated_result) do
33
- mock("Paginated Result", :total_pages => 3,
34
- :current_page => current_page,
35
- :next_page => next_page,
36
- :previous_page => previous_page,
37
- :total_entries => 3,
38
- :collect => entries,
39
- :per_page => 1)
40
- end
32
+
41
33
  let(:base_path) { "/things" }
42
34
  let(:json_options) {{}}
43
35
 
44
- describe "#to_hash" do
45
- let(:presenter) { PaginatedCollectionPresenter.new(paginated_result,
46
- base_path)}
47
- subject { JSON.parse(presenter.to_json(json_options))['paginated_collection'] }
48
-
49
- it "includes the total pages" do
50
- subject['total_pages'].should == 3
51
- end
52
-
53
- it "includes the total entries" do
54
- subject['total_entries'].should == 3
55
- end
56
-
57
- it "includes the per page" do
58
- subject['per_page'].should == 1
59
- end
60
-
61
- it "includes the self link" do
62
- subject['_links']['self'].should == {"href" => "/things"}
63
- end
64
-
65
- it "includes the next_page link" do
66
- subject['_links']['next_page'].should == {"href" => "/things?page=2"}
67
- end
68
-
69
- it "does not include the previous_page link on the first page" do
70
- subject['_links'].should_not have_key('previous_page')
71
- end
72
-
73
- it "includes the paginated results under the entries key" do
74
- subject['entries'].should == [{'name' => 'Bob',
75
- 'age' => 41,
76
- 'lowercased_name' => 'bob'}]
77
- end
78
-
79
- it "includes current_page" do
80
- subject["current_page"].should == 1
81
- end
36
+ shared_examples_for "pagination" do
37
+ describe "#to_hash" do
38
+ let(:presenter) { PaginatedCollectionPresenter.new(paginated_result,
39
+ base_path)}
40
+ subject { JSON.parse(presenter.to_json(json_options))['paginated_collection'] }
82
41
 
83
- it "includes next_page" do
84
- subject["next_page"].should == 2
85
- end
42
+ it "includes the total pages" do
43
+ subject['total_pages'].should == 3
44
+ end
86
45
 
87
- it "includes previous_page" do
88
- subject["previous_page"].should == nil
89
- end
46
+ it "includes the total entries" do
47
+ subject['total_entries'].should == 3
48
+ end
90
49
 
91
- context "on last page" do
92
- let(:previous_page) { 2 }
93
- let(:current_page) { 3 }
94
- let(:next_page) { nil }
50
+ it "includes the per page" do
51
+ subject['per_page'].should == 1
52
+ end
95
53
 
96
54
  it "includes the self link" do
97
- subject['_links']['self'].should == {"href" => "/things?page=3"}
55
+ subject['_links']['self'].should == {"href" => "/things"}
56
+ end
57
+
58
+ it "includes the next_page link" do
59
+ subject['_links']['next_page'].should == {"href" => "/things?page=2"}
98
60
  end
99
61
 
100
- it "does not include the next_page link" do
101
- subject['_links'].should_not have_key('next_page')
62
+ it "does not include the previous_page link on the first page" do
63
+ subject['_links'].should_not have_key('previous_page')
102
64
  end
103
65
 
104
- it "includes the previous_page link" do
105
- subject['_links']['previous_page'].should == {"href" => "/things?page=2"}
66
+ it "includes the paginated results under the entries key" do
67
+ subject['entries'].should == [{'name' => 'Bob',
68
+ 'age' => 41,
69
+ 'lowercased_name' => 'bob'}]
106
70
  end
107
71
 
108
72
  it "includes current_page" do
109
- subject["current_page"].should == 3
73
+ subject["current_page"].should == 1
110
74
  end
111
75
 
112
76
  it "includes next_page" do
113
- subject["next_page"].should == nil
77
+ subject["next_page"].should == 2
114
78
  end
115
79
 
116
80
  it "includes previous_page" do
117
- subject["previous_page"].should == 2
81
+ subject["previous_page"].should == nil
118
82
  end
119
- end
120
83
 
121
- context "middle page" do
122
- let(:previous_page) { 1 }
123
- let(:current_page) { 2 }
124
- let(:next_page) { 3 }
84
+ context "on last page" do
85
+ let(:previous_page) { 2 }
86
+ let(:current_page) { 3 }
87
+ let(:next_page) { nil }
125
88
 
126
- it "includes the self link" do
127
- subject['_links']['self'].should == {"href" => "/things?page=2"}
128
- end
89
+ it "includes the self link" do
90
+ subject['_links']['self'].should == {"href" => "/things?page=3"}
91
+ end
129
92
 
130
- it "includes the next_page link" do
131
- subject['_links']['next_page'].should == {"href" => "/things?page=3"}
132
- end
93
+ it "does not include the next_page link" do
94
+ subject['_links'].should_not have_key('next_page')
95
+ end
133
96
 
134
- it "includes the previous_page link" do
135
- subject['_links']['previous_page'].should == {"href" => "/things"}
136
- end
97
+ it "includes the previous_page link" do
98
+ subject['_links']['previous_page'].should == {"href" => "/things?page=2"}
99
+ end
137
100
 
138
- it "includes current_page" do
139
- subject["current_page"].should == 2
140
- end
101
+ it "includes current_page" do
102
+ subject["current_page"].should == 3
103
+ end
141
104
 
142
- it "includes next_page" do
143
- subject["next_page"].should == 3
105
+ it "includes next_page" do
106
+ subject["next_page"].should == nil
107
+ end
108
+
109
+ it "includes previous_page" do
110
+ subject["previous_page"].should == 2
111
+ end
144
112
  end
145
113
 
146
- it "includes previous_page" do
147
- subject["previous_page"].should == 1
114
+ context "middle page" do
115
+ let(:previous_page) { 1 }
116
+ let(:current_page) { 2 }
117
+ let(:next_page) { 3 }
118
+
119
+ it "includes the self link" do
120
+ subject['_links']['self'].should == {"href" => "/things?page=2"}
121
+ end
122
+
123
+ it "includes the next_page link" do
124
+ subject['_links']['next_page'].should == {"href" => "/things?page=3"}
125
+ end
126
+
127
+ it "includes the previous_page link" do
128
+ subject['_links']['previous_page'].should == {"href" => "/things"}
129
+ end
130
+
131
+ it "includes current_page" do
132
+ subject["current_page"].should == 2
133
+ end
134
+
135
+ it "includes next_page" do
136
+ subject["next_page"].should == 3
137
+ end
138
+
139
+ it "includes previous_page" do
140
+ subject["previous_page"].should == 1
141
+ end
148
142
  end
149
- end
150
143
 
151
- context "attribute whitelisting" do
152
- let(:json_options) {{:include => [:name]}}
144
+ context "attribute whitelisting" do
145
+ let(:json_options) {{:include => [:name]}}
146
+
147
+ it "the include option is passed to the entries" do
148
+ subject['entries'].should == [{'name' => 'Bob'}]
149
+ end
150
+
151
+ context "using the :from property option" do
152
+ let(:json_options) {{:include => ["lowercased_name"]}}
153
153
 
154
- it "the include option is passed to the entries" do
155
- subject['entries'].should == [{'name' => 'Bob'}]
154
+ it "the include option is passed to the entries" do
155
+ subject['entries'].should == [{'lowercased_name' => 'bob'}]
156
+ end
157
+ end
156
158
  end
157
159
 
158
- context "using the :from property option" do
159
- let(:json_options) {{:include => ["lowercased_name"]}}
160
+ context "attribute blacklisting" do
161
+ let(:json_options) {{:exclude => [:name]}}
160
162
 
161
163
  it "the include option is passed to the entries" do
162
- subject['entries'].should == [{'lowercased_name' => 'bob'}]
164
+ subject['entries'].should == [{'age' => 41, 'lowercased_name' => 'bob'}]
163
165
  end
164
166
  end
165
167
  end
168
+ end
169
+
170
+ context "will-pagination integration" do
171
+ let(:paginated_result) do
172
+ mock("Paginated Result", :total_pages => 3,
173
+ :current_page => current_page,
174
+ :next_page => next_page,
175
+ :previous_page => previous_page,
176
+ :total_entries => 3,
177
+ :collect => entries,
178
+ :per_page => 1)
179
+ end
166
180
 
167
- context "attribute blacklisting" do
168
- let(:json_options) {{:exclude => [:name]}}
181
+ it_should_behave_like "pagination"
182
+ end
169
183
 
170
- it "the include option is passed to the entries" do
171
- subject['entries'].should == [{'age' => 41, 'lowercased_name' => 'bob'}]
172
- end
184
+ context "kaminari integration" do
185
+ let(:is_first_page) { current_page == 1 }
186
+ let(:is_last_page) { current_page == 3 }
187
+ let(:paginated_result) do
188
+ mock("Paginated Result", :current_page => current_page,
189
+ :collect => entries,
190
+ :last_page? => is_last_page,
191
+ :first_page? => is_first_page,
192
+ :limit_value => 1,
193
+ :total_count => 3,
194
+ :num_pages => 3)
173
195
  end
196
+
197
+ it_should_behave_like "pagination"
174
198
  end
175
199
  end
176
200
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: roar-extensions
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2012-09-27 00:00:00.000000000 Z
13
+ date: 2012-10-29 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: roar
@@ -160,6 +160,7 @@ files:
160
160
  - lib/roar_extensions/helpers/embedded_parameter_parsing.rb
161
161
  - lib/roar_extensions/hypermedia_extensions.rb
162
162
  - lib/roar_extensions/json_hal_extensions.rb
163
+ - lib/roar_extensions/kaminari_shim.rb
163
164
  - lib/roar_extensions/link_presenter.rb
164
165
  - lib/roar_extensions/money_presenter.rb
165
166
  - lib/roar_extensions/paginated_collection_presenter.rb