roar-rails 1.0.0 → 1.0.1

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1dbecd0bbc7e521087763fd2ff0313d7d3cda940
4
- data.tar.gz: 3b14e95c24d51ee0003f97c4c079dafc62a40699
3
+ metadata.gz: a55ffa3152e3f698afddef93555e37f5b61d232c
4
+ data.tar.gz: b3a48ed2ee0c550d255e1a5b031609c53f050a8e
5
5
  SHA512:
6
- metadata.gz: 0bcfc77fe5ff9d9e0fdb48d962bad6ffd2f44fcbd7446d31b4fbf3f1c14bbadad93ead2356103024f2923d0d1104f0317c93398d5ede28b5e5beaa499d4b30d8
7
- data.tar.gz: 87b69c8d04bfbf819c32b0bbe2dca74ac929d374f315837a87d449a76656eaf2b0ba090ef93b605bfeaa0579c34f91070c768f3f886be9e1902059dffebeb73c
6
+ metadata.gz: a6701490dd9bfbe6730bf702ac41d536e3158982ab469b7a8f217d6469051220b85cfe6019636a261196a5d91bedd579df740482dd6dbb909fedc342a221593c
7
+ data.tar.gz: 08128efd725c3cae0bf5c5a7bf78ed84e1f71c9c3f48b7569a8676dfe7470bbd21f26894f3f3c2bbfd9ad82ff33bfb450d70bf785bca53a83058a2ea8dc0bff1
@@ -6,3 +6,4 @@ gemfile:
6
6
  - gemfiles/Gemfile.rails3-0
7
7
  - gemfiles/Gemfile.rails3-2
8
8
  - gemfiles/Gemfile.rails4-0
9
+ - gemfiles/Gemfile.rails4-2
@@ -1,3 +1,9 @@
1
+ ## 1.0.1
2
+
3
+ * Added a dependency on `responders` gem for Rails 4.2 support
4
+ * `#render` from `ControllerAdditions::Render` now accepts a status code
5
+ * Added support for JSON-API.
6
+
1
7
  ## 1.0.0
2
8
 
3
9
  * Requires Roar >= 1.0.0.
@@ -270,21 +270,30 @@ end
270
270
 
271
271
  Put your representers in `app/representers` and they will be autoloaded by Rails. Also, frequently used modules as media representers and features don't need to be required manually.
272
272
 
273
- ## Rails 4.1 + HAL
273
+ ## Rails 4.1+ and HAL/JSON-API
274
274
 
275
275
  **Note**: this is a temporary work-around, we're trying to fix that in Rails/roar-rails itself [May 2014].
276
276
 
277
- Rails 4.1 expects you to manually register a global HAL renderer, or `respond_with` will throw the exception `ActionController::MissingRenderer (No renderer defined for format: hal)`.
277
+ Rails 4.1 and up expects you to manually register a global HAL or JSON-API renderer, or `respond_with` will throw an `ActionController::MissingRenderer` exception.
278
278
 
279
279
  One fix is to add this to `config/initializers/mime_types.rb` right below `Mime::Type.register 'application/hal+json', :hal`:
280
280
 
281
281
  ```ruby
282
282
  ActionController::Renderers.add :hal do |obj, options|
283
283
  self.content_type ||= Mime[:hal]
284
- obj.to_hal
284
+ obj
285
285
  end
286
286
  ```
287
287
 
288
+ Similarly, for JSON-API, below `Mime::Type.register 'application/vnd.api+json', :json_api` add:
289
+
290
+
291
+ ```ruby
292
+ ActionController::Renderers.add :json_api do |obj, options|
293
+ self.content_type ||= Mime[:json_api]
294
+ obj
295
+ end
296
+ ```
288
297
  ## Contributors
289
298
 
290
299
  * [Railslove](http://www.railslove.de) and especially Michael Bumann [bumi] have heavily supported development of roar-rails ("resource :singers").
@@ -0,0 +1,12 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in roar-rails.gemspec
4
+ gemspec :path => '../'
5
+
6
+ gem 'railties', '~> 4.2.0'
7
+ gem 'actionpack', '~> 4.2.0'
8
+
9
+ gem 'activemodel', '~> 4.2.0'
10
+ gem 'activerecord', '~> 4.2.0'
11
+
12
+ gem 'minitest', '~> 5.5.0'
@@ -10,7 +10,7 @@ Examples:
10
10
  This will create a representer as follows:
11
11
 
12
12
  module BandRepresenter
13
- include Roar::Representer::JSON
13
+ include Roar::JSON
14
14
 
15
15
  property :id
16
16
  property :name
@@ -18,12 +18,12 @@ Examples:
18
18
 
19
19
  You can pass :class and :extend property options:
20
20
 
21
- rails g singer id instrument:equipament:instrument_representer
21
+ rails g singer id instrument:equipment:instrument_representer
22
22
 
23
23
  This will add property options:
24
24
 
25
25
  module SingerRepresenter
26
- include Roar::Representer::JSON
26
+ include Roar::JSON
27
27
 
28
28
  property :id
29
29
  property :instrument, :class => Equipment, :extend => InstrumentRepresenter
@@ -34,7 +34,7 @@ Examples:
34
34
  rails g representer Band id name --format=xml
35
35
 
36
36
  module BandRepresenter
37
- include Roar::Representer::XML
37
+ include Roar::XML
38
38
 
39
39
  property :id
40
40
  property :name
@@ -1,5 +1,5 @@
1
1
  module <%= class_name %>Representer
2
- include Roar::Representer::<%= format %>
2
+ include Roar::<%= format %>
3
3
  <% property_options.each do |property| %>
4
4
  <%= property -%>
5
5
  <% end %>
@@ -9,6 +9,7 @@ module Roar
9
9
 
10
10
  module JSON
11
11
  autoload("HAL", "roar/rails/hal")
12
+ autoload("JSONAPI", "roar/rails/json_api")
12
13
  end
13
14
 
14
15
  autoload("Hypermedia", "roar/hypermedia")
@@ -28,7 +29,10 @@ module Roar
28
29
  require 'roar/rails/rails3_1_strategy'
29
30
  elsif rails_version.~ 3.2
30
31
  require 'roar/rails/rails3_2_strategy'
32
+ elsif rails_version.~ 4.2
33
+ require 'roar/rails/rails4_2_strategy'
31
34
  else
35
+ # fallback to 4.0 strategy
32
36
  require 'roar/rails/rails4_0_strategy'
33
37
  end
34
38
 
@@ -1,3 +1,4 @@
1
+ require 'responders'
1
2
  require 'uber/inheritable_attr'
2
3
  require 'roar/rails/responder'
3
4
  require 'roar/rails/formats'
@@ -79,7 +80,8 @@ module Roar::Rails
79
80
  module Render
80
81
  def render(options)
81
82
  format = options.keys.first
82
- super format => prepare_model_for(format, options.values.first, options)
83
+ status = options[:status] || :ok
84
+ super format => prepare_model_for(format, options.values.first, options), :status => status
83
85
  end
84
86
  end
85
87
  end
@@ -0,0 +1,7 @@
1
+ require 'roar/json/json_api'
2
+
3
+ Roar::JSON::JSONAPI.class_eval do
4
+ def to_json_api(*args); to_json(*args); end
5
+ def from_json_api(*args); from_json(*args); end
6
+ end
7
+
@@ -0,0 +1,13 @@
1
+ require 'action_controller/responder'
2
+
3
+ module Roar::Rails
4
+ class Responder < ActionController::Responder
5
+ module VersionStrategy
6
+ end
7
+ end
8
+
9
+ module TestCase
10
+ module VersionStrategy
11
+ end
12
+ end
13
+ end
@@ -33,7 +33,7 @@ module Roar
33
33
  def create_response(model)
34
34
  render_method = "to_#{format}"
35
35
  media_format = Mime[format]
36
- Response.new(model.send(render_method, options), media_format)
36
+ Response.new(model.send(render_method, options), media_format).body
37
37
  end
38
38
 
39
39
  def handle_lonely_collection!(collection)
@@ -1,5 +1,5 @@
1
1
  module Roar
2
2
  module Rails
3
- VERSION = "1.0.0"
3
+ VERSION = "1.0.1"
4
4
  end
5
5
  end
@@ -23,6 +23,7 @@ Gem::Specification.new do |s|
23
23
  s.add_runtime_dependency "actionpack"
24
24
  s.add_runtime_dependency "railties", ">= 3.0.0"
25
25
  s.add_runtime_dependency "uber", ">= 0.0.5"
26
+ s.add_runtime_dependency "responders"
26
27
 
27
28
  s.add_development_dependency "minitest"
28
29
  s.add_development_dependency "activemodel"
@@ -38,6 +38,22 @@ class ConsumeHalWithNoHalRespondTest < ActionController::TestCase
38
38
  end
39
39
  end
40
40
 
41
+ class ConsumeJsonApiWithNoJsonApiRespondTest < ActionController::TestCase
42
+ include Roar::Rails::TestCase
43
+
44
+ tests UnnamespaceSingersController
45
+
46
+ # Content-type is set properly, it's a registered mime but responder doesn't do #from_json_api.
47
+ # FIXME: why does that still find a representer?
48
+ test "#consume parses hal document and updates the model" do
49
+ @request.env['CONTENT_TYPE'] = 'application/vnd.api+json'
50
+ # assert_raises Roar::Rails::UnsupportedMediaType do
51
+ assert_raises NoMethodError do # currently, we don't know if a format is supported in general, or not.
52
+ post :consume_json, "{\"name\": \"Bumi\"}"
53
+ end
54
+ end
55
+ end
56
+
41
57
  class ConsumeWithConfigurationTest < ActionController::TestCase
42
58
  include Roar::Rails::TestCase
43
59
 
@@ -109,6 +125,36 @@ class ConsumeHalTest < ActionController::TestCase
109
125
  end
110
126
  end
111
127
 
128
+ class ConsumeJsonApiTest < ActionController::TestCase
129
+ include Roar::Rails::TestCase
130
+
131
+ module MusicianRepresenter
132
+ include Roar::JSON::JSONAPI
133
+ type :singer
134
+ property :name
135
+ end
136
+
137
+
138
+ class SingersController < ActionController::Base
139
+ include Roar::Rails::ControllerAdditions
140
+ represents :json_api, :entity => MusicianRepresenter
141
+
142
+ def consume_json_api
143
+ singer = consume!(Singer.new)
144
+ render :text => singer.inspect
145
+ end
146
+ end
147
+
148
+ tests SingersController
149
+
150
+ test "#consume parses JSON-API document and updates the model" do
151
+ @request.env['CONTENT_TYPE'] = 'application/vnd.api+json'
152
+ post :consume_json_api, "{\"singer\": {\"name\": \"Bumi\"}}"
153
+
154
+ assert_equal %{#<struct Singer name="Bumi">}, @response.body
155
+ end
156
+ end
157
+
112
158
  class ConsumeWithOptionsOverridingConfigurationTest < ActionController::TestCase
113
159
  include Roar::Rails::TestCase
114
160
 
@@ -31,4 +31,10 @@ Dummy::Application.configure do
31
31
  # config.active_record.schema_format = :sql
32
32
 
33
33
  config.representer.default_url_options = {:host => "roar.apotomo.de"}
34
+
35
+ if Roar::Rails.rails_version.~ 4.2
36
+ config.eager_load = false
37
+ config.active_support.test_order = :random
38
+ end
39
+
34
40
  end
@@ -0,0 +1,35 @@
1
+ require 'test_helper'
2
+
3
+ class HalRendererTest < ActionController::TestCase
4
+ include Roar::Rails::TestCase
5
+
6
+ class SingersController < ActionController::Base
7
+ module JsonApiSingerRepresenter
8
+ include Roar::JSON::JSONAPI
9
+
10
+ type :singer
11
+ property :name
12
+ end
13
+
14
+ include Roar::Rails::ControllerAdditions
15
+ represents :json_api, :entity => JsonApiSingerRepresenter
16
+
17
+ def show
18
+ singer = Musician.new("Bumi")
19
+ respond_with singer
20
+ end
21
+ end
22
+
23
+ tests SingersController
24
+
25
+ test "should render correctly in response to a application/vnd.api+json" do
26
+ get :show, :id => "bumi", :format => :json_api
27
+ assert_body '{"singer":{"name":"Bumi"}}'
28
+ end
29
+
30
+ test "should have a content_type of application/vnd.api+json" do
31
+ get :show, :id => "bumi", :format => :json_api
32
+ assert_equal response.content_type, 'application/vnd.api+json'
33
+ end
34
+ end
35
+
@@ -17,7 +17,7 @@ class RenderTest < ActionController::TestCase
17
17
 
18
18
  def show
19
19
  singer = Musician.new("Bumi")
20
- render :json => singer
20
+ render :json => singer, status: 201
21
21
  end
22
22
  end
23
23
 
@@ -26,5 +26,6 @@ class RenderTest < ActionController::TestCase
26
26
  test "should use Representer for #render" do
27
27
  get :show, :id => "bumi", :format => :json
28
28
  assert_body '{"title":"Bumi"}'
29
+ assert_equal 201, response.status
29
30
  end
30
- end
31
+ end
@@ -11,8 +11,17 @@ Singer = Struct.new(:name)
11
11
  # Rails.backtrace_cleaner.remove_silencers!
12
12
 
13
13
  Mime::Type.register 'application/hal+json', :hal
14
+ Mime::Type.register 'application/vnd.api+json', :json_api
14
15
 
15
- ActionController.add_renderer :hal do |js, options|
16
+
17
+
18
+ # see also https://github.com/jingweno/msgpack_rails/issues/3
19
+ ::ActionController::Renderers.add :hal do |js, options|
16
20
  self.content_type ||= Mime::HAL
17
21
  js.is_a?(String) ? js : js.to_json
18
22
  end
23
+
24
+ ::ActionController::Renderers.add :json_api do |js, options|
25
+ self.content_type ||= Mime::JSONAPI
26
+ js.is_a?(String) ? js : js.to_json
27
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: roar-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nick Sutterer
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-12-23 00:00:00.000000000 Z
11
+ date: 2015-01-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: roar
@@ -86,6 +86,20 @@ dependencies:
86
86
  - - ">="
87
87
  - !ruby/object:Gem::Version
88
88
  version: 0.0.5
89
+ - !ruby/object:Gem::Dependency
90
+ name: responders
91
+ requirement: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ type: :runtime
97
+ prerelease: false
98
+ version_requirements: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
89
103
  - !ruby/object:Gem::Dependency
90
104
  name: minitest
91
105
  requirement: !ruby/object:Gem::Requirement
@@ -175,6 +189,7 @@ files:
175
189
  - gemfiles/Gemfile.rails3-2
176
190
  - gemfiles/Gemfile.rails4-0
177
191
  - gemfiles/Gemfile.rails4-1
192
+ - gemfiles/Gemfile.rails4-2
178
193
  - lib/generators/rails/USAGE
179
194
  - lib/generators/rails/representer_generator.rb
180
195
  - lib/generators/rails/templates/representer.rb
@@ -182,11 +197,12 @@ files:
182
197
  - lib/roar/rails/controller_additions.rb
183
198
  - lib/roar/rails/formats.rb
184
199
  - lib/roar/rails/hal.rb
200
+ - lib/roar/rails/json_api.rb
185
201
  - lib/roar/rails/rails3_0_strategy.rb
186
202
  - lib/roar/rails/rails3_1_strategy.rb
187
203
  - lib/roar/rails/rails3_2_strategy.rb
188
204
  - lib/roar/rails/rails4_0_strategy.rb
189
- - lib/roar/rails/rails4_1_strategy.rb
205
+ - lib/roar/rails/rails4_2_strategy.rb
190
206
  - lib/roar/rails/railtie.rb
191
207
  - lib/roar/rails/responder.rb
192
208
  - lib/roar/rails/test_case.rb
@@ -213,6 +229,7 @@ files:
213
229
  - test/dummy/config/routes.rb
214
230
  - test/dummy/db/test.sqlite3
215
231
  - test/formats_test.rb
232
+ - test/json_api_renderer_test.rb
216
233
  - test/json_hal_renderer_test.rb
217
234
  - test/render_test.rb
218
235
  - test/representer_generator_test.rb
@@ -264,6 +281,7 @@ test_files:
264
281
  - test/dummy/config/routes.rb
265
282
  - test/dummy/db/test.sqlite3
266
283
  - test/formats_test.rb
284
+ - test/json_api_renderer_test.rb
267
285
  - test/json_hal_renderer_test.rb
268
286
  - test/render_test.rb
269
287
  - test/representer_generator_test.rb
@@ -1,21 +0,0 @@
1
- module Roar::Rails
2
- class Responder < ActionController::Responder
3
- module VersionStrategy
4
- extend ActiveSupport::Concern
5
- included do
6
- # FIXME: this totally SUCKS, why am i supposed to add a global "renderer" (whatever that is) to handle hal requests. this should be a generic behaviour in Rails core.
7
- # TODO: replace renderer/responder layer in Rails with a simpler implementation.
8
- # ActionController.add_renderer :hal do |js, options|
9
- # self.content_type ||= Mime::HAL
10
- # js.to_json
11
- # end
12
- end
13
- end
14
- end
15
-
16
- module TestCase
17
- module VersionStrategy
18
- end
19
- end
20
- end
21
-