decorate-responder 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/.travis.yml +4 -0
- data/Gemfile +14 -0
- data/LICENSE.txt +22 -0
- data/README.md +73 -0
- data/Rakefile +10 -0
- data/decorate-responder.gemspec +19 -0
- data/lib/decorate-responder/version.rb +12 -0
- data/lib/decorate-responder.rb +8 -0
- data/lib/responders/decorate_responder.rb +14 -0
- data/test/decorate_responder_test.rb +20 -0
- data/test/explicit_decorate_responder_test.rb +13 -0
- data/test/test_helper.rb +78 -0
- metadata +67 -0
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Jan Graichen
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
# Decorate::Responder [![Build Status](https://travis-ci.org/jgraichen/decorate-responder.png?branch=master)](https://travis-ci.org/jgraichen/decorate-responder)
|
2
|
+
|
3
|
+
A Rails responder that will apply a decorator to a resource in the responder chain.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'decorate-responder'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install decorate-responder
|
18
|
+
|
19
|
+
You will also need a decorator gem like [draper](drapergem/draper) or your own decorator implementation.
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
Add `Responders::DecorateResponder` to your responder chain:
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
class AppResponder < Responder
|
27
|
+
include Responders::DecorateResponder
|
28
|
+
end
|
29
|
+
|
30
|
+
class MyController < ApplicationController
|
31
|
+
self.responder = AppResponder
|
32
|
+
end
|
33
|
+
```
|
34
|
+
|
35
|
+
Or use it with [plataformatec/responders](https://github.com/plataformatec/responders):
|
36
|
+
|
37
|
+
```ruby
|
38
|
+
class MyController < ApplicationController
|
39
|
+
responders Responders::DecorateResponder
|
40
|
+
end
|
41
|
+
```
|
42
|
+
|
43
|
+
If you're using a decorator gem such as [draper](drapergem/draper) that injects a `decorate` method into your resources they will get decorated automatically.
|
44
|
+
|
45
|
+
You can also explicitly decorate your resources by adding a `decorate` method to your controller:
|
46
|
+
|
47
|
+
```ruby
|
48
|
+
class AppController
|
49
|
+
responders Responders::DecorateResponder
|
50
|
+
|
51
|
+
def decorate(resource)
|
52
|
+
MyDecorator.new resource
|
53
|
+
end
|
54
|
+
end
|
55
|
+
```
|
56
|
+
|
57
|
+
If the resource and controller do not have a `decorate` method the resource will not be decorated.
|
58
|
+
|
59
|
+
## Contributing
|
60
|
+
|
61
|
+
1. Fork it
|
62
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
63
|
+
3. Add tests for your feature.
|
64
|
+
4. Add your feature.
|
65
|
+
5. Commit your changes (`git commit -am 'Add some feature'`)
|
66
|
+
6. Push to the branch (`git push origin my-new-feature`)
|
67
|
+
7. Create new Pull Request
|
68
|
+
|
69
|
+
## License
|
70
|
+
|
71
|
+
[MIT License](http://www.opensource.org/licenses/mit-license.php)
|
72
|
+
|
73
|
+
Copyright (c) 2013, Jan Graichen
|
data/Rakefile
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'decorate-responder/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "decorate-responder"
|
8
|
+
gem.version = DecorateResponder::VERSION
|
9
|
+
gem.authors = ["Jan Graichen"]
|
10
|
+
gem.email = ["jg@altimos.de"]
|
11
|
+
gem.description = %q{A Rails responder to decorate resources.}
|
12
|
+
gem.summary = %q{A Rails responder to decorate resources.}
|
13
|
+
gem.homepage = ""
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module Responders
|
2
|
+
module DecorateResponder
|
3
|
+
def to_format
|
4
|
+
@resource = decorate_resource(resource)
|
5
|
+
super
|
6
|
+
end
|
7
|
+
|
8
|
+
def decorate_resource(res)
|
9
|
+
return controller.decorate(res) if controller.respond_to? :decorate
|
10
|
+
return res.decorate if res.respond_to? :decorate
|
11
|
+
res
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'test_helper.rb'
|
2
|
+
|
3
|
+
class DecorateResponderTest < ActionController::TestCase
|
4
|
+
tests AppController
|
5
|
+
|
6
|
+
def json; JSON[@response.body] end
|
7
|
+
|
8
|
+
def test_non_decoration
|
9
|
+
get :index, :format => :json, :resource => [ "abc" ]
|
10
|
+
|
11
|
+
assert_equal 1, json.size
|
12
|
+
assert_equal "abc", json[0]
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_decoration
|
16
|
+
get :index, :format => :json, :resource => User.new(:name => "John")
|
17
|
+
|
18
|
+
assert_equal "UserDecorator", json["class"]
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'test_helper.rb'
|
2
|
+
|
3
|
+
class ExplicitDecorateResponderTest < ActionController::TestCase
|
4
|
+
tests ExplicitDecorateController
|
5
|
+
|
6
|
+
def json; JSON[@response.body] end
|
7
|
+
|
8
|
+
def test_decoration
|
9
|
+
get :index, :format => :json, :resource => User.new(:name => "John")
|
10
|
+
|
11
|
+
assert_equal "MyDecorator", json["class"]
|
12
|
+
end
|
13
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
require 'minitest/autorun'
|
2
|
+
require 'bundler'
|
3
|
+
|
4
|
+
Bundler.setup
|
5
|
+
|
6
|
+
# Configure Rails
|
7
|
+
ENV["RAILS_ENV"] = "test"
|
8
|
+
|
9
|
+
require 'active_support'
|
10
|
+
require 'action_controller'
|
11
|
+
require 'draper'
|
12
|
+
|
13
|
+
require 'decorate-responder'
|
14
|
+
|
15
|
+
Responders::Routes = ActionDispatch::Routing::RouteSet.new
|
16
|
+
Responders::Routes.draw do
|
17
|
+
match '/index' => 'app#index'
|
18
|
+
match '/ex' => 'explicit_decorate#index'
|
19
|
+
end
|
20
|
+
|
21
|
+
class ActiveSupport::TestCase
|
22
|
+
setup do @routes = Responders::Routes end
|
23
|
+
end
|
24
|
+
|
25
|
+
class AppResponder < ActionController::Responder
|
26
|
+
include Responders::DecorateResponder
|
27
|
+
end
|
28
|
+
|
29
|
+
class User
|
30
|
+
attr_reader :name
|
31
|
+
def initialize(name)
|
32
|
+
@name = name
|
33
|
+
end
|
34
|
+
|
35
|
+
def decorate
|
36
|
+
UserDecorator.new self
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
class UserDecorator < Draper::Decorator
|
41
|
+
decorates User
|
42
|
+
|
43
|
+
def as_json(options)
|
44
|
+
{ :class => "UserDecorator" }
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
class MyDecorator < Draper::Decorator
|
49
|
+
def as_json(options)
|
50
|
+
{ :class => "MyDecorator" }
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
class AppController < ActionController::Base
|
55
|
+
include Responders::Routes.url_helpers
|
56
|
+
|
57
|
+
self.responder = AppResponder
|
58
|
+
respond_to :json
|
59
|
+
|
60
|
+
def index
|
61
|
+
respond_with params[:resource]
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
class ExplicitDecorateController < ActionController::Base
|
66
|
+
include Responders::Routes.url_helpers
|
67
|
+
|
68
|
+
self.responder = AppResponder
|
69
|
+
respond_to :json
|
70
|
+
|
71
|
+
def index
|
72
|
+
respond_with params[:resource]
|
73
|
+
end
|
74
|
+
|
75
|
+
def decorate(resource)
|
76
|
+
MyDecorator.new(resource)
|
77
|
+
end
|
78
|
+
end
|
metadata
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: decorate-responder
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Jan Graichen
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-02-09 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: A Rails responder to decorate resources.
|
15
|
+
email:
|
16
|
+
- jg@altimos.de
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- ".gitignore"
|
22
|
+
- ".travis.yml"
|
23
|
+
- Gemfile
|
24
|
+
- LICENSE.txt
|
25
|
+
- README.md
|
26
|
+
- Rakefile
|
27
|
+
- decorate-responder.gemspec
|
28
|
+
- lib/decorate-responder.rb
|
29
|
+
- lib/decorate-responder/version.rb
|
30
|
+
- lib/responders/decorate_responder.rb
|
31
|
+
- test/decorate_responder_test.rb
|
32
|
+
- test/explicit_decorate_responder_test.rb
|
33
|
+
- test/test_helper.rb
|
34
|
+
homepage: ''
|
35
|
+
licenses: []
|
36
|
+
post_install_message:
|
37
|
+
rdoc_options: []
|
38
|
+
require_paths:
|
39
|
+
- lib
|
40
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ">="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
segments:
|
47
|
+
- 0
|
48
|
+
hash: -288045878216154046
|
49
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
segments:
|
56
|
+
- 0
|
57
|
+
hash: -288045878216154046
|
58
|
+
requirements: []
|
59
|
+
rubyforge_project:
|
60
|
+
rubygems_version: 1.8.25
|
61
|
+
signing_key:
|
62
|
+
specification_version: 3
|
63
|
+
summary: A Rails responder to decorate resources.
|
64
|
+
test_files:
|
65
|
+
- test/decorate_responder_test.rb
|
66
|
+
- test/explicit_decorate_responder_test.rb
|
67
|
+
- test/test_helper.rb
|