active_decorator 0.9.0 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.travis.yml +1 -1
- data/README.md +19 -5
- data/gemfiles/Gemfile-rails.4.0.x +1 -0
- data/gemfiles/Gemfile-rails.4.1.x +1 -0
- data/gemfiles/Gemfile-rails.4.2.x +1 -0
- data/gemfiles/Gemfile-rails.5.1.x +1 -1
- data/lib/active_decorator/helpers.rb +12 -5
- data/lib/active_decorator/version.rb +1 -1
- data/lib/active_decorator/view_context.rb +16 -12
- data/lib/generators/rspec/templates/decorator_spec.rb +2 -2
- data/lib/generators/test_unit/templates/decorator_test.rb +1 -1
- data/test/decorator_test.rb +15 -0
- data/test/fake_app/app/views/books/errata2.html.erb +1 -0
- data/test/fake_app/fake_app.rb +9 -0
- data/test/features/name_error_handling_test.rb +12 -1
- metadata +11 -9
- data/spec/lib/decorator_spec.rb +0 -15
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8ec56959877091a50db872942aaeb8352cdb9269
|
4
|
+
data.tar.gz: 5a64a878439cc739235815ad6b9345d243cd191b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7935a3abad55b5a751acfe09fab8cf4dcd7f1b4e5c66b09d2a00fbc0cccbf9562ea84ee76767cc77313160174f427fbcb38ec17f767a2f503c01329e8d377472
|
7
|
+
data.tar.gz: f84157d3533f18cfd9aafed0e5b23f48f210e3dd8a351e8c0bb2df95afec646c634ca650f833e388fb11c1134527ea58c7b7c5f7f4fe7933b98a8221a6672a25
|
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -17,7 +17,7 @@ A simple and Rubyish view helper for Rails 3, Rails 4 and Rails 5. Keep your hel
|
|
17
17
|
|
18
18
|
* Ruby 2.0.0, 2.1.x, 2.2.x, 2.3.x, 2.4.x, and 2.5 (trunk)
|
19
19
|
|
20
|
-
* Rails 3.2.x, 4.0.x, 4.1.x, 4.2.x, 5.0, and 5.
|
20
|
+
* Rails 3.2.x, 4.0.x, 4.1.x, 4.2.x, 5.0, 5.1, and 5.2 (edge)
|
21
21
|
|
22
22
|
|
23
23
|
## Supported ORMs ##
|
@@ -47,8 +47,8 @@ end
|
|
47
47
|
* Controller
|
48
48
|
```ruby
|
49
49
|
class AuthorsController < ApplicationController
|
50
|
-
def show(id)
|
51
|
-
@
|
50
|
+
def show(id) # powered by action_args
|
51
|
+
@author = Author.find id
|
52
52
|
end
|
53
53
|
end
|
54
54
|
```
|
@@ -87,7 +87,7 @@ end
|
|
87
87
|
```ruby
|
88
88
|
class AuthorsController < ApplicationController
|
89
89
|
def show(id)
|
90
|
-
@
|
90
|
+
@author = Author.find id
|
91
91
|
end
|
92
92
|
end
|
93
93
|
```
|
@@ -112,6 +112,20 @@ end
|
|
112
112
|
</ul>
|
113
113
|
```
|
114
114
|
|
115
|
+
### Using ActiveDecorator outside of Action View ###
|
116
|
+
|
117
|
+
Sometimes your may want to use decorators outside of Action View, for example,
|
118
|
+
for background tasks for ActiveJob.
|
119
|
+
For such use case, ActiveDecorator module provides `with_view_context` method
|
120
|
+
that takes some kind of Action View and a block.
|
121
|
+
|
122
|
+
```ruby
|
123
|
+
ActiveDecorator::ViewContext.run_with ApplicationController.new.view_context do
|
124
|
+
## perform some heavy jobs here
|
125
|
+
end
|
126
|
+
```
|
127
|
+
|
128
|
+
|
115
129
|
## Testing
|
116
130
|
|
117
131
|
You can test a decorator using your favorite test framework by decorating the model instance with
|
@@ -120,7 +134,7 @@ You can test a decorator using your favorite test framework by decorating the mo
|
|
120
134
|
ActiveDecorator::Decorator.instance.decorate(model_instance)
|
121
135
|
```
|
122
136
|
|
123
|
-
Considering an `Organization` model and
|
137
|
+
Considering an `Organization` model and its simple decorator:
|
124
138
|
|
125
139
|
```ruby
|
126
140
|
module OrganizationDecorator
|
@@ -4,13 +4,20 @@ module ActiveDecorator
|
|
4
4
|
module Helpers
|
5
5
|
def method_missing(method, *args, &block)
|
6
6
|
super
|
7
|
-
rescue NoMethodError, NameError
|
7
|
+
rescue NoMethodError, NameError => e
|
8
|
+
# the error is not mine, so just releases it as is.
|
9
|
+
raise e if e.name != method
|
10
|
+
|
8
11
|
begin
|
9
12
|
(view_context = ActiveDecorator::ViewContext.current).send method, *args, &block
|
10
|
-
rescue NoMethodError
|
11
|
-
raise
|
12
|
-
|
13
|
-
raise
|
13
|
+
rescue NoMethodError => e
|
14
|
+
raise e if e.name != method
|
15
|
+
|
16
|
+
raise NoMethodError.new("undefined method `#{method}' for either #{self} or #{view_context}", method)
|
17
|
+
rescue NameError => e
|
18
|
+
raise e if e.name != method
|
19
|
+
|
20
|
+
raise NameError.new("undefined local variable `#{method}' for either #{self} or #{view_context}", method)
|
14
21
|
end
|
15
22
|
end
|
16
23
|
end
|
@@ -4,16 +4,26 @@ module ActiveDecorator
|
|
4
4
|
module ViewContext
|
5
5
|
class << self
|
6
6
|
def current
|
7
|
-
|
7
|
+
view_context_stack.last
|
8
8
|
end
|
9
9
|
|
10
10
|
def push(view_context)
|
11
|
-
|
12
|
-
Thread.current[:active_decorator_view_contexts] << view_context
|
11
|
+
view_context_stack.push view_context
|
13
12
|
end
|
14
13
|
|
15
14
|
def pop
|
16
|
-
|
15
|
+
view_context_stack.pop
|
16
|
+
end
|
17
|
+
|
18
|
+
def view_context_stack
|
19
|
+
Thread.current[:active_decorator_view_contexts] ||= []
|
20
|
+
end
|
21
|
+
|
22
|
+
def run_with(view_context)
|
23
|
+
push view_context
|
24
|
+
yield
|
25
|
+
ensure
|
26
|
+
pop
|
17
27
|
end
|
18
28
|
end
|
19
29
|
|
@@ -23,20 +33,14 @@ module ActiveDecorator
|
|
23
33
|
included do
|
24
34
|
if Rails::VERSION::MAJOR >= 4
|
25
35
|
around_action do |controller, blk|
|
26
|
-
|
27
|
-
ActiveDecorator::ViewContext.push controller.view_context
|
36
|
+
ActiveDecorator::ViewContext.run_with(controller.view_context) do
|
28
37
|
blk.call
|
29
|
-
ensure
|
30
|
-
ActiveDecorator::ViewContext.pop
|
31
38
|
end
|
32
39
|
end
|
33
40
|
else
|
34
41
|
around_filter do |controller, blk|
|
35
|
-
|
36
|
-
ActiveDecorator::ViewContext.push controller.view_context
|
42
|
+
ActiveDecorator::ViewContext.run_with(controller.view_context) do
|
37
43
|
blk.call
|
38
|
-
ensure
|
39
|
-
ActiveDecorator::ViewContext.pop
|
40
44
|
end
|
41
45
|
end
|
42
46
|
end
|
@@ -1,7 +1,7 @@
|
|
1
1
|
require '<%= File.exists?('spec/rails_helper.rb') ? 'rails_helper' : 'spec_helper' %>'
|
2
2
|
|
3
|
-
describe <%=
|
4
|
-
let(:<%= singular_name %>) { <%= class_name %>.new.extend <%=
|
3
|
+
describe <%= class_name %>Decorator do
|
4
|
+
let(:<%= singular_name %>) { <%= class_name %>.new.extend <%= class_name %>Decorator }
|
5
5
|
subject { <%= singular_name %> }
|
6
6
|
it { should be_a <%= class_name %> }
|
7
7
|
end
|
@@ -2,7 +2,7 @@ require 'test_helper'
|
|
2
2
|
|
3
3
|
class <%= singular_name.camelize %>DecoratorTest < ActiveSupport::TestCase
|
4
4
|
def setup
|
5
|
-
@<%= singular_name %> = <%= class_name %>.new.extend <%=
|
5
|
+
@<%= singular_name %> = <%= class_name %>.new.extend <%= class_name %>Decorator
|
6
6
|
end
|
7
7
|
|
8
8
|
# test "the truth" do
|
@@ -0,0 +1,15 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require 'test_helper'
|
3
|
+
|
4
|
+
class DecoratorTest < Test::Unit::TestCase
|
5
|
+
|
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
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
<%= @book.errata2 %>
|
data/test/fake_app/fake_app.rb
CHANGED
@@ -29,6 +29,7 @@ ActiveDecoratorTestApp::Application.routes.draw do
|
|
29
29
|
resources :books, only: [:index, :show] do
|
30
30
|
member do
|
31
31
|
get :errata
|
32
|
+
get :errata2
|
32
33
|
get :error
|
33
34
|
post :purchase
|
34
35
|
end
|
@@ -115,6 +116,10 @@ module BookDecorator
|
|
115
116
|
poof!
|
116
117
|
end
|
117
118
|
|
119
|
+
def errata2
|
120
|
+
title.boom!
|
121
|
+
end
|
122
|
+
|
118
123
|
def error
|
119
124
|
"ERROR"
|
120
125
|
end
|
@@ -203,6 +208,10 @@ class BooksController < ApplicationController
|
|
203
208
|
@book = Author.find(params[:author_id]).books.find(params[:id])
|
204
209
|
end
|
205
210
|
|
211
|
+
def errata2
|
212
|
+
@book = Author.find(params[:author_id]).books.find(params[:id])
|
213
|
+
end
|
214
|
+
|
206
215
|
def error
|
207
216
|
@book = Author.find(params[:author_id]).books.find(params[:id])
|
208
217
|
raise CustomError, "error"
|
@@ -12,6 +12,17 @@ class NameErrorHandlingTest < ActionDispatch::IntegrationTest
|
|
12
12
|
visit "/authors/#{@rhg.author.id}/books/#{@rhg.id}/errata"
|
13
13
|
end
|
14
14
|
|
15
|
-
assert_match(/undefined method `poof
|
15
|
+
assert_match(/undefined method `poof!' for/, err.message)
|
16
|
+
assert_match 'poof!', err.cause.name if err.respond_to?(:cause)
|
17
|
+
end
|
18
|
+
|
19
|
+
test "Don't touch NameError that was not raised from a decorator module but from other classes" do
|
20
|
+
err = assert_raises ActionView::Template::Error do
|
21
|
+
visit "/authors/#{@rhg.author.id}/books/#{@rhg.id}/errata2"
|
22
|
+
end
|
23
|
+
|
24
|
+
assert_match(/undefined method `boom!' for/, err.message)
|
25
|
+
assert_match 'boom!', err.cause.name if err.respond_to?(:cause)
|
26
|
+
assert_not_match(/active_decorator\/lib\/active_decorator\/.* in method_missing'$/, err.backtrace[0])
|
16
27
|
end
|
17
28
|
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: 0.
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Akira Matsuda
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-06-29 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: A simple and Rubyish view helper for Rails
|
14
14
|
email:
|
@@ -17,8 +17,8 @@ executables: []
|
|
17
17
|
extensions: []
|
18
18
|
extra_rdoc_files: []
|
19
19
|
files:
|
20
|
-
-
|
21
|
-
-
|
20
|
+
- .gitignore
|
21
|
+
- .travis.yml
|
22
22
|
- Gemfile
|
23
23
|
- MIT-LICENSE
|
24
24
|
- README.md
|
@@ -48,9 +48,9 @@ files:
|
|
48
48
|
- lib/generators/rspec/templates/decorator_spec.rb
|
49
49
|
- lib/generators/test_unit/decorator_generator.rb
|
50
50
|
- lib/generators/test_unit/templates/decorator_test.rb
|
51
|
-
- spec/lib/decorator_spec.rb
|
52
51
|
- test/configuration_test.rb
|
53
52
|
- test/controllers/fake_detection_test.rb
|
53
|
+
- test/decorator_test.rb
|
54
54
|
- test/fake_app/app/views/authors/index.html.erb
|
55
55
|
- test/fake_app/app/views/authors/show.html.erb
|
56
56
|
- test/fake_app/app/views/authors/show.json.jbuilder
|
@@ -59,6 +59,7 @@ files:
|
|
59
59
|
- test/fake_app/app/views/books/_book.json.jbuilder
|
60
60
|
- test/fake_app/app/views/books/_book_locals.html.erb
|
61
61
|
- test/fake_app/app/views/books/errata.html.erb
|
62
|
+
- test/fake_app/app/views/books/errata2.html.erb
|
62
63
|
- test/fake_app/app/views/books/error.html.erb
|
63
64
|
- test/fake_app/app/views/books/index.html.erb
|
64
65
|
- test/fake_app/app/views/books/purchase.html.erb
|
@@ -81,24 +82,24 @@ require_paths:
|
|
81
82
|
- lib
|
82
83
|
required_ruby_version: !ruby/object:Gem::Requirement
|
83
84
|
requirements:
|
84
|
-
- -
|
85
|
+
- - '>='
|
85
86
|
- !ruby/object:Gem::Version
|
86
87
|
version: '0'
|
87
88
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
88
89
|
requirements:
|
89
|
-
- -
|
90
|
+
- - '>='
|
90
91
|
- !ruby/object:Gem::Version
|
91
92
|
version: '0'
|
92
93
|
requirements: []
|
93
94
|
rubyforge_project:
|
94
|
-
rubygems_version: 2.6.
|
95
|
+
rubygems_version: 2.6.12
|
95
96
|
signing_key:
|
96
97
|
specification_version: 4
|
97
98
|
summary: A simple and Rubyish view helper for Rails
|
98
99
|
test_files:
|
99
|
-
- spec/lib/decorator_spec.rb
|
100
100
|
- test/configuration_test.rb
|
101
101
|
- test/controllers/fake_detection_test.rb
|
102
|
+
- test/decorator_test.rb
|
102
103
|
- test/fake_app/app/views/authors/index.html.erb
|
103
104
|
- test/fake_app/app/views/authors/show.html.erb
|
104
105
|
- test/fake_app/app/views/authors/show.json.jbuilder
|
@@ -107,6 +108,7 @@ test_files:
|
|
107
108
|
- test/fake_app/app/views/books/_book.json.jbuilder
|
108
109
|
- test/fake_app/app/views/books/_book_locals.html.erb
|
109
110
|
- test/fake_app/app/views/books/errata.html.erb
|
111
|
+
- test/fake_app/app/views/books/errata2.html.erb
|
110
112
|
- test/fake_app/app/views/books/error.html.erb
|
111
113
|
- test/fake_app/app/views/books/index.html.erb
|
112
114
|
- test/fake_app/app/views/books/purchase.html.erb
|
data/spec/lib/decorator_spec.rb
DELETED
@@ -1,15 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
require 'spec_helper'
|
3
|
-
|
4
|
-
describe ActiveDecorator::Decorator do
|
5
|
-
subject { ActiveDecorator::Decorator.instance }
|
6
|
-
let(:book) { Book.new(title: 'Boek') }
|
7
|
-
|
8
|
-
it 'returns the object on decoration' do
|
9
|
-
subject.decorate(book).should == book
|
10
|
-
end
|
11
|
-
|
12
|
-
it "returns the object when it already is decorated on decorate" do
|
13
|
-
subject.decorate(subject.decorate(book)).should == book
|
14
|
-
end
|
15
|
-
end
|