active_decorator 0.4.0 → 0.5.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/README.md +13 -1
- data/lib/active_decorator.rb +1 -0
- data/lib/active_decorator/config.rb +20 -0
- data/lib/active_decorator/decorator.rb +1 -1
- data/lib/active_decorator/railtie.rb +4 -2
- data/lib/active_decorator/version.rb +1 -1
- data/lib/active_decorator/view_context.rb +26 -5
- data/spec/configuration_spec.rb +29 -0
- data/spec/fake_app/{authors → app/views/authors}/index.html.erb +0 -0
- data/spec/fake_app/{authors → app/views/authors}/show.html.erb +0 -0
- data/spec/fake_app/{authors → app/views/authors}/show.json.jbuilder +0 -0
- data/spec/fake_app/app/views/book_mailer/thanks.text.erb +1 -0
- data/spec/fake_app/{books → app/views/books}/_book.html.erb +0 -0
- data/spec/fake_app/{books → app/views/books}/_book.json.jbuilder +0 -0
- data/spec/fake_app/{books → app/views/books}/_book_locals.html.erb +0 -0
- data/spec/fake_app/{books → app/views/books}/index.html.erb +0 -0
- data/spec/fake_app/app/views/books/purchase.html.erb +1 -0
- data/spec/fake_app/app/views/books/show.html.erb +4 -0
- data/spec/fake_app/{movies → app/views/movies}/show.html.erb +0 -0
- data/spec/fake_app/fake_app.rb +26 -2
- data/spec/features/action_view_helpers_spec.rb +7 -1
- metadata +28 -20
- data/spec/fake_app/books/show.html.erb +0 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8c9a39129fbb4e1377d75be5b7267e7b0074c584
|
4
|
+
data.tar.gz: 7ef274bdeee7522ae1e4d2e72efcd3c1f8c1ad3a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f9b90b4730880cc22f36c7ff07225837d1d02a24cb1118f4306d27870905767a5ad93ec0ea1115b6eecb1410715a0a7a9435b5a7f7a67ee31e737e58cad867b1
|
7
|
+
data.tar.gz: f1ad27c204dcb408bbb87086da228aac33db149da114e99216c9dd3ba7ddccb245131c84b37ec050a0478f4ceed07e5f567060f1802680ef6d920d6daa72ac1a
|
data/README.md
CHANGED
@@ -14,7 +14,7 @@ A simple and Rubyish view helper for Rails 3 and Rails 4. Keep your helpers and
|
|
14
14
|
|
15
15
|
## Supported versions ##
|
16
16
|
|
17
|
-
Rails 3.0.x, 3.1.x, 3.2.x, 4.0.x,
|
17
|
+
Rails 3.0.x, 3.1.x, 3.2.x, 4.0.x, 4.1.x, 4.2.x, and 5.0 (edge)
|
18
18
|
|
19
19
|
|
20
20
|
## Supported ORMs ##
|
@@ -63,6 +63,18 @@ end
|
|
63
63
|
<% end %>
|
64
64
|
```
|
65
65
|
|
66
|
+
## Configuring the decorator suffix
|
67
|
+
|
68
|
+
By default, ActiveDecorator searches a decorator module named `target_class.name + "Decorator"`
|
69
|
+
|
70
|
+
If you would like a different rule, you can configure in your initializer.
|
71
|
+
|
72
|
+
```ruby
|
73
|
+
ActiveDecorator.configure do |config|
|
74
|
+
config.decorator_suffix = 'Presenter'
|
75
|
+
end
|
76
|
+
```
|
77
|
+
|
66
78
|
## Contributing to ActiveDecorator ##
|
67
79
|
|
68
80
|
* Fork, fix, then send me a pull request.
|
data/lib/active_decorator.rb
CHANGED
@@ -0,0 +1,20 @@
|
|
1
|
+
module ActiveDecorator
|
2
|
+
def self.configure(&block)
|
3
|
+
yield @config ||= ActiveDecorator::Configuration.new
|
4
|
+
end
|
5
|
+
|
6
|
+
def self.config
|
7
|
+
@config
|
8
|
+
end
|
9
|
+
|
10
|
+
# need a Class for 3.0
|
11
|
+
class Configuration #:nodoc:
|
12
|
+
include ActiveSupport::Configurable
|
13
|
+
|
14
|
+
config_accessor :decorator_suffix
|
15
|
+
end
|
16
|
+
|
17
|
+
configure do |config|
|
18
|
+
config.decorator_suffix = 'Decorator'
|
19
|
+
end
|
20
|
+
end
|
@@ -37,7 +37,7 @@ module ActiveDecorator
|
|
37
37
|
def decorator_for(model_class)
|
38
38
|
return @@decorators[model_class] if @@decorators.has_key? model_class
|
39
39
|
|
40
|
-
decorator_name = "#{model_class.name}
|
40
|
+
decorator_name = "#{model_class.name}#{ActiveDecorator.config.decorator_suffix}"
|
41
41
|
d = decorator_name.constantize
|
42
42
|
unless Class === d
|
43
43
|
d.send :include, ActiveDecorator::Helpers
|
@@ -12,8 +12,10 @@ module ActiveDecorator
|
|
12
12
|
ActionController::Base.send :include, ActiveDecorator::ViewContext::Filter
|
13
13
|
end
|
14
14
|
ActiveSupport.on_load(:action_mailer) do
|
15
|
-
|
16
|
-
|
15
|
+
if ActionMailer::Base.respond_to? :before_action
|
16
|
+
require 'active_decorator/monkey/abstract_controller/rendering'
|
17
|
+
ActionMailer::Base.send :include, ActiveDecorator::ViewContext::Filter
|
18
|
+
end
|
17
19
|
end
|
18
20
|
end
|
19
21
|
end
|
@@ -2,11 +2,16 @@ module ActiveDecorator
|
|
2
2
|
module ViewContext
|
3
3
|
class << self
|
4
4
|
def current
|
5
|
-
Thread.current[:
|
5
|
+
Thread.current[:active_decorator_view_contexts].last
|
6
6
|
end
|
7
7
|
|
8
|
-
def
|
9
|
-
Thread.current[:
|
8
|
+
def push(view_context)
|
9
|
+
Thread.current[:active_decorator_view_contexts] ||= []
|
10
|
+
Thread.current[:active_decorator_view_contexts] << view_context
|
11
|
+
end
|
12
|
+
|
13
|
+
def pop
|
14
|
+
Thread.current[:active_decorator_view_contexts].pop if Thread.current[:active_decorator_view_contexts]
|
10
15
|
end
|
11
16
|
end
|
12
17
|
|
@@ -14,8 +19,24 @@ module ActiveDecorator
|
|
14
19
|
extend ActiveSupport::Concern
|
15
20
|
|
16
21
|
included do
|
17
|
-
|
18
|
-
|
22
|
+
if Rails::VERSION::MAJOR >= 4
|
23
|
+
around_action do |controller, blk|
|
24
|
+
begin
|
25
|
+
ActiveDecorator::ViewContext.push controller.view_context
|
26
|
+
blk.call
|
27
|
+
ensure
|
28
|
+
ActiveDecorator::ViewContext.pop
|
29
|
+
end
|
30
|
+
end
|
31
|
+
else
|
32
|
+
around_filter do |controller, blk|
|
33
|
+
begin
|
34
|
+
ActiveDecorator::ViewContext.push controller.view_context
|
35
|
+
blk.call
|
36
|
+
ensure
|
37
|
+
ActiveDecorator::ViewContext.pop
|
38
|
+
end
|
39
|
+
end
|
19
40
|
end
|
20
41
|
end
|
21
42
|
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
Comic = Struct.new(:title, :price)
|
4
|
+
|
5
|
+
module ComicPresenter
|
6
|
+
def price
|
7
|
+
"$#{super}"
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
describe ActiveDecorator::Configuration do
|
12
|
+
let(:comic) { ActiveDecorator::Decorator.instance.decorate(Comic.new("amatsuda's (Poignant) Guide to ActiveDecorator", 3)) }
|
13
|
+
|
14
|
+
context 'with a custom decorator_suffix' do
|
15
|
+
before do
|
16
|
+
ActiveDecorator.configure do |config|
|
17
|
+
config.decorator_suffix = 'Presenter'
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
after do
|
22
|
+
ActiveDecorator.configure do |config|
|
23
|
+
config.decorator_suffix = 'Decorator'
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
specify { comic.price.should == '$3' }
|
28
|
+
end
|
29
|
+
end
|
File without changes
|
File without changes
|
File without changes
|
@@ -0,0 +1 @@
|
|
1
|
+
Thank you for purchasing <%= @book.upcased_title %>.
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
@@ -0,0 +1 @@
|
|
1
|
+
done
|
File without changes
|
data/spec/fake_app/fake_app.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
require 'active_record'
|
2
2
|
require 'action_controller/railtie'
|
3
|
+
require 'action_mailer/railtie'
|
3
4
|
require 'action_view/railtie'
|
4
5
|
require 'jbuilder'
|
5
6
|
|
@@ -13,6 +14,9 @@ module ActiveDecoratorTestApp
|
|
13
14
|
config.active_support.deprecation = :log
|
14
15
|
config.eager_load = false
|
15
16
|
config.action_dispatch.show_exceptions = false
|
17
|
+
config.root = File.dirname(__FILE__)
|
18
|
+
|
19
|
+
config.action_mailer.delivery_method = :test
|
16
20
|
end
|
17
21
|
end
|
18
22
|
ActiveDecoratorTestApp::Application.initialize!
|
@@ -20,7 +24,11 @@ ActiveDecoratorTestApp::Application.initialize!
|
|
20
24
|
# routes
|
21
25
|
ActiveDecoratorTestApp::Application.routes.draw do
|
22
26
|
resources :authors, :only => [:index, :show] do
|
23
|
-
resources :books, :only => [:index, :show]
|
27
|
+
resources :books, :only => [:index, :show] do
|
28
|
+
member do
|
29
|
+
post :purchase
|
30
|
+
end
|
31
|
+
end
|
24
32
|
end
|
25
33
|
resources :movies, :only => :show
|
26
34
|
end
|
@@ -58,7 +66,7 @@ module BookDecorator
|
|
58
66
|
end
|
59
67
|
|
60
68
|
def link
|
61
|
-
link_to title, "#{request.protocol}#{request.host_with_port}/assets/sample.png"
|
69
|
+
link_to title, "#{request.protocol}#{request.host_with_port}/assets/sample.png", :class => 'title'
|
62
70
|
end
|
63
71
|
|
64
72
|
def cover_image
|
@@ -105,6 +113,14 @@ class BooksController < ApplicationController
|
|
105
113
|
def show
|
106
114
|
@book = Author.find(params[:author_id]).books.find(params[:id])
|
107
115
|
end
|
116
|
+
|
117
|
+
def purchase
|
118
|
+
@book = Author.find(params[:author_id]).books.find(params[:id])
|
119
|
+
|
120
|
+
@view_context_before_sending_mail = ActiveDecorator::ViewContext.current
|
121
|
+
BookMailer.thanks(@book).deliver
|
122
|
+
raise 'Wrong ViewContext!' if ActiveDecorator::ViewContext.current != @view_context_before_sending_mail
|
123
|
+
end
|
108
124
|
end
|
109
125
|
class MoviesController < ApplicationController
|
110
126
|
def show
|
@@ -112,6 +128,14 @@ class MoviesController < ApplicationController
|
|
112
128
|
end
|
113
129
|
end
|
114
130
|
|
131
|
+
# mailers
|
132
|
+
class BookMailer < ActionMailer::Base
|
133
|
+
def thanks(book)
|
134
|
+
@book = book
|
135
|
+
mail :from => 'nobody@example.com', :to => 'test@example.com', :subject => 'Thanks'
|
136
|
+
end
|
137
|
+
end
|
138
|
+
|
115
139
|
# migrations
|
116
140
|
class CreateAllTables < ActiveRecord::Migration
|
117
141
|
def self.up
|
@@ -8,9 +8,15 @@ feature 'fallback to helpers' do
|
|
8
8
|
|
9
9
|
scenario 'invoking action_view helper methods' do
|
10
10
|
visit "/authors/#{@rhg.author.id}/books/#{@rhg.id}"
|
11
|
-
within 'a' do
|
11
|
+
within 'a.title' do
|
12
12
|
page.should have_content 'RHG'
|
13
13
|
end
|
14
14
|
page.should have_css('img')
|
15
15
|
end
|
16
|
+
|
17
|
+
scenario 'make sure that action_view + action_mailer works' do
|
18
|
+
visit "/authors/#{@rhg.author.id}/books/#{@rhg.id}"
|
19
|
+
click_link 'purchase'
|
20
|
+
page.should have_content 'done'
|
21
|
+
end
|
16
22
|
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: 0.5.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: 2015-
|
11
|
+
date: 2015-02-21 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: A simple and Rubyish view helper for Rails 3
|
14
14
|
email:
|
@@ -32,6 +32,7 @@ files:
|
|
32
32
|
- gemfiles/Gemfile-rails.4.1.x
|
33
33
|
- gemfiles/Gemfile-rails.4.2.x
|
34
34
|
- lib/active_decorator.rb
|
35
|
+
- lib/active_decorator/config.rb
|
35
36
|
- lib/active_decorator/decorator.rb
|
36
37
|
- lib/active_decorator/helpers.rb
|
37
38
|
- lib/active_decorator/monkey/abstract_controller/rendering.rb
|
@@ -45,17 +46,20 @@ files:
|
|
45
46
|
- lib/generators/rspec/templates/decorator_spec.rb
|
46
47
|
- lib/generators/test_unit/decorator_generator.rb
|
47
48
|
- lib/generators/test_unit/templates/decorator_test.rb
|
49
|
+
- spec/configuration_spec.rb
|
48
50
|
- spec/controllers/fake_detection_spec.rb
|
49
|
-
- spec/fake_app/authors/index.html.erb
|
50
|
-
- spec/fake_app/authors/show.html.erb
|
51
|
-
- spec/fake_app/authors/show.json.jbuilder
|
52
|
-
- spec/fake_app/
|
53
|
-
- spec/fake_app/books/_book.
|
54
|
-
- spec/fake_app/books/
|
55
|
-
- spec/fake_app/books/
|
56
|
-
- spec/fake_app/books/
|
51
|
+
- spec/fake_app/app/views/authors/index.html.erb
|
52
|
+
- spec/fake_app/app/views/authors/show.html.erb
|
53
|
+
- spec/fake_app/app/views/authors/show.json.jbuilder
|
54
|
+
- spec/fake_app/app/views/book_mailer/thanks.text.erb
|
55
|
+
- spec/fake_app/app/views/books/_book.html.erb
|
56
|
+
- spec/fake_app/app/views/books/_book.json.jbuilder
|
57
|
+
- spec/fake_app/app/views/books/_book_locals.html.erb
|
58
|
+
- spec/fake_app/app/views/books/index.html.erb
|
59
|
+
- spec/fake_app/app/views/books/purchase.html.erb
|
60
|
+
- spec/fake_app/app/views/books/show.html.erb
|
61
|
+
- spec/fake_app/app/views/movies/show.html.erb
|
57
62
|
- spec/fake_app/fake_app.rb
|
58
|
-
- spec/fake_app/movies/show.html.erb
|
59
63
|
- spec/features/action_view_helpers_spec.rb
|
60
64
|
- spec/features/controller_ivar_spec.rb
|
61
65
|
- spec/features/jbuilder_spec.rb
|
@@ -85,19 +89,23 @@ signing_key:
|
|
85
89
|
specification_version: 4
|
86
90
|
summary: A simple and Rubyish view helper for Rails 3
|
87
91
|
test_files:
|
92
|
+
- spec/configuration_spec.rb
|
88
93
|
- spec/controllers/fake_detection_spec.rb
|
89
|
-
- spec/fake_app/authors/index.html.erb
|
90
|
-
- spec/fake_app/authors/show.html.erb
|
91
|
-
- spec/fake_app/authors/show.json.jbuilder
|
92
|
-
- spec/fake_app/
|
93
|
-
- spec/fake_app/books/_book.
|
94
|
-
- spec/fake_app/books/
|
95
|
-
- spec/fake_app/books/
|
96
|
-
- spec/fake_app/books/
|
94
|
+
- spec/fake_app/app/views/authors/index.html.erb
|
95
|
+
- spec/fake_app/app/views/authors/show.html.erb
|
96
|
+
- spec/fake_app/app/views/authors/show.json.jbuilder
|
97
|
+
- spec/fake_app/app/views/book_mailer/thanks.text.erb
|
98
|
+
- spec/fake_app/app/views/books/_book.html.erb
|
99
|
+
- spec/fake_app/app/views/books/_book.json.jbuilder
|
100
|
+
- spec/fake_app/app/views/books/_book_locals.html.erb
|
101
|
+
- spec/fake_app/app/views/books/index.html.erb
|
102
|
+
- spec/fake_app/app/views/books/purchase.html.erb
|
103
|
+
- spec/fake_app/app/views/books/show.html.erb
|
104
|
+
- spec/fake_app/app/views/movies/show.html.erb
|
97
105
|
- spec/fake_app/fake_app.rb
|
98
|
-
- spec/fake_app/movies/show.html.erb
|
99
106
|
- spec/features/action_view_helpers_spec.rb
|
100
107
|
- spec/features/controller_ivar_spec.rb
|
101
108
|
- spec/features/jbuilder_spec.rb
|
102
109
|
- spec/features/partial_spec.rb
|
103
110
|
- spec/spec_helper.rb
|
111
|
+
has_rdoc:
|