active_decorator 1.1.1 → 1.3.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (36) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +2 -0
  3. data/.travis.yml +58 -67
  4. data/CHANGELOG.md +28 -0
  5. data/README.md +4 -4
  6. data/active_decorator.gemspec +3 -1
  7. data/gemfiles/Gemfile-rails.4.2.x +2 -0
  8. data/gemfiles/Gemfile-rails.5.0.x +2 -0
  9. data/gemfiles/Gemfile-rails.5.1.x +1 -0
  10. data/gemfiles/Gemfile-rails.5.2.x +3 -1
  11. data/gemfiles/Gemfile-rails.6.0.x +7 -0
  12. data/lib/active_decorator.rb +5 -1
  13. data/lib/active_decorator/config.rb +7 -1
  14. data/lib/active_decorator/decorator.rb +7 -6
  15. data/lib/active_decorator/helpers.rb +1 -0
  16. data/lib/active_decorator/monkey/action_controller/base/rescue_from.rb +2 -2
  17. data/lib/active_decorator/monkey/active_record/associations.rb +28 -24
  18. data/lib/active_decorator/railtie.rb +8 -6
  19. data/lib/active_decorator/version.rb +1 -1
  20. data/lib/active_decorator/view_context.rb +3 -11
  21. data/lib/generators/rails/templates/decorator.rb +2 -0
  22. data/lib/generators/rspec/templates/decorator_spec.rb +2 -0
  23. data/lib/generators/test_unit/templates/decorator_test.rb +2 -0
  24. data/test/decorator_test.rb +25 -1
  25. data/test/fake_app/app/decorators/comic_decorator.rb +5 -0
  26. data/test/fake_app/app/views/authors/show.html.erb +1 -1
  27. data/test/fake_app/fake_app.rb +42 -19
  28. data/test/features/action_controller_api_test.rb +7 -2
  29. data/test/features/association_test.rb +2 -4
  30. data/test/models/association_test.rb +75 -0
  31. data/test/test_helper.rb +6 -1
  32. metadata +26 -13
  33. data/gemfiles/Gemfile-rails.3.2.x +0 -8
  34. data/gemfiles/Gemfile-rails.4.0.x +0 -6
  35. data/gemfiles/Gemfile-rails.4.1.x +0 -6
  36. data/test/fake_app/app/views/api/bookstores/show.json.jbuilder +0 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 95d46d933899535231f0e24953747370ac289afed99ac881dd91a1bafca06d00
4
- data.tar.gz: '08989f206e82987244927a07c17f147f59f25c268bb61b59763dbed7cf11aa06'
3
+ metadata.gz: '029394fb4c79f67fd71559154ad159840fd13e9635754f0748bf0e489cfdb411'
4
+ data.tar.gz: a668184bfb8b7e77e2b205af385b8d319f161f0a5eb6b98e395b18e91e2e89db
5
5
  SHA512:
6
- metadata.gz: e89c4cc6d373b308858919ebf119c18bc3547e09d0f0dda9d1c52643a844eb50696aac9f5e0372f7c274acbe5d3f34db5b66ad02db62cd3a0808f7c3b566436b
7
- data.tar.gz: d62771d0c66da65522e8a3b05ba03a5bce188da0c6bad782bac5d8b7d53827ca5471fbd756e7533b7b4701b4539f0a038098c0be37dac514b688c32821b1cd93
6
+ metadata.gz: 6207c58d396a6d465776ec79b74e20e59f3b04368c045cecaf5d075c13ba64717d3fd636e7ed0eb0106fc816068c3630abeb63561d431454e54913ddc55612ac
7
+ data.tar.gz: 3881e7bc7470797115cdbc7286afb3eff68b6c77ea36b16f1cb11bdc86232c8861cbf3c7df7cf45efa0f2293285a4a73fd8c01f1823b98c87815f9a676ae531c
data/.gitignore CHANGED
@@ -4,3 +4,5 @@ Gemfile.lock
4
4
  gemfiles/*.lock
5
5
  pkg/*
6
6
  log
7
+ tmp
8
+ .byebug_history
@@ -1,94 +1,85 @@
1
1
  language: ruby
2
2
 
3
- sudo: false
4
-
5
- script: bundle exec rake test
6
-
7
3
  addons:
8
4
  chrome: stable
9
5
 
10
6
  before_install:
11
7
  - sudo apt update -qq
12
8
  - sudo apt install -y chromium-chromedriver
13
- - sudo ln -s /usr/lib/chromium-browser/chromedriver /usr/bin/
14
- - gem update bundler --no-document
9
+ - gem i bundler -v '<2'
10
+
11
+ cache: bundler
15
12
 
16
- rvm:
17
- - 2.5.3
18
- - 2.4.5
19
- - 2.3.7
20
- - 2.2.10
21
- - 2.1.10
22
- - 2.0.0
23
- gemfile:
24
- - gemfiles/Gemfile-rails.5.2.x
25
- - gemfiles/Gemfile-rails.5.1.x
26
- - gemfiles/Gemfile-rails.5.0.x
27
- - gemfiles/Gemfile-rails.4.2.x
28
- - gemfiles/Gemfile-rails.4.1.x
29
- - gemfiles/Gemfile-rails.4.0.x
30
- - gemfiles/Gemfile-rails.3.2.x
13
+ # rvm:
14
+ # - 2.6.6
15
+ # - 2.5.8
16
+ # - 2.4.6
17
+ # - 2.3.8
18
+ # - 2.2.10
19
+ # - 2.1.10
20
+ # - 2.0.0
21
+ # gemfile:
22
+ # - gemfiles/Gemfile-rails.5.2.x
23
+ # - gemfiles/Gemfile-rails.5.1.x
24
+ # - gemfiles/Gemfile-rails.5.0.x
25
+ # - gemfiles/Gemfile-rails.4.2.x
31
26
 
32
27
  matrix:
33
- exclude:
34
- - rvm: 2.1.10
28
+ include:
29
+ - rvm: 2.7.1
30
+ gemfile: gemfiles/Gemfile-rails.6.0.x
31
+ - rvm: 2.7.1
32
+ gemfile: gemfiles/Gemfile-rails.6.0.x
33
+ env: API=1
34
+
35
+ - rvm: 2.6.6
36
+ gemfile: gemfiles/Gemfile-rails.6.0.x
37
+ - rvm: 2.6.6
38
+ gemfile: gemfiles/Gemfile-rails.6.0.x
39
+ env: API=1
40
+ - rvm: 2.6.6
35
41
  gemfile: gemfiles/Gemfile-rails.5.2.x
36
- - rvm: 2.0.0
42
+ - rvm: 2.6.6
37
43
  gemfile: gemfiles/Gemfile-rails.5.2.x
38
- - rvm: 2.4.5
39
- gemfile: gemfiles/Gemfile-rails.5.1.x
40
- - rvm: 2.3.7
41
- gemfile: gemfiles/Gemfile-rails.5.1.x
42
- - rvm: 2.1.10
44
+ env: API=1
45
+ - rvm: 2.6.6
43
46
  gemfile: gemfiles/Gemfile-rails.5.1.x
44
- - rvm: 2.0.0
47
+ - rvm: 2.6.6
45
48
  gemfile: gemfiles/Gemfile-rails.5.1.x
46
- - rvm: 2.4.5
47
- gemfile: gemfiles/Gemfile-rails.5.0.x
48
- - rvm: 2.3.7
49
+ env: API=1
50
+ - rvm: 2.6.6
49
51
  gemfile: gemfiles/Gemfile-rails.5.0.x
50
- - rvm: 2.1.10
52
+ - rvm: 2.6.6
51
53
  gemfile: gemfiles/Gemfile-rails.5.0.x
52
- - rvm: 2.0.0
53
- gemfile: gemfiles/Gemfile-rails.5.0.x
54
- - rvm: 2.5.3
55
- gemfile: gemfiles/Gemfile-rails.4.2.x
56
- - rvm: 2.4.5
54
+ env: API=1
55
+
56
+ - rvm: 2.5.8
57
+ gemfile: gemfiles/Gemfile-rails.5.2.x
58
+
59
+ - rvm: 2.4.6
60
+ gemfile: gemfiles/Gemfile-rails.5.2.x
61
+
62
+ - rvm: 2.3.8
63
+ gemfile: gemfiles/Gemfile-rails.5.2.x
64
+ - rvm: 2.3.8
57
65
  gemfile: gemfiles/Gemfile-rails.4.2.x
58
- - rvm: 2.5.3
59
- gemfile: gemfiles/Gemfile-rails.4.1.x
60
- - rvm: 2.4.5
61
- gemfile: gemfiles/Gemfile-rails.4.1.x
62
- - rvm: 2.2.10
63
- gemfile: gemfiles/Gemfile-rails.4.1.x
64
- - rvm: 2.1.10
65
- gemfile: gemfiles/Gemfile-rails.4.1.x
66
- - rvm: 2.5.3
67
- gemfile: gemfiles/Gemfile-rails.4.0.x
68
- - rvm: 2.4.5
69
- gemfile: gemfiles/Gemfile-rails.4.0.x
66
+
70
67
  - rvm: 2.2.10
71
- gemfile: gemfiles/Gemfile-rails.4.0.x
68
+ gemfile: gemfiles/Gemfile-rails.5.2.x
69
+
72
70
  - rvm: 2.1.10
73
- gemfile: gemfiles/Gemfile-rails.4.0.x
74
- - rvm: 2.5.3
75
- gemfile: gemfiles/Gemfile-rails.3.2.x
76
- - rvm: 2.4.5
77
- gemfile: gemfiles/Gemfile-rails.3.2.x
78
- include:
71
+ gemfile: gemfiles/Gemfile-rails.4.2.x
72
+
73
+ - rvm: 2.0.0
74
+ gemfile: gemfiles/Gemfile-rails.4.2.x
75
+
79
76
  - rvm: ruby-head
80
77
  gemfile: gemfiles/Gemfile-rails.edge
81
- - rvm: 2.5.3
82
- gemfile: gemfiles/Gemfile-rails.5.2.x
83
- env: API=1
84
- - rvm: 2.5.3
85
- gemfile: gemfiles/Gemfile-rails.5.1.x
86
- env: API=1
87
- - rvm: 2.5.3
88
- gemfile: gemfiles/Gemfile-rails.5.0.x
89
- env: API=1
78
+
90
79
  - rvm: ruby-head
91
80
  gemfile: gemfiles/Gemfile-rails.edge
92
81
  env: API=1
82
+
93
83
  allow_failures:
94
84
  - rvm: ruby-head
85
+ - gemfile: gemfiles/Gemfile-rails.edge
@@ -1,3 +1,31 @@
1
+ ## 1.3.3
2
+
3
+ * Fixed Ruby 2.7 keyword arguments warning [@pocke]
4
+
5
+ ## 1.3.2
6
+
7
+ * Fixed NameError on ActionController::API controllers without jbuilder enhancement [@kamillle]
8
+
9
+ ## 1.3.1
10
+
11
+ * Switched back from Ruby's `const_get` to Active Support `constantize` for fetching decorator modules, due to inability to properly detect namespaced decorator [@sinsoku]
12
+
13
+ ## 1.3.0
14
+
15
+ * Switched from Active Support `constantize` to Ruby's `const_get` when fetching decorator modules
16
+
17
+ * Switched `config` from ActiveSupport::Configurable to a simple Struct
18
+
19
+ * Association decoration now propagates from AssociationRelation to spawned Relations (e.g. `@post.comments.order(:id).each`)
20
+
21
+ * Dropped support for Rails 3.2, 4.0, and 4.1
22
+
23
+
24
+ ## 1.2.0
25
+
26
+ * Decorate values in Hash recursively [@FumiyaShibusawa]
27
+
28
+
1
29
  ## 1.1.1
2
30
 
3
31
  * Improved ActionController::API support for Rails 5.0.x [@frodsan]
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # ActiveDecorator [![Build Status](https://travis-ci.org/amatsuda/active_decorator.svg?branch=master)](https://travis-ci.org/amatsuda/active_decorator) [![Code Climate](https://codeclimate.com/github/amatsuda/active_decorator/badges/gpa.svg)](https://codeclimate.com/github/amatsuda/active_decorator)
2
2
 
3
- A simple and Rubyish view helper for Rails 3, Rails 4, Rails 5, and Rails 6. Keep your helpers and views Object-Oriented!
3
+ A simple and Rubyish view helper for Rails 4, Rails 5, and Rails 6. Keep your helpers and views Object-Oriented!
4
4
 
5
5
 
6
6
  ## Features ##
@@ -15,9 +15,9 @@ A simple and Rubyish view helper for Rails 3, Rails 4, Rails 5, and Rails 6. Kee
15
15
 
16
16
  ## Supported versions ##
17
17
 
18
- * Ruby 2.0.0, 2.1.x, 2.2.x, 2.3.x, 2.4.x, and 2.5, and 2.6 (trunk)
18
+ * Ruby 2.0.0, 2.1.x, 2.2.x, 2.3.x, 2.4.x, 2.5.x, 2.6.x, and 2.7 (trunk)
19
19
 
20
- * Rails 3.2.x, 4.0.x, 4.1.x, 4.2.x, 5.0, 5.1, 5.2, and 6.0 (edge)
20
+ * Rails 4.2.x, 5.0, 5.1, 5.2, 6.0, and 6.1 (edge)
21
21
 
22
22
 
23
23
  ## Supported ORMs ##
@@ -105,7 +105,7 @@ end
105
105
  ```erb
106
106
  <p><%= @author.name %></p>
107
107
  <ul>
108
- <% @author.books.each do |book| %>
108
+ <% @author.books.order(:id).each do |book| %>
109
109
  <%# `book` here is auto-decorated because @author is a decorated instance %>
110
110
  <li><%= book.link %></li>
111
111
  <% end %>
@@ -18,11 +18,13 @@ Gem::Specification.new do |s|
18
18
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
19
  s.require_paths = ["lib"]
20
20
 
21
+ s.add_dependency 'activesupport'
22
+
21
23
  s.add_development_dependency 'test-unit-rails'
22
24
  s.add_development_dependency 'selenium-webdriver'
23
25
  s.add_development_dependency 'puma'
24
26
  s.add_development_dependency 'capybara'
25
27
  s.add_development_dependency 'sqlite3'
26
28
  s.add_development_dependency 'rake'
27
- s.add_development_dependency 'jbuilder'
29
+ s.add_development_dependency 'byebug'
28
30
  end
@@ -3,4 +3,6 @@ source 'https://rubygems.org'
3
3
  gemspec path: '..'
4
4
 
5
5
  gem 'rails', '~> 4.2.0'
6
+ gem 'sqlite3', '< 1.4'
6
7
  gem 'nokogiri', '~> 1.6.0'
8
+ gem 'jbuilder' unless ENV['API']
@@ -3,3 +3,5 @@ source 'https://rubygems.org'
3
3
  gemspec path: '..'
4
4
 
5
5
  gem 'rails', '~> 5.0.0'
6
+ gem 'sqlite3', '< 1.4'
7
+ gem 'jbuilder' unless ENV['API']
@@ -3,3 +3,4 @@ source 'https://rubygems.org'
3
3
  gemspec path: '..'
4
4
 
5
5
  gem 'rails', '~> 5.1.0'
6
+ gem 'jbuilder' unless ENV['API']
@@ -2,4 +2,6 @@ source 'https://rubygems.org'
2
2
 
3
3
  gemspec path: '..'
4
4
 
5
- gem 'rails', '~> 5.2.0'
5
+ gem 'rails', git: 'https://github.com/rails/rails', branch: '5-2-stable'
6
+ gem 'webdrivers', '< 4'
7
+ gem 'jbuilder' unless ENV['API']
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec path: '..'
4
+
5
+ gem 'rails', '~> 6.0.0'
6
+ gem 'webdrivers'
7
+ gem 'jbuilder' unless ENV['API']
@@ -2,5 +2,9 @@
2
2
 
3
3
  require 'active_decorator/version'
4
4
  require 'active_decorator/decorator'
5
- require 'active_decorator/railtie'
5
+ begin
6
+ require 'rails'
7
+ require 'active_decorator/railtie'
8
+ rescue LoadError
9
+ end
6
10
  require 'active_decorator/config'
@@ -1,7 +1,13 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActiveDecorator
4
- include ActiveSupport::Configurable
4
+ def self.config
5
+ @_config ||= Struct.new(:decorator_suffix).new
6
+ end
7
+
8
+ def self.configure
9
+ yield config
10
+ end
5
11
 
6
12
  config.decorator_suffix = 'Decorator'
7
13
  end
@@ -15,6 +15,7 @@ module ActiveDecorator
15
15
  # Decorates the given object.
16
16
  # Plus, performs special decoration for the classes below:
17
17
  # Array: decorates its each element
18
+ # Hash: decorates its each value
18
19
  # AR::Relation: decorates its each record lazily
19
20
  # AR model: decorates its associations on the fly
20
21
  #
@@ -29,6 +30,10 @@ module ActiveDecorator
29
30
  obj.each do |r|
30
31
  decorate r
31
32
  end
33
+ elsif obj.is_a?(Hash)
34
+ obj.each_value do |v|
35
+ decorate v
36
+ end
32
37
  elsif defined?(ActiveRecord) && obj.is_a?(ActiveRecord::Relation)
33
38
  # don't call each nor to_a immediately
34
39
  if obj.respond_to?(:records)
@@ -85,18 +90,14 @@ module ActiveDecorator
85
90
  # For AR 3 and 4
86
91
  module RelationDecoratorLegacy
87
92
  def to_a
88
- super.tap do |arr|
89
- ActiveDecorator::Decorator.instance.decorate arr
90
- end
93
+ ActiveDecorator::Decorator.instance.decorate super
91
94
  end
92
95
  end
93
96
 
94
97
  # For AR 5+
95
98
  module RelationDecorator
96
99
  def records
97
- super.tap do |arr|
98
- ActiveDecorator::Decorator.instance.decorate arr
99
- end
100
+ ActiveDecorator::Decorator.instance.decorate super
100
101
  end
101
102
  end
102
103
  end
@@ -25,5 +25,6 @@ module ActiveDecorator
25
25
  raise e1
26
26
  end
27
27
  end
28
+ ruby2_keywords :method_missing if respond_to?(:ruby2_keywords, true)
28
29
  end
29
30
  end
@@ -7,10 +7,10 @@ module ActiveDecorator
7
7
  module ActionController
8
8
  module Base
9
9
  def rescue_with_handler(*)
10
- ActiveDecorator::ViewContext.push(view_context)
10
+ ActiveDecorator::ViewContext.push(view_context) if defined?(view_context)
11
11
  super
12
12
  ensure
13
- ActiveDecorator::ViewContext.pop
13
+ ActiveDecorator::ViewContext.pop if defined?(view_context)
14
14
  end
15
15
  end
16
16
  end
@@ -11,15 +11,7 @@ module ActiveDecorator
11
11
  end
12
12
  end
13
13
 
14
- # @see https://github.com/rails/rails/commit/03855e790de2224519f55382e3c32118be31eeff
15
- if Rails.version.to_f < 4.1
16
- module CollectionAssociation
17
- private
18
- def first_or_last(*)
19
- ActiveDecorator::Decorator.instance.decorate_association(owner, super)
20
- end
21
- end
22
- elsif Rails.version.to_f < 5.1
14
+ if Rails.version.to_f < 5.1
23
15
  module CollectionAssociation
24
16
  private
25
17
  def first_nth_or_last(*)
@@ -28,29 +20,41 @@ module ActiveDecorator
28
20
  end
29
21
  end
30
22
 
31
- if Rails.version.to_f >= 4.0
32
- module CollectionProxy
33
- def take(limit = nil)
23
+ module CollectionProxy
24
+ def take(*)
25
+ ActiveDecorator::Decorator.instance.decorate_association(@association.owner, super)
26
+ end
27
+
28
+ if Rails.version.to_f >= 5.1
29
+ def last(*)
34
30
  ActiveDecorator::Decorator.instance.decorate_association(@association.owner, super)
35
31
  end
36
32
 
37
- if Rails.version.to_f >= 5.1
38
- def last(limit = nil)
39
- ActiveDecorator::Decorator.instance.decorate_association(@association.owner, super)
40
- end
41
-
42
- private
33
+ private
43
34
 
44
- def find_nth_with_limit(index, limit)
45
- ActiveDecorator::Decorator.instance.decorate_association(@association.owner, super)
46
- end
35
+ def find_nth_with_limit(*)
36
+ ActiveDecorator::Decorator.instance.decorate_association(@association.owner, super)
37
+ end
47
38
 
48
- def find_nth_from_last(index)
49
- ActiveDecorator::Decorator.instance.decorate_association(@association.owner, super)
50
- end
39
+ def find_nth_from_last(*)
40
+ ActiveDecorator::Decorator.instance.decorate_association(@association.owner, super)
51
41
  end
52
42
  end
53
43
  end
44
+
45
+ module CollectionAssociation
46
+ private
47
+
48
+ def build_record(*)
49
+ ActiveDecorator::Decorator.instance.decorate_association(@owner, super)
50
+ end
51
+ end
52
+ end
53
+
54
+ module AssociationRelation
55
+ def spawn(*)
56
+ ActiveDecorator::Decorator.instance.decorate_association(@association.owner, super)
57
+ end
54
58
  end
55
59
  end
56
60
  end
@@ -1,8 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'active_decorator/view_context'
4
- require 'rails'
5
-
6
3
  module ActiveDecorator
7
4
  class Railtie < ::Rails::Railtie
8
5
  initializer 'active_decorator' do
@@ -18,6 +15,7 @@ module ActiveDecorator
18
15
  require 'active_decorator/monkey/action_controller/base/rescue_from'
19
16
  ActionController::Base.send :prepend, ActiveDecorator::Monkey::ActionController::Base
20
17
 
18
+ require 'active_decorator/view_context'
21
19
  ActionController::Base.send :include, ActiveDecorator::ViewContext::Filter
22
20
  end
23
21
 
@@ -38,6 +36,7 @@ module ActiveDecorator
38
36
  ActionMailer::Base.send :prepend, ActiveDecorator::Monkey::AbstractController::Rendering
39
37
 
40
38
  if ActionMailer::Base.respond_to? :before_action
39
+ require 'active_decorator/view_context'
41
40
  ActionMailer::Base.send :include, ActiveDecorator::ViewContext::Filter
42
41
  end
43
42
  end
@@ -45,12 +44,15 @@ module ActiveDecorator
45
44
  ActiveSupport.on_load :active_record do
46
45
  require 'active_decorator/monkey/active_record/associations'
47
46
  ActiveRecord::Associations::Association.send :prepend, ActiveDecorator::Monkey::ActiveRecord::Associations::Association
47
+
48
48
  if Rails.version.to_f < 5.1
49
49
  ActiveRecord::Associations::CollectionAssociation.send :prepend, ActiveDecorator::Monkey::ActiveRecord::Associations::CollectionAssociation
50
50
  end
51
- if Rails.version.to_f >= 4.0
52
- ActiveRecord::Associations::CollectionProxy.send :prepend, ActiveDecorator::Monkey::ActiveRecord::Associations::CollectionProxy
53
- end
51
+
52
+ ActiveRecord::AssociationRelation.send :prepend, ActiveDecorator::Monkey::ActiveRecord::AssociationRelation
53
+
54
+ ActiveRecord::Associations::CollectionProxy.send :prepend, ActiveDecorator::Monkey::ActiveRecord::Associations::CollectionProxy
55
+ ActiveRecord::Associations::CollectionAssociation.send :prepend, ActiveDecorator::Monkey::ActiveRecord::Associations::CollectionAssociation
54
56
  end
55
57
  end
56
58
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActiveDecorator
4
- VERSION = '1.1.1'
4
+ VERSION = '1.3.3'
5
5
  end
@@ -32,17 +32,9 @@ module ActiveDecorator
32
32
  extend ActiveSupport::Concern
33
33
 
34
34
  included do
35
- if Rails::VERSION::MAJOR >= 4
36
- around_action do |controller, blk|
37
- ActiveDecorator::ViewContext.run_with(controller.view_context) do
38
- blk.call
39
- end
40
- end
41
- else
42
- around_filter do |controller, blk|
43
- ActiveDecorator::ViewContext.run_with(controller.view_context) do
44
- blk.call
45
- end
35
+ around_action do |controller, blk|
36
+ ActiveDecorator::ViewContext.run_with(controller.view_context) do
37
+ blk.call
46
38
  end
47
39
  end
48
40
  end
@@ -1,2 +1,4 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module <%= class_name %>Decorator
2
4
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require '<%= File.exists?('spec/rails_helper.rb') ? 'rails_helper' : 'spec_helper' %>'
2
4
 
3
5
  RSpec.describe <%= class_name %>Decorator do
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'test_helper'
2
4
 
3
5
  class <%= class_name %>DecoratorTest < ActiveSupport::TestCase
@@ -3,7 +3,6 @@
3
3
  require 'test_helper'
4
4
 
5
5
  class DecoratorTest < Test::Unit::TestCase
6
-
7
6
  test 'it returns the object on decoration' do
8
7
  book = Book.new title: 'Boek'
9
8
  assert_equal book, ActiveDecorator::Decorator.instance.decorate(book)
@@ -31,4 +30,29 @@ class DecoratorTest < Test::Unit::TestCase
31
30
  books = Book.all
32
31
  assert_equal books, ActiveDecorator::Decorator.instance.decorate(ActiveDecorator::Decorator.instance.decorate(books))
33
32
  end
33
+
34
+ test 'decorating Array decorates its each element' do
35
+ array = [Book.new(title: 'Boek')]
36
+ assert_equal array, ActiveDecorator::Decorator.instance.decorate(array)
37
+
38
+ array.each do |value|
39
+ assert value.is_a?(BookDecorator)
40
+ end
41
+ end
42
+
43
+ test 'decorating Hash decorates its each value' do
44
+ hash = {some_record: Book.new(title: 'Boek')}
45
+ assert_equal hash, ActiveDecorator::Decorator.instance.decorate(hash)
46
+
47
+ hash.values.each do |value|
48
+ assert value.is_a?(BookDecorator)
49
+ end
50
+ end
51
+
52
+ test "Don't use the wrong decorator for nested classes" do
53
+ comic = Foo::Comic.new
54
+
55
+ assert_equal comic, ActiveDecorator::Decorator.instance.decorate(comic)
56
+ assert !comic.is_a?(ComicDecorator)
57
+ end
34
58
  end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ # decorator to test auto-loading behavior
4
+ # this module is intended not to be loaded.
5
+ module ComicDecorator; end
@@ -2,7 +2,7 @@
2
2
  <%= @author.capitalized_name %>
3
3
  <%= @author.books.first.upcased_title %>
4
4
  <%= @author.books.last.upcased_title %>
5
- <% if Rails.version.to_f >= 4.0 && p = @author.publishers.take %><%= p.upcased_name %><% end %>
5
+ <% if p = @author.publishers.take %><%= p.upcased_name %><% end %>
6
6
  <% if Rails.version.to_f >= 5.1 && p = @author.publishers.second_to_last %><%= p.reversed_name %><% end %>
7
7
  <% if p = @author.profile %><%= p.address %><% end %>
8
8
  <% if h = @author.profile_history %><%= h.update_date %><% end %>
@@ -1,11 +1,13 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ ENV['RAILS_ENV'] ||= 'test'
4
+
3
5
  require 'active_record'
4
6
  require 'action_controller/railtie'
5
7
  require 'action_mailer/railtie'
6
8
  require 'action_view/railtie'
7
9
  require 'ostruct'
8
- require 'jbuilder'
10
+ require 'jbuilder' unless ENV['API']
9
11
 
10
12
  # config
11
13
  ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
@@ -46,7 +48,11 @@ ActiveDecoratorTestApp::Application.routes.draw do
46
48
 
47
49
  if Rails::VERSION::MAJOR >= 5
48
50
  namespace :api do
49
- resources :bookstores, only: :show
51
+ resources :bookstores, only: :show do
52
+ collection do
53
+ get :error
54
+ end
55
+ end
50
56
  end
51
57
  end
52
58
  end
@@ -162,6 +168,15 @@ module CompanyDecorator
162
168
  name.reverse
163
169
  end
164
170
  end
171
+ module BookstoreDecorator
172
+ def initial
173
+ name.first
174
+ end
175
+
176
+ def to_json
177
+ {name: name, initial: initial}
178
+ end
179
+ end
165
180
 
166
181
  # decorator fake
167
182
  class MovieDecorator; end
@@ -173,24 +188,13 @@ unless ENV['API']
173
188
  end
174
189
  class AuthorsController < ApplicationController
175
190
  def index
176
- if Author.respond_to?(:scoped)
177
- # ActiveRecord 3.x
178
- if params[:variable_type] == 'array'
179
- @authors = Author.all
180
- elsif params[:variable_type] == 'proxy'
181
- @authors = RelationProxy.new(Author.scoped)
182
- else
183
- @authors = Author.scoped
184
- end
191
+ # ActiveRecord 4.x
192
+ if params[:variable_type] == 'array'
193
+ @authors = Author.all.to_a
194
+ elsif params[:variable_type] == 'proxy'
195
+ @authors = RelationProxy.new(Author.all)
185
196
  else
186
- # ActiveRecord 4.x
187
- if params[:variable_type] == 'array'
188
- @authors = Author.all.to_a
189
- elsif params[:variable_type] == 'proxy'
190
- @authors = RelationProxy.new(Author.all)
191
- else
192
- @authors = Author.all
193
- end
197
+ @authors = Author.all
194
198
  end
195
199
  end
196
200
 
@@ -243,8 +247,23 @@ unless ENV['API']
243
247
  else
244
248
  module Api
245
249
  class BookstoresController < ActionController::API
250
+ class CustomError < StandardError; end
251
+
252
+ rescue_from CustomError do
253
+ head :internal_server_error
254
+ end
255
+
246
256
  def show
247
257
  @bookstore = Bookstore.find params[:id]
258
+ render json: serialize('bookstore')
259
+ end
260
+
261
+ private def serialize(name)
262
+ view_assigns[name].to_json
263
+ end
264
+
265
+ def error
266
+ raise CustomError, 'boom!'
248
267
  end
249
268
  end
250
269
  end
@@ -286,3 +305,7 @@ class RelationProxy < BasicObject
286
305
  @ar_relation.public_send(method, *args, &block)
287
306
  end
288
307
  end
308
+
309
+ module Foo
310
+ class Comic; end
311
+ end
@@ -1,5 +1,4 @@
1
1
  # frozen_string_literal: true
2
-
3
2
  require 'test_helper'
4
3
 
5
4
  if Rails::VERSION::MAJOR >= 5
@@ -12,7 +11,13 @@ class ActionControllerAPITest < ActionDispatch::IntegrationTest
12
11
  test 'decorating objects in api only controllers' do
13
12
  get "/api/bookstores/#{Bookstore.last.id}.json"
14
13
 
15
- assert_equal({"name" => "junkudo"}, response.parsed_body)
14
+ assert_equal({"initial" => "j", "name" => "junkudo"}, response.parsed_body)
15
+ end
16
+
17
+ test 'error handling in api only controllers' do
18
+ get "/api/bookstores/error.json"
19
+
20
+ assert_response :error
16
21
  end
17
22
  end
18
23
 
@@ -2,7 +2,7 @@
2
2
 
3
3
  require 'test_helper'
4
4
 
5
- class AssociationTest < ActionDispatch::IntegrationTest
5
+ class AssociationIntegrationTest < ActionDispatch::IntegrationTest
6
6
  setup do
7
7
  company = Company.create! name: 'NaCl'
8
8
  @matz = company.authors.create! name: 'matz'
@@ -23,9 +23,7 @@ class AssociationTest < ActionDispatch::IntegrationTest
23
23
  visit "/authors/#{@matz.id}"
24
24
  assert page.has_content? 'the world of code'.upcase
25
25
  assert page.has_content? 'the ruby programming language'.upcase
26
- if Rails.version.to_f >= 4.0
27
- assert page.has_content? 'nikkei linux'.upcase
28
- end
26
+ assert page.has_content? 'nikkei linux'.upcase
29
27
  if Rails.version.to_f >= 5.1
30
28
  assert page.has_content? 'nikkei linux'.reverse
31
29
  end
@@ -0,0 +1,75 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'test_helper'
4
+
5
+ class AssociationTest < Test::Unit::TestCase
6
+ setup do
7
+ a = Author.create! name: 'pragdave'
8
+ ActiveDecorator::Decorator.instance.decorate a
9
+
10
+ @books = a.books
11
+
12
+ b = @books.create! title: 'pragprog'
13
+ @id = b.id
14
+ end
15
+
16
+ test 'build' do
17
+ b = @books.build title: 'pickaxe'
18
+ assert b.is_a? ActiveDecorator::Decorated
19
+ end
20
+
21
+ test 'create!' do
22
+ b = @books.create! title: 'pickaxe'
23
+ assert b.is_a? ActiveDecorator::Decorated
24
+ end
25
+
26
+ test 'each' do
27
+ @books.each do |b|
28
+ assert b.is_a? ActiveDecorator::Decorated
29
+ end
30
+ end
31
+
32
+ test 'first' do
33
+ assert @books.first.is_a? ActiveDecorator::Decorated
34
+ end
35
+
36
+ test 'last' do
37
+ assert @books.last.is_a? ActiveDecorator::Decorated
38
+ end
39
+
40
+ test 'find' do
41
+ assert @books.find(@id).is_a? ActiveDecorator::Decorated
42
+ end
43
+
44
+ test 'take' do
45
+ assert @books.take.is_a? ActiveDecorator::Decorated
46
+ end
47
+
48
+ sub_test_case 'when method chained' do
49
+ setup do
50
+ @books = @books.order(:id)
51
+ end
52
+
53
+ test 'each' do
54
+ @books.each do |b|
55
+ assert b.is_a? ActiveDecorator::Decorated
56
+ end
57
+ end
58
+
59
+ test 'first' do
60
+ assert @books.first.is_a? ActiveDecorator::Decorated
61
+ end
62
+
63
+ test 'last' do
64
+ assert @books.last.is_a? ActiveDecorator::Decorated
65
+ end
66
+
67
+ test 'find' do
68
+ assert @books.find(@id).is_a? ActiveDecorator::Decorated
69
+ end
70
+
71
+ test 'take' do
72
+ assert @books.take.is_a? ActiveDecorator::Decorated
73
+ end
74
+ end
75
+ end
@@ -11,6 +11,7 @@ require 'active_decorator'
11
11
  Bundler.require
12
12
  require 'capybara'
13
13
  require 'selenium/webdriver'
14
+ require 'byebug'
14
15
 
15
16
  # needs to load the app next
16
17
  require 'fake_app/fake_app'
@@ -30,7 +31,11 @@ rescue LoadError
30
31
  include Capybara::DSL
31
32
  end
32
33
  else
33
- ActionDispatch::SystemTestCase.driven_by(:selenium, using: :headless_chrome)
34
+ if ActionPack::VERSION::STRING > '5.2'
35
+ ActionDispatch::SystemTestCase.driven_by :selenium, using: :headless_chrome
36
+ else
37
+ ActionDispatch::SystemTestCase.driven_by :selenium_chrome_headless
38
+ end
34
39
  end
35
40
 
36
41
  module DatabaseDeleter
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_decorator
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.1
4
+ version: 1.3.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Akira Matsuda
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-11-14 00:00:00.000000000 Z
11
+ date: 2020-09-17 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: test-unit-rails
15
29
  requirement: !ruby/object:Gem::Requirement
@@ -95,7 +109,7 @@ dependencies:
95
109
  - !ruby/object:Gem::Version
96
110
  version: '0'
97
111
  - !ruby/object:Gem::Dependency
98
- name: jbuilder
112
+ name: byebug
99
113
  requirement: !ruby/object:Gem::Requirement
100
114
  requirements:
101
115
  - - ">="
@@ -123,13 +137,11 @@ files:
123
137
  - README.md
124
138
  - Rakefile
125
139
  - active_decorator.gemspec
126
- - gemfiles/Gemfile-rails.3.2.x
127
- - gemfiles/Gemfile-rails.4.0.x
128
- - gemfiles/Gemfile-rails.4.1.x
129
140
  - gemfiles/Gemfile-rails.4.2.x
130
141
  - gemfiles/Gemfile-rails.5.0.x
131
142
  - gemfiles/Gemfile-rails.5.1.x
132
143
  - gemfiles/Gemfile-rails.5.2.x
144
+ - gemfiles/Gemfile-rails.6.0.x
133
145
  - gemfiles/Gemfile-rails.edge
134
146
  - lib/active_decorator.rb
135
147
  - lib/active_decorator/config.rb
@@ -153,7 +165,7 @@ files:
153
165
  - test/configuration_test.rb
154
166
  - test/controllers/fake_detection_test.rb
155
167
  - test/decorator_test.rb
156
- - test/fake_app/app/views/api/bookstores/show.json.jbuilder
168
+ - test/fake_app/app/decorators/comic_decorator.rb
157
169
  - test/fake_app/app/views/authors/index.html.erb
158
170
  - test/fake_app/app/views/authors/show.html.erb
159
171
  - test/fake_app/app/views/authors/show.json.jbuilder
@@ -177,12 +189,13 @@ files:
177
189
  - test/features/jbuilder_test.rb
178
190
  - test/features/name_error_handling_test.rb
179
191
  - test/features/partial_test.rb
192
+ - test/models/association_test.rb
180
193
  - test/test_helper.rb
181
194
  homepage: https://github.com/amatsuda/active_decorator
182
195
  licenses:
183
196
  - MIT
184
197
  metadata: {}
185
- post_install_message:
198
+ post_install_message:
186
199
  rdoc_options: []
187
200
  require_paths:
188
201
  - lib
@@ -197,16 +210,15 @@ required_rubygems_version: !ruby/object:Gem::Requirement
197
210
  - !ruby/object:Gem::Version
198
211
  version: '0'
199
212
  requirements: []
200
- rubyforge_project:
201
- rubygems_version: 2.7.6
202
- signing_key:
213
+ rubygems_version: 3.2.0.rc.1
214
+ signing_key:
203
215
  specification_version: 4
204
216
  summary: A simple and Rubyish view helper for Rails
205
217
  test_files:
206
218
  - test/configuration_test.rb
207
219
  - test/controllers/fake_detection_test.rb
208
220
  - test/decorator_test.rb
209
- - test/fake_app/app/views/api/bookstores/show.json.jbuilder
221
+ - test/fake_app/app/decorators/comic_decorator.rb
210
222
  - test/fake_app/app/views/authors/index.html.erb
211
223
  - test/fake_app/app/views/authors/show.html.erb
212
224
  - test/fake_app/app/views/authors/show.json.jbuilder
@@ -230,4 +242,5 @@ test_files:
230
242
  - test/features/jbuilder_test.rb
231
243
  - test/features/name_error_handling_test.rb
232
244
  - test/features/partial_test.rb
245
+ - test/models/association_test.rb
233
246
  - test/test_helper.rb
@@ -1,8 +0,0 @@
1
- source 'https://rubygems.org'
2
-
3
- gemspec path: '..'
4
-
5
- gem 'rails', '~> 3.2.0'
6
- gem 'test-unit-rails', '1.0.2'
7
- gem 'capybara', '~> 2.0.0'
8
- gem 'nokogiri', '~> 1.5.0'
@@ -1,6 +0,0 @@
1
- source 'https://rubygems.org'
2
-
3
- gemspec path: '..'
4
-
5
- gem 'rails', '~> 4.0.0'
6
- gem 'nokogiri', '~> 1.6.0'
@@ -1,6 +0,0 @@
1
- source 'https://rubygems.org'
2
-
3
- gemspec path: '..'
4
-
5
- gem 'rails', '~> 4.1.0'
6
- gem 'nokogiri', '~> 1.6.0'
@@ -1 +0,0 @@
1
- json.name @bookstore.name