roar-rails 0.1.3 → 0.1.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.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/CHANGES.markdown +5 -0
- data/README.markdown +32 -0
- data/lib/generators/rails/USAGE +42 -0
- data/lib/generators/rails/representer_generator.rb +70 -0
- data/lib/generators/rails/templates/representer.rb +6 -0
- data/lib/roar/rails/controller_additions.rb +1 -3
- data/lib/roar/rails/version.rb +1 -1
- data/test/consume_test.rb +5 -7
- data/test/representer_generator_test.rb +57 -0
- metadata +6 -4
- data/test/dummy/tmp/app/cells/blog/post/latest.html.erb +0 -7
- data/test/dummy/tmp/app/cells/blog/post_cell.rb +0 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e2c494afb7574bfff6fcd23e35e7a2748fe9a9e1
|
4
|
+
data.tar.gz: 1bac4790fcb074bda7d834c879707f909ca10a7f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3f10afc52cf7f202fde0e71fdabeda3eff795608ee5c494c955cbbb5f0b22aa2b1e08797c2e008fd4c91a7d07511c549e3ce44cd12b1ca958dc2cd7b42a79e58
|
7
|
+
data.tar.gz: f7ab0351b2e51a8d5d198f48e2e7c9ac77b3c0ce33803121b450890b073fa145b5487ec89d30a6fc6438ae5f90ee459ae7b9a7554357a969c23cd14547b5acb4
|
data/.gitignore
CHANGED
data/CHANGES.markdown
CHANGED
@@ -1,3 +1,8 @@
|
|
1
|
+
h2. 0.1.4
|
2
|
+
|
3
|
+
* Added a generator to create modules from the command-line. Thanks to Guilherme Cavalcanti <guiocavalcanti> for working on this.
|
4
|
+
* We always use `request.body.read` now, however, we call `rewind` beforehand.
|
5
|
+
|
1
6
|
h2. 0.1.3
|
2
7
|
|
3
8
|
* Fix a bug where `TestCase` wouldn't work in 3.2.
|
data/README.markdown
CHANGED
@@ -11,13 +11,44 @@ Roar is a framework for parsing and rendering REST documents. For a better overv
|
|
11
11
|
* URL helpers in representers
|
12
12
|
* Better tests
|
13
13
|
* Autoloading
|
14
|
+
* Generators
|
14
15
|
|
15
16
|
This gem works with all Rails >= 3.x.
|
16
17
|
|
18
|
+
|
19
|
+
## Generators
|
20
|
+
|
21
|
+
The generator will create the representer modules in `app/representers` for you.
|
22
|
+
|
23
|
+
Here's an example.
|
24
|
+
|
25
|
+
```shell
|
26
|
+
rails g representer Band id name
|
27
|
+
```
|
28
|
+
|
29
|
+
This will create the file `app/representers/band_representer.rb` with the following content,
|
30
|
+
|
31
|
+
```ruby
|
32
|
+
module BandRepresenter
|
33
|
+
include Roar::Representer::JSON
|
34
|
+
|
35
|
+
property :id
|
36
|
+
property :name
|
37
|
+
end
|
38
|
+
```
|
39
|
+
|
40
|
+
You can change the format (e.g. XML), and pass arbitrary options to customize the generated representer. For all available options, just run
|
41
|
+
|
42
|
+
```shell
|
43
|
+
rails g representer
|
44
|
+
```
|
45
|
+
|
46
|
+
|
17
47
|
## Rendering with #respond_with
|
18
48
|
|
19
49
|
roar-rails provides a number of baked-in rendering methods.
|
20
50
|
|
51
|
+
|
21
52
|
### Conventional Rendering
|
22
53
|
|
23
54
|
Easily render resources using representers with the built-in responder.
|
@@ -129,6 +160,7 @@ end
|
|
129
160
|
|
130
161
|
In decorators' link blocks you currently have to use `represented` to get the actual represented model (this is `self` in module representers).
|
131
162
|
|
163
|
+
|
132
164
|
## Passing Options
|
133
165
|
|
134
166
|
Both rendering and consuming support passing user options to the representer.
|
@@ -0,0 +1,42 @@
|
|
1
|
+
Description:
|
2
|
+
Creates a new Representer and its properties.
|
3
|
+
|
4
|
+
This generates a representer module in app/representers directory.
|
5
|
+
|
6
|
+
Examples:
|
7
|
+
|
8
|
+
rails g representer Band id name
|
9
|
+
|
10
|
+
This will create a representer as follows:
|
11
|
+
|
12
|
+
module BandRepresenter
|
13
|
+
include Roar::Representer::JSON
|
14
|
+
|
15
|
+
property :id
|
16
|
+
property :name
|
17
|
+
end
|
18
|
+
|
19
|
+
You can pass :class and :extend property options:
|
20
|
+
|
21
|
+
rails g singer id instrument:equipament:instrument_representer
|
22
|
+
|
23
|
+
This will add property options:
|
24
|
+
|
25
|
+
module SingerRepresenter
|
26
|
+
include Roar::Representer::JSON
|
27
|
+
|
28
|
+
property :id
|
29
|
+
property :instrument, :class => Equipament, :extend => InstrumentRepresenter
|
30
|
+
end
|
31
|
+
|
32
|
+
You can also choose representer format with --format option
|
33
|
+
|
34
|
+
rails g representer Band id name --format=xml
|
35
|
+
|
36
|
+
module BandRepresenter
|
37
|
+
include Roar::Representer::XML
|
38
|
+
|
39
|
+
property :id
|
40
|
+
property :name
|
41
|
+
end
|
42
|
+
|
@@ -0,0 +1,70 @@
|
|
1
|
+
module Rails
|
2
|
+
module Generators
|
3
|
+
class RepresenterGenerator < Rails::Generators::NamedBase
|
4
|
+
source_root File.expand_path("../templates", __FILE__)
|
5
|
+
|
6
|
+
argument :properties, :type => :array, :default => [],
|
7
|
+
:banner => "property[:class[:extend]] property[:class[:extend]]"
|
8
|
+
|
9
|
+
class_option :format, :default => :json, :banner => "--format=JSON",
|
10
|
+
:desc => "Use different formats JSON, JSON::HAL or XML"
|
11
|
+
|
12
|
+
def generate_representer_file
|
13
|
+
template('representer.rb', file_path)
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
def format
|
19
|
+
options[:format].upcase
|
20
|
+
end
|
21
|
+
|
22
|
+
def property_options
|
23
|
+
PropertyBuilder.new(properties)
|
24
|
+
end
|
25
|
+
|
26
|
+
def file_path
|
27
|
+
base_path = 'app/representers'
|
28
|
+
File.join(base_path, class_path, "#{file_name}_representer.rb")
|
29
|
+
end
|
30
|
+
|
31
|
+
class PropertyBuilder
|
32
|
+
include Enumerable
|
33
|
+
|
34
|
+
def initialize(properties)
|
35
|
+
@raw = properties
|
36
|
+
end
|
37
|
+
|
38
|
+
def each(&block)
|
39
|
+
properties_with_options.each(&block)
|
40
|
+
end
|
41
|
+
|
42
|
+
private
|
43
|
+
|
44
|
+
def properties_with_options
|
45
|
+
properties.map do |(name, klass, representer)|
|
46
|
+
p = [name_option(name)]
|
47
|
+
p << hash_option(:class, klass)
|
48
|
+
p << hash_option(:extend, representer)
|
49
|
+
|
50
|
+
p.compact.join(', ')
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
def name_option(name)
|
55
|
+
return unless name
|
56
|
+
"property :#{name}"
|
57
|
+
end
|
58
|
+
|
59
|
+
def hash_option(key, value)
|
60
|
+
return unless key && value
|
61
|
+
":#{key} => #{value.classify}"
|
62
|
+
end
|
63
|
+
|
64
|
+
def properties
|
65
|
+
@raw.map { |p| p.split(':') }
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
data/lib/roar/rails/version.rb
CHANGED
data/test/consume_test.rb
CHANGED
@@ -72,17 +72,15 @@ class ConsumeWithOptionsOverridingConfigurationTest < ActionController::TestCase
|
|
72
72
|
end
|
73
73
|
|
74
74
|
class RequestBodyStringTest < ConsumeTest
|
75
|
-
test "
|
75
|
+
test "#read rewinds before reading" do
|
76
76
|
@request.instance_eval do
|
77
77
|
def body
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
self
|
83
|
-
end
|
78
|
+
incoming = super
|
79
|
+
incoming.read
|
80
|
+
incoming
|
84
81
|
end
|
85
82
|
end
|
83
|
+
|
86
84
|
post :consume_json, "{\"name\": \"Bumi\"}", :format => 'json'
|
87
85
|
assert_equal %{#<struct Singer name="Bumi">}, @response.body
|
88
86
|
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'rails/generators'
|
3
|
+
|
4
|
+
require 'generators/rails/representer_generator'
|
5
|
+
|
6
|
+
class RepresentetGeneratorTest < Rails::Generators::TestCase
|
7
|
+
destination File.join(Rails.root, "tmp")
|
8
|
+
setup :prepare_destination
|
9
|
+
tests Rails::Generators::RepresenterGenerator
|
10
|
+
|
11
|
+
test "create a representer with correct class_name" do
|
12
|
+
run_generator %w(singer)
|
13
|
+
|
14
|
+
assert_file representer_path('singer'), /module SingerRepresenter/
|
15
|
+
end
|
16
|
+
|
17
|
+
test "create a representer with correct properties" do
|
18
|
+
run_generator %w(singer name id)
|
19
|
+
|
20
|
+
assert_file representer_path('singer'), /property :name/
|
21
|
+
assert_file representer_path('singer'), /property :id/
|
22
|
+
end
|
23
|
+
|
24
|
+
test "create a representer with default json support" do
|
25
|
+
run_generator %w(singer)
|
26
|
+
|
27
|
+
assert_file representer_path('singer'), /include Roar::Representer::JSON/
|
28
|
+
end
|
29
|
+
|
30
|
+
test "create a representer with different format support" do
|
31
|
+
run_generator %w(singer --format=XML)
|
32
|
+
|
33
|
+
assert_file representer_path('singer'), /include Roar::Representer::XML/
|
34
|
+
end
|
35
|
+
|
36
|
+
test "create a representer with property, class and exnted" do
|
37
|
+
run_generator %w(singer band:band:group_representer instrument:equipament:instrument_representer)
|
38
|
+
|
39
|
+
assert_file representer_path('singer'),
|
40
|
+
/property :band, :class => Band, :extend => GroupRepresenter/
|
41
|
+
assert_file representer_path('singer'),
|
42
|
+
/property :instrument, :class => Equipament, :extend => InstrumentRepresenter/
|
43
|
+
end
|
44
|
+
|
45
|
+
test "create a representer with property and class only" do
|
46
|
+
run_generator %w(singer band:band instrument:equipament)
|
47
|
+
|
48
|
+
assert_file representer_path('singer'),
|
49
|
+
/property :band, :class => Band/
|
50
|
+
assert_file representer_path('singer'),
|
51
|
+
/property :instrument, :class => Equipament/
|
52
|
+
end
|
53
|
+
|
54
|
+
def representer_path(name)
|
55
|
+
"app/representers/#{name}_representer.rb"
|
56
|
+
end
|
57
|
+
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: 0.1.
|
4
|
+
version: 0.1.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nick Sutterer
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-08-
|
11
|
+
date: 2013-08-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: roar
|
@@ -167,6 +167,9 @@ files:
|
|
167
167
|
- gemfiles/Gemfile.rails3-0
|
168
168
|
- gemfiles/Gemfile.rails3-2
|
169
169
|
- gemfiles/Gemfile.rails4-0
|
170
|
+
- lib/generators/rails/USAGE
|
171
|
+
- lib/generators/rails/representer_generator.rb
|
172
|
+
- lib/generators/rails/templates/representer.rb
|
170
173
|
- lib/roar-rails.rb
|
171
174
|
- lib/roar/rails/controller_additions.rb
|
172
175
|
- lib/roar/rails/hal.rb
|
@@ -218,10 +221,9 @@ files:
|
|
218
221
|
- test/dummy/public/javascripts/rails.js
|
219
222
|
- test/dummy/public/stylesheets/.gitkeep
|
220
223
|
- test/dummy/script/rails
|
221
|
-
- test/dummy/tmp/app/cells/blog/post/latest.html.erb
|
222
|
-
- test/dummy/tmp/app/cells/blog/post_cell.rb
|
223
224
|
- test/json_hal_renderer_test.rb
|
224
225
|
- test/representer_computer_test.rb
|
226
|
+
- test/representer_generator_test.rb
|
225
227
|
- test/representer_test.rb
|
226
228
|
- test/responder_test.rb
|
227
229
|
- test/test_case_test.rb
|