active_decorator 1.3.3 → 1.3.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (41) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +10 -0
  3. data/CHANGELOG.md +4 -0
  4. data/active_decorator.gemspec +3 -3
  5. data/gemfiles/Gemfile-rails.6.1.x +7 -0
  6. data/gemfiles/Gemfile-rails.edge +6 -0
  7. data/lib/active_decorator/monkey/action_view/collection_renderer.rb +12 -0
  8. data/lib/active_decorator/monkey/action_view/object_renderer.rb +12 -0
  9. data/lib/active_decorator/monkey/action_view/partial_renderer.rb +20 -8
  10. data/lib/active_decorator/railtie.rb +8 -0
  11. data/lib/active_decorator/version.rb +1 -1
  12. metadata +7 -62
  13. data/test/configuration_test.rb +0 -28
  14. data/test/controllers/fake_detection_test.rb +0 -14
  15. data/test/decorator_test.rb +0 -58
  16. data/test/fake_app/app/decorators/comic_decorator.rb +0 -5
  17. data/test/fake_app/app/views/authors/index.html.erb +0 -16
  18. data/test/fake_app/app/views/authors/show.html.erb +0 -10
  19. data/test/fake_app/app/views/authors/show.json.jbuilder +0 -2
  20. data/test/fake_app/app/views/book_mailer/thanks.text.erb +0 -1
  21. data/test/fake_app/app/views/books/_book.html.erb +0 -2
  22. data/test/fake_app/app/views/books/_book.json.jbuilder +0 -2
  23. data/test/fake_app/app/views/books/_book_locals.html.erb +0 -2
  24. data/test/fake_app/app/views/books/errata.html.erb +0 -1
  25. data/test/fake_app/app/views/books/errata2.html.erb +0 -1
  26. data/test/fake_app/app/views/books/error.html.erb +0 -1
  27. data/test/fake_app/app/views/books/index.html.erb +0 -4
  28. data/test/fake_app/app/views/books/purchase.html.erb +0 -1
  29. data/test/fake_app/app/views/books/show.html.erb +0 -4
  30. data/test/fake_app/app/views/movies/show.html.erb +0 -2
  31. data/test/fake_app/fake_app.rb +0 -311
  32. data/test/fake_app/public/images/cover.png +0 -0
  33. data/test/features/action_controller_api_test.rb +0 -24
  34. data/test/features/action_view_helpers_test.rb +0 -38
  35. data/test/features/association_test.rb +0 -41
  36. data/test/features/controller_ivar_test.rb +0 -41
  37. data/test/features/jbuilder_test.rb +0 -16
  38. data/test/features/name_error_handling_test.rb +0 -29
  39. data/test/features/partial_test.rb +0 -29
  40. data/test/models/association_test.rb +0 -75
  41. data/test/test_helper.rb +0 -52
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: '029394fb4c79f67fd71559154ad159840fd13e9635754f0748bf0e489cfdb411'
4
- data.tar.gz: a668184bfb8b7e77e2b205af385b8d319f161f0a5eb6b98e395b18e91e2e89db
3
+ metadata.gz: abd22d415613228c6c98fe9a0bf7255496dabaada709c34ba2cbe032a2fcc9b6
4
+ data.tar.gz: af38045be9a3de1832be692fa8f349010e7f79c7144d8d1f9f248aa3c311abb3
5
5
  SHA512:
6
- metadata.gz: 6207c58d396a6d465776ec79b74e20e59f3b04368c045cecaf5d075c13ba64717d3fd636e7ed0eb0106fc816068c3630abeb63561d431454e54913ddc55612ac
7
- data.tar.gz: 3881e7bc7470797115cdbc7286afb3eff68b6c77ea36b16f1cb11bdc86232c8861cbf3c7df7cf45efa0f2293285a4a73fd8c01f1823b98c87815f9a676ae531c
6
+ metadata.gz: 39f6198f7583dbc4df7bb3071161f234ce858cda7d17a27c07caab910bc3a1c652468cd42dfd7bf094cb53ceedc9da8c9a35d031a0e258398dc41f15b44d2925
7
+ data.tar.gz: 3b342f062765a460b03b3adbe2562cf01579a4b54427713dd3abe88694e371acee44ded648a218e9a2fd2872285635a08016cf11a0b68ecae0631243c6909c2f
@@ -26,12 +26,22 @@ cache: bundler
26
26
 
27
27
  matrix:
28
28
  include:
29
+ - rvm: 2.7.1
30
+ gemfile: gemfiles/Gemfile-rails.6.1.x
31
+ - rvm: 2.7.1
32
+ gemfile: gemfiles/Gemfile-rails.6.1.x
33
+ env: API=1
29
34
  - rvm: 2.7.1
30
35
  gemfile: gemfiles/Gemfile-rails.6.0.x
31
36
  - rvm: 2.7.1
32
37
  gemfile: gemfiles/Gemfile-rails.6.0.x
33
38
  env: API=1
34
39
 
40
+ - rvm: 2.6.6
41
+ gemfile: gemfiles/Gemfile-rails.6.1.x
42
+ - rvm: 2.6.6
43
+ gemfile: gemfiles/Gemfile-rails.6.1.x
44
+ env: API=1
35
45
  - rvm: 2.6.6
36
46
  gemfile: gemfiles/Gemfile-rails.6.0.x
37
47
  - rvm: 2.6.6
@@ -1,3 +1,7 @@
1
+ ## 1.3.4
2
+
3
+ * Support Rails 6.1 [@y-yagi]
4
+
1
5
  ## 1.3.3
2
6
 
3
7
  * Fixed Ruby 2.7 keyword arguments warning [@pocke]
@@ -13,9 +13,9 @@ Gem::Specification.new do |s|
13
13
  s.summary = %q{A simple and Rubyish view helper for Rails}
14
14
  s.description = %q{A simple and Rubyish view helper for Rails}
15
15
 
16
- s.files = `git ls-files`.split("\n")
17
- s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
- s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
16
+ s.files = Dir.chdir(File.expand_path('..', __FILE__)) do
17
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ end
19
19
  s.require_paths = ["lib"]
20
20
 
21
21
  s.add_dependency 'activesupport'
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec path: '..'
4
+
5
+ gem 'rails', '~> 6.1.0'
6
+ gem 'webdrivers'
7
+ gem 'jbuilder' unless ENV['API']
@@ -10,3 +10,9 @@ end
10
10
  github 'rails/rails' do
11
11
  gem 'rails'
12
12
  end
13
+
14
+ unless ENV['API']
15
+ github 'rails/jbuilder' do
16
+ gem 'jbuilder'
17
+ end
18
+ end
@@ -0,0 +1,12 @@
1
+ module ActiveDecorator
2
+ module Monkey
3
+ module ActionView
4
+ module CollectionRenderer
5
+ def render_collection_with_partial(collection, *)
6
+ ActiveDecorator::Decorator.instance.decorate collection
7
+ super
8
+ end
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,12 @@
1
+ module ActiveDecorator
2
+ module Monkey
3
+ module ActionView
4
+ module ObjectRenderer
5
+ def render_object_with_partial(object, *)
6
+ ActiveDecorator::Decorator.instance.decorate object
7
+ super
8
+ end
9
+ end
10
+ end
11
+ end
12
+ end
@@ -5,18 +5,30 @@ module ActiveDecorator
5
5
  module Monkey
6
6
  module ActionView
7
7
  module PartialRenderer
8
+ if Rails.version.to_f >= 6.1
9
+ def initialize(*)
10
+ super
11
+
12
+ @locals.each_value do |v|
13
+ ActiveDecorator::Decorator.instance.decorate v
14
+ end
15
+ end
16
+ end
17
+
8
18
  private
9
19
 
10
- def setup(*)
11
- super
20
+ if Rails.version.to_f < 6.1
21
+ def setup(*)
22
+ super
12
23
 
13
- @locals.each_value do |v|
14
- ActiveDecorator::Decorator.instance.decorate v
15
- end if @locals
16
- ActiveDecorator::Decorator.instance.decorate @object if @object
17
- ActiveDecorator::Decorator.instance.decorate @collection unless @collection.blank?
24
+ @locals.each_value do |v|
25
+ ActiveDecorator::Decorator.instance.decorate v
26
+ end if @locals
27
+ ActiveDecorator::Decorator.instance.decorate @object if @object
28
+ ActiveDecorator::Decorator.instance.decorate @collection unless @collection.blank?
18
29
 
19
- self
30
+ self
31
+ end
20
32
  end
21
33
  end
22
34
  end
@@ -6,6 +6,14 @@ module ActiveDecorator
6
6
  ActiveSupport.on_load :action_view do
7
7
  require 'active_decorator/monkey/action_view/partial_renderer'
8
8
  ActionView::PartialRenderer.send :prepend, ActiveDecorator::Monkey::ActionView::PartialRenderer
9
+
10
+ if Rails.version.to_f >= 6.1
11
+ require 'active_decorator/monkey/action_view/collection_renderer'
12
+ ActionView::CollectionRenderer.send :prepend, ActiveDecorator::Monkey::ActionView::CollectionRenderer
13
+
14
+ require 'active_decorator/monkey/action_view/object_renderer'
15
+ ActionView::ObjectRenderer.send :prepend, ActiveDecorator::Monkey::ActionView::ObjectRenderer
16
+ end
9
17
  end
10
18
 
11
19
  ActiveSupport.on_load :action_controller do
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActiveDecorator
4
- VERSION = '1.3.3'
4
+ VERSION = '1.3.4'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_decorator
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.3
4
+ version: 1.3.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Akira Matsuda
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-09-17 00:00:00.000000000 Z
11
+ date: 2020-12-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -142,6 +142,7 @@ files:
142
142
  - gemfiles/Gemfile-rails.5.1.x
143
143
  - gemfiles/Gemfile-rails.5.2.x
144
144
  - gemfiles/Gemfile-rails.6.0.x
145
+ - gemfiles/Gemfile-rails.6.1.x
145
146
  - gemfiles/Gemfile-rails.edge
146
147
  - lib/active_decorator.rb
147
148
  - lib/active_decorator/config.rb
@@ -150,6 +151,8 @@ files:
150
151
  - lib/active_decorator/helpers.rb
151
152
  - lib/active_decorator/monkey/abstract_controller/rendering.rb
152
153
  - lib/active_decorator/monkey/action_controller/base/rescue_from.rb
154
+ - lib/active_decorator/monkey/action_view/collection_renderer.rb
155
+ - lib/active_decorator/monkey/action_view/object_renderer.rb
153
156
  - lib/active_decorator/monkey/action_view/partial_renderer.rb
154
157
  - lib/active_decorator/monkey/active_record/associations.rb
155
158
  - lib/active_decorator/railtie.rb
@@ -162,35 +165,6 @@ files:
162
165
  - lib/generators/rspec/templates/decorator_spec.rb
163
166
  - lib/generators/test_unit/decorator_generator.rb
164
167
  - lib/generators/test_unit/templates/decorator_test.rb
165
- - test/configuration_test.rb
166
- - test/controllers/fake_detection_test.rb
167
- - test/decorator_test.rb
168
- - test/fake_app/app/decorators/comic_decorator.rb
169
- - test/fake_app/app/views/authors/index.html.erb
170
- - test/fake_app/app/views/authors/show.html.erb
171
- - test/fake_app/app/views/authors/show.json.jbuilder
172
- - test/fake_app/app/views/book_mailer/thanks.text.erb
173
- - test/fake_app/app/views/books/_book.html.erb
174
- - test/fake_app/app/views/books/_book.json.jbuilder
175
- - test/fake_app/app/views/books/_book_locals.html.erb
176
- - test/fake_app/app/views/books/errata.html.erb
177
- - test/fake_app/app/views/books/errata2.html.erb
178
- - test/fake_app/app/views/books/error.html.erb
179
- - test/fake_app/app/views/books/index.html.erb
180
- - test/fake_app/app/views/books/purchase.html.erb
181
- - test/fake_app/app/views/books/show.html.erb
182
- - test/fake_app/app/views/movies/show.html.erb
183
- - test/fake_app/fake_app.rb
184
- - test/fake_app/public/images/cover.png
185
- - test/features/action_controller_api_test.rb
186
- - test/features/action_view_helpers_test.rb
187
- - test/features/association_test.rb
188
- - test/features/controller_ivar_test.rb
189
- - test/features/jbuilder_test.rb
190
- - test/features/name_error_handling_test.rb
191
- - test/features/partial_test.rb
192
- - test/models/association_test.rb
193
- - test/test_helper.rb
194
168
  homepage: https://github.com/amatsuda/active_decorator
195
169
  licenses:
196
170
  - MIT
@@ -210,37 +184,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
210
184
  - !ruby/object:Gem::Version
211
185
  version: '0'
212
186
  requirements: []
213
- rubygems_version: 3.2.0.rc.1
187
+ rubygems_version: 3.2.0
214
188
  signing_key:
215
189
  specification_version: 4
216
190
  summary: A simple and Rubyish view helper for Rails
217
- test_files:
218
- - test/configuration_test.rb
219
- - test/controllers/fake_detection_test.rb
220
- - test/decorator_test.rb
221
- - test/fake_app/app/decorators/comic_decorator.rb
222
- - test/fake_app/app/views/authors/index.html.erb
223
- - test/fake_app/app/views/authors/show.html.erb
224
- - test/fake_app/app/views/authors/show.json.jbuilder
225
- - test/fake_app/app/views/book_mailer/thanks.text.erb
226
- - test/fake_app/app/views/books/_book.html.erb
227
- - test/fake_app/app/views/books/_book.json.jbuilder
228
- - test/fake_app/app/views/books/_book_locals.html.erb
229
- - test/fake_app/app/views/books/errata.html.erb
230
- - test/fake_app/app/views/books/errata2.html.erb
231
- - test/fake_app/app/views/books/error.html.erb
232
- - test/fake_app/app/views/books/index.html.erb
233
- - test/fake_app/app/views/books/purchase.html.erb
234
- - test/fake_app/app/views/books/show.html.erb
235
- - test/fake_app/app/views/movies/show.html.erb
236
- - test/fake_app/fake_app.rb
237
- - test/fake_app/public/images/cover.png
238
- - test/features/action_controller_api_test.rb
239
- - test/features/action_view_helpers_test.rb
240
- - test/features/association_test.rb
241
- - test/features/controller_ivar_test.rb
242
- - test/features/jbuilder_test.rb
243
- - test/features/name_error_handling_test.rb
244
- - test/features/partial_test.rb
245
- - test/models/association_test.rb
246
- - test/test_helper.rb
191
+ test_files: []
@@ -1,28 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'test_helper'
4
-
5
- Comic = Struct.new(:title, :price)
6
-
7
- module ComicPresenter
8
- def price
9
- "$#{super}"
10
- end
11
- end
12
-
13
- class ConfigurationTest < Test::Unit::TestCase
14
- test 'with a custom decorator_suffix' do
15
- begin
16
- ActiveDecorator.configure do |config|
17
- config.decorator_suffix = 'Presenter'
18
- end
19
-
20
- comic = ActiveDecorator::Decorator.instance.decorate Comic.new("amatsuda's (Poignant) Guide to ActiveDecorator", 3)
21
- assert_equal '$3', comic.price
22
- ensure
23
- ActiveDecorator.configure do |config|
24
- config.decorator_suffix = 'Decorator'
25
- end
26
- end
27
- end
28
- end
@@ -1,14 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'test_helper'
4
-
5
- class MoviesControllerTest < ActionController::TestCase
6
- test 'reveals fakes' do
7
- movie = Movie.create
8
- if Rails::VERSION::MAJOR >= 5
9
- assert_nothing_raised { get :show, params: {id: movie.id} }
10
- else
11
- assert_nothing_raised { get :show, id: movie.id }
12
- end
13
- end
14
- end
@@ -1,58 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'test_helper'
4
-
5
- class DecoratorTest < Test::Unit::TestCase
6
- test 'it returns the object on decoration' do
7
- book = Book.new title: 'Boek'
8
- assert_equal book, ActiveDecorator::Decorator.instance.decorate(book)
9
- end
10
-
11
- test 'it returns the object when it already is decorated on decorate' do
12
- book = Book.new title: 'Boek'
13
- assert_equal book, ActiveDecorator::Decorator.instance.decorate(ActiveDecorator::Decorator.instance.decorate(book))
14
- end
15
-
16
- test 'it returns the object of ActiveRecord::Relation on decorate' do
17
- 3.times do |index|
18
- Book.create title: "ve#{index}"
19
- end
20
-
21
- books = Book.all
22
- assert_equal books, ActiveDecorator::Decorator.instance.decorate(books)
23
- end
24
-
25
- test 'it returns the object of ActiveRecord::Relation when it already is decorated on decorate' do
26
- 3.times do |index|
27
- Book.create title: "ve#{index}"
28
- end
29
-
30
- books = Book.all
31
- assert_equal books, ActiveDecorator::Decorator.instance.decorate(ActiveDecorator::Decorator.instance.decorate(books))
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
58
- end
@@ -1,5 +0,0 @@
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
@@ -1,16 +0,0 @@
1
- <% @authors.each do |author| %>
2
- <%= author.name %>
3
- <%= author.reverse_name %>
4
-
5
- <% if params[:partial] == 'collection' %>
6
- <%= render author.books %>
7
- <% elsif params[:partial] == 'locals' %>
8
- <% author.books.each do |book| %>
9
- <%= render partial: 'books/book_locals', locals: {b: book} %>
10
- <% end %>
11
- <% else %>
12
- <% author.books.each do |book| %>
13
- <%= render book %>
14
- <% end %>
15
- <% end %>
16
- <% end %>
@@ -1,10 +0,0 @@
1
- <%= @author.name %>
2
- <%= @author.capitalized_name %>
3
- <%= @author.books.first.upcased_title %>
4
- <%= @author.books.last.upcased_title %>
5
- <% if p = @author.publishers.take %><%= p.upcased_name %><% end %>
6
- <% if Rails.version.to_f >= 5.1 && p = @author.publishers.second_to_last %><%= p.reversed_name %><% end %>
7
- <% if p = @author.profile %><%= p.address %><% end %>
8
- <% if h = @author.profile_history %><%= h.update_date %><% end %>
9
- <% if m = @author.magazines.first %><%= m.upcased_title %><% end %>
10
- <% if c = @author.company %><%= c.reverse_name %><% end %>
@@ -1,2 +0,0 @@
1
- json.name @author.name
2
- json.books @author.books, partial: 'books/book', as: :book
@@ -1 +0,0 @@
1
- Thank you for purchasing <%= @book.upcased_title %>.
@@ -1,2 +0,0 @@
1
- <%= book.title %>
2
- <%= book.reverse_title %>
@@ -1,2 +0,0 @@
1
- json.title book.title
2
- json.reverse_title book.reverse_title
@@ -1,2 +0,0 @@
1
- <%= b.title %>
2
- <%= b.upcased_title %>
@@ -1 +0,0 @@
1
- <%= @book.errata %>
@@ -1 +0,0 @@
1
- <%= @book.errata2 %>
@@ -1 +0,0 @@
1
- <%= @book.error %>
@@ -1,4 +0,0 @@
1
- <% @books.each do |book| %>
2
- <%= book.title %>
3
- <%= book.reverse_title %>
4
- <% end %>
@@ -1,4 +0,0 @@
1
- <%= @book.link %>
2
- <%= @book.cover_image %>
3
-
4
- <%= link_to 'purchase', [:purchase, @book.author, @book], method: :post %>
@@ -1,2 +0,0 @@
1
- <%= @movie.name %>
2
- <% if a = @movie.author %><%= a.reverse_name %><% end %>
@@ -1,311 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- ENV['RAILS_ENV'] ||= 'test'
4
-
5
- require 'active_record'
6
- require 'action_controller/railtie'
7
- require 'action_mailer/railtie'
8
- require 'action_view/railtie'
9
- require 'ostruct'
10
- require 'jbuilder' unless ENV['API']
11
-
12
- # config
13
- ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
14
-
15
- module ActiveDecoratorTestApp
16
- class Application < Rails::Application
17
- config.secret_token = '"confusion" will be my epitaph.'
18
- config.session_store :cookie_store, key: '_myapp_session'
19
- config.active_support.deprecation = :log
20
- config.eager_load = false
21
- config.action_dispatch.show_exceptions = false
22
- config.root = File.dirname(__FILE__)
23
-
24
- config.action_mailer.delivery_method = :test
25
- end
26
- end
27
- ActiveDecoratorTestApp::Application.initialize!
28
-
29
- # routes
30
- ActiveDecoratorTestApp::Application.routes.draw do
31
- resources :authors, only: [:index, :show] do
32
- resources :books, only: [:index, :show] do
33
- member do
34
- get :errata
35
- get :errata2
36
- get :error
37
- post :purchase
38
- end
39
- end
40
- resources :novels, only: [:index, :show] do
41
- member do
42
- get :error
43
- post :purchase
44
- end
45
- end
46
- end
47
- resources :movies, only: :show
48
-
49
- if Rails::VERSION::MAJOR >= 5
50
- namespace :api do
51
- resources :bookstores, only: :show do
52
- collection do
53
- get :error
54
- end
55
- end
56
- end
57
- end
58
- end
59
-
60
- # models
61
- class Author < ActiveRecord::Base
62
- has_many :books
63
- has_many :publishers, through: :books
64
- has_many :movies
65
- has_one :profile
66
- has_one :profile_history, through: :profile
67
- has_and_belongs_to_many :magazines
68
- belongs_to :company
69
- end
70
- class Book < ActiveRecord::Base
71
- belongs_to :author
72
- belongs_to :publisher
73
- accepts_nested_attributes_for :publisher
74
- end
75
- class Novel < Book
76
- end
77
- class Movie < ActiveRecord::Base
78
- belongs_to :author
79
- end
80
- class Publisher < ActiveRecord::Base
81
- has_many :books
82
- end
83
- class Profile < ActiveRecord::Base
84
- belongs_to :author
85
- has_one :profile_history
86
- accepts_nested_attributes_for :profile_history
87
- end
88
- class ProfileHistory < ActiveRecord::Base
89
- belongs_to :profile
90
- end
91
- class Magazine < ActiveRecord::Base
92
- has_and_belongs_to_many :authors
93
- end
94
- class Company < ActiveRecord::Base
95
- has_many :authors
96
- end
97
- class Bookstore < ActiveRecord::Base
98
- end
99
-
100
- # helpers
101
- module ApplicationHelper; end
102
-
103
- # decorators
104
- module AuthorDecorator
105
- def reverse_name
106
- name.reverse
107
- end
108
-
109
- def capitalized_name
110
- name.capitalize
111
- end
112
- end
113
- module BookDecorator
114
- def reverse_title
115
- title.reverse
116
- end
117
-
118
- def upcased_title
119
- title.upcase
120
- end
121
-
122
- def link
123
- link_to title, "#{request.protocol}#{request.host_with_port}/assets/sample.png", class: 'title'
124
- end
125
-
126
- def cover_image
127
- image_tag 'cover.png'
128
- end
129
-
130
- def errata
131
- poof!
132
- end
133
-
134
- def errata2
135
- title.boom!
136
- end
137
-
138
- def error
139
- "ERROR"
140
- end
141
- end
142
- module PublisherDecorator
143
- def upcased_name
144
- name.upcase
145
- end
146
-
147
- def reversed_name
148
- name.reverse
149
- end
150
- end
151
- module ProfileDecorator
152
- def address
153
- "secret"
154
- end
155
- end
156
- module ProfileHistoryDecorator
157
- def update_date
158
- updated_on.strftime('%Y/%m/%d')
159
- end
160
- end
161
- module MagazineDecorator
162
- def upcased_title
163
- title.upcase
164
- end
165
- end
166
- module CompanyDecorator
167
- def reverse_name
168
- name.reverse
169
- end
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
180
-
181
- # decorator fake
182
- class MovieDecorator; end
183
-
184
- # controllers
185
- unless ENV['API']
186
- class ApplicationController < ActionController::Base
187
- self.append_view_path File.dirname(__FILE__)
188
- end
189
- class AuthorsController < ApplicationController
190
- def index
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)
196
- else
197
- @authors = Author.all
198
- end
199
- end
200
-
201
- def show
202
- @author = Author.find params[:id]
203
- end
204
- end
205
- class BooksController < ApplicationController
206
- class CustomError < StandardError; end
207
-
208
- rescue_from CustomError do
209
- render :error
210
- end
211
-
212
- def index
213
- @author = Author.find params[:author_id]
214
- @books = @author.books
215
- end
216
-
217
- def show
218
- @book = Author.find(params[:author_id]).books.find(params[:id])
219
- end
220
-
221
- def errata
222
- @book = Author.find(params[:author_id]).books.find(params[:id])
223
- end
224
-
225
- def errata2
226
- @book = Author.find(params[:author_id]).books.find(params[:id])
227
- end
228
-
229
- def error
230
- @book = Author.find(params[:author_id]).books.find(params[:id])
231
- raise CustomError, "error"
232
- end
233
-
234
- def purchase
235
- @book = Author.find(params[:author_id]).books.find(params[:id])
236
-
237
- @view_context_before_sending_mail = ActiveDecorator::ViewContext.current
238
- BookMailer.thanks(@book).deliver
239
- raise 'Wrong ViewContext!' if ActiveDecorator::ViewContext.current != @view_context_before_sending_mail
240
- end
241
- end
242
- class MoviesController < ApplicationController
243
- def show
244
- @movie = Movie.find params[:id]
245
- end
246
- end
247
- else
248
- module Api
249
- class BookstoresController < ActionController::API
250
- class CustomError < StandardError; end
251
-
252
- rescue_from CustomError do
253
- head :internal_server_error
254
- end
255
-
256
- def show
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!'
267
- end
268
- end
269
- end
270
- end
271
-
272
- # mailers
273
- class BookMailer < ActionMailer::Base
274
- def thanks(book)
275
- @book = book
276
- mail from: 'nobody@example.com', to: 'test@example.com', subject: 'Thanks'
277
- end
278
- end
279
-
280
- # migrations
281
- class CreateAllTables < ActiveRecord::VERSION::MAJOR >= 5 ? ActiveRecord::Migration[5.0] : ActiveRecord::Migration
282
- def self.up
283
- create_table(:authors) {|t| t.string :name; t.references :company}
284
- create_table(:books) {|t| t.string :title; t.string :type; t.references :author; t.references :publisher }
285
- create_table(:profiles) {|t| t.string :address; t.references :author}
286
- create_table(:profile_histories) {|t| t.date :updated_on; t.references :profile}
287
- create_table(:publishers) {|t| t.string :name}
288
- create_table(:movies) {|t| t.string :name; t.references :author}
289
- create_table(:magazines) {|t| t.string :title}
290
- create_table(:authors_magazines) {|t| t.references :author; t.references :magazine}
291
- create_table(:companies) {|t| t.string :name}
292
- create_table(:bookstores) {|t| t.string :name}
293
- end
294
- end
295
-
296
- # Proxy for ActiveRecord::Relation
297
- class RelationProxy < BasicObject
298
- attr_accessor :ar_relation
299
-
300
- def initialize(ar_relation)
301
- @ar_relation = ar_relation
302
- end
303
-
304
- def method_missing(method, *args, &block)
305
- @ar_relation.public_send(method, *args, &block)
306
- end
307
- end
308
-
309
- module Foo
310
- class Comic; end
311
- end
@@ -1,24 +0,0 @@
1
- # frozen_string_literal: true
2
- require 'test_helper'
3
-
4
- if Rails::VERSION::MAJOR >= 5
5
-
6
- class ActionControllerAPITest < ActionDispatch::IntegrationTest
7
- setup do
8
- Bookstore.create! name: 'junkudo'
9
- end
10
-
11
- test 'decorating objects in api only controllers' do
12
- get "/api/bookstores/#{Bookstore.last.id}.json"
13
-
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
21
- end
22
- end
23
-
24
- end
@@ -1,38 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'test_helper'
4
-
5
- class ActionViewHelpersTest < ActionDispatch::IntegrationTest
6
- setup do
7
- aamine = Author.create! name: 'aamine'
8
- @rhg = aamine.books.create! title: 'RHG'
9
- @rhg_novel = aamine.books.create! title: 'RHG Novel', type: 'Novel'
10
- end
11
-
12
- test 'invoking action_view helper methods' do
13
- visit "/authors/#{@rhg.author.id}/books/#{@rhg.id}"
14
- within 'a.title' do
15
- assert page.has_content? 'RHG'
16
- end
17
- assert page.has_css?('img')
18
- end
19
-
20
- test 'invoking action_view helper methods on model subclass' do
21
- visit "/authors/#{@rhg_novel.author.id}/books/#{@rhg_novel.id}"
22
- within 'a.title' do
23
- assert page.has_content? 'RHG Novel'
24
- end
25
- assert page.has_css?('img')
26
- end
27
-
28
- test 'invoking action_view helper methods in rescue_from view' do
29
- visit "/authors/#{@rhg.author.id}/books/#{@rhg.id}/error"
30
- assert page.has_content?('ERROR')
31
- end
32
-
33
- test 'make sure that action_view + action_mailer works' do
34
- visit "/authors/#{@rhg.author.id}/books/#{@rhg.id}"
35
- click_link 'purchase'
36
- assert page.has_content? 'done'
37
- end
38
- end
@@ -1,41 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'test_helper'
4
-
5
- class AssociationIntegrationTest < ActionDispatch::IntegrationTest
6
- setup do
7
- company = Company.create! name: 'NaCl'
8
- @matz = company.authors.create! name: 'matz'
9
- @matz.books.create!(
10
- title: 'the world of code',
11
- publisher_attributes: { name: 'nikkei linux' }
12
- )
13
- @matz.books.create!(
14
- title: 'the ruby programming language',
15
- publisher_attributes: { name: "o'reilly" }
16
- )
17
- @matz.create_profile! address: 'Matsue city, Shimane'
18
- @matz.profile.create_profile_history! updated_on: Date.new(2017, 2, 7)
19
- @matz.magazines.create! title: 'rubima'
20
- end
21
-
22
- test 'decorating associated objects' do
23
- visit "/authors/#{@matz.id}"
24
- assert page.has_content? 'the world of code'.upcase
25
- assert page.has_content? 'the ruby programming language'.upcase
26
- assert page.has_content? 'nikkei linux'.upcase
27
- if Rails.version.to_f >= 5.1
28
- assert page.has_content? 'nikkei linux'.reverse
29
- end
30
- assert page.has_content? 'secret'
31
- assert page.has_content? '2017/02/07'
32
- assert page.has_content? 'rubima'.upcase
33
- assert page.has_content? 'NaCl'.reverse
34
- end
35
-
36
- test "decorating associated objects that owner doesn't have decorator" do
37
- movie = Movie.create! author: @matz
38
- visit "/movies/#{movie.id}"
39
- assert page.has_content? 'matz'.reverse
40
- end
41
- end
@@ -1,41 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'test_helper'
4
-
5
- class ControllerIvarTest < ActionDispatch::IntegrationTest
6
- setup do
7
- @matz = Author.create! name: 'matz'
8
- @matz.books.create! title: 'the world of code'
9
- Author.create! name: 'takahashim'
10
- end
11
-
12
- test 'decorating a model object in ivar' do
13
- visit "/authors/#{@matz.id}"
14
- assert page.has_content? 'matz'
15
- assert page.has_content? 'matz'.capitalize
16
- end
17
-
18
- test 'decorating model scope in ivar' do
19
- visit '/authors'
20
- assert page.has_content? 'takahashim'
21
- assert page.has_content? 'takahashim'.reverse
22
- end
23
-
24
- test "decorating models' array in ivar" do
25
- visit '/authors?variable_type=array'
26
- assert page.has_content? 'takahashim'
27
- assert page.has_content? 'takahashim'.reverse
28
- end
29
-
30
- test "decorating models' proxy object in ivar" do
31
- visit '/authors?variable_type=proxy'
32
- assert page.has_content? 'takahashim'
33
- assert page.has_content? 'takahashim'.reverse
34
- end
35
-
36
- test 'decorating model association proxy in ivar' do
37
- visit "/authors/#{@matz.id}/books"
38
- assert page.has_content? 'the world of code'
39
- assert page.has_content? 'the world of code'.reverse
40
- end
41
- end
@@ -1,16 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'test_helper'
4
-
5
- class JbuilderTest < ActionDispatch::IntegrationTest
6
- setup do
7
- Author.create! name: 'aamine'
8
- nari = Author.create! name: 'nari'
9
- nari.books.create! title: 'the gc book'
10
- end
11
-
12
- test 'decorating objects in Jbuilder partials' do
13
- visit "/authors/#{Author.last.id}.json"
14
- assert_equal '{"name":"nari","books":[{"title":"the gc book","reverse_title":"koob cg eht"}]}', page.source
15
- end
16
- end
@@ -1,29 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'test_helper'
4
-
5
- class NameErrorHandlingTest < ActionDispatch::IntegrationTest
6
- setup do
7
- aamine = Author.create! name: 'aamine'
8
- @rhg = aamine.books.create! title: 'RHG'
9
- end
10
-
11
- test 'raising NameError in a decorator' do
12
- err = assert_raises ActionView::Template::Error do
13
- visit "/authors/#{@rhg.author.id}/books/#{@rhg.id}/errata"
14
- end
15
-
16
- assert_match(/undefined method `poof!' for/, err.message)
17
- assert_match 'poof!', err.cause.name if err.respond_to?(:cause)
18
- end
19
-
20
- test "Don't touch NameError that was not raised from a decorator module but from other classes" do
21
- err = assert_raises ActionView::Template::Error do
22
- visit "/authors/#{@rhg.author.id}/books/#{@rhg.id}/errata2"
23
- end
24
-
25
- assert_match(/undefined method `boom!' for/, err.message)
26
- assert_match 'boom!', err.cause.name if err.respond_to?(:cause)
27
- assert_not_match(/active_decorator\/lib\/active_decorator\/.* in method_missing'$/, err.backtrace[0])
28
- end
29
- end
@@ -1,29 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'test_helper'
4
-
5
- class PartialTest < ActionDispatch::IntegrationTest
6
- setup do
7
- Author.create! name: 'aamine'
8
- nari = Author.create! name: 'nari'
9
- nari.books.create! title: 'the gc book'
10
- end
11
-
12
- test 'decorating implicit @object' do
13
- visit '/authors'
14
- assert page.has_content? 'the gc book'
15
- assert page.has_content? 'the gc book'.reverse
16
- end
17
-
18
- test 'decorating implicit @collection' do
19
- visit '/authors?partial=collection'
20
- assert page.has_content? 'the gc book'
21
- assert page.has_content? 'the gc book'.reverse
22
- end
23
-
24
- test 'decorating objects in @locals' do
25
- visit '/authors?partial=locals'
26
- assert page.has_content? 'the gc book'
27
- assert page.has_content? 'the gc book'.upcase
28
- end
29
- end
@@ -1,75 +0,0 @@
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
@@ -1,52 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
4
- $LOAD_PATH.unshift(File.dirname(__FILE__))
5
- # load Rails first
6
- require 'rails'
7
-
8
- # load the plugin
9
- require 'active_decorator'
10
-
11
- Bundler.require
12
- require 'capybara'
13
- require 'selenium/webdriver'
14
- require 'byebug'
15
-
16
- # needs to load the app next
17
- require 'fake_app/fake_app'
18
-
19
- require 'test/unit/rails/test_help'
20
-
21
- begin
22
- require 'action_dispatch/system_test_case'
23
- rescue LoadError
24
- Capybara.register_driver :chrome do |app|
25
- options = Selenium::WebDriver::Chrome::Options.new(args: %w[no-sandbox headless disable-gpu])
26
- Capybara::Selenium::Driver.new(app, browser: :chrome, options: options)
27
- end
28
- Capybara.javascript_driver = :chrome
29
-
30
- class ActionDispatch::IntegrationTest
31
- include Capybara::DSL
32
- end
33
- else
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
39
- end
40
-
41
- module DatabaseDeleter
42
- def setup
43
- Book.delete_all
44
- Author.delete_all
45
- Movie.delete_all
46
- super
47
- end
48
- end
49
-
50
- Test::Unit::TestCase.send :prepend, DatabaseDeleter
51
-
52
- CreateAllTables.up unless ActiveRecord::Base.connection.table_exists? 'authors'