draper_new 3.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +16 -0
- data/.rspec +2 -0
- data/.travis.yml +15 -0
- data/.yardopts +1 -0
- data/CHANGELOG.md +230 -0
- data/CONTRIBUTING.md +20 -0
- data/Gemfile +16 -0
- data/Guardfile +26 -0
- data/LICENSE +7 -0
- data/README.md +587 -0
- data/Rakefile +69 -0
- data/draper_new.gemspec +31 -0
- data/gemfiles/4.0.gemfile +3 -0
- data/gemfiles/4.1.gemfile +3 -0
- data/gemfiles/4.2.6.gemfile +3 -0
- data/gemfiles/4.2.gemfile +3 -0
- data/lib/draper.rb +63 -0
- data/lib/draper/automatic_delegation.rb +56 -0
- data/lib/draper/collection_decorator.rb +100 -0
- data/lib/draper/decoratable.rb +96 -0
- data/lib/draper/decoratable/equality.rb +26 -0
- data/lib/draper/decorated_association.rb +35 -0
- data/lib/draper/decorates_assigned.rb +44 -0
- data/lib/draper/decorator.rb +293 -0
- data/lib/draper/delegation.rb +13 -0
- data/lib/draper/factory.rb +91 -0
- data/lib/draper/finders.rb +37 -0
- data/lib/draper/helper_proxy.rb +44 -0
- data/lib/draper/helper_support.rb +5 -0
- data/lib/draper/lazy_helpers.rb +15 -0
- data/lib/draper/railtie.rb +70 -0
- data/lib/draper/tasks/test.rake +22 -0
- data/lib/draper/test/devise_helper.rb +30 -0
- data/lib/draper/test/minitest_integration.rb +6 -0
- data/lib/draper/test/rspec_integration.rb +20 -0
- data/lib/draper/test_case.rb +42 -0
- data/lib/draper/undecorate.rb +9 -0
- data/lib/draper/version.rb +3 -0
- data/lib/draper/view_context.rb +104 -0
- data/lib/draper/view_context/build_strategy.rb +48 -0
- data/lib/draper/view_helpers.rb +37 -0
- data/lib/generators/controller_override.rb +17 -0
- data/lib/generators/mini_test/decorator_generator.rb +20 -0
- data/lib/generators/mini_test/templates/decorator_spec.rb +4 -0
- data/lib/generators/mini_test/templates/decorator_test.rb +4 -0
- data/lib/generators/rails/decorator_generator.rb +36 -0
- data/lib/generators/rails/templates/decorator.rb +19 -0
- data/lib/generators/rspec/decorator_generator.rb +9 -0
- data/lib/generators/rspec/templates/decorator_spec.rb +4 -0
- data/lib/generators/test_unit/decorator_generator.rb +9 -0
- data/lib/generators/test_unit/templates/decorator_test.rb +4 -0
- data/spec/draper/collection_decorator_spec.rb +307 -0
- data/spec/draper/decoratable/equality_spec.rb +10 -0
- data/spec/draper/decoratable_spec.rb +202 -0
- data/spec/draper/decorated_association_spec.rb +84 -0
- data/spec/draper/decorates_assigned_spec.rb +71 -0
- data/spec/draper/decorator_spec.rb +816 -0
- data/spec/draper/factory_spec.rb +251 -0
- data/spec/draper/finders_spec.rb +166 -0
- data/spec/draper/helper_proxy_spec.rb +61 -0
- data/spec/draper/lazy_helpers_spec.rb +21 -0
- data/spec/draper/undecorate_spec.rb +19 -0
- data/spec/draper/view_context/build_strategy_spec.rb +116 -0
- data/spec/draper/view_context_spec.rb +154 -0
- data/spec/draper/view_helpers_spec.rb +8 -0
- data/spec/dummy/.rspec +2 -0
- data/spec/dummy/Rakefile +7 -0
- data/spec/dummy/app/controllers/application_controller.rb +4 -0
- data/spec/dummy/app/controllers/localized_urls.rb +5 -0
- data/spec/dummy/app/controllers/posts_controller.rb +20 -0
- data/spec/dummy/app/decorators/mongoid_post_decorator.rb +4 -0
- data/spec/dummy/app/decorators/post_decorator.rb +60 -0
- data/spec/dummy/app/helpers/application_helper.rb +5 -0
- data/spec/dummy/app/mailers/application_mailer.rb +3 -0
- data/spec/dummy/app/mailers/post_mailer.rb +19 -0
- data/spec/dummy/app/models/admin.rb +5 -0
- data/spec/dummy/app/models/mongoid_post.rb +5 -0
- data/spec/dummy/app/models/post.rb +3 -0
- data/spec/dummy/app/models/user.rb +5 -0
- data/spec/dummy/app/views/layouts/application.html.erb +11 -0
- data/spec/dummy/app/views/post_mailer/decorated_email.html.erb +1 -0
- data/spec/dummy/app/views/posts/_post.html.erb +40 -0
- data/spec/dummy/app/views/posts/show.html.erb +1 -0
- data/spec/dummy/bin/rails +4 -0
- data/spec/dummy/config.ru +4 -0
- data/spec/dummy/config/application.rb +71 -0
- data/spec/dummy/config/boot.rb +5 -0
- data/spec/dummy/config/database.yml +25 -0
- data/spec/dummy/config/environment.rb +5 -0
- data/spec/dummy/config/environments/development.rb +33 -0
- data/spec/dummy/config/environments/production.rb +57 -0
- data/spec/dummy/config/environments/test.rb +31 -0
- data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/spec/dummy/config/initializers/inflections.rb +15 -0
- data/spec/dummy/config/initializers/mime_types.rb +5 -0
- data/spec/dummy/config/initializers/secret_token.rb +8 -0
- data/spec/dummy/config/initializers/session_store.rb +8 -0
- data/spec/dummy/config/locales/en.yml +5 -0
- data/spec/dummy/config/mongoid.yml +79 -0
- data/spec/dummy/config/routes.rb +9 -0
- data/spec/dummy/db/migrate/20121019115657_create_posts.rb +8 -0
- data/spec/dummy/db/schema.rb +21 -0
- data/spec/dummy/db/seeds.rb +2 -0
- data/spec/dummy/fast_spec/post_decorator_spec.rb +37 -0
- data/spec/dummy/lib/tasks/test.rake +16 -0
- data/spec/dummy/public/404.html +26 -0
- data/spec/dummy/public/422.html +26 -0
- data/spec/dummy/public/500.html +25 -0
- data/spec/dummy/public/favicon.ico +0 -0
- data/spec/dummy/script/rails +6 -0
- data/spec/dummy/spec/decorators/active_model_serializers_spec.rb +16 -0
- data/spec/dummy/spec/decorators/devise_spec.rb +64 -0
- data/spec/dummy/spec/decorators/helpers_spec.rb +21 -0
- data/spec/dummy/spec/decorators/post_decorator_spec.rb +66 -0
- data/spec/dummy/spec/decorators/spec_type_spec.rb +7 -0
- data/spec/dummy/spec/decorators/view_context_spec.rb +22 -0
- data/spec/dummy/spec/mailers/post_mailer_spec.rb +33 -0
- data/spec/dummy/spec/models/mongoid_post_spec.rb +8 -0
- data/spec/dummy/spec/models/post_spec.rb +6 -0
- data/spec/dummy/spec/shared_examples/decoratable.rb +24 -0
- data/spec/dummy/spec/spec_helper.rb +8 -0
- data/spec/dummy/test/decorators/minitest/devise_test.rb +64 -0
- data/spec/dummy/test/decorators/minitest/helpers_test.rb +21 -0
- data/spec/dummy/test/decorators/minitest/spec_type_test.rb +52 -0
- data/spec/dummy/test/decorators/minitest/view_context_test.rb +24 -0
- data/spec/dummy/test/decorators/test_unit/devise_test.rb +64 -0
- data/spec/dummy/test/decorators/test_unit/helpers_test.rb +21 -0
- data/spec/dummy/test/decorators/test_unit/view_context_test.rb +24 -0
- data/spec/dummy/test/minitest_helper.rb +2 -0
- data/spec/dummy/test/test_helper.rb +3 -0
- data/spec/generators/controller/controller_generator_spec.rb +22 -0
- data/spec/generators/decorator/decorator_generator_spec.rb +92 -0
- data/spec/integration/integration_spec.rb +66 -0
- data/spec/performance/active_record.rb +4 -0
- data/spec/performance/benchmark.rb +55 -0
- data/spec/performance/decorators.rb +45 -0
- data/spec/performance/models.rb +20 -0
- data/spec/spec_helper.rb +41 -0
- data/spec/support/dummy_app.rb +85 -0
- data/spec/support/matchers/have_text.rb +50 -0
- data/spec/support/shared_examples/decoratable_equality.rb +40 -0
- data/spec/support/shared_examples/view_helpers.rb +39 -0
- metadata +420 -0
@@ -0,0 +1,37 @@
|
|
1
|
+
module Draper
|
2
|
+
# Provides automatically-decorated finder methods for your decorators. You
|
3
|
+
# do not have to extend this module directly; it is extended by
|
4
|
+
# {Decorator.decorates_finders}.
|
5
|
+
module Finders
|
6
|
+
|
7
|
+
def find(id, options = {})
|
8
|
+
decorate(object_class.find(id), options)
|
9
|
+
end
|
10
|
+
|
11
|
+
def all(options = {})
|
12
|
+
decorate_collection(object_class.all, options)
|
13
|
+
end
|
14
|
+
|
15
|
+
def first(options = {})
|
16
|
+
decorate(object_class.first, options)
|
17
|
+
end
|
18
|
+
|
19
|
+
def last(options = {})
|
20
|
+
decorate(object_class.last, options)
|
21
|
+
end
|
22
|
+
|
23
|
+
# Decorates dynamic finder methods (`find_all_by_` and friends).
|
24
|
+
def method_missing(method, *args, &block)
|
25
|
+
return super unless method =~ /^find_(all_|last_|or_(initialize_|create_))?by_/
|
26
|
+
|
27
|
+
result = object_class.send(method, *args, &block)
|
28
|
+
options = args.extract_options!
|
29
|
+
|
30
|
+
if method =~ /^find_all/
|
31
|
+
decorate_collection(result, options)
|
32
|
+
else
|
33
|
+
decorate(result, options)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
module Draper
|
2
|
+
# Provides access to helper methods - both Rails built-in helpers, and those
|
3
|
+
# defined in your application.
|
4
|
+
class HelperProxy
|
5
|
+
|
6
|
+
# @overload initialize(view_context)
|
7
|
+
def initialize(view_context = nil)
|
8
|
+
view_context ||= current_view_context # backwards compatibility
|
9
|
+
|
10
|
+
@view_context = view_context
|
11
|
+
end
|
12
|
+
|
13
|
+
# Sends helper methods to the view context.
|
14
|
+
def method_missing(method, *args, &block)
|
15
|
+
self.class.define_proxy method
|
16
|
+
send(method, *args, &block)
|
17
|
+
end
|
18
|
+
|
19
|
+
# Checks if the context responds to an instance method, or is able to
|
20
|
+
# proxy it to the view context.
|
21
|
+
def respond_to_missing?(method, include_private = false)
|
22
|
+
super || view_context.respond_to?(method)
|
23
|
+
end
|
24
|
+
|
25
|
+
delegate :capture, to: :view_context
|
26
|
+
|
27
|
+
protected
|
28
|
+
|
29
|
+
attr_reader :view_context
|
30
|
+
|
31
|
+
private
|
32
|
+
|
33
|
+
def self.define_proxy(name)
|
34
|
+
define_method name do |*args, &block|
|
35
|
+
view_context.send(name, *args, &block)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def current_view_context
|
40
|
+
ActiveSupport::Deprecation.warn("wrong number of arguments (0 for 1) passed to Draper::HelperProxy.new", caller[1..-1])
|
41
|
+
Draper::ViewContext.current.view_context
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module Draper
|
2
|
+
# Include this module in your decorators to get direct access to the helpers
|
3
|
+
# so that you can stop typing `h.` everywhere, at the cost of mixing in a
|
4
|
+
# bazillion methods.
|
5
|
+
module LazyHelpers
|
6
|
+
|
7
|
+
# Sends missing methods to the {HelperProxy}.
|
8
|
+
def method_missing(method, *args, &block)
|
9
|
+
helpers.send(method, *args, &block)
|
10
|
+
rescue NoMethodError
|
11
|
+
super
|
12
|
+
end
|
13
|
+
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,70 @@
|
|
1
|
+
require 'rails/railtie'
|
2
|
+
|
3
|
+
module ActiveModel
|
4
|
+
class Railtie < Rails::Railtie
|
5
|
+
generators do |app|
|
6
|
+
app ||= Rails.application # Rails 3.0.x does not yield `app`
|
7
|
+
|
8
|
+
Rails::Generators.configure! app.config.generators
|
9
|
+
require_relative '../generators/controller_override'
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
module Draper
|
15
|
+
class Railtie < Rails::Railtie
|
16
|
+
|
17
|
+
config.after_initialize do |app|
|
18
|
+
app.config.paths.add 'app/decorators', eager_load: true
|
19
|
+
|
20
|
+
if Rails.env.test?
|
21
|
+
require 'draper/test_case'
|
22
|
+
require 'draper/test/rspec_integration' if defined?(RSpec) and RSpec.respond_to?(:configure)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
initializer "draper.setup_action_controller" do |app|
|
27
|
+
ActiveSupport.on_load :action_controller do
|
28
|
+
Draper.setup_action_controller self
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
initializer "draper.setup_action_mailer" do |app|
|
33
|
+
ActiveSupport.on_load :action_mailer do
|
34
|
+
Draper.setup_action_mailer self
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
initializer "draper.setup_orm" do |app|
|
39
|
+
[:active_record, :mongoid].each do |orm|
|
40
|
+
ActiveSupport.on_load orm do
|
41
|
+
Draper.setup_orm self
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
initializer "draper.setup_active_model_serializers" do |app|
|
47
|
+
ActiveSupport.on_load :active_model_serializers do
|
48
|
+
if defined?(ActiveModel::ArraySerializerSupport)
|
49
|
+
Draper::CollectionDecorator.send :include, ActiveModel::ArraySerializerSupport
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
initializer "draper.minitest-rails_integration" do |app|
|
55
|
+
ActiveSupport.on_load :minitest do
|
56
|
+
require "draper/test/minitest_integration"
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
console do
|
61
|
+
require 'action_controller/test_case'
|
62
|
+
ApplicationController.new.view_context
|
63
|
+
Draper::ViewContext.build
|
64
|
+
end
|
65
|
+
|
66
|
+
rake_tasks do
|
67
|
+
Dir[File.join(File.dirname(__FILE__),'tasks/*.rake')].each { |f| load f }
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'rake/testtask'
|
2
|
+
|
3
|
+
test_task = if Rails.version.to_f < 3.2
|
4
|
+
require 'rails/test_unit/railtie'
|
5
|
+
Rake::TestTask
|
6
|
+
else
|
7
|
+
require 'rails/test_unit/sub_test_task'
|
8
|
+
Rails::SubTestTask
|
9
|
+
end
|
10
|
+
|
11
|
+
namespace :test do
|
12
|
+
test_task.new(:decorators => "test:prepare") do |t|
|
13
|
+
t.libs << "test"
|
14
|
+
t.pattern = "test/decorators/**/*_test.rb"
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
if Rails.version.to_f < 4.2 && Rake::Task.task_defined?('test:run')
|
19
|
+
Rake::Task['test:run'].enhance do
|
20
|
+
Rake::Task['test:decorators'].invoke
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module Draper
|
2
|
+
module DeviseHelper
|
3
|
+
def sign_in(resource_or_scope, resource = nil)
|
4
|
+
scope = begin
|
5
|
+
Devise::Mapping.find_scope!(resource_or_scope)
|
6
|
+
rescue RuntimeError => e
|
7
|
+
# Draper 1.0 didn't require the mapping to exist
|
8
|
+
ActiveSupport::Deprecation.warn("#{e.message}.\nUse `sign_in :user, mock_user` instead.", caller)
|
9
|
+
:user
|
10
|
+
end
|
11
|
+
|
12
|
+
_stub_current_scope scope, resource || resource_or_scope
|
13
|
+
end
|
14
|
+
|
15
|
+
def sign_out(resource_or_scope)
|
16
|
+
scope = Devise::Mapping.find_scope!(resource_or_scope)
|
17
|
+
_stub_current_scope scope, nil
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def _stub_current_scope(scope, resource)
|
23
|
+
Draper::ViewContext.current.controller.singleton_class.class_eval do
|
24
|
+
define_method "current_#{scope}" do
|
25
|
+
resource
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module Draper
|
2
|
+
module DecoratorExampleGroup
|
3
|
+
include Draper::TestCase::Behavior
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
included { metadata[:type] = :decorator }
|
7
|
+
end
|
8
|
+
|
9
|
+
RSpec.configure do |config|
|
10
|
+
if RSpec::Core::Version::STRING.starts_with?("3")
|
11
|
+
config.include DecoratorExampleGroup, file_path: %r{spec/decorators}, type: :decorator
|
12
|
+
else
|
13
|
+
config.include DecoratorExampleGroup, example_group: {file_path: %r{spec/decorators}}, type: :decorator
|
14
|
+
end
|
15
|
+
|
16
|
+
[:decorator, :controller, :mailer].each do |type|
|
17
|
+
config.before(:each, type: type) { Draper::ViewContext.clear! }
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
module Draper
|
2
|
+
require 'active_support/test_case'
|
3
|
+
|
4
|
+
class TestCase < ::ActiveSupport::TestCase
|
5
|
+
module ViewContextTeardown
|
6
|
+
def teardown
|
7
|
+
super
|
8
|
+
Draper::ViewContext.clear!
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
module Behavior
|
13
|
+
if defined?(::Devise)
|
14
|
+
require 'draper/test/devise_helper'
|
15
|
+
include Draper::DeviseHelper
|
16
|
+
end
|
17
|
+
|
18
|
+
if defined?(::Capybara) && (defined?(::RSpec) || defined?(::MiniTest::Matchers))
|
19
|
+
require 'capybara/rspec/matchers'
|
20
|
+
include ::Capybara::RSpecMatchers
|
21
|
+
end
|
22
|
+
|
23
|
+
include Draper::ViewHelpers::ClassMethods
|
24
|
+
alias_method :helper, :helpers
|
25
|
+
end
|
26
|
+
|
27
|
+
include Behavior
|
28
|
+
include ViewContextTeardown
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
if defined?(ActionController::TestCase)
|
33
|
+
class ActionController::TestCase
|
34
|
+
include Draper::TestCase::ViewContextTeardown
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
if defined?(ActionMailer::TestCase)
|
39
|
+
class ActionMailer::TestCase
|
40
|
+
include Draper::TestCase::ViewContextTeardown
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,104 @@
|
|
1
|
+
require 'draper/view_context/build_strategy'
|
2
|
+
require 'request_store'
|
3
|
+
|
4
|
+
module Draper
|
5
|
+
module ViewContext
|
6
|
+
# Hooks into a controller or mailer to save the view context in {current}.
|
7
|
+
def view_context
|
8
|
+
super.tap do |context|
|
9
|
+
Draper::ViewContext.current = context
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
# Set the current controller
|
14
|
+
def activate_draper
|
15
|
+
Draper::ViewContext.controller = self
|
16
|
+
end
|
17
|
+
|
18
|
+
# Returns the current controller.
|
19
|
+
def self.controller
|
20
|
+
RequestStore.store[:current_controller]
|
21
|
+
end
|
22
|
+
|
23
|
+
# Sets the current controller.
|
24
|
+
def self.controller=(controller)
|
25
|
+
RequestStore.store[:current_controller] = controller
|
26
|
+
end
|
27
|
+
|
28
|
+
# Returns the current view context, or builds one if none is saved.
|
29
|
+
#
|
30
|
+
# @return [HelperProxy]
|
31
|
+
def self.current
|
32
|
+
RequestStore.store.fetch(:current_view_context) { build! }
|
33
|
+
end
|
34
|
+
|
35
|
+
# Sets the current view context.
|
36
|
+
def self.current=(view_context)
|
37
|
+
RequestStore.store[:current_view_context] = Draper::HelperProxy.new(view_context)
|
38
|
+
end
|
39
|
+
|
40
|
+
# Clears the saved controller and view context.
|
41
|
+
def self.clear!
|
42
|
+
RequestStore.store.delete :current_controller
|
43
|
+
RequestStore.store.delete :current_view_context
|
44
|
+
end
|
45
|
+
|
46
|
+
# Builds a new view context for usage in tests. See {test_strategy} for
|
47
|
+
# details of how the view context is built.
|
48
|
+
def self.build
|
49
|
+
build_strategy.call
|
50
|
+
end
|
51
|
+
|
52
|
+
# Builds a new view context and sets it as the current view context.
|
53
|
+
#
|
54
|
+
# @return [HelperProxy]
|
55
|
+
def self.build!
|
56
|
+
# send because we want to return the HelperProxy returned from #current=
|
57
|
+
send :current=, build
|
58
|
+
end
|
59
|
+
|
60
|
+
# Configures the strategy used to build view contexts in tests, which
|
61
|
+
# defaults to `:full` if `test_strategy` has not been called. Evaluates
|
62
|
+
# the block, if given, in the context of the view context's class.
|
63
|
+
#
|
64
|
+
# @example Pass a block to add helper methods to the view context:
|
65
|
+
# Draper::ViewContext.test_strategy :fast do
|
66
|
+
# include ApplicationHelper
|
67
|
+
# end
|
68
|
+
#
|
69
|
+
# @param [:full, :fast] name
|
70
|
+
# the strategy to use:
|
71
|
+
#
|
72
|
+
# `:full` - build a fully-working view context. Your Rails environment
|
73
|
+
# must be loaded, including your `ApplicationController`.
|
74
|
+
#
|
75
|
+
# `:fast` - build a minimal view context in tests, with no dependencies
|
76
|
+
# on other components of your application.
|
77
|
+
def self.test_strategy(name, &block)
|
78
|
+
@build_strategy = Draper::ViewContext::BuildStrategy.new(name, &block)
|
79
|
+
end
|
80
|
+
|
81
|
+
# @private
|
82
|
+
def self.build_strategy
|
83
|
+
@build_strategy ||= Draper::ViewContext::BuildStrategy.new(:full)
|
84
|
+
end
|
85
|
+
|
86
|
+
# @deprecated Use {controller} instead.
|
87
|
+
def self.current_controller
|
88
|
+
ActiveSupport::Deprecation.warn("Draper::ViewContext.current_controller is deprecated (use controller instead)", caller)
|
89
|
+
self.controller || ApplicationController.new
|
90
|
+
end
|
91
|
+
|
92
|
+
# @deprecated Use {controller=} instead.
|
93
|
+
def self.current_controller=(controller)
|
94
|
+
ActiveSupport::Deprecation.warn("Draper::ViewContext.current_controller= is deprecated (use controller instead)", caller)
|
95
|
+
self.controller = controller
|
96
|
+
end
|
97
|
+
|
98
|
+
# @deprecated Use {build} instead.
|
99
|
+
def self.build_view_context
|
100
|
+
ActiveSupport::Deprecation.warn("Draper::ViewContext.build_view_context is deprecated (use build instead)", caller)
|
101
|
+
build
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
module Draper
|
2
|
+
module ViewContext
|
3
|
+
# @private
|
4
|
+
module BuildStrategy
|
5
|
+
|
6
|
+
def self.new(name, &block)
|
7
|
+
const_get(name.to_s.camelize).new(&block)
|
8
|
+
end
|
9
|
+
|
10
|
+
class Fast
|
11
|
+
def initialize(&block)
|
12
|
+
@view_context_class = Class.new(ActionView::Base, &block)
|
13
|
+
end
|
14
|
+
|
15
|
+
def call
|
16
|
+
view_context_class.new
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
attr_reader :view_context_class
|
22
|
+
end
|
23
|
+
|
24
|
+
class Full
|
25
|
+
def initialize(&block)
|
26
|
+
@block = block
|
27
|
+
end
|
28
|
+
|
29
|
+
def call
|
30
|
+
controller.view_context.tap do |context|
|
31
|
+
context.singleton_class.class_eval(&block) if block
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
private
|
36
|
+
|
37
|
+
attr_reader :block
|
38
|
+
|
39
|
+
def controller
|
40
|
+
(Draper::ViewContext.controller || ApplicationController.new).tap do |controller|
|
41
|
+
controller.request ||= ActionController::TestRequest.new if defined?(ActionController::TestRequest)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|