decorate-responder 1.0.3 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 7e2ed8a107be5875df2018ef0de8b9f23e5a34e2
4
- data.tar.gz: 05661393b1584cafd1a81e19ac5996a153e94a4b
2
+ SHA256:
3
+ metadata.gz: 341221a91749fda160099631f71595082dbd6e83018c6e73edd369b1d9baaf3e
4
+ data.tar.gz: 56b00d4a5fd527a688bd1a247753f8b24f2dc899d87c22df5fa8b22780c6a544
5
5
  SHA512:
6
- metadata.gz: 712c96ef7be18a34553452c3aaf66deab8ce635392c66e533cd307c4ffc83b159d38a3ab86fa4b10f7c7cc6308099318d6740a4fe09db11884712bfdf7f22e06
7
- data.tar.gz: 1edc44ac77087b155089987e25ba287f179a4f39f53f213f3dad4fafde1d41da8f2fa132ba63b5bbed3c288931e283a51be9ee215db6a47201bf41d0c7abae4e
6
+ metadata.gz: d86488e8d1fd0b95b37e4fc8c39b674e7b6013418403147516c0cdd2f42cbf0cf0dadd6d3959462c885a6de887ee2c8dfa8b43a48f937980743a7dead06b35ef
7
+ data.tar.gz: bde2ee45d29787b502a86ae691a3eece24f326c66d43d70c89b10dba249473250861bc8121952ee921e59eeb92785da01cf9fefca5f2b8767af18590519e3939
data/.travis.yml CHANGED
@@ -1,10 +1,18 @@
1
+ dist: xenial
2
+ cache: bundler
1
3
  language: ruby
2
4
  rvm:
3
- - 1.9.3
4
- env:
5
- - RAILS_VERSION=3.2
6
- - RAILS_VERSION=4.0
7
- - RAILS_VERSION=head
8
- matrix:
9
- allow_failure:
10
- - env: RAILS_VERSION=head
5
+ - 2.6.1
6
+ - 2.5.3
7
+ - 2.4.5
8
+ - 2.3.8
9
+ gemfile:
10
+ - Gemfile
11
+ - gemfiles/Gemfile.rails-5-2
12
+ - gemfiles/Gemfile.rails-5-1
13
+ - gemfiles/Gemfile.rails-5-0
14
+ - gemfiles/Gemfile.rails-4-2
15
+ jobs:
16
+ exclude:
17
+ - rvm: 2.6.1
18
+ gemfile: gemfiles/Gemfile.rails-4-2
data/CHANGELOG.md ADDED
@@ -0,0 +1,16 @@
1
+ # Changelog
2
+ All notable changes to this project will be documented in this file.
3
+
4
+ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
5
+ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
6
+
7
+ ## [Unreleased]
8
+
9
+ ## 2.0.0 - 2019-02-18
10
+ ### Added
11
+ - Add #decoration_context controller method (#5)
12
+
13
+ ### Removed
14
+ - Remove support for Rails < 4.2 (#4)
15
+
16
+ [unreleased]: https://github.com/jgraichen/decorate-responder/compare/v2.0.0...HEAD
data/Gemfile CHANGED
@@ -1,12 +1,9 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
- # Specify your gem's dependencies in decorate-responder.gemspec
4
- gemspec
5
-
6
3
  gem 'draper'
7
4
  # draper requires activemodel
8
5
  gem 'activemodel'
9
6
 
10
- RAILS_VERSION = ENV['RAILS_VERSION'] || '4.0'
11
-
12
- eval_gemfile File.expand_path("../gemfiles/Gemfile.rails-#{RAILS_VERSION.gsub(/\.+/, '-')}", __FILE__)
7
+ # Load the actual gem's dependencies from decorate-responder.gemspec
8
+ gemroot = File.dirname File.absolute_path __FILE__
9
+ gemspec path: gemroot
data/README.md CHANGED
@@ -16,7 +16,7 @@ Or install it yourself as:
16
16
 
17
17
  $ gem install decorate-responder
18
18
 
19
- You will also need a decorator gem like [draper](drapergem/draper) or your own decorator implementation.
19
+ You will also need a decorator gem like [draper](https://github.com/drapergem/draper) or your own decorator implementation.
20
20
 
21
21
  ## Usage
22
22
 
@@ -40,9 +40,22 @@ class MyController < ApplicationController
40
40
  end
41
41
  ```
42
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.
43
+ If you're using a decorator gem such as [draper](https://github.com/drapergem/draper) that injects a `decorate` method into your resources they will get decorated automatically.
44
44
 
45
- You can also explicitly decorate your resources by adding a `decorate` method to your controller:
45
+ When using draper, you may want to pass a [context hash](https://github.com/drapergem/draper#adding-context) from the controller to the decorator.
46
+ You can provide this context by adding a (public!) `decoration_context` method to the controller:
47
+
48
+ ```ruby
49
+ class AppController
50
+ responders Responder::DecorateResponder
51
+
52
+ def decoration_context
53
+ { color: false }
54
+ end
55
+ end
56
+ ```
57
+
58
+ If you want to build even more advanced decoration logic, you can also explicitly decorate your resources by adding a `decorate` method to your controller:
46
59
 
47
60
  ```ruby
48
61
  class AppController
data/Rakefile CHANGED
@@ -1,10 +1,10 @@
1
- require "bundler/gem_tasks"
1
+ require 'bundler/gem_tasks'
2
2
  require 'rake/testtask'
3
3
 
4
4
  Rake::TestTask.new do |t|
5
- t.libs.push "test"
5
+ t.libs.push 'test'
6
6
  t.test_files = FileList['test/*_test.rb']
7
7
  t.verbose = true
8
8
  end
9
9
 
10
- task :default => :test
10
+ task default: :test
@@ -1,22 +1,25 @@
1
1
  # -*- encoding: utf-8 -*-
2
+
2
3
  lib = File.expand_path('../lib', __FILE__)
3
4
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
5
  require 'decorate-responder/version'
5
6
 
6
7
  Gem::Specification.new do |gem|
7
- gem.name = "decorate-responder"
8
+ gem.name = 'decorate-responder'
8
9
  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 = ""
10
+ gem.authors = ['Jan Graichen']
11
+ gem.email = ['jg@altimos.de']
12
+ gem.description = 'A Rails responder to decorate resources.'
13
+ gem.summary = 'A Rails responder to decorate resources.'
14
+ gem.homepage = ''
14
15
  gem.license = 'MIT'
15
16
 
16
17
  gem.files = `git ls-files`.split($/)
17
- gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
18
+ gem.executables = gem.files.grep(%r{^bin/}).map { |f| File.basename(f) }
18
19
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
19
- gem.require_paths = ["lib"]
20
+ gem.require_paths = ['lib']
21
+
22
+ gem.add_runtime_dependency 'responders', '~> 2.0'
20
23
 
21
24
  gem.add_development_dependency 'rake'
22
25
  gem.add_development_dependency 'minitest'
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Load root Gemfile
4
+ self.instance_eval Bundler.read_file 'Gemfile'
5
+
6
+ gem 'actionpack', '~> 4.2.0'
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Load root Gemfile
4
+ self.instance_eval Bundler.read_file 'Gemfile'
5
+
6
+ gem 'actionpack', '~> 5.0.0'
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Load root Gemfile
4
+ self.instance_eval Bundler.read_file 'Gemfile'
5
+
6
+ gem 'actionpack', '~> 5.1.0'
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Load root Gemfile
4
+ self.instance_eval Bundler.read_file 'Gemfile'
5
+
6
+ gem 'actionpack', '~> 5.2.0'
@@ -1,8 +1,8 @@
1
1
  module DecorateResponder
2
2
  module VERSION
3
- MAJOR = 1
3
+ MAJOR = 2
4
4
  MINOR = 0
5
- PATCH = 3
5
+ PATCH = 0
6
6
  STAGE = nil
7
7
 
8
8
  def self.to_s
@@ -1,13 +1,21 @@
1
1
  module Responders
2
2
  module DecorateResponder
3
- def to_format
3
+ def respond
4
4
  @resource = decorate_resource(resource)
5
5
  super
6
6
  end
7
7
 
8
8
  def decorate_resource(res)
9
9
  return controller.decorate(res) if controller.respond_to? :decorate
10
- return res.decorate if res.respond_to? :decorate
10
+
11
+ if res.respond_to? :decorate
12
+ if controller.respond_to? :decoration_context
13
+ return res.decorate context: controller.decoration_context
14
+ else
15
+ return res.decorate
16
+ end
17
+ end
18
+
11
19
  res
12
20
  end
13
21
  end
@@ -3,18 +3,24 @@ require 'test_helper.rb'
3
3
  class DecorateResponderTest < ActionController::TestCase
4
4
  tests AppController
5
5
 
6
- def json; JSON[@response.body] end
6
+ def json
7
+ JSON[@response.body]
8
+ end
7
9
 
8
10
  def test_non_decoration
9
- get :index, :format => :json, :resource => [ "abc" ]
11
+ AppController.resource = ['abc']
12
+
13
+ get :index, format: :json
10
14
 
11
15
  assert_equal 1, json.size
12
- assert_equal "abc", json[0]
16
+ assert_equal 'abc', json[0]
13
17
  end
14
18
 
15
19
  def test_decoration
16
- get :index, :format => :json, :resource => User.new(:name => "John")
20
+ AppController.resource = User.new(name: 'John')
21
+
22
+ get :index, format: :json
17
23
 
18
- assert_equal "UserDecorator", json["class"]
24
+ assert_equal 'UserDecorator', json['class']
19
25
  end
20
26
  end
@@ -0,0 +1,27 @@
1
+ require 'test_helper.rb'
2
+
3
+ class DecorateWithContextResponderTest < ActionController::TestCase
4
+ tests DecorateWithContextController
5
+
6
+ def json
7
+ JSON[@response.body]
8
+ end
9
+
10
+ def test_non_decoration
11
+ DecorateWithContextController.resource = ['abc']
12
+
13
+ get :index, format: :json
14
+
15
+ assert_equal 1, json.size
16
+ assert_equal 'abc', json[0]
17
+ end
18
+
19
+ def test_decoration_with_context
20
+ DecorateWithContextController.resource = User.new(name: 'John')
21
+
22
+ get :index, format: :json
23
+
24
+ assert_equal 'UserDecorator', json['class']
25
+ assert_equal 'bar', json['foo']
26
+ end
27
+ end
@@ -3,11 +3,15 @@ require 'test_helper.rb'
3
3
  class ExplicitDecorateResponderTest < ActionController::TestCase
4
4
  tests ExplicitDecorateController
5
5
 
6
- def json; JSON[@response.body] end
6
+ def json
7
+ JSON[@response.body]
8
+ end
7
9
 
8
10
  def test_decoration
9
- get :index, :format => :json, :resource => User.new(:name => "John")
11
+ ExplicitDecorateController.resource = User.new(name: 'John')
12
+
13
+ get :index, format: :json
10
14
 
11
- assert_equal "MyDecorator", json["class"]
15
+ assert_equal 'MyDecorator', json['class']
12
16
  end
13
17
  end
data/test/test_helper.rb CHANGED
@@ -1,25 +1,25 @@
1
1
  require 'minitest/autorun'
2
2
  require 'bundler'
3
3
 
4
- Bundler.setup
5
-
6
4
  # Configure Rails
7
- ENV["RAILS_ENV"] = "test"
5
+ ENV['RAILS_ENV'] = 'test'
6
+
7
+ Bundler.require
8
8
 
9
- require 'active_support'
10
- require 'action_controller'
11
- require 'draper'
9
+ require 'action_dispatch'
12
10
 
11
+ require 'responders'
13
12
  require 'decorate-responder'
14
13
 
15
14
  Responders::Routes = ActionDispatch::Routing::RouteSet.new
16
15
  Responders::Routes.draw do
17
16
  get '/index' => 'app#index'
18
17
  get '/ex' => 'explicit_decorate#index'
18
+ get '/ctx' => 'decorate_with_context#index'
19
19
  end
20
20
 
21
21
  class ActiveSupport::TestCase
22
- setup do @routes = Responders::Routes end
22
+ setup { @routes = Responders::Routes }
23
23
  end
24
24
 
25
25
  class AppResponder < ActionController::Responder
@@ -32,47 +32,68 @@ class User
32
32
  @name = name
33
33
  end
34
34
 
35
- def decorate
36
- UserDecorator.new self
35
+ def decorate(*args)
36
+ UserDecorator.new self, *args
37
37
  end
38
38
  end
39
39
 
40
40
  class UserDecorator < Draper::Decorator
41
41
  decorates User
42
42
 
43
- def as_json(options)
44
- { :class => "UserDecorator" }
43
+ def as_json(_options)
44
+ { class: 'UserDecorator' }.merge(context)
45
45
  end
46
46
  end
47
47
 
48
48
  class MyDecorator < Draper::Decorator
49
- def as_json(options)
50
- { :class => "MyDecorator" }
49
+ def as_json(_options)
50
+ { class: 'MyDecorator' }
51
51
  end
52
52
  end
53
53
 
54
54
  class AppController < ActionController::Base
55
55
  include Responders::Routes.url_helpers
56
56
 
57
+ cattr_accessor :resource
58
+
57
59
  self.responder = AppResponder
58
60
  respond_to :json
59
61
 
60
62
  def index
61
- respond_with params[:resource]
63
+ respond_with self.class.resource
62
64
  end
63
65
  end
64
66
 
65
67
  class ExplicitDecorateController < ActionController::Base
66
68
  include Responders::Routes.url_helpers
67
69
 
70
+ cattr_accessor :resource
71
+
68
72
  self.responder = AppResponder
69
73
  respond_to :json
70
74
 
71
75
  def index
72
- respond_with params[:resource]
76
+ respond_with self.class.resource
73
77
  end
74
78
 
75
79
  def decorate(resource)
76
80
  MyDecorator.new(resource)
77
81
  end
78
82
  end
83
+
84
+ class DecorateWithContextController < ActionController::Base
85
+ include Responders::Routes.url_helpers
86
+
87
+ cattr_accessor :resource
88
+
89
+ self.responder = AppResponder
90
+ respond_to :json
91
+
92
+ def index
93
+ respond_with self.class.resource
94
+ end
95
+
96
+ def decoration_context
97
+ {foo: 'bar'}
98
+ end
99
+ end
metadata CHANGED
@@ -1,41 +1,55 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: decorate-responder
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.3
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jan Graichen
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-07-24 00:00:00.000000000 Z
11
+ date: 2019-02-19 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: responders
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: rake
15
29
  requirement: !ruby/object:Gem::Requirement
16
30
  requirements:
17
- - - '>='
31
+ - - ">="
18
32
  - !ruby/object:Gem::Version
19
33
  version: '0'
20
34
  type: :development
21
35
  prerelease: false
22
36
  version_requirements: !ruby/object:Gem::Requirement
23
37
  requirements:
24
- - - '>='
38
+ - - ">="
25
39
  - !ruby/object:Gem::Version
26
40
  version: '0'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: minitest
29
43
  requirement: !ruby/object:Gem::Requirement
30
44
  requirements:
31
- - - '>='
45
+ - - ">="
32
46
  - !ruby/object:Gem::Version
33
47
  version: '0'
34
48
  type: :development
35
49
  prerelease: false
36
50
  version_requirements: !ruby/object:Gem::Requirement
37
51
  requirements:
38
- - - '>='
52
+ - - ">="
39
53
  - !ruby/object:Gem::Version
40
54
  version: '0'
41
55
  description: A Rails responder to decorate resources.
@@ -45,20 +59,23 @@ executables: []
45
59
  extensions: []
46
60
  extra_rdoc_files: []
47
61
  files:
48
- - .gitignore
49
- - .travis.yml
62
+ - ".gitignore"
63
+ - ".travis.yml"
64
+ - CHANGELOG.md
50
65
  - Gemfile
51
66
  - LICENSE.txt
52
67
  - README.md
53
68
  - Rakefile
54
69
  - decorate-responder.gemspec
55
- - gemfiles/Gemfile.rails-3-2
56
- - gemfiles/Gemfile.rails-4-0
57
- - gemfiles/Gemfile.rails-head
70
+ - gemfiles/Gemfile.rails-4-2
71
+ - gemfiles/Gemfile.rails-5-0
72
+ - gemfiles/Gemfile.rails-5-1
73
+ - gemfiles/Gemfile.rails-5-2
58
74
  - lib/decorate-responder.rb
59
75
  - lib/decorate-responder/version.rb
60
76
  - lib/responders/decorate_responder.rb
61
77
  - test/decorate_responder_test.rb
78
+ - test/decorate_with_context_responder_test.rb
62
79
  - test/explicit_decorate_responder_test.rb
63
80
  - test/test_helper.rb
64
81
  homepage: ''
@@ -71,21 +88,21 @@ require_paths:
71
88
  - lib
72
89
  required_ruby_version: !ruby/object:Gem::Requirement
73
90
  requirements:
74
- - - '>='
91
+ - - ">="
75
92
  - !ruby/object:Gem::Version
76
93
  version: '0'
77
94
  required_rubygems_version: !ruby/object:Gem::Requirement
78
95
  requirements:
79
- - - '>='
96
+ - - ">="
80
97
  - !ruby/object:Gem::Version
81
98
  version: '0'
82
99
  requirements: []
83
- rubyforge_project:
84
- rubygems_version: 2.0.3
100
+ rubygems_version: 3.0.2
85
101
  signing_key:
86
102
  specification_version: 4
87
103
  summary: A Rails responder to decorate resources.
88
104
  test_files:
89
105
  - test/decorate_responder_test.rb
106
+ - test/decorate_with_context_responder_test.rb
90
107
  - test/explicit_decorate_responder_test.rb
91
108
  - test/test_helper.rb
@@ -1,2 +0,0 @@
1
- gem 'actionpack', '~> 3.2.0'
2
- gem 'minitest', '< 5.0'
@@ -1 +0,0 @@
1
- gem 'actionpack', '~> 4.0.0'
@@ -1 +0,0 @@
1
- gem 'actionpack', github: 'rails/rails'