draper 0.18.0 → 1.0.0.beta1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (123) hide show
  1. data/.gitignore +3 -0
  2. data/.travis.yml +5 -0
  3. data/CHANGELOG.markdown +8 -0
  4. data/Gemfile +4 -0
  5. data/Rakefile +50 -31
  6. data/Readme.markdown +42 -48
  7. data/draper.gemspec +2 -1
  8. data/lib/draper.rb +42 -7
  9. data/lib/draper/collection_decorator.rb +88 -0
  10. data/lib/draper/decoratable.rb +44 -0
  11. data/lib/draper/decorated_association.rb +55 -0
  12. data/lib/draper/decorator.rb +208 -0
  13. data/lib/draper/finders.rb +44 -0
  14. data/lib/draper/helper_proxy.rb +16 -0
  15. data/lib/draper/railtie.rb +17 -21
  16. data/lib/draper/security.rb +48 -0
  17. data/lib/draper/tasks/tu.rake +5 -0
  18. data/lib/draper/test/minitest_integration.rb +1 -1
  19. data/lib/draper/test/test_unit_integration.rb +9 -0
  20. data/lib/draper/version.rb +1 -1
  21. data/lib/draper/view_context.rb +6 -13
  22. data/lib/draper/view_helpers.rb +36 -0
  23. data/lib/generators/decorator/decorator_generator.rb +1 -1
  24. data/lib/generators/decorator/templates/decorator.rb +0 -1
  25. data/spec/draper/collection_decorator_spec.rb +240 -0
  26. data/spec/draper/decoratable_spec.rb +164 -0
  27. data/spec/draper/decorated_association_spec.rb +130 -0
  28. data/spec/draper/decorator_spec.rb +497 -0
  29. data/spec/draper/finders_spec.rb +156 -0
  30. data/spec/draper/helper_proxy_spec.rb +12 -0
  31. data/spec/draper/security_spec.rb +158 -0
  32. data/spec/draper/view_helpers_spec.rb +41 -0
  33. data/spec/dummy/.rspec +2 -0
  34. data/spec/dummy/README.rdoc +261 -0
  35. data/spec/dummy/Rakefile +7 -0
  36. data/spec/dummy/app/controllers/application_controller.rb +4 -0
  37. data/spec/dummy/app/controllers/localized_urls.rb +5 -0
  38. data/spec/dummy/app/controllers/posts_controller.rb +17 -0
  39. data/spec/dummy/app/decorators/post_decorator.rb +25 -0
  40. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  41. data/spec/dummy/app/mailers/application_mailer.rb +3 -0
  42. data/spec/dummy/app/mailers/post_mailer.rb +9 -0
  43. data/spec/dummy/app/models/post.rb +3 -0
  44. data/spec/dummy/app/views/layouts/application.html.erb +11 -0
  45. data/spec/dummy/app/views/post_mailer/decorated_email.html.erb +1 -0
  46. data/spec/dummy/app/views/posts/_post.html.erb +19 -0
  47. data/spec/dummy/app/views/posts/show.html.erb +1 -0
  48. data/spec/dummy/config.ru +4 -0
  49. data/spec/dummy/config/application.rb +64 -0
  50. data/spec/dummy/config/boot.rb +10 -0
  51. data/spec/dummy/config/database.yml +25 -0
  52. data/spec/dummy/config/environment.rb +5 -0
  53. data/spec/dummy/config/environments/development.rb +34 -0
  54. data/spec/dummy/config/environments/production.rb +55 -0
  55. data/spec/dummy/config/environments/test.rb +32 -0
  56. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  57. data/spec/dummy/config/initializers/inflections.rb +15 -0
  58. data/spec/dummy/config/initializers/mime_types.rb +5 -0
  59. data/spec/dummy/config/initializers/secret_token.rb +7 -0
  60. data/spec/dummy/config/initializers/session_store.rb +8 -0
  61. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  62. data/spec/dummy/config/locales/en.yml +5 -0
  63. data/spec/dummy/config/routes.rb +7 -0
  64. data/spec/dummy/db/migrate/20121019115657_create_posts.rb +8 -0
  65. data/spec/dummy/db/schema.rb +21 -0
  66. data/spec/dummy/db/seeds.rb +2 -0
  67. data/spec/dummy/lib/tasks/spec.rake +5 -0
  68. data/spec/dummy/public/404.html +26 -0
  69. data/spec/dummy/public/422.html +26 -0
  70. data/spec/dummy/public/500.html +25 -0
  71. data/spec/dummy/public/favicon.ico +0 -0
  72. data/spec/dummy/script/rails +6 -0
  73. data/spec/dummy/spec/decorators/post_decorator_spec.rb +23 -0
  74. data/spec/dummy/spec/mailers/post_mailer_spec.rb +29 -0
  75. data/spec/dummy/spec/spec_helper.rb +9 -0
  76. data/spec/generators/decorator/decorator_generator_spec.rb +3 -4
  77. data/spec/integration/integration_spec.rb +33 -0
  78. data/{performance → spec/performance}/active_record.rb +0 -0
  79. data/{performance/bechmark.rb → spec/performance/benchmark.rb} +0 -0
  80. data/{performance → spec/performance}/decorators.rb +2 -4
  81. data/{performance → spec/performance}/models.rb +0 -0
  82. data/spec/spec_helper.rb +20 -41
  83. data/spec/support/action_controller.rb +12 -0
  84. data/spec/support/active_model.rb +7 -0
  85. data/spec/support/{samples/active_record.rb → active_record.rb} +5 -0
  86. data/spec/support/{samples → decorators}/decorator_with_application_helper.rb +1 -1
  87. data/spec/support/decorators/namespaced_product_decorator.rb +5 -0
  88. data/spec/support/decorators/non_active_model_product_decorator.rb +2 -0
  89. data/spec/support/decorators/product_decorator.rb +19 -0
  90. data/spec/support/{samples → decorators}/products_decorator.rb +1 -1
  91. data/spec/support/decorators/some_thing_decorator.rb +2 -0
  92. data/spec/support/{samples → decorators}/specific_product_decorator.rb +0 -2
  93. data/spec/support/{samples → decorators}/widget_decorator.rb +0 -0
  94. data/spec/support/dummy_app.rb +84 -0
  95. data/spec/support/matchers/have_text.rb +50 -0
  96. data/spec/support/{samples → models}/namespaced_product.rb +1 -3
  97. data/spec/support/{samples → models}/non_active_model_product.rb +1 -0
  98. data/spec/support/{samples → models}/product.rb +13 -2
  99. data/spec/support/models/some_thing.rb +5 -0
  100. data/spec/support/models/uninferrable_decorator_model.rb +3 -0
  101. data/spec/support/{samples → models}/widget.rb +0 -0
  102. metadata +185 -68
  103. data/lib/draper/active_model_support.rb +0 -27
  104. data/lib/draper/base.rb +0 -312
  105. data/lib/draper/decorated_enumerable_proxy.rb +0 -90
  106. data/lib/draper/model_support.rb +0 -25
  107. data/lib/draper/rspec_integration.rb +0 -2
  108. data/lib/draper/system.rb +0 -18
  109. data/spec/draper/base_spec.rb +0 -873
  110. data/spec/draper/decorated_enumerable_proxy_spec.rb +0 -45
  111. data/spec/draper/model_support_spec.rb +0 -48
  112. data/spec/support/samples/decorator.rb +0 -5
  113. data/spec/support/samples/decorator_with_allows.rb +0 -3
  114. data/spec/support/samples/decorator_with_denies.rb +0 -3
  115. data/spec/support/samples/decorator_with_denies_all.rb +0 -3
  116. data/spec/support/samples/decorator_with_multiple_allows.rb +0 -4
  117. data/spec/support/samples/decorator_with_special_methods.rb +0 -13
  118. data/spec/support/samples/enumerable_proxy.rb +0 -3
  119. data/spec/support/samples/namespaced_product_decorator.rb +0 -7
  120. data/spec/support/samples/product_decorator.rb +0 -7
  121. data/spec/support/samples/sequel_product.rb +0 -13
  122. data/spec/support/samples/some_thing.rb +0 -2
  123. data/spec/support/samples/some_thing_decorator.rb +0 -3
@@ -0,0 +1,5 @@
1
+ # Sample localization file for English. Add more files in this directory for other locales.
2
+ # See https://github.com/svenfuchs/rails-i18n/tree/master/rails%2Flocale for starting points.
3
+
4
+ en:
5
+ hello: "Hello world"
@@ -0,0 +1,7 @@
1
+ Dummy::Application.routes.draw do
2
+ scope "(:locale)", locale: /en|zh/ do
3
+ resources :posts, only: [:show] do
4
+ get "mail", on: :member
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,8 @@
1
+ class CreatePosts < ActiveRecord::Migration
2
+ def change
3
+ create_table :posts do |t|
4
+
5
+ t.timestamps
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,21 @@
1
+ # encoding: UTF-8
2
+ # This file is auto-generated from the current state of the database. Instead
3
+ # of editing this file, please use the migrations feature of Active Record to
4
+ # incrementally modify your database, and then regenerate this schema definition.
5
+ #
6
+ # Note that this schema.rb definition is the authoritative source for your
7
+ # database schema. If you need to create the application database on another
8
+ # system, you should be using db:schema:load, not running all the migrations
9
+ # from scratch. The latter is a flawed and unsustainable approach (the more migrations
10
+ # you'll amass, the slower it'll run and the greater likelihood for issues).
11
+ #
12
+ # It's strongly recommended to check this file into your version control system.
13
+
14
+ ActiveRecord::Schema.define(:version => 20121019115657) do
15
+
16
+ create_table "posts", :force => true do |t|
17
+ t.datetime "created_at", :null => false
18
+ t.datetime "updated_at", :null => false
19
+ end
20
+
21
+ end
@@ -0,0 +1,2 @@
1
+ Post.delete_all
2
+ Post.create id: 1
@@ -0,0 +1,5 @@
1
+ require 'rspec/core/rake_task'
2
+
3
+ RSpec::Core::RakeTask.new
4
+
5
+ task "default" => "spec"
@@ -0,0 +1,26 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>The page you were looking for doesn't exist (404)</title>
5
+ <style type="text/css">
6
+ body { background-color: #fff; color: #666; text-align: center; font-family: arial, sans-serif; }
7
+ div.dialog {
8
+ width: 25em;
9
+ padding: 0 4em;
10
+ margin: 4em auto 0 auto;
11
+ border: 1px solid #ccc;
12
+ border-right-color: #999;
13
+ border-bottom-color: #999;
14
+ }
15
+ h1 { font-size: 100%; color: #f00; line-height: 1.5em; }
16
+ </style>
17
+ </head>
18
+
19
+ <body>
20
+ <!-- This file lives in public/404.html -->
21
+ <div class="dialog">
22
+ <h1>The page you were looking for doesn't exist.</h1>
23
+ <p>You may have mistyped the address or the page may have moved.</p>
24
+ </div>
25
+ </body>
26
+ </html>
@@ -0,0 +1,26 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>The change you wanted was rejected (422)</title>
5
+ <style type="text/css">
6
+ body { background-color: #fff; color: #666; text-align: center; font-family: arial, sans-serif; }
7
+ div.dialog {
8
+ width: 25em;
9
+ padding: 0 4em;
10
+ margin: 4em auto 0 auto;
11
+ border: 1px solid #ccc;
12
+ border-right-color: #999;
13
+ border-bottom-color: #999;
14
+ }
15
+ h1 { font-size: 100%; color: #f00; line-height: 1.5em; }
16
+ </style>
17
+ </head>
18
+
19
+ <body>
20
+ <!-- This file lives in public/422.html -->
21
+ <div class="dialog">
22
+ <h1>The change you wanted was rejected.</h1>
23
+ <p>Maybe you tried to change something you didn't have access to.</p>
24
+ </div>
25
+ </body>
26
+ </html>
@@ -0,0 +1,25 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>We're sorry, but something went wrong (500)</title>
5
+ <style type="text/css">
6
+ body { background-color: #fff; color: #666; text-align: center; font-family: arial, sans-serif; }
7
+ div.dialog {
8
+ width: 25em;
9
+ padding: 0 4em;
10
+ margin: 4em auto 0 auto;
11
+ border: 1px solid #ccc;
12
+ border-right-color: #999;
13
+ border-bottom-color: #999;
14
+ }
15
+ h1 { font-size: 100%; color: #f00; line-height: 1.5em; }
16
+ </style>
17
+ </head>
18
+
19
+ <body>
20
+ <!-- This file lives in public/500.html -->
21
+ <div class="dialog">
22
+ <h1>We're sorry, but something went wrong.</h1>
23
+ </div>
24
+ </body>
25
+ </html>
File without changes
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+ # This command will automatically be run when you run "rails" with Rails 3 gems installed from the root of your application.
3
+
4
+ APP_PATH = File.expand_path('../../config/application', __FILE__)
5
+ require File.expand_path('../../config/boot', __FILE__)
6
+ require 'rails/commands'
@@ -0,0 +1,23 @@
1
+ require 'spec_helper'
2
+
3
+ describe PostDecorator do
4
+ subject { PostDecorator.new(source) }
5
+ let(:source) { Post.create }
6
+
7
+ it "can use path helpers with its model" do
8
+ subject.path_with_model.should == "/en/posts/#{source.id}"
9
+ end
10
+
11
+ it "can use path helpers with its id" do
12
+ subject.path_with_id.should == "/en/posts/#{source.id}"
13
+ end
14
+
15
+ it "can use url helpers with its model" do
16
+ subject.url_with_model.should == "http://www.example.com/en/posts/#{source.id}"
17
+ end
18
+
19
+ it "can use url helpers with its id" do
20
+ subject.url_with_id.should == "http://www.example.com/en/posts/#{source.id}"
21
+ end
22
+
23
+ end
@@ -0,0 +1,29 @@
1
+ require 'spec_helper'
2
+
3
+ describe PostMailer do
4
+ describe "#decorated_email" do
5
+ subject { Capybara.string(email.body.to_s) }
6
+ let(:email) { PostMailer.decorated_email(post).deliver }
7
+ let(:post) { Post.create }
8
+
9
+ it "decorates" do
10
+ subject.should have_content "Today"
11
+ end
12
+
13
+ it "can use path helpers with a model" do
14
+ subject.should have_css "#path_with_model", text: "/en/posts/#{post.id}"
15
+ end
16
+
17
+ it "can use path helpers with an id" do
18
+ subject.should have_css "#path_with_id", text: "/en/posts/#{post.id}"
19
+ end
20
+
21
+ it "can use url helpers with a model" do
22
+ subject.should have_css "#url_with_model", text: "http://www.example.com/en/posts/#{post.id}"
23
+ end
24
+
25
+ it "can use url helpers with an id" do
26
+ subject.should have_css "#url_with_id", text: "http://www.example.com/en/posts/#{post.id}"
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,9 @@
1
+ ENV['RAILS_ENV'] ||= 'test'
2
+ require File.expand_path('../../config/environment', __FILE__)
3
+ require 'rspec/rails'
4
+ require 'draper/test/rspec_integration'
5
+
6
+ RSpec.configure do |config|
7
+ config.treat_symbols_as_metadata_keys_with_true_values = true
8
+ config.order = :random
9
+ end
@@ -15,8 +15,7 @@ describe Rails::Generators::DecoratorGenerator do
15
15
  describe 'app/decorators/your_model_decorator.rb' do
16
16
  subject { file('app/decorators/your_model_decorator.rb') }
17
17
  it { should exist }
18
- it { should contain "class YourModelDecorator < Draper::Base" }
19
- it { should contain "decorates :your_model" }
18
+ it { should contain "class YourModelDecorator < Draper::Decorator" }
20
19
  end
21
20
  end
22
21
 
@@ -31,12 +30,12 @@ describe Rails::Generators::DecoratorGenerator do
31
30
  end
32
31
 
33
32
  context 'parent decorator' do
34
- describe 'decorator inhereted from Draper::Base' do
33
+ describe 'decorator inhereted from Draper::Decorator' do
35
34
  before { run_generator ["YourModel"] }
36
35
 
37
36
  subject { file('app/decorators/your_model_decorator.rb') }
38
37
  it { should exist }
39
- it { should contain "class YourModelDecorator < Draper::Base" }
38
+ it { should contain "class YourModelDecorator < Draper::Decorator" }
40
39
  end
41
40
 
42
41
  describe "decorator inhereted from ApplicationDecorator if it's present" do
@@ -0,0 +1,33 @@
1
+ require 'spec_helper'
2
+ require 'support/dummy_app'
3
+ require 'support/matchers/have_text'
4
+
5
+ app = DummyApp.new(ENV["RAILS_ENV"])
6
+
7
+ app.start_server do
8
+ {view: "/posts/1", mailer: "/posts/1/mail"}.each do |type, path|
9
+ page = app.get(path)
10
+
11
+ describe "in a #{type}" do
12
+ it "runs in the correct environment" do
13
+ page.should have_text(app.environment).in("#environment")
14
+ end
15
+
16
+ it "can use path helpers with a model" do
17
+ page.should have_text("/en/posts/1").in("#path_with_model")
18
+ end
19
+
20
+ it "can use path helpers with an id" do
21
+ page.should have_text("/en/posts/1").in("#path_with_id")
22
+ end
23
+
24
+ it "can use url helpers with a model" do
25
+ page.should have_text("http://www.example.com/en/posts/1").in("#url_with_model")
26
+ end
27
+
28
+ it "can use url helpers with an id" do
29
+ page.should have_text("http://www.example.com/en/posts/1").in("#url_with_id")
30
+ end
31
+ end
32
+ end
33
+ end
File without changes
@@ -1,6 +1,5 @@
1
1
  require "./performance/models"
2
- class ProductDecorator < Draper::Base
3
- decorates :product
2
+ class ProductDecorator < Draper::Decorator
4
3
 
5
4
  def awesome_title
6
5
  "Awesome Title"
@@ -21,8 +20,7 @@ class ProductDecorator < Draper::Base
21
20
 
22
21
  end
23
22
 
24
- class FastProductDecorator < Draper::Base
25
- decorates :product
23
+ class FastProductDecorator < Draper::Decorator
26
24
 
27
25
  def awesome_title
28
26
  "Awesome Title"
File without changes
data/spec/spec_helper.rb CHANGED
@@ -3,51 +3,30 @@ require 'ammeter/init'
3
3
  require 'rails'
4
4
 
5
5
  require 'action_view'
6
- Bundler.require
7
-
8
- require './spec/support/samples/active_record'
9
- require './spec/support/samples/decorator'
10
- require './spec/support/samples/decorator_with_allows'
11
- require './spec/support/samples/decorator_with_multiple_allows'
12
- require './spec/support/samples/decorator_with_application_helper'
13
- require './spec/support/samples/decorator_with_denies'
14
- require './spec/support/samples/decorator_with_denies_all'
15
- require './spec/support/samples/decorator_with_special_methods'
16
- require './spec/support/samples/enumerable_proxy'
17
- require './spec/support/samples/namespaced_product'
18
- require './spec/support/samples/namespaced_product_decorator'
19
- require './spec/support/samples/non_active_model_product'
20
- require './spec/support/samples/product'
21
- require './spec/support/samples/product_decorator'
22
- require './spec/support/samples/products_decorator'
23
- require './spec/support/samples/sequel_product'
24
- require './spec/support/samples/specific_product_decorator'
25
- require './spec/support/samples/some_thing'
26
- require './spec/support/samples/some_thing_decorator'
27
- require './spec/support/samples/widget'
28
- require './spec/support/samples/widget_decorator'
29
-
30
6
  require 'action_controller'
31
7
  require 'action_controller/test_case'
32
8
 
33
- module ActionController
34
- class Base
35
- Draper::System.setup_action_controller(self)
36
- end
37
- end
38
-
39
- module ActiveRecord
40
- class Relation
41
- end
42
- end
43
-
44
- class ApplicationController < ActionController::Base
45
- def hello_world
46
- "Hello, World!"
47
- end
48
- helper_method :hello_world
49
- end
9
+ Bundler.require
50
10
 
11
+ require './spec/support/active_model'
12
+ require './spec/support/active_record'
13
+ require './spec/support/action_controller'
14
+
15
+ require './spec/support/models/product'
16
+ require './spec/support/models/namespaced_product'
17
+ require './spec/support/models/non_active_model_product'
18
+ require './spec/support/models/widget'
19
+ require './spec/support/models/some_thing'
20
+ require './spec/support/models/uninferrable_decorator_model'
21
+
22
+ require './spec/support/decorators/product_decorator'
23
+ require './spec/support/decorators/namespaced_product_decorator'
24
+ require './spec/support/decorators/non_active_model_product_decorator'
25
+ require './spec/support/decorators/widget_decorator'
26
+ require './spec/support/decorators/specific_product_decorator'
27
+ require './spec/support/decorators/products_decorator'
28
+ require './spec/support/decorators/some_thing_decorator'
29
+ require './spec/support/decorators/decorator_with_application_helper'
51
30
 
52
31
  class << Rails
53
32
  undef application # Avoid silly Rails bug: https://github.com/rails/rails/pull/6429
@@ -0,0 +1,12 @@
1
+ module ActionController
2
+ class Base
3
+ Draper.setup_action_controller(self)
4
+ end
5
+ end
6
+
7
+ class ApplicationController < ActionController::Base
8
+ def hello_world
9
+ "Hello, World!"
10
+ end
11
+ helper_method :hello_world
12
+ end
@@ -0,0 +1,7 @@
1
+ module ActiveModel
2
+ module Serialization
3
+ def serializable_hash
4
+ {overridable: send(:overridable)}
5
+ end
6
+ end
7
+ end
@@ -15,3 +15,8 @@ module ActiveRecord
15
15
 
16
16
  end
17
17
  end
18
+
19
+ module ActiveRecord
20
+ class Relation
21
+ end
22
+ end
@@ -1,4 +1,4 @@
1
- class DecoratorWithApplicationHelper < Draper::Base
1
+ class DecoratorWithApplicationHelper < Draper::Decorator
2
2
  def uses_hello_world
3
3
  h.hello_world
4
4
  end
@@ -0,0 +1,5 @@
1
+ module Namespace
2
+ class ProductDecorator < Draper::Decorator
3
+ has_finders
4
+ end
5
+ end
@@ -0,0 +1,2 @@
1
+ class NonActiveModelProductDecorator
2
+ end
@@ -0,0 +1,19 @@
1
+ class ProductDecorator < Draper::Decorator
2
+ has_finders
3
+
4
+ def awesome_title
5
+ "Awesome Title"
6
+ end
7
+
8
+ def overridable
9
+ :overridden
10
+ end
11
+
12
+ def self.my_class_method
13
+ end
14
+
15
+ private
16
+
17
+ def awesome_private_title
18
+ end
19
+ end