roar-rails 0.0.6 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGES.markdown CHANGED
@@ -1,3 +1,7 @@
1
+ h2. 0.0.7
2
+
3
+ * Introduce `:represent_with` and `:represent_items_with` for `#respond_with`. In turn, deprecate the old behaviour since it will change in 1.0.
4
+
1
5
  h2. 0.0.6
2
6
 
3
7
  * Make roar-rails rely on roar 0.10 and bigger.
data/README.markdown CHANGED
@@ -26,7 +26,7 @@ class SingersController < ApplicationController
26
26
  end
27
27
  ```
28
28
 
29
- Need to use a representer with a different name than your model? Pass it in using the `:with_representer` option:
29
+ Need to use a representer with a different name than your model? Pass it in using the `:represent_with` option:
30
30
 
31
31
  ```ruby
32
32
  class SingersController < ApplicationController
@@ -35,7 +35,19 @@ class SingersController < ApplicationController
35
35
 
36
36
  def show
37
37
  singer = Musician.find_by_id(params[:id])
38
- respond_with singer, :with_representer => SingerRepresenter
38
+ respond_with singer, :represent_with => SingerRepresenter
39
+ end
40
+ end
41
+ ```
42
+
43
+ If you don't want to write a dedicated representer for a collection of items (highly recommended, thou) but rather use a representer for each item, use the `+represent_items_with+` option.
44
+
45
+ ```ruby
46
+ class SingersController < ApplicationController
47
+
48
+ def index
49
+ singers = Musician.find(:all)
50
+ respond_with singers, :represent_items_with => SingerRepresenter
39
51
  end
40
52
  end
41
53
  ```
@@ -8,18 +8,29 @@ module Roar::Rails
8
8
 
9
9
  private
10
10
  def representer_for_model(model)
11
- (model.class.name + "Representer").constantize
11
+ class_name = model.class.name
12
+ "#{class_name}Representer".constantize
12
13
  end
13
14
  end
14
15
 
15
16
  module Responder
16
17
  include ModelMethods
17
18
 
18
- def display(model, given_options={})
19
- # TODO: remove the [] semantics, this should be done with a Collection representer.
20
- representer = options.delete(:with_representer)
19
+ # DISCUSS: why THE FUCK is options not passed as a method argument but kept as an internal instance variable in the responder? this is something i will never understand about Rails.
20
+ def display(model, *args)
21
+ if representer = options.delete(:represent_with)
22
+ # this is the new behaviour.
23
+ model.extend(representer) # FIXME: move to method.
24
+ return super
25
+ end
26
+
27
+
28
+ representer = options.delete(:with_representer) and ActiveSupport::Deprecation.warn(":with_representer is deprecated and will be removed in roar-rails 1.0. Use :represent_with or :represent_items_with.")
29
+ representer ||= options.delete(:represent_items_with) # new API.
21
30
 
22
31
  if model.respond_to?(:map!)
32
+ ActiveSupport::Deprecation.warn("Calling #respond_with with a collection will misbehave in future versions of roar-rails. Use :represent_items_with to get the old behaviour.")
33
+
23
34
  model.map! do |m|
24
35
  extend_with_representer!(m, representer)
25
36
  m.to_hash
@@ -27,6 +38,7 @@ module Roar::Rails
27
38
  else
28
39
  extend_with_representer!(model, representer)
29
40
  end
41
+
30
42
  super
31
43
  end
32
44
  end
@@ -1,5 +1,5 @@
1
1
  module Roar
2
2
  module Rails
3
- VERSION = "0.0.6"
3
+ VERSION = "0.0.7"
4
4
  end
5
5
  end
@@ -2,6 +2,16 @@ require 'test_helper'
2
2
 
3
3
  Singer = Struct.new(:name)
4
4
 
5
+ module SingersRepresenter
6
+ include Roar::Representer::JSON
7
+
8
+ collection :singers, :extend => SingerRepresenter
9
+ def singers
10
+ each
11
+ end
12
+ end
13
+
14
+
5
15
  class ResponderTest < ActionController::TestCase
6
16
  include Roar::Rails::TestCase
7
17
 
@@ -9,48 +19,103 @@ class ResponderTest < ActionController::TestCase
9
19
  include Roar::Rails::ControllerAdditions
10
20
  respond_to :json
11
21
 
12
- def explicit_representer
22
+ def execute
23
+ instance_exec &@block
24
+ end
25
+ end
26
+
27
+ def get(&block)
28
+ @controller.instance_eval do
29
+ @block = block
30
+ end
31
+
32
+ super :execute, :format => 'json'
33
+ end
34
+
35
+
36
+ tests SingersController
37
+
38
+ test ":with_representer is deprecated" do
39
+ assert_deprecated do
40
+ get do
41
+ singer = Musician.new("Bumi")
42
+ respond_with singer, :with_representer => SingerRepresenter
43
+ end
44
+ end
45
+ end
46
+
47
+
48
+ test "responder allows specifying representer" do # TODO: remove in 1.0.
49
+ get do
13
50
  singer = Musician.new("Bumi")
14
51
  respond_with singer, :with_representer => SingerRepresenter
15
52
  end
53
+
54
+ assert_equal singer.to_json, @response.body
55
+ end
16
56
 
17
- def implicit_representer
57
+ test "responder finds representer by convention" do
58
+ get do
18
59
  singer = Singer.new("Bumi")
19
60
  respond_with singer
20
61
  end
62
+
63
+ assert_equal singer.to_json, @response.body
64
+ end
65
+
66
+
21
67
 
22
- def collection_of_representers
23
- singers = [Singer.new("Bumi"), Singer.new("Bjork"), Singer.new("Sinead")]
24
- respond_with singers
68
+ test "responder works with collections" do # TODO: remove in 1.0.
69
+ assert_deprecated do
70
+ get do
71
+ singers = [Singer.new("Bumi"), Singer.new("Bjork"), Singer.new("Sinead")]
72
+ respond_with singers
73
+ end
25
74
  end
75
+
76
+ assert_equal singers.map(&:to_hash).to_json, @response.body
77
+ end
26
78
 
27
- def collection_of_custom_representers
79
+ test "custom responder works with collections" do # TODO: remove in 1.0.
80
+ get do
28
81
  singers = [Singer.new("Bumi"), Singer.new("Bjork"), Singer.new("Sinead")]
29
82
  respond_with singers, :with_representer => SingerAliasRepresenter
30
83
  end
84
+
85
+ assert_equal singers.map {|s| s.extend(SingerAliasRepresenter).to_hash }.to_json, @response.body
31
86
  end
32
-
33
- tests SingersController
34
-
35
- test "responder allows specifying representer" do
36
- get :explicit_representer, :format => 'json'
37
- assert_equal singer.to_json, @response.body
38
- end
39
-
40
- test "responder finds representer by convention" do
41
- get :implicit_representer, :format => 'json'
42
- assert_equal singer.to_json, @response.body
87
+
88
+
89
+
90
+ test "use passed :represent_with representer for single model" do
91
+ get do
92
+ singer = Musician.new("Bumi")
93
+ respond_with singer, :with_representer => SingerRepresenter
94
+ end
95
+
96
+ assert_equal singer.extend(SingerRepresenter).to_json, @response.body
43
97
  end
44
-
45
- test "responder works with collections" do
46
- get :collection_of_representers, :format => 'json'
47
- assert_equal singers.map(&:to_hash).to_json, @response.body
98
+
99
+ test "use passed :represent_with representer for collection" do
100
+ get do
101
+ singers = [Singer.new("Bumi"), Singer.new("Bjork"), Singer.new("Sinead")]
102
+ respond_with singers, :represent_with => SingersRepresenter
103
+ end
104
+
105
+ assert_equal({:singers => singers.collect {|s| s.extend(SingerRepresenter).to_hash }}.to_json, @response.body)
48
106
  end
49
-
50
- test "custom responder works with collections" do
51
- get :collection_of_custom_representers, :format => 'json'
52
- assert_equal singers.map {|s| s.extend(SingerAliasRepresenter).to_hash }.to_json, @response.body
107
+
108
+ test "use passed :represent_items_with for collection items" do
109
+ get do
110
+ singers = [Singer.new("Bumi"), Singer.new("Bjork"), Singer.new("Sinead")]
111
+ respond_with singers, :represent_items_with => SingerRepresenter
112
+ end
113
+
114
+ assert_equal(singers.collect {|s| s.extend(SingerRepresenter).to_hash }.to_json, @response.body)
53
115
  end
116
+
117
+
118
+
54
119
 
55
120
  def singer(name="Bumi")
56
121
  singer = Musician.new(name)
@@ -60,5 +125,4 @@ class ResponderTest < ActionController::TestCase
60
125
  def singers
61
126
  [singer("Bumi"), singer("Bjork"), singer("Sinead")]
62
127
  end
63
-
64
128
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: roar-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.7
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-06-05 00:00:00.000000000 Z
12
+ date: 2012-06-10 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: roar