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,69 @@
1
+ require 'rake'
2
+ require 'bundler/gem_tasks'
3
+ require 'rspec/core/rake_task'
4
+
5
+ def run_in_dummy_app(command)
6
+ success = system("cd spec/dummy && #{command}")
7
+ raise "#{command} failed" unless success
8
+ end
9
+
10
+ task "default" => "ci"
11
+
12
+ desc "Run all tests for CI"
13
+ task "ci" => "spec"
14
+
15
+ desc "Run all specs"
16
+ task "spec" => "spec:all"
17
+
18
+ namespace "spec" do
19
+ task "all" => ["draper", "generators", "integration"]
20
+
21
+ def spec_task(name)
22
+ desc "Run #{name} specs"
23
+ RSpec::Core::RakeTask.new(name) do |t|
24
+ t.pattern = "spec/#{name}/**/*_spec.rb"
25
+ end
26
+ end
27
+
28
+ spec_task "draper"
29
+ spec_task "generators"
30
+
31
+ desc "Run integration specs"
32
+ task "integration" => ["db:setup", "integration:all"]
33
+
34
+ namespace "integration" do
35
+ task "all" => ["development", "production", "test"]
36
+
37
+ ["development", "production"].each do |environment|
38
+ task environment do
39
+ Rake::Task["spec:integration:run"].execute environment
40
+ end
41
+ end
42
+
43
+ task "run" do |t, environment|
44
+ puts "Running integration specs in #{environment}"
45
+
46
+ ENV["RAILS_ENV"] = environment
47
+ success = system("rspec spec/integration")
48
+
49
+ raise "Integration specs failed in #{environment}" unless success
50
+ end
51
+
52
+ task "test" do
53
+ puts "Running rake in dummy app"
54
+ ENV["RAILS_ENV"] = "test"
55
+ run_in_dummy_app "rake"
56
+ end
57
+ end
58
+ end
59
+
60
+ namespace "db" do
61
+ desc "Set up databases for integration testing"
62
+ task "setup" do
63
+ puts "Setting up databases"
64
+ run_in_dummy_app "rm -f db/*.sqlite3"
65
+ run_in_dummy_app "RAILS_ENV=development rake db:schema:load db:seed"
66
+ run_in_dummy_app "RAILS_ENV=production rake db:schema:load db:seed"
67
+ run_in_dummy_app "RAILS_ENV=test rake db:schema:load"
68
+ end
69
+ end
@@ -0,0 +1,32 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "draper/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "jamesgolick-draper"
7
+ s.version = Draper::VERSION
8
+ s.authors = ["Jeff Casimir", "Steve Klabnik"]
9
+ s.email = ["jeff@casimircreative.com", "steve@steveklabnik.com"]
10
+ s.homepage = "http://github.com/drapergem/draper"
11
+ s.summary = "View Models for Rails"
12
+ s.description = "Draper adds an object-oriented layer of presentation logic to your Rails apps."
13
+ s.license = "MIT"
14
+
15
+ s.files = `git ls-files`.split("\n")
16
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
18
+ s.require_paths = ["lib"]
19
+
20
+ s.add_dependency 'activesupport', '>= 3.0'
21
+ s.add_dependency 'actionpack', '>= 3.0'
22
+ s.add_dependency 'request_store', '~> 1.0.3'
23
+
24
+ s.add_development_dependency 'ammeter'
25
+ s.add_development_dependency 'rake', '>= 0.9.2'
26
+ s.add_development_dependency 'rspec', '~> 2.12'
27
+ s.add_development_dependency 'rspec-mocks', '>= 2.12.1'
28
+ s.add_development_dependency 'rspec-rails', '~> 2.12'
29
+ s.add_development_dependency 'minitest-rails', '~> 0.2'
30
+ s.add_development_dependency 'capybara'
31
+ s.add_development_dependency 'active_model_serializers'
32
+ end
@@ -0,0 +1,64 @@
1
+ require 'action_view'
2
+ require 'active_model/naming'
3
+ require 'active_model/serialization'
4
+ require 'active_model/serializers/json'
5
+ require 'active_model/serializers/xml'
6
+ require 'active_support/inflector'
7
+ require 'active_support/core_ext/hash/keys'
8
+ require 'active_support/core_ext/hash/reverse_merge'
9
+
10
+ require 'draper/version'
11
+ require 'draper/view_helpers'
12
+ require 'draper/delegation'
13
+ require 'draper/automatic_delegation'
14
+ require 'draper/finders'
15
+ require 'draper/decorator'
16
+ require 'draper/helper_proxy'
17
+ require 'draper/lazy_helpers'
18
+ require 'draper/decoratable'
19
+ require 'draper/factory'
20
+ require 'draper/decorated_association'
21
+ require 'draper/helper_support'
22
+ require 'draper/view_context'
23
+ require 'draper/collection_decorator'
24
+ require 'draper/decorates_assigned'
25
+ require 'draper/railtie' if defined?(Rails)
26
+
27
+ module Draper
28
+ def self.setup_action_controller(base)
29
+ base.class_eval do
30
+ include Draper::ViewContext
31
+ extend Draper::HelperSupport
32
+ extend Draper::DecoratesAssigned
33
+
34
+ before_filter do |controller|
35
+ Draper::ViewContext.clear!
36
+ Draper::ViewContext.controller = controller
37
+ end
38
+ end
39
+ end
40
+
41
+ def self.setup_action_mailer(base)
42
+ base.class_eval do
43
+ include Draper::ViewContext
44
+ end
45
+ end
46
+
47
+ def self.setup_orm(base)
48
+ base.class_eval do
49
+ include Draper::Decoratable
50
+ end
51
+ end
52
+
53
+ class UninferrableDecoratorError < NameError
54
+ def initialize(klass)
55
+ super("Could not infer a decorator for #{klass}.")
56
+ end
57
+ end
58
+
59
+ class UninferrableSourceError < NameError
60
+ def initialize(klass)
61
+ super("Could not infer a source for #{klass}.")
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,56 @@
1
+ module Draper
2
+ module AutomaticDelegation
3
+ extend ActiveSupport::Concern
4
+
5
+ # Delegates missing instance methods to the source object.
6
+ def method_missing(method, *args, &block)
7
+ return super unless delegatable?(method)
8
+
9
+ self.class.delegate method
10
+ send(method, *args, &block)
11
+ end
12
+
13
+ # Checks if the decorator responds to an instance method, or is able to
14
+ # proxy it to the source object.
15
+ def respond_to_missing?(method, include_private = false)
16
+ super || delegatable?(method)
17
+ end
18
+
19
+ # @private
20
+ def delegatable?(method)
21
+ source.respond_to?(method)
22
+ end
23
+
24
+ module ClassMethods
25
+ # Proxies missing class methods to the source class.
26
+ def method_missing(method, *args, &block)
27
+ return super unless delegatable?(method)
28
+
29
+ source_class.send(method, *args, &block)
30
+ end
31
+
32
+ # Checks if the decorator responds to a class method, or is able to proxy
33
+ # it to the source class.
34
+ def respond_to_missing?(method, include_private = false)
35
+ super || delegatable?(method)
36
+ end
37
+
38
+ # @private
39
+ def delegatable?(method)
40
+ source_class? && source_class.respond_to?(method)
41
+ end
42
+
43
+ # @private
44
+ # Avoids reloading the model class when ActiveSupport clears autoloaded
45
+ # dependencies in development mode.
46
+ def before_remove_const
47
+ end
48
+ end
49
+
50
+ included do
51
+ private :delegatable?
52
+ private_class_method :delegatable?
53
+ end
54
+
55
+ end
56
+ end
@@ -0,0 +1,96 @@
1
+ module Draper
2
+ class CollectionDecorator
3
+ include Enumerable
4
+ include Draper::ViewHelpers
5
+ extend Draper::Delegation
6
+
7
+ # @return [Class] the decorator class used to decorate each item, as set by
8
+ # {#initialize}.
9
+ attr_reader :decorator_class
10
+
11
+ # @return [Hash] extra data to be used in user-defined methods, and passed
12
+ # to each item's decorator.
13
+ attr_accessor :context
14
+
15
+ array_methods = Array.instance_methods - Object.instance_methods
16
+ delegate :==, :as_json, *array_methods, to: :decorated_collection
17
+
18
+ # @param [Enumerable] source
19
+ # collection to decorate.
20
+ # @option options [Class, nil] :with (nil)
21
+ # the decorator class used to decorate each item. When `nil`, it is
22
+ # inferred from the collection decorator class if possible (e.g.
23
+ # `ProductsDecorator` maps to `ProductDecorator`), otherwise each item's
24
+ # {Decoratable#decorate decorate} method will be used.
25
+ # @option options [Hash] :context ({})
26
+ # extra data to be stored in the collection decorator and used in
27
+ # user-defined methods, and passed to each item's decorator.
28
+ def initialize(source, options = {})
29
+ options.assert_valid_keys(:with, :context)
30
+ @source = source
31
+ @decorator_class = options[:with]
32
+ @context = options.fetch(:context, {})
33
+ end
34
+
35
+ class << self
36
+ alias_method :decorate, :new
37
+ end
38
+
39
+ # @return [Array] the decorated items.
40
+ def decorated_collection
41
+ @decorated_collection ||= source.map{|item| decorate_item(item)}
42
+ end
43
+
44
+ # Delegated to the decorated collection when using the block form
45
+ # (`Enumerable#find`) or to the decorator class if not
46
+ # (`ActiveRecord::FinderMethods#find`)
47
+ def find(*args, &block)
48
+ if block_given?
49
+ decorated_collection.find(*args, &block)
50
+ else
51
+ decorator_class.find(*args)
52
+ end
53
+ end
54
+
55
+ def to_s
56
+ "#<#{self.class.name} of #{decorator_class || "inferred decorators"} for #{source.inspect}>"
57
+ end
58
+
59
+ def context=(value)
60
+ @context = value
61
+ each {|item| item.context = value } if @decorated_collection
62
+ end
63
+
64
+ # @return [true]
65
+ def decorated?
66
+ true
67
+ end
68
+
69
+ alias_method :decorated_with?, :instance_of?
70
+
71
+ def kind_of?(klass)
72
+ decorated_collection.kind_of?(klass) || super
73
+ end
74
+ alias_method :is_a?, :kind_of?
75
+
76
+ protected
77
+
78
+ # @return the collection being decorated.
79
+ attr_reader :source
80
+
81
+ # Decorates the given item.
82
+ def decorate_item(item)
83
+ item_decorator.call(item, context: context)
84
+ end
85
+
86
+ private
87
+
88
+ def item_decorator
89
+ if decorator_class
90
+ decorator_class.method(:decorate)
91
+ else
92
+ ->(item, options) { item.decorate(options) }
93
+ end
94
+ end
95
+ end
96
+ end
@@ -0,0 +1,82 @@
1
+ require 'draper/decoratable/equality'
2
+
3
+ module Draper
4
+ # Provides shortcuts to decorate objects directly, so you can do
5
+ # `@product.decorate` instead of `ProductDecorator.new(@product)`.
6
+ #
7
+ # This module is included by default into `ActiveRecord::Base` and
8
+ # `Mongoid::Document`, but you're using another ORM, or want to decorate
9
+ # plain old Ruby objects, you can include it manually.
10
+ module Decoratable
11
+ extend ActiveSupport::Concern
12
+ include Draper::Decoratable::Equality
13
+
14
+ # Decorates the object using the inferred {#decorator_class}.
15
+ # @param [Hash] options
16
+ # see {Decorator#initialize}
17
+ def decorate(options = {})
18
+ decorator_class.decorate(self, options)
19
+ end
20
+
21
+ # (see ClassMethods#decorator_class)
22
+ def decorator_class
23
+ self.class.decorator_class
24
+ end
25
+
26
+ # The list of decorators that have been applied to the object.
27
+ #
28
+ # @return [Array<Class>] `[]`
29
+ def applied_decorators
30
+ []
31
+ end
32
+
33
+ # (see Decorator#decorated_with?)
34
+ # @return [false]
35
+ def decorated_with?(decorator_class)
36
+ false
37
+ end
38
+
39
+ # Checks if this object is decorated.
40
+ #
41
+ # @return [false]
42
+ def decorated?
43
+ false
44
+ end
45
+
46
+ module ClassMethods
47
+
48
+ # Decorates a collection of objects. Used at the end of a scope chain.
49
+ #
50
+ # @example
51
+ # Product.popular.decorate
52
+ # @param [Hash] options
53
+ # see {Decorator.decorate_collection}.
54
+ def decorate(options = {})
55
+ collection = Rails::VERSION::MAJOR >= 4 ? all : scoped
56
+ decorator_class.decorate_collection(collection, options.reverse_merge(with: nil))
57
+ end
58
+
59
+ # Infers the decorator class to be used by {Decoratable#decorate} (e.g.
60
+ # `Product` maps to `ProductDecorator`).
61
+ #
62
+ # @return [Class] the inferred decorator class.
63
+ def decorator_class
64
+ prefix = respond_to?(:model_name) ? model_name : name
65
+ decorator_name = "#{prefix}Decorator"
66
+ decorator_name.constantize
67
+ rescue NameError => error
68
+ raise unless error.missing_name?(decorator_name)
69
+ raise Draper::UninferrableDecoratorError.new(self)
70
+ end
71
+
72
+ # Compares with possibly-decorated objects.
73
+ #
74
+ # @return [Boolean]
75
+ def ===(other)
76
+ super || (other.respond_to?(:source) && super(other.source))
77
+ end
78
+
79
+ end
80
+
81
+ end
82
+ end
@@ -0,0 +1,18 @@
1
+ module Draper
2
+ module Decoratable
3
+ module Equality
4
+ # Compares self with a possibly-decorated object.
5
+ #
6
+ # @return [Boolean]
7
+ def ==(other)
8
+ super ||
9
+ other.respond_to?(:decorated?) && other.decorated? &&
10
+ other.respond_to?(:source) && self == other.source
11
+ end
12
+
13
+ def self.test(first, other)
14
+ first == other
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,35 @@
1
+ module Draper
2
+ # @private
3
+ class DecoratedAssociation
4
+
5
+ def initialize(owner, association, options)
6
+ options.assert_valid_keys(:with, :scope, :context)
7
+
8
+ @owner = owner
9
+ @association = association
10
+
11
+ @scope = options[:scope]
12
+
13
+ decorator_class = options[:with]
14
+ context = options.fetch(:context, ->(context){ context })
15
+ @factory = Draper::Factory.new(with: decorator_class, context: context)
16
+ end
17
+
18
+ def call
19
+ decorate unless defined?(@decorated)
20
+ @decorated
21
+ end
22
+
23
+ private
24
+
25
+ attr_reader :factory, :owner, :association, :scope
26
+
27
+ def decorate
28
+ associated = owner.source.send(association)
29
+ associated = associated.send(scope) if scope
30
+
31
+ @decorated = factory.decorate(associated, context_args: owner.context)
32
+ end
33
+
34
+ end
35
+ end