roar-rails 0.0.3 → 0.0.4

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.
data/CHANGES.markdown CHANGED
@@ -1,3 +1,7 @@
1
+ h2. 0.0.4
2
+
3
+ * Added `#consume!` to infer representer name and parse the incoming document for a model.
4
+
1
5
  h2. 0.0.3
2
6
 
3
7
  * Works with Rails 3.0 now, too. Fixed the `mounted_helpers` import.
data/README.markdown CHANGED
@@ -5,6 +5,7 @@ _Makes using Roar's representers in your Rails app fun._
5
5
  ## Features
6
6
 
7
7
  * Rendering with responders
8
+ * Parsing incoming documents
8
9
  * URL helpers in representers
9
10
  * Better tests
10
11
  * Autoloading
@@ -15,17 +16,13 @@ Easily render resources using representers with the built-in responder.
15
16
 
16
17
  ```ruby
17
18
  class SingersController < ApplicationController
19
+ include Roar::Rails::ControllerAdditions
18
20
  respond_to :json
19
21
 
20
22
  def show
21
23
  singer = Singer.find_by_id(params[:id])
22
24
  respond_with singer
23
25
  end
24
-
25
- def self.responder
26
- Class.new(super).send :include, Roar::Rails::Responder
27
- end
28
-
29
26
  end
30
27
  ```
31
28
 
@@ -33,17 +30,13 @@ Need to use a representer with a different name than your model? Pass it in usin
33
30
 
34
31
  ```ruby
35
32
  class SingersController < ApplicationController
33
+ include Roar::Rails::ControllerAdditions
36
34
  respond_to :json
37
35
 
38
36
  def show
39
37
  singer = Musician.find_by_id(params[:id])
40
38
  respond_with singer, :with_representer => SingerRepresenter
41
39
  end
42
-
43
- def self.responder
44
- Class.new(super).send :include, Roar::Rails::Responder
45
- end
46
-
47
40
  end
48
41
  ```
49
42
 
@@ -63,6 +56,34 @@ class SingersController < ApplicationController
63
56
  end
64
57
  ```
65
58
 
59
+
60
+ ## Parsing incoming documents
61
+
62
+ In `#create` and `#update` actions it is often necessary to parse the incoming representation and map it to a model instance. Use the `#consume!` method for this.
63
+
64
+ ```ruby
65
+ class SingersController < ApplicationController
66
+ respond_to :json
67
+
68
+ def create
69
+ singer = Singer.new
70
+ consume!(singer)
71
+
72
+ respond_with singer
73
+ end
74
+ end
75
+ ```
76
+
77
+ The `consume!` call will roughly do the following.
78
+
79
+ ```ruby
80
+ singer.
81
+ extend(SingerRepresenter)
82
+ from_json(request.body)
83
+ ```
84
+
85
+ So, `#consume!` helps you figuring out the representer module and reading the incoming document.
86
+
66
87
  ## URL Helpers
67
88
 
68
89
  Any URL helpers from the Rails app are automatically available in representers.
data/lib/roar-rails.rb CHANGED
@@ -1,6 +1,5 @@
1
1
  require "roar/rails/version"
2
2
  require "roar/representer"
3
- require "roar/rails/responder"
4
3
  require "roar/rails/railtie"
5
4
 
6
5
  module Roar::Representer
@@ -15,6 +14,9 @@ end
15
14
 
16
15
  module Roar
17
16
  module Rails
18
- # Your code goes here...
17
+ autoload("TestCase", "roar/rails/test_case")
18
+ autoload("ControllerAdditions", "roar/rails/controller_additions")
19
+ autoload("Responder", "roar/rails/responder")
20
+ autoload("ModelMethods", "roar/rails/responder")
19
21
  end
20
22
  end
@@ -0,0 +1,25 @@
1
+ module Roar::Rails
2
+ module ControllerAdditions
3
+ extend ActiveSupport::Concern
4
+ include ModelMethods
5
+
6
+ module ClassMethods
7
+ def responder
8
+ Class.new(super).send :include, Roar::Rails::Responder
9
+ end
10
+ end
11
+
12
+
13
+ def consume!(model)
14
+ format = formats.first # FIXME: i expected request.content_mime_type to do the job. copied from responder.rb. this will return the wrong format when the controller responds to :json and :xml and the Content-type is :xml (?)
15
+ extend_with_representer!(model)
16
+ model.send(compute_parsing_method(format), request.body.string) # e.g. from_json("...")
17
+ model
18
+ end
19
+
20
+ private
21
+ def compute_parsing_method(format)
22
+ "from_#{format}"
23
+ end
24
+ end
25
+ end
@@ -1,23 +1,31 @@
1
1
  module Roar::Rails
2
- module Responder
3
- def extend_with_representer!(resource, representer=nil)
4
- representer ||= representer_for_resource(resource)
5
- resource.extend(representer)
2
+ module ModelMethods
3
+ # DISCUSS: move this into a generic namespace as we could need that in Sinatra as well.
4
+ def extend_with_representer!(model, representer=nil)
5
+ representer ||= representer_for_model(model)
6
+ model.extend(representer)
7
+ end
8
+
9
+ private
10
+ def representer_for_model(model)
11
+ (model.class.name + "Representer").constantize
6
12
  end
7
- def display(resource, given_options={})
8
- if resource.respond_to?(:map!)
9
- resource.map! do |r|
13
+ end
14
+
15
+ module Responder
16
+ include ModelMethods
17
+
18
+ def display(model, given_options={})
19
+ # TODO: remove the [] semantics, this should be done with a Collection representer.
20
+ if model.respond_to?(:map!)
21
+ model.map! do |r|
10
22
  extend_with_representer!(r)
11
23
  r.to_hash
12
24
  end
13
25
  else
14
- extend_with_representer!(resource, options.delete(:with_representer))
26
+ extend_with_representer!(model, options.delete(:with_representer))
15
27
  end
16
28
  super
17
29
  end
18
- private
19
- def representer_for_resource(resource)
20
- (resource.class.name + "Representer").constantize
21
- end
22
30
  end
23
31
  end
@@ -1,5 +1,5 @@
1
1
  module Roar
2
2
  module Rails
3
- VERSION = "0.0.3"
3
+ VERSION = "0.0.4"
4
4
  end
5
5
  end
@@ -0,0 +1,27 @@
1
+ require 'test_helper'
2
+
3
+ class ConsumeTest < ActionController::TestCase
4
+ include Roar::Rails::TestCase
5
+
6
+ class SingersController < ActionController::Base
7
+ include Roar::Rails::ControllerAdditions
8
+ respond_to :json, :xml
9
+
10
+ def consume_json
11
+ singer = consume!(Singer.new)
12
+ render :text => singer.to_json
13
+ end
14
+ end
15
+
16
+ tests SingersController
17
+
18
+ test "#consume parses incoming document and updates the model" do
19
+ post :consume_json, "{\"name\": \"Bumi\"}", :format => 'json'
20
+ assert_equal singer.to_json, @response.body
21
+ end
22
+
23
+ def singer(name="Bumi")
24
+ singer = Musician.new(name)
25
+ singer.extend SingerRepresenter
26
+ end
27
+ end
@@ -1,32 +1,29 @@
1
1
  require 'test_helper'
2
2
 
3
3
  Singer = Struct.new(:name)
4
- class SingersController < ActionController::Base
5
- respond_to :json
6
-
7
- def explicit_representer
8
- singer = Musician.new("Bumi")
9
- respond_with singer, :with_representer => SingerRepresenter
10
- end
11
-
12
- def implicit_representer
13
- singer = Singer.new("Bumi")
14
- respond_with singer
15
- end
16
-
17
- def collection_of_representers
18
- singers = [Singer.new("Bumi"), Singer.new("Bjork"), Singer.new("Sinead")]
19
- respond_with singers
20
- end
21
-
22
- def self.responder
23
- Class.new(super).send :include, Roar::Rails::Responder
24
- end
25
-
26
- end
27
4
 
28
5
  class ResponderTest < ActionController::TestCase
29
6
  include Roar::Rails::TestCase
7
+
8
+ class SingersController < ActionController::Base
9
+ include Roar::Rails::ControllerAdditions
10
+ respond_to :json
11
+
12
+ def explicit_representer
13
+ singer = Musician.new("Bumi")
14
+ respond_with singer, :with_representer => SingerRepresenter
15
+ end
16
+
17
+ def implicit_representer
18
+ singer = Singer.new("Bumi")
19
+ respond_with singer
20
+ end
21
+
22
+ def collection_of_representers
23
+ singers = [Singer.new("Bumi"), Singer.new("Bjork"), Singer.new("Sinead")]
24
+ respond_with singers
25
+ end
26
+ end
30
27
 
31
28
  tests SingersController
32
29
 
metadata CHANGED
@@ -1,116 +1,89 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: roar-rails
3
- version: !ruby/object:Gem::Version
4
- prerelease: false
5
- segments:
6
- - 0
7
- - 0
8
- - 3
9
- version: 0.0.3
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.4
5
+ prerelease:
10
6
  platform: ruby
11
- authors:
7
+ authors:
12
8
  - Nick Sutterer
13
9
  autorequire:
14
10
  bindir: bin
15
11
  cert_chain: []
16
-
17
- date: 2012-02-17 00:00:00 +01:00
18
- default_executable:
19
- dependencies:
20
- - !ruby/object:Gem::Dependency
12
+ date: 2012-03-06 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
21
15
  name: roar
22
- prerelease: false
23
- requirement: &id001 !ruby/object:Gem::Requirement
16
+ requirement: &75385160 !ruby/object:Gem::Requirement
24
17
  none: false
25
- requirements:
18
+ requirements:
26
19
  - - ~>
27
- - !ruby/object:Gem::Version
28
- segments:
29
- - 0
30
- - 9
31
- - 2
20
+ - !ruby/object:Gem::Version
32
21
  version: 0.9.2
33
22
  type: :runtime
34
- version_requirements: *id001
35
- - !ruby/object:Gem::Dependency
36
- name: test_xml
37
23
  prerelease: false
38
- requirement: &id002 !ruby/object:Gem::Requirement
24
+ version_requirements: *75385160
25
+ - !ruby/object:Gem::Dependency
26
+ name: test_xml
27
+ requirement: &73843430 !ruby/object:Gem::Requirement
39
28
  none: false
40
- requirements:
41
- - - ">="
42
- - !ruby/object:Gem::Version
43
- segments:
44
- - 0
45
- version: "0"
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
46
33
  type: :runtime
47
- version_requirements: *id002
48
- - !ruby/object:Gem::Dependency
49
- name: actionpack
50
34
  prerelease: false
51
- requirement: &id003 !ruby/object:Gem::Requirement
35
+ version_requirements: *73843430
36
+ - !ruby/object:Gem::Dependency
37
+ name: actionpack
38
+ requirement: &73842350 !ruby/object:Gem::Requirement
52
39
  none: false
53
- requirements:
40
+ requirements:
54
41
  - - ~>
55
- - !ruby/object:Gem::Version
56
- segments:
57
- - 3
58
- - 0
59
- version: "3.0"
42
+ - !ruby/object:Gem::Version
43
+ version: '3.0'
60
44
  type: :runtime
61
- version_requirements: *id003
62
- - !ruby/object:Gem::Dependency
63
- name: railties
64
45
  prerelease: false
65
- requirement: &id004 !ruby/object:Gem::Requirement
46
+ version_requirements: *73842350
47
+ - !ruby/object:Gem::Dependency
48
+ name: railties
49
+ requirement: &73841770 !ruby/object:Gem::Requirement
66
50
  none: false
67
- requirements:
51
+ requirements:
68
52
  - - ~>
69
- - !ruby/object:Gem::Version
70
- segments:
71
- - 3
72
- - 0
73
- version: "3.0"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
74
55
  type: :runtime
75
- version_requirements: *id004
76
- - !ruby/object:Gem::Dependency
77
- name: minitest
78
56
  prerelease: false
79
- requirement: &id005 !ruby/object:Gem::Requirement
57
+ version_requirements: *73841770
58
+ - !ruby/object:Gem::Dependency
59
+ name: minitest
60
+ requirement: &73840790 !ruby/object:Gem::Requirement
80
61
  none: false
81
- requirements:
82
- - - ">="
83
- - !ruby/object:Gem::Version
84
- segments:
85
- - 2
86
- - 8
87
- - 1
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
88
65
  version: 2.8.1
89
66
  type: :development
90
- version_requirements: *id005
91
- - !ruby/object:Gem::Dependency
92
- name: tzinfo
93
67
  prerelease: false
94
- requirement: &id006 !ruby/object:Gem::Requirement
68
+ version_requirements: *73840790
69
+ - !ruby/object:Gem::Dependency
70
+ name: tzinfo
71
+ requirement: &73839600 !ruby/object:Gem::Requirement
95
72
  none: false
96
- requirements:
97
- - - ">="
98
- - !ruby/object:Gem::Version
99
- segments:
100
- - 0
101
- version: "0"
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
102
77
  type: :development
103
- version_requirements: *id006
78
+ prerelease: false
79
+ version_requirements: *73839600
104
80
  description: Rails extensions for using Roar in the popular web framework.
105
- email:
81
+ email:
106
82
  - apotonick@gmail.com
107
83
  executables: []
108
-
109
84
  extensions: []
110
-
111
85
  extra_rdoc_files: []
112
-
113
- files:
86
+ files:
114
87
  - .gitignore
115
88
  - .travis.yml
116
89
  - CHANGES.markdown
@@ -119,6 +92,7 @@ files:
119
92
  - Rakefile
120
93
  - gemfiles/Gemfile.rails3-0
121
94
  - lib/roar-rails.rb
95
+ - lib/roar/rails/controller_additions.rb
122
96
  - lib/roar/rails/railtie.rb
123
97
  - lib/roar/rails/responder.rb
124
98
  - lib/roar/rails/test_case.rb
@@ -126,6 +100,7 @@ files:
126
100
  - lib/roar/rails/validations_representer.rb
127
101
  - lib/roar/rails/version.rb
128
102
  - roar-rails.gemspec
103
+ - test/consume_test.rb
129
104
  - test/dummy/Rakefile
130
105
  - test/dummy/app/controllers/application_controller.rb
131
106
  - test/dummy/app/controllers/singers_controller.rb
@@ -165,37 +140,28 @@ files:
165
140
  - test/test_case_test.rb
166
141
  - test/test_helper.rb
167
142
  - test/validations_test.rb
168
- has_rdoc: true
169
- homepage: ""
143
+ homepage: ''
170
144
  licenses: []
171
-
172
145
  post_install_message:
173
146
  rdoc_options: []
174
-
175
- require_paths:
147
+ require_paths:
176
148
  - lib
177
- required_ruby_version: !ruby/object:Gem::Requirement
149
+ required_ruby_version: !ruby/object:Gem::Requirement
178
150
  none: false
179
- requirements:
180
- - - ">="
181
- - !ruby/object:Gem::Version
182
- segments:
183
- - 0
184
- version: "0"
185
- required_rubygems_version: !ruby/object:Gem::Requirement
151
+ requirements:
152
+ - - ! '>='
153
+ - !ruby/object:Gem::Version
154
+ version: '0'
155
+ required_rubygems_version: !ruby/object:Gem::Requirement
186
156
  none: false
187
- requirements:
188
- - - ">="
189
- - !ruby/object:Gem::Version
190
- segments:
191
- - 0
192
- version: "0"
157
+ requirements:
158
+ - - ! '>='
159
+ - !ruby/object:Gem::Version
160
+ version: '0'
193
161
  requirements: []
194
-
195
162
  rubyforge_project: roar-rails
196
- rubygems_version: 1.3.7
163
+ rubygems_version: 1.8.10
197
164
  signing_key:
198
165
  specification_version: 3
199
166
  summary: Use Roar in Rails.
200
167
  test_files: []
201
-