jamesgolick-draper 1.1.1a

Sign up to get free protection for your applications and to get access to all the features.
Files changed (136) hide show
  1. data/.gitignore +16 -0
  2. data/.rspec +2 -0
  3. data/.travis.yml +22 -0
  4. data/.yardopts +1 -0
  5. data/CHANGELOG.md +150 -0
  6. data/CONTRIBUTING.md +15 -0
  7. data/Gemfile +39 -0
  8. data/Guardfile +26 -0
  9. data/LICENSE +7 -0
  10. data/README.md +417 -0
  11. data/Rakefile +69 -0
  12. data/draper.gemspec +32 -0
  13. data/lib/draper.rb +64 -0
  14. data/lib/draper/automatic_delegation.rb +56 -0
  15. data/lib/draper/collection_decorator.rb +96 -0
  16. data/lib/draper/decoratable.rb +82 -0
  17. data/lib/draper/decoratable/equality.rb +18 -0
  18. data/lib/draper/decorated_association.rb +35 -0
  19. data/lib/draper/decorates_assigned.rb +44 -0
  20. data/lib/draper/decorator.rb +248 -0
  21. data/lib/draper/delegation.rb +13 -0
  22. data/lib/draper/factory.rb +87 -0
  23. data/lib/draper/finders.rb +37 -0
  24. data/lib/draper/helper_proxy.rb +38 -0
  25. data/lib/draper/helper_support.rb +5 -0
  26. data/lib/draper/lazy_helpers.rb +15 -0
  27. data/lib/draper/railtie.rb +63 -0
  28. data/lib/draper/tasks/test.rake +22 -0
  29. data/lib/draper/test/devise_helper.rb +30 -0
  30. data/lib/draper/test/minitest_integration.rb +6 -0
  31. data/lib/draper/test/rspec_integration.rb +16 -0
  32. data/lib/draper/test_case.rb +53 -0
  33. data/lib/draper/version.rb +3 -0
  34. data/lib/draper/view_context.rb +99 -0
  35. data/lib/draper/view_context/build_strategy.rb +48 -0
  36. data/lib/draper/view_helpers.rb +37 -0
  37. data/lib/generators/decorator/decorator_generator.rb +36 -0
  38. data/lib/generators/decorator/templates/decorator.rb +19 -0
  39. data/lib/generators/mini_test/decorator_generator.rb +20 -0
  40. data/lib/generators/mini_test/templates/decorator_spec.rb +4 -0
  41. data/lib/generators/mini_test/templates/decorator_test.rb +4 -0
  42. data/lib/generators/resource_override.rb +12 -0
  43. data/lib/generators/rspec/decorator_generator.rb +9 -0
  44. data/lib/generators/rspec/templates/decorator_spec.rb +4 -0
  45. data/lib/generators/test_unit/decorator_generator.rb +9 -0
  46. data/lib/generators/test_unit/templates/decorator_test.rb +4 -0
  47. data/spec/draper/collection_decorator_spec.rb +279 -0
  48. data/spec/draper/decoratable/equality_spec.rb +10 -0
  49. data/spec/draper/decoratable_spec.rb +176 -0
  50. data/spec/draper/decorated_association_spec.rb +84 -0
  51. data/spec/draper/decorates_assigned_spec.rb +71 -0
  52. data/spec/draper/decorator_spec.rb +634 -0
  53. data/spec/draper/factory_spec.rb +238 -0
  54. data/spec/draper/finders_spec.rb +166 -0
  55. data/spec/draper/helper_proxy_spec.rb +53 -0
  56. data/spec/draper/lazy_helpers_spec.rb +21 -0
  57. data/spec/draper/view_context/build_strategy_spec.rb +116 -0
  58. data/spec/draper/view_context_spec.rb +154 -0
  59. data/spec/draper/view_helpers_spec.rb +8 -0
  60. data/spec/dummy/.rspec +2 -0
  61. data/spec/dummy/Rakefile +7 -0
  62. data/spec/dummy/app/controllers/application_controller.rb +4 -0
  63. data/spec/dummy/app/controllers/localized_urls.rb +5 -0
  64. data/spec/dummy/app/controllers/posts_controller.rb +20 -0
  65. data/spec/dummy/app/decorators/mongoid_post_decorator.rb +2 -0
  66. data/spec/dummy/app/decorators/post_decorator.rb +56 -0
  67. data/spec/dummy/app/helpers/application_helper.rb +5 -0
  68. data/spec/dummy/app/mailers/application_mailer.rb +3 -0
  69. data/spec/dummy/app/mailers/post_mailer.rb +19 -0
  70. data/spec/dummy/app/models/admin.rb +5 -0
  71. data/spec/dummy/app/models/mongoid_post.rb +5 -0
  72. data/spec/dummy/app/models/post.rb +3 -0
  73. data/spec/dummy/app/models/user.rb +5 -0
  74. data/spec/dummy/app/views/layouts/application.html.erb +11 -0
  75. data/spec/dummy/app/views/post_mailer/decorated_email.html.erb +1 -0
  76. data/spec/dummy/app/views/posts/_post.html.erb +34 -0
  77. data/spec/dummy/app/views/posts/show.html.erb +1 -0
  78. data/spec/dummy/bin/rails +4 -0
  79. data/spec/dummy/config.ru +4 -0
  80. data/spec/dummy/config/application.rb +71 -0
  81. data/spec/dummy/config/boot.rb +5 -0
  82. data/spec/dummy/config/database.yml +25 -0
  83. data/spec/dummy/config/environment.rb +5 -0
  84. data/spec/dummy/config/environments/development.rb +33 -0
  85. data/spec/dummy/config/environments/production.rb +57 -0
  86. data/spec/dummy/config/environments/test.rb +31 -0
  87. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  88. data/spec/dummy/config/initializers/inflections.rb +15 -0
  89. data/spec/dummy/config/initializers/mime_types.rb +5 -0
  90. data/spec/dummy/config/initializers/secret_token.rb +8 -0
  91. data/spec/dummy/config/initializers/session_store.rb +8 -0
  92. data/spec/dummy/config/locales/en.yml +5 -0
  93. data/spec/dummy/config/mongoid.yml +80 -0
  94. data/spec/dummy/config/routes.rb +9 -0
  95. data/spec/dummy/db/migrate/20121019115657_create_posts.rb +8 -0
  96. data/spec/dummy/db/schema.rb +21 -0
  97. data/spec/dummy/db/seeds.rb +2 -0
  98. data/spec/dummy/fast_spec/post_decorator_spec.rb +38 -0
  99. data/spec/dummy/lib/tasks/test.rake +16 -0
  100. data/spec/dummy/public/404.html +26 -0
  101. data/spec/dummy/public/422.html +26 -0
  102. data/spec/dummy/public/500.html +25 -0
  103. data/spec/dummy/public/favicon.ico +0 -0
  104. data/spec/dummy/script/rails +6 -0
  105. data/spec/dummy/spec/decorators/active_model_serializers_spec.rb +11 -0
  106. data/spec/dummy/spec/decorators/devise_spec.rb +64 -0
  107. data/spec/dummy/spec/decorators/helpers_spec.rb +21 -0
  108. data/spec/dummy/spec/decorators/post_decorator_spec.rb +58 -0
  109. data/spec/dummy/spec/decorators/spec_type_spec.rb +7 -0
  110. data/spec/dummy/spec/decorators/view_context_spec.rb +22 -0
  111. data/spec/dummy/spec/mailers/post_mailer_spec.rb +33 -0
  112. data/spec/dummy/spec/models/mongoid_post_spec.rb +8 -0
  113. data/spec/dummy/spec/models/post_spec.rb +6 -0
  114. data/spec/dummy/spec/shared_examples/decoratable.rb +24 -0
  115. data/spec/dummy/spec/spec_helper.rb +9 -0
  116. data/spec/dummy/test/decorators/minitest/devise_test.rb +64 -0
  117. data/spec/dummy/test/decorators/minitest/helpers_test.rb +21 -0
  118. data/spec/dummy/test/decorators/minitest/spec_type_test.rb +52 -0
  119. data/spec/dummy/test/decorators/minitest/view_context_test.rb +24 -0
  120. data/spec/dummy/test/decorators/test_unit/devise_test.rb +64 -0
  121. data/spec/dummy/test/decorators/test_unit/helpers_test.rb +21 -0
  122. data/spec/dummy/test/decorators/test_unit/view_context_test.rb +24 -0
  123. data/spec/dummy/test/minitest_helper.rb +4 -0
  124. data/spec/dummy/test/test_helper.rb +3 -0
  125. data/spec/generators/decorator/decorator_generator_spec.rb +130 -0
  126. data/spec/integration/integration_spec.rb +58 -0
  127. data/spec/performance/active_record.rb +4 -0
  128. data/spec/performance/benchmark.rb +55 -0
  129. data/spec/performance/decorators.rb +45 -0
  130. data/spec/performance/models.rb +20 -0
  131. data/spec/spec_helper.rb +37 -0
  132. data/spec/support/dummy_app.rb +85 -0
  133. data/spec/support/matchers/have_text.rb +50 -0
  134. data/spec/support/shared_examples/decoratable_equality.rb +40 -0
  135. data/spec/support/shared_examples/view_helpers.rb +39 -0
  136. metadata +451 -0
@@ -0,0 +1,5 @@
1
+ module Draper::HelperSupport
2
+ def decorate(input, &block)
3
+ capture { block.call(input.decorate) }
4
+ end
5
+ 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,63 @@
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 'generators/resource_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
+ require 'draper/test/minitest_integration' if defined?(MiniTest::Rails)
24
+ end
25
+ end
26
+
27
+ initializer "draper.setup_action_controller" do |app|
28
+ ActiveSupport.on_load :action_controller do
29
+ Draper.setup_action_controller self
30
+ end
31
+ end
32
+
33
+ initializer "draper.setup_action_mailer" do |app|
34
+ ActiveSupport.on_load :action_mailer do
35
+ Draper.setup_action_mailer self
36
+ end
37
+ end
38
+
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
44
+ end
45
+ end
46
+
47
+ initializer "draper.setup_active_model_serializers" do |app|
48
+ ActiveSupport.on_load :active_model_serializers do
49
+ Draper::CollectionDecorator.send :include, ActiveModel::ArraySerializerSupport
50
+ end
51
+ end
52
+
53
+ console do
54
+ require 'action_controller/test_case'
55
+ ApplicationController.new.view_context
56
+ Draper::ViewContext.build
57
+ end
58
+
59
+ rake_tasks do
60
+ Dir[File.join(File.dirname(__FILE__),'tasks/*.rake')].each { |f| load f }
61
+ end
62
+ end
63
+ 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,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,6 @@
1
+ class Draper::TestCase
2
+ register_spec_type(self) do |desc|
3
+ desc < Draper::Decorator || desc < Draper::CollectionDecorator if desc.is_a?(Class)
4
+ end
5
+ register_spec_type(/Decorator( ?Test)?\z/i, self)
6
+ end
@@ -0,0 +1,16 @@
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
+ config.include DecoratorExampleGroup, example_group: {file_path: %r{spec/decorators}}, type: :decorator
11
+
12
+ [:decorator, :controller, :mailer].each do |type|
13
+ config.after(:each, type: type) { Draper::ViewContext.clear! }
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,53 @@
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 ViewContextTeardown
17
+ def teardown
18
+ super
19
+ Draper::ViewContext.clear!
20
+ end
21
+ end
22
+
23
+ module Behavior
24
+ if defined?(::Devise)
25
+ require 'draper/test/devise_helper'
26
+ include Draper::DeviseHelper
27
+ end
28
+
29
+ if defined?(::Capybara) && (defined?(::RSpec) || defined?(::MiniTest::Matchers))
30
+ require 'capybara/rspec/matchers'
31
+ include ::Capybara::RSpecMatchers
32
+ end
33
+
34
+ include Draper::ViewHelpers::ClassMethods
35
+ alias_method :helper, :helpers
36
+ end
37
+
38
+ include Behavior
39
+ include ViewContextTeardown
40
+ end
41
+ end
42
+
43
+ if defined?(ActionController::TestCase)
44
+ class ActionController::TestCase
45
+ include Draper::TestCase::ViewContextTeardown
46
+ end
47
+ end
48
+
49
+ if defined?(ActionMailer::TestCase)
50
+ class ActionMailer::TestCase
51
+ include Draper::TestCase::ViewContextTeardown
52
+ end
53
+ end
@@ -0,0 +1,3 @@
1
+ module Draper
2
+ VERSION = "1.1.1a"
3
+ end
@@ -0,0 +1,99 @@
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
+ # Returns the current controller.
14
+ def self.controller
15
+ RequestStore.store[:current_controller]
16
+ end
17
+
18
+ # Sets the current controller.
19
+ def self.controller=(controller)
20
+ RequestStore.store[:current_controller] = controller
21
+ end
22
+
23
+ # Returns the current view context, or builds one if none is saved.
24
+ #
25
+ # @return [HelperProxy]
26
+ def self.current
27
+ RequestStore.store.fetch(:current_view_context) { build! }
28
+ end
29
+
30
+ # Sets the current view context.
31
+ def self.current=(view_context)
32
+ RequestStore.store[:current_view_context] = Draper::HelperProxy.new(view_context)
33
+ end
34
+
35
+ # Clears the saved controller and view context.
36
+ def self.clear!
37
+ RequestStore.store.delete :current_controller
38
+ RequestStore.store.delete :current_view_context
39
+ end
40
+
41
+ # Builds a new view context for usage in tests. See {test_strategy} for
42
+ # details of how the view context is built.
43
+ def self.build
44
+ build_strategy.call
45
+ end
46
+
47
+ # Builds a new view context and sets it as the current view context.
48
+ #
49
+ # @return [HelperProxy]
50
+ def self.build!
51
+ # send because we want to return the HelperProxy returned from #current=
52
+ send :current=, build
53
+ end
54
+
55
+ # Configures the strategy used to build view contexts in tests, which
56
+ # defaults to `:full` if `test_strategy` has not been called. Evaluates
57
+ # the block, if given, in the context of the view context's class.
58
+ #
59
+ # @example Pass a block to add helper methods to the view context:
60
+ # Draper::ViewContext.test_strategy :fast do
61
+ # include ApplicationHelper
62
+ # end
63
+ #
64
+ # @param [:full, :fast] name
65
+ # the strategy to use:
66
+ #
67
+ # `:full` - build a fully-working view context. Your Rails environment
68
+ # must be loaded, including your `ApplicationController`.
69
+ #
70
+ # `:fast` - build a minimal view context in tests, with no dependencies
71
+ # on other components of your application.
72
+ def self.test_strategy(name, &block)
73
+ @build_strategy = Draper::ViewContext::BuildStrategy.new(name, &block)
74
+ end
75
+
76
+ # @private
77
+ def self.build_strategy
78
+ @build_strategy ||= Draper::ViewContext::BuildStrategy.new(:full)
79
+ end
80
+
81
+ # @deprecated Use {controller} instead.
82
+ def self.current_controller
83
+ ActiveSupport::Deprecation.warn("Draper::ViewContext.current_controller is deprecated (use controller instead)", caller)
84
+ self.controller || ApplicationController.new
85
+ end
86
+
87
+ # @deprecated Use {controller=} instead.
88
+ def self.current_controller=(controller)
89
+ ActiveSupport::Deprecation.warn("Draper::ViewContext.current_controller= is deprecated (use controller instead)", caller)
90
+ self.controller = controller
91
+ end
92
+
93
+ # @deprecated Use {build} instead.
94
+ def self.build_view_context
95
+ ActiveSupport::Deprecation.warn("Draper::ViewContext.build_view_context is deprecated (use build instead)", caller)
96
+ build
97
+ end
98
+ end
99
+ 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
@@ -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
+ Draper::ViewContext.current
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
+ Draper::ViewContext.current
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
@@ -0,0 +1,36 @@
1
+ module Rails
2
+ module Generators
3
+ class DecoratorGenerator < NamedBase
4
+ source_root File.expand_path("../templates", __FILE__)
5
+ check_class_collision suffix: "Decorator"
6
+
7
+ class_option :parent, type: :string, desc: "The parent class for the generated decorator"
8
+
9
+ def create_decorator_file
10
+ template 'decorator.rb', File.join('app/decorators', class_path, "#{file_name}_decorator.rb")
11
+ end
12
+
13
+ hook_for :test_framework
14
+
15
+ private
16
+
17
+ def parent_class_name
18
+ options.fetch("parent") do
19
+ begin
20
+ require 'application_decorator'
21
+ ApplicationDecorator
22
+ rescue LoadError
23
+ "Draper::Decorator"
24
+ end
25
+ end
26
+ end
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
35
+ end
36
+ end