draper 0.18.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.
- data/.gitignore +3 -0
- data/.travis.yml +11 -0
- data/.yardopts +1 -1
- data/CHANGELOG.md +161 -0
- data/CONTRIBUTING.md +11 -0
- data/Gemfile +20 -0
- data/LICENSE +7 -0
- data/README.md +324 -0
- data/Rakefile +52 -31
- data/draper.gemspec +11 -9
- data/lib/draper/automatic_delegation.rb +50 -0
- data/lib/draper/collection_decorator.rb +107 -0
- data/lib/draper/decoratable.rb +83 -0
- data/lib/draper/decorated_association.rb +74 -0
- data/lib/draper/decorator.rb +238 -0
- data/lib/draper/delegation.rb +13 -0
- data/lib/draper/finders.rb +37 -0
- data/lib/draper/helper_proxy.rb +17 -0
- data/lib/draper/lazy_helpers.rb +10 -6
- data/lib/draper/railtie.rb +27 -21
- data/lib/draper/tasks/test.rake +22 -0
- data/lib/draper/test/devise_helper.rb +34 -0
- data/lib/draper/test/minitest_integration.rb +2 -3
- data/lib/draper/test/rspec_integration.rb +3 -13
- data/lib/draper/test_case.rb +33 -0
- data/lib/draper/version.rb +1 -1
- data/lib/draper/view_context.rb +12 -17
- data/lib/draper/view_helpers.rb +37 -0
- data/lib/draper.rb +47 -8
- data/lib/generators/decorator/decorator_generator.rb +10 -2
- data/lib/generators/decorator/templates/decorator.rb +12 -27
- 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/test_unit/templates/decorator_test.rb +1 -1
- data/spec/draper/collection_decorator_spec.rb +286 -0
- data/spec/draper/decoratable_spec.rb +192 -0
- data/spec/draper/decorated_association_spec.rb +142 -0
- data/spec/draper/decorator_spec.rb +624 -0
- data/spec/draper/finders_spec.rb +132 -0
- data/spec/draper/helper_proxy_spec.rb +12 -0
- data/spec/draper/view_helpers_spec.rb +41 -0
- data/spec/dummy/.rspec +2 -0
- data/spec/dummy/README.rdoc +261 -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 +11 -0
- data/spec/dummy/app/decorators/post_decorator.rb +32 -0
- data/spec/dummy/app/helpers/application_helper.rb +2 -0
- data/spec/dummy/app/mailers/application_mailer.rb +3 -0
- data/spec/dummy/app/mailers/post_mailer.rb +9 -0
- data/spec/dummy/app/models/post.rb +3 -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 +19 -0
- data/spec/dummy/app/views/posts/show.html.erb +1 -0
- data/spec/dummy/config/application.rb +64 -0
- data/spec/dummy/config/boot.rb +10 -0
- data/spec/dummy/config/database.yml +25 -0
- data/spec/dummy/config/environment.rb +5 -0
- data/spec/dummy/config/environments/development.rb +34 -0
- data/spec/dummy/config/environments/production.rb +55 -0
- data/spec/dummy/config/environments/test.rb +32 -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 +7 -0
- data/spec/dummy/config/initializers/session_store.rb +8 -0
- data/spec/dummy/config/locales/en.yml +5 -0
- data/spec/dummy/config/routes.rb +7 -0
- data/spec/dummy/config.ru +4 -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/lib/tasks/test.rake +10 -0
- data/spec/dummy/log/.gitkeep +0 -0
- data/spec/dummy/mini_test/mini_test_integration_test.rb +46 -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/post_decorator_spec.rb +26 -0
- data/spec/dummy/spec/decorators/rspec_integration_spec.rb +19 -0
- data/spec/dummy/spec/mailers/post_mailer_spec.rb +29 -0
- data/spec/dummy/spec/models/post_spec.rb +14 -0
- data/spec/dummy/spec/spec_helper.rb +8 -0
- data/spec/generators/decorator/decorator_generator_spec.rb +47 -7
- data/spec/integration/integration_spec.rb +33 -0
- data/{performance → spec/performance}/decorators.rb +2 -4
- data/spec/spec_helper.rb +20 -45
- data/spec/support/action_controller.rb +12 -0
- data/spec/support/active_model.rb +7 -0
- data/spec/support/active_record.rb +9 -0
- data/spec/support/{samples → decorators}/decorator_with_application_helper.rb +1 -1
- data/spec/support/decorators/namespaced_product_decorator.rb +5 -0
- data/spec/support/decorators/non_active_model_product_decorator.rb +2 -0
- data/spec/support/decorators/product_decorator.rb +23 -0
- data/spec/support/{samples → decorators}/products_decorator.rb +1 -1
- data/spec/support/decorators/some_thing_decorator.rb +2 -0
- data/spec/support/{samples → decorators}/specific_product_decorator.rb +0 -2
- data/spec/support/dummy_app.rb +85 -0
- data/spec/support/matchers/have_text.rb +50 -0
- data/spec/support/{samples → models}/namespaced_product.rb +1 -3
- data/spec/support/{samples → models}/non_active_model_product.rb +1 -0
- data/spec/support/{samples → models}/product.rb +17 -2
- data/spec/support/models/some_thing.rb +5 -0
- data/spec/support/models/uninferrable_decorator_model.rb +3 -0
- metadata +241 -87
- data/CHANGELOG.markdown +0 -57
- data/Readme.markdown +0 -387
- data/lib/draper/active_model_support.rb +0 -27
- data/lib/draper/base.rb +0 -312
- data/lib/draper/decorated_enumerable_proxy.rb +0 -90
- data/lib/draper/model_support.rb +0 -25
- data/lib/draper/rspec_integration.rb +0 -2
- data/lib/draper/system.rb +0 -18
- data/spec/draper/base_spec.rb +0 -873
- data/spec/draper/decorated_enumerable_proxy_spec.rb +0 -45
- data/spec/draper/model_support_spec.rb +0 -48
- data/spec/minitest-rails/spec_type_spec.rb +0 -63
- data/spec/support/samples/active_record.rb +0 -17
- data/spec/support/samples/decorator.rb +0 -5
- data/spec/support/samples/decorator_with_allows.rb +0 -3
- data/spec/support/samples/decorator_with_denies.rb +0 -3
- data/spec/support/samples/decorator_with_denies_all.rb +0 -3
- data/spec/support/samples/decorator_with_multiple_allows.rb +0 -4
- data/spec/support/samples/decorator_with_special_methods.rb +0 -13
- data/spec/support/samples/enumerable_proxy.rb +0 -3
- data/spec/support/samples/namespaced_product_decorator.rb +0 -7
- data/spec/support/samples/product_decorator.rb +0 -7
- data/spec/support/samples/sequel_product.rb +0 -13
- data/spec/support/samples/some_thing.rb +0 -2
- data/spec/support/samples/some_thing_decorator.rb +0 -3
- /data/{performance → spec/performance}/active_record.rb +0 -0
- /data/{performance/bechmark.rb → spec/performance/benchmark.rb} +0 -0
- /data/{performance → spec/performance}/models.rb +0 -0
- /data/spec/support/{samples → decorators}/widget_decorator.rb +0 -0
- /data/spec/support/{samples → models}/widget.rb +0 -0
data/lib/draper/railtie.rb
CHANGED
|
@@ -3,8 +3,10 @@ require 'rails/railtie'
|
|
|
3
3
|
module ActiveModel
|
|
4
4
|
class Railtie < Rails::Railtie
|
|
5
5
|
generators do |app|
|
|
6
|
-
Rails
|
|
7
|
-
|
|
6
|
+
app ||= Rails.application # Rails 3.0.x does not yield `app`
|
|
7
|
+
|
|
8
|
+
Rails::Generators.configure! app.config.generators
|
|
9
|
+
require 'generators/resource_override'
|
|
8
10
|
end
|
|
9
11
|
end
|
|
10
12
|
end
|
|
@@ -12,40 +14,44 @@ end
|
|
|
12
14
|
module Draper
|
|
13
15
|
class Railtie < Rails::Railtie
|
|
14
16
|
|
|
15
|
-
##
|
|
16
|
-
# The `app/decorators` path is eager loaded
|
|
17
|
-
#
|
|
18
|
-
# This is the standard "Rails Way" to add paths from which constants
|
|
19
|
-
# can be loaded.
|
|
20
|
-
#
|
|
21
17
|
config.after_initialize do |app|
|
|
22
|
-
app.config.paths.add 'app/decorators', :
|
|
18
|
+
app.config.paths.add 'app/decorators', eager_load: true
|
|
19
|
+
|
|
20
|
+
unless Rails.env.production?
|
|
21
|
+
require 'draper/test_case'
|
|
22
|
+
require 'draper/test/rspec_integration' if defined?(RSpec) and RSpec.respond_to?(:configure)
|
|
23
|
+
require 'draper/test/minitest_integration' if defined?(MiniTest::Rails)
|
|
24
|
+
end
|
|
23
25
|
end
|
|
24
26
|
|
|
25
|
-
initializer "draper.
|
|
26
|
-
ActiveSupport.on_load
|
|
27
|
-
Draper
|
|
27
|
+
initializer "draper.setup_action_controller" do |app|
|
|
28
|
+
ActiveSupport.on_load :action_controller do
|
|
29
|
+
Draper.setup_action_controller self
|
|
28
30
|
end
|
|
29
31
|
end
|
|
30
32
|
|
|
31
|
-
initializer "draper.
|
|
32
|
-
ActiveSupport.on_load
|
|
33
|
-
Draper
|
|
33
|
+
initializer "draper.setup_action_mailer" do |app|
|
|
34
|
+
ActiveSupport.on_load :action_mailer do
|
|
35
|
+
Draper.setup_action_mailer self
|
|
34
36
|
end
|
|
35
37
|
end
|
|
36
38
|
|
|
37
|
-
initializer "draper.
|
|
38
|
-
|
|
39
|
-
|
|
39
|
+
initializer "draper.setup_orm" do |app|
|
|
40
|
+
[:active_record, :mongoid].each do |orm|
|
|
41
|
+
ActiveSupport.on_load orm do
|
|
42
|
+
Draper.setup_orm self
|
|
43
|
+
end
|
|
40
44
|
end
|
|
41
45
|
end
|
|
42
46
|
|
|
43
47
|
console do
|
|
44
48
|
require 'action_controller/test_case'
|
|
45
49
|
ApplicationController.new.view_context
|
|
46
|
-
Draper::ViewContext.
|
|
47
|
-
|
|
48
|
-
|
|
50
|
+
Draper::ViewContext.build_view_context
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
rake_tasks do
|
|
54
|
+
Dir[File.join(File.dirname(__FILE__),'tasks/*.rake')].each { |f| load f }
|
|
49
55
|
end
|
|
50
56
|
end
|
|
51
57
|
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 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,34 @@
|
|
|
1
|
+
module Draper
|
|
2
|
+
module DeviseHelper
|
|
3
|
+
def sign_in(user)
|
|
4
|
+
warden.stub :authenticate! => user
|
|
5
|
+
controller.stub :current_user => user
|
|
6
|
+
user
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
private
|
|
10
|
+
|
|
11
|
+
def request
|
|
12
|
+
@request ||= ::ActionDispatch::TestRequest.new
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def controller
|
|
16
|
+
return @controller if @controller
|
|
17
|
+
@controller = ApplicationController.new
|
|
18
|
+
@controller.request = request
|
|
19
|
+
::Draper::ViewContext.current = @controller.view_context
|
|
20
|
+
@controller
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
# taken from Devise's helper but uses the request method instead of @request
|
|
24
|
+
# and we don't really need the rest of their helper
|
|
25
|
+
def warden
|
|
26
|
+
@warden ||= begin
|
|
27
|
+
manager = Warden::Manager.new(nil) do |config|
|
|
28
|
+
config.merge! Devise.warden_config
|
|
29
|
+
end
|
|
30
|
+
request.env['warden'] = Warden::Proxy.new(request.env, manager)
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
class
|
|
2
|
-
# Use AS::TestCase for the base class when describing a decorator
|
|
1
|
+
class Draper::TestCase
|
|
3
2
|
register_spec_type(self) do |desc|
|
|
4
|
-
desc < Draper::
|
|
3
|
+
desc < Draper::Decorator || desc < Draper::CollectionDecorator if desc.is_a?(Class)
|
|
5
4
|
end
|
|
6
5
|
register_spec_type(/Decorator( ?Test)?\z/i, self)
|
|
7
6
|
end
|
|
@@ -1,22 +1,12 @@
|
|
|
1
1
|
module Draper
|
|
2
2
|
module DecoratorExampleGroup
|
|
3
|
+
include Draper::TestCase::Behavior
|
|
3
4
|
extend ActiveSupport::Concern
|
|
5
|
+
|
|
4
6
|
included { metadata[:type] = :decorator }
|
|
5
7
|
end
|
|
6
|
-
end
|
|
7
|
-
|
|
8
|
-
RSpec.configure do |config|
|
|
9
|
-
# Automatically tag specs in specs/decorators as type: :decorator
|
|
10
|
-
config.include Draper::DecoratorExampleGroup, :type => :decorator, :example_group => {
|
|
11
|
-
:file_path => /spec[\\\/]decorators/
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
end
|
|
15
|
-
|
|
16
|
-
if defined?(Capybara)
|
|
17
|
-
require 'capybara/rspec/matchers'
|
|
18
8
|
|
|
19
9
|
RSpec.configure do |config|
|
|
20
|
-
config.include
|
|
10
|
+
config.include DecoratorExampleGroup, example_group: {file_path: %r{spec/decorators}}, type: :decorator
|
|
21
11
|
end
|
|
22
12
|
end
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
module Draper
|
|
2
|
+
begin
|
|
3
|
+
require 'minitest/rails'
|
|
4
|
+
rescue LoadError
|
|
5
|
+
end
|
|
6
|
+
|
|
7
|
+
active_support_test_case = begin
|
|
8
|
+
require 'minitest/rails/active_support' # minitest-rails < 0.5
|
|
9
|
+
::MiniTest::Rails::ActiveSupport::TestCase
|
|
10
|
+
rescue LoadError
|
|
11
|
+
require 'active_support/test_case'
|
|
12
|
+
::ActiveSupport::TestCase
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
class TestCase < active_support_test_case
|
|
16
|
+
module Behavior
|
|
17
|
+
if defined?(::Devise)
|
|
18
|
+
require 'draper/test/devise_helper'
|
|
19
|
+
include Draper::DeviseHelper
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
if defined?(::Capybara) && (defined?(::RSpec) || defined?(::MiniTest::Matchers))
|
|
23
|
+
require 'capybara/rspec/matchers'
|
|
24
|
+
include ::Capybara::RSpecMatchers
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
include Draper::ViewHelpers::ClassMethods
|
|
28
|
+
alias_method :helper, :helpers
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
include Behavior
|
|
32
|
+
end
|
|
33
|
+
end
|
data/lib/draper/version.rb
CHANGED
data/lib/draper/view_context.rb
CHANGED
|
@@ -1,36 +1,31 @@
|
|
|
1
|
+
require 'request_store'
|
|
2
|
+
|
|
1
3
|
module Draper
|
|
2
4
|
module ViewContext
|
|
5
|
+
def view_context
|
|
6
|
+
super.tap do |context|
|
|
7
|
+
Draper::ViewContext.current = context
|
|
8
|
+
end
|
|
9
|
+
end
|
|
10
|
+
|
|
3
11
|
def self.current_controller
|
|
4
|
-
|
|
12
|
+
RequestStore.store[:current_controller] || ApplicationController.new
|
|
5
13
|
end
|
|
6
14
|
|
|
7
15
|
def self.current_controller=(controller)
|
|
8
|
-
|
|
16
|
+
RequestStore.store[:current_controller] = controller
|
|
9
17
|
end
|
|
10
18
|
|
|
11
19
|
def self.current
|
|
12
|
-
|
|
20
|
+
RequestStore.store[:current_view_context] ||= build_view_context
|
|
13
21
|
end
|
|
14
22
|
|
|
15
23
|
def self.current=(context)
|
|
16
|
-
|
|
24
|
+
RequestStore.store[:current_view_context] = context
|
|
17
25
|
end
|
|
18
26
|
|
|
19
|
-
def view_context
|
|
20
|
-
super.tap do |context|
|
|
21
|
-
Draper::ViewContext.current = context
|
|
22
|
-
end
|
|
23
|
-
end
|
|
24
|
-
|
|
25
|
-
private
|
|
26
|
-
|
|
27
27
|
def self.build_view_context
|
|
28
28
|
current_controller.view_context.tap do |context|
|
|
29
|
-
context.instance_eval do
|
|
30
|
-
def url_options
|
|
31
|
-
ActionMailer::Base.default_url_options
|
|
32
|
-
end
|
|
33
|
-
end unless context.request
|
|
34
29
|
if defined?(ActionController::TestRequest)
|
|
35
30
|
context.controller.request ||= ActionController::TestRequest.new
|
|
36
31
|
context.request ||= context.controller.request
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
module Draper
|
|
2
|
+
# Provides the {#helpers} method used in {Decorator} and {CollectionDecorator}
|
|
3
|
+
# to call the Rails helpers.
|
|
4
|
+
module ViewHelpers
|
|
5
|
+
extend ActiveSupport::Concern
|
|
6
|
+
|
|
7
|
+
module ClassMethods
|
|
8
|
+
|
|
9
|
+
# Access the helpers proxy to call built-in and user-defined
|
|
10
|
+
# Rails helpers from a class context.
|
|
11
|
+
#
|
|
12
|
+
# @return [HelperProxy] the helpers proxy
|
|
13
|
+
def helpers
|
|
14
|
+
@helpers ||= Draper::HelperProxy.new
|
|
15
|
+
end
|
|
16
|
+
alias_method :h, :helpers
|
|
17
|
+
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
# Access the helpers proxy to call built-in and user-defined
|
|
21
|
+
# Rails helpers. Aliased to `h` for convenience.
|
|
22
|
+
#
|
|
23
|
+
# @return [HelperProxy] the helpers proxy
|
|
24
|
+
def helpers
|
|
25
|
+
self.class.helpers
|
|
26
|
+
end
|
|
27
|
+
alias_method :h, :helpers
|
|
28
|
+
|
|
29
|
+
# Alias for `helpers.localize`, since localize is something that's used
|
|
30
|
+
# quite often. Further aliased to `l` for convenience.
|
|
31
|
+
def localize(*args)
|
|
32
|
+
helpers.localize(*args)
|
|
33
|
+
end
|
|
34
|
+
alias_method :l, :localize
|
|
35
|
+
|
|
36
|
+
end
|
|
37
|
+
end
|
data/lib/draper.rb
CHANGED
|
@@ -1,16 +1,55 @@
|
|
|
1
1
|
require 'action_view'
|
|
2
2
|
|
|
3
3
|
require 'draper/version'
|
|
4
|
-
require 'draper/
|
|
5
|
-
require 'draper/
|
|
6
|
-
require 'draper/
|
|
4
|
+
require 'draper/view_helpers'
|
|
5
|
+
require 'draper/delegation'
|
|
6
|
+
require 'draper/automatic_delegation'
|
|
7
|
+
require 'draper/finders'
|
|
8
|
+
require 'draper/decorator'
|
|
9
|
+
require 'draper/helper_proxy'
|
|
7
10
|
require 'draper/lazy_helpers'
|
|
8
|
-
require 'draper/
|
|
11
|
+
require 'draper/decoratable'
|
|
12
|
+
require 'draper/decorated_association'
|
|
9
13
|
require 'draper/helper_support'
|
|
10
14
|
require 'draper/view_context'
|
|
11
|
-
require 'draper/
|
|
15
|
+
require 'draper/collection_decorator'
|
|
12
16
|
require 'draper/railtie' if defined?(Rails)
|
|
13
17
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
18
|
+
require 'active_support/core_ext/hash/keys'
|
|
19
|
+
|
|
20
|
+
module Draper
|
|
21
|
+
def self.setup_action_controller(base)
|
|
22
|
+
base.class_eval do
|
|
23
|
+
include Draper::ViewContext
|
|
24
|
+
extend Draper::HelperSupport
|
|
25
|
+
before_filter ->(controller) {
|
|
26
|
+
Draper::ViewContext.current = nil
|
|
27
|
+
Draper::ViewContext.current_controller = controller
|
|
28
|
+
}
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def self.setup_action_mailer(base)
|
|
33
|
+
base.class_eval do
|
|
34
|
+
include Draper::ViewContext
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def self.setup_orm(base)
|
|
39
|
+
base.class_eval do
|
|
40
|
+
include Draper::Decoratable
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
class UninferrableDecoratorError < NameError
|
|
45
|
+
def initialize(klass)
|
|
46
|
+
super("Could not infer a decorator for #{klass}.")
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
class UninferrableSourceError < NameError
|
|
51
|
+
def initialize(klass)
|
|
52
|
+
super("Could not infer a source for #{klass}.")
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
end
|
|
@@ -20,9 +20,17 @@ module Rails
|
|
|
20
20
|
elsif defined?(ApplicationDecorator)
|
|
21
21
|
"ApplicationDecorator"
|
|
22
22
|
else
|
|
23
|
-
"Draper::
|
|
23
|
+
"Draper::Decorator"
|
|
24
24
|
end
|
|
25
25
|
end
|
|
26
|
-
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
# Rails 3.0.X compatibility, stolen from https://github.com/jnunemaker/mongomapper/pull/385/files#L1R32
|
|
29
|
+
unless methods.include?(:module_namespacing)
|
|
30
|
+
def module_namespacing(&block)
|
|
31
|
+
yield if block
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
27
35
|
end
|
|
28
36
|
end
|
|
@@ -1,34 +1,19 @@
|
|
|
1
|
-
|
|
1
|
+
<%- module_namespacing do -%>
|
|
2
|
+
<%- if parent_class_name.present? -%>
|
|
2
3
|
class <%= class_name %>Decorator < <%= parent_class_name %>
|
|
3
|
-
|
|
4
|
+
<%- else -%>
|
|
5
|
+
class <%= class_name %>
|
|
6
|
+
<%- end -%>
|
|
7
|
+
delegate_all
|
|
4
8
|
|
|
5
|
-
#
|
|
6
|
-
#
|
|
7
|
-
#
|
|
8
|
-
# Normal Usage: helpers.number_to_currency(2)
|
|
9
|
-
# Abbreviated : h.number_to_currency(2)
|
|
10
|
-
#
|
|
11
|
-
# Or, optionally enable "lazy helpers" by including this module:
|
|
12
|
-
# include Draper::LazyHelpers
|
|
13
|
-
# Then use the helpers with no proxy:
|
|
14
|
-
# number_to_currency(2)
|
|
15
|
-
|
|
16
|
-
# Defining an Interface
|
|
17
|
-
# Control access to the wrapped subject's methods using one of the following:
|
|
18
|
-
#
|
|
19
|
-
# To allow only the listed methods (whitelist):
|
|
20
|
-
# allows :method1, :method2
|
|
21
|
-
#
|
|
22
|
-
# To allow everything except the listed methods (blacklist):
|
|
23
|
-
# denies :method1, :method2
|
|
24
|
-
|
|
25
|
-
# Presentation Methods
|
|
26
|
-
# Define your own instance methods, even overriding accessors
|
|
27
|
-
# generated by ActiveRecord:
|
|
9
|
+
# Define presentation-specific methods here. Helpers are accessed through
|
|
10
|
+
# `helpers` (aka `h`). You can override attributes, for example:
|
|
28
11
|
#
|
|
29
12
|
# def created_at
|
|
30
|
-
#
|
|
31
|
-
#
|
|
13
|
+
# helpers.content_tag :span, class: 'time' do
|
|
14
|
+
# source.created_at.strftime("%a %m/%d/%y")
|
|
15
|
+
# end
|
|
32
16
|
# end
|
|
17
|
+
|
|
33
18
|
end
|
|
34
19
|
<% end -%>
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
require 'generators/mini_test'
|
|
2
|
+
|
|
3
|
+
module MiniTest
|
|
4
|
+
module Generators
|
|
5
|
+
class DecoratorGenerator < Base
|
|
6
|
+
def self.source_root
|
|
7
|
+
File.expand_path('../templates', __FILE__)
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
class_option :spec, :type => :boolean, :default => false, :desc => "Use MiniTest::Spec DSL"
|
|
11
|
+
|
|
12
|
+
check_class_collision suffix: "DecoratorTest"
|
|
13
|
+
|
|
14
|
+
def create_test_file
|
|
15
|
+
template_type = options[:spec] ? "spec" : "test"
|
|
16
|
+
template "decorator_#{template_type}.rb", File.join("test/decorators", class_path, "#{singular_name}_decorator_test.rb")
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|