decorate-responder 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 1.8.7
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in decorate-responder.gemspec
4
+ gemspec
5
+
6
+ group :development do
7
+ gem 'minitest'
8
+ gem 'rake'
9
+ end
10
+
11
+ group :test do
12
+ gem 'draper'
13
+ gem 'actionpack'
14
+ end
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,10 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.libs.push "test"
6
+ t.test_files = FileList['test/*_test.rb']
7
+ t.verbose = true
8
+ end
9
+
10
+ task :default => :test
@@ -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,12 @@
1
+ module DecorateResponder
2
+ module VERSION
3
+ MAJOR = 1
4
+ MINOR = 0
5
+ PATCH = 0
6
+ STAGE = nil
7
+
8
+ def self.to_s
9
+ [MAJOR, MINOR, PATCH, STAGE].reject(&:nil?).join '.'
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,8 @@
1
+
2
+ module Responders
3
+ autoload :DecorateResponder, 'responders/decorate_responder'
4
+ end
5
+
6
+ module DecroateResponder
7
+ autoload :VERSION, 'decorate-responder/version'
8
+ 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
@@ -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