draper 0.18.0 → 1.1.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 +164 -0
- data/CONTRIBUTING.md +15 -0
- data/Gemfile +34 -0
- data/LICENSE +7 -0
- data/README.md +416 -0
- data/Rakefile +53 -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/equality.rb +14 -0
- data/lib/draper/decoratable.rb +79 -0
- data/lib/draper/decorated_association.rb +74 -0
- data/lib/draper/decorator.rb +235 -0
- data/lib/draper/delegation.rb +13 -0
- data/lib/draper/finders.rb +37 -0
- data/lib/draper/helper_proxy.rb +36 -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 +30 -0
- data/lib/draper/test/minitest_integration.rb +2 -3
- data/lib/draper/test/rspec_integration.rb +7 -13
- data/lib/draper/test_case.rb +53 -0
- data/lib/draper/version.rb +1 -1
- data/lib/draper/view_context/build_strategy.rb +48 -0
- data/lib/draper/view_context.rb +81 -24
- data/lib/draper/view_helpers.rb +37 -0
- data/lib/draper.rb +50 -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 +259 -0
- data/spec/draper/decoratable/equality_spec.rb +10 -0
- data/spec/draper/decoratable_spec.rb +167 -0
- data/spec/draper/decorated_association_spec.rb +145 -0
- data/spec/draper/decorator_spec.rb +598 -0
- data/spec/draper/finders_spec.rb +166 -0
- data/spec/draper/helper_proxy_spec.rb +42 -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 +18 -0
- data/spec/dummy/app/decorators/post_decorator.rb +56 -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 +34 -0
- data/spec/dummy/app/views/posts/show.html.erb +1 -0
- data/spec/dummy/bin/rails +4 -0
- data/spec/dummy/config/application.rb +70 -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 +80 -0
- data/spec/dummy/config/routes.rb +9 -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/fast_spec/post_decorator_spec.rb +38 -0
- data/spec/dummy/lib/tasks/test.rake +16 -0
- data/spec/dummy/log/.gitkeep +0 -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/devise_spec.rb +64 -0
- data/spec/dummy/spec/decorators/helpers_spec.rb +21 -0
- data/spec/dummy/spec/decorators/post_decorator_spec.rb +46 -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 +10 -0
- data/spec/dummy/spec/models/post_spec.rb +14 -0
- data/spec/dummy/spec/spec_helper.rb +9 -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 +4 -0
- data/spec/dummy/test/test_helper.rb +3 -0
- data/spec/generators/decorator/decorator_generator_spec.rb +48 -7
- data/spec/integration/integration_spec.rb +58 -0
- data/{performance → spec/performance}/decorators.rb +2 -4
- data/spec/spec_helper.rb +25 -43
- 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 +253 -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_application_helper.rb +0 -25
- 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.rb +0 -51
- data/spec/support/samples/namespaced_product_decorator.rb +0 -7
- data/spec/support/samples/non_active_model_product.rb +0 -2
- data/spec/support/samples/product.rb +0 -80
- data/spec/support/samples/product_decorator.rb +0 -7
- data/spec/support/samples/products_decorator.rb +0 -10
- 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/spec/support/samples/specific_product_decorator.rb +0 -4
- data/spec/support/samples/widget.rb +0 -2
- data/spec/support/samples/widget_decorator.rb +0 -2
- /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
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
|
|
3
|
+
if defined?(Devise)
|
|
4
|
+
describe "A decorator spec" do
|
|
5
|
+
it "can sign in a real user" do
|
|
6
|
+
user = User.new
|
|
7
|
+
sign_in user
|
|
8
|
+
|
|
9
|
+
expect(helper.current_user).to be user
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
it "can sign in a mock user" do
|
|
13
|
+
user = double("User")
|
|
14
|
+
sign_in :user, user
|
|
15
|
+
|
|
16
|
+
expect(helper.current_user).to be user
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
it "can sign in a real admin" do
|
|
20
|
+
admin = Admin.new
|
|
21
|
+
sign_in admin
|
|
22
|
+
|
|
23
|
+
expect(helper.current_admin).to be admin
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
it "can sign in a mock admin" do
|
|
27
|
+
admin = double("Admin")
|
|
28
|
+
sign_in :admin, admin
|
|
29
|
+
|
|
30
|
+
expect(helper.current_admin).to be admin
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
it "can sign out a real user" do
|
|
34
|
+
user = User.new
|
|
35
|
+
sign_in user
|
|
36
|
+
sign_out user
|
|
37
|
+
|
|
38
|
+
expect(helper.current_user).to be_nil
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
it "can sign out a mock user" do
|
|
42
|
+
user = double("User")
|
|
43
|
+
sign_in :user, user
|
|
44
|
+
sign_out :user
|
|
45
|
+
|
|
46
|
+
expect(helper.current_user).to be_nil
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
it "can sign out without a user" do
|
|
50
|
+
sign_out :user
|
|
51
|
+
|
|
52
|
+
expect(helper.current_user).to be_nil
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
it "is backwards-compatible" do
|
|
56
|
+
user = double("User")
|
|
57
|
+
ActiveSupport::Deprecation.silence do
|
|
58
|
+
sign_in user
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
expect(helper.current_user).to be user
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
end
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
|
|
3
|
+
describe "A decorator spec" do
|
|
4
|
+
it "can access helpers through `helper`" do
|
|
5
|
+
expect(helper.content_tag(:p, "Help!")).to eq "<p>Help!</p>"
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
it "can access helpers through `helpers`" do
|
|
9
|
+
expect(helpers.content_tag(:p, "Help!")).to eq "<p>Help!</p>"
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
it "can access helpers through `h`" do
|
|
13
|
+
expect(h.content_tag(:p, "Help!")).to eq "<p>Help!</p>"
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
it "gets the same helper object as a decorator" do
|
|
17
|
+
decorator = Draper::Decorator.new(Object.new)
|
|
18
|
+
|
|
19
|
+
expect(helpers).to be decorator.helpers
|
|
20
|
+
end
|
|
21
|
+
end
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
|
|
3
|
+
describe PostDecorator do
|
|
4
|
+
let(:decorator) { PostDecorator.new(source) }
|
|
5
|
+
let(:source) { Post.create }
|
|
6
|
+
|
|
7
|
+
it "can use built-in helpers" do
|
|
8
|
+
expect(decorator.truncated).to eq "Once upon a..."
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
it "can use built-in private helpers" do
|
|
12
|
+
expect(decorator.html_escaped).to eq "<script>danger</script>"
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
it "can use user-defined helpers from app/helpers" do
|
|
16
|
+
expect(decorator.hello_world).to eq "Hello, world!"
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
it "can use path helpers with its model" do
|
|
20
|
+
expect(decorator.path_with_model).to eq "/en/posts/#{source.id}"
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
it "can use path helpers with its id" do
|
|
24
|
+
expect(decorator.path_with_id).to eq "/en/posts/#{source.id}"
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
it "can use url helpers with its model" do
|
|
28
|
+
expect(decorator.url_with_model).to eq "http://www.example.com:12345/en/posts/#{source.id}"
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
it "can use url helpers with its id" do
|
|
32
|
+
expect(decorator.url_with_id).to eq "http://www.example.com:12345/en/posts/#{source.id}"
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
it "can be passed implicitly to url_for" do
|
|
36
|
+
expect(decorator.link).to eq "<a href=\"/en/posts/#{source.id}\">#{source.id}</a>"
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
it "serializes overriden attributes" do
|
|
40
|
+
expect(decorator.serializable_hash["updated_at"]).to be :overridden
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
it "uses a test view context from ApplicationController" do
|
|
44
|
+
expect(Draper::ViewContext.current.controller).to be_an ApplicationController
|
|
45
|
+
end
|
|
46
|
+
end
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
|
|
3
|
+
def it_does_not_leak_view_context
|
|
4
|
+
2.times do
|
|
5
|
+
it "has an independent view context" do
|
|
6
|
+
expect(Draper::ViewContext.current).not_to be :leaked
|
|
7
|
+
Draper::ViewContext.current = :leaked
|
|
8
|
+
end
|
|
9
|
+
end
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
describe "A decorator spec", type: :decorator do
|
|
13
|
+
it_does_not_leak_view_context
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
describe "A controller spec", type: :controller do
|
|
17
|
+
it_does_not_leak_view_context
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
describe "A mailer spec", type: :mailer do
|
|
21
|
+
it_does_not_leak_view_context
|
|
22
|
+
end
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
|
|
3
|
+
describe PostMailer do
|
|
4
|
+
describe "#decorated_email" do
|
|
5
|
+
let(:email_body) { 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
|
+
expect(email_body).to have_content "Today"
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
it "can use path helpers with a model" do
|
|
14
|
+
expect(email_body).to have_css "#path_with_model", text: "/en/posts/#{post.id}"
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
it "can use path helpers with an id" do
|
|
18
|
+
expect(email_body).to have_css "#path_with_id", text: "/en/posts/#{post.id}"
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
it "can use url helpers with a model" do
|
|
22
|
+
expect(email_body).to have_css "#url_with_model", text: "http://www.example.com:12345/en/posts/#{post.id}"
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
it "can use url helpers with an id" do
|
|
26
|
+
expect(email_body).to have_css "#url_with_id", text: "http://www.example.com:12345/en/posts/#{post.id}"
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
it "uses the correct view context controller" do
|
|
30
|
+
expect(email_body).to have_css "#controller", text: "PostMailer"
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
|
|
3
|
+
describe Post do
|
|
4
|
+
describe "#==" do
|
|
5
|
+
it "is true for other instances' decorators" do
|
|
6
|
+
Post.create
|
|
7
|
+
one = Post.first
|
|
8
|
+
other = Post.first
|
|
9
|
+
|
|
10
|
+
expect(one).not_to be other
|
|
11
|
+
expect(one == other.decorate).to be_true
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
end
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
ENV['RAILS_ENV'] ||= 'test'
|
|
2
|
+
require File.expand_path('../../config/environment', __FILE__)
|
|
3
|
+
require 'rspec/rails'
|
|
4
|
+
|
|
5
|
+
RSpec.configure do |config|
|
|
6
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
|
7
|
+
config.expect_with(:rspec) {|c| c.syntax = :expect}
|
|
8
|
+
config.order = :random
|
|
9
|
+
end
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
require 'minitest_helper'
|
|
2
|
+
|
|
3
|
+
if defined?(Devise)
|
|
4
|
+
describe "A decorator test" do
|
|
5
|
+
it "can sign in a real user" do
|
|
6
|
+
user = User.new
|
|
7
|
+
sign_in user
|
|
8
|
+
|
|
9
|
+
assert_same user, helper.current_user
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
it "can sign in a mock user" do
|
|
13
|
+
user = Object.new
|
|
14
|
+
sign_in :user, user
|
|
15
|
+
|
|
16
|
+
assert_same user, helper.current_user
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
it "can sign in a real admin" do
|
|
20
|
+
admin = Admin.new
|
|
21
|
+
sign_in admin
|
|
22
|
+
|
|
23
|
+
assert_same admin, helper.current_admin
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
it "can sign in a mock admin" do
|
|
27
|
+
admin = Object.new
|
|
28
|
+
sign_in :admin, admin
|
|
29
|
+
|
|
30
|
+
assert_same admin, helper.current_admin
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
it "can sign out a real user" do
|
|
34
|
+
user = User.new
|
|
35
|
+
sign_in user
|
|
36
|
+
sign_out user
|
|
37
|
+
|
|
38
|
+
assert helper.current_user.nil?
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
it "can sign out a mock user" do
|
|
42
|
+
user = Object.new
|
|
43
|
+
sign_in :user, user
|
|
44
|
+
sign_out :user
|
|
45
|
+
|
|
46
|
+
assert helper.current_user.nil?
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
it "can sign out without a user" do
|
|
50
|
+
sign_out :user
|
|
51
|
+
|
|
52
|
+
assert helper.current_user.nil?
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
it "is backwards-compatible" do
|
|
56
|
+
user = Object.new
|
|
57
|
+
ActiveSupport::Deprecation.silence do
|
|
58
|
+
sign_in user
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
assert_same user, helper.current_user
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
end
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
require 'minitest_helper'
|
|
2
|
+
|
|
3
|
+
describe "A decorator test" do
|
|
4
|
+
it "can access helpers through `helper`" do
|
|
5
|
+
assert_equal "<p>Help!</p>", helper.content_tag(:p, "Help!")
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
it "can access helpers through `helpers`" do
|
|
9
|
+
assert_equal "<p>Help!</p>", helpers.content_tag(:p, "Help!")
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
it "can access helpers through `h`" do
|
|
13
|
+
assert_equal "<p>Help!</p>", h.content_tag(:p, "Help!")
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
it "gets the same helper object as a decorator" do
|
|
17
|
+
decorator = Draper::Decorator.new(Object.new)
|
|
18
|
+
|
|
19
|
+
assert_same decorator.helpers, helpers
|
|
20
|
+
end
|
|
21
|
+
end
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
require 'minitest_helper'
|
|
2
|
+
|
|
3
|
+
def it_is_a_decorator_test
|
|
4
|
+
it "is a decorator test" do
|
|
5
|
+
assert_kind_of Draper::TestCase, self
|
|
6
|
+
end
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
def it_is_not_a_decorator_test
|
|
10
|
+
it "is not a decorator test" do
|
|
11
|
+
refute_kind_of Draper::TestCase, self
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
ProductDecorator = Class.new(Draper::Decorator)
|
|
16
|
+
ProductsDecorator = Class.new(Draper::CollectionDecorator)
|
|
17
|
+
|
|
18
|
+
describe ProductDecorator do
|
|
19
|
+
it_is_a_decorator_test
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
describe ProductsDecorator do
|
|
23
|
+
it_is_a_decorator_test
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
describe "ProductDecorator" do
|
|
27
|
+
it_is_a_decorator_test
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
describe "AnyDecorator" do
|
|
31
|
+
it_is_a_decorator_test
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
describe "Any decorator" do
|
|
35
|
+
it_is_a_decorator_test
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
describe "AnyDecoratorTest" do
|
|
39
|
+
it_is_a_decorator_test
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
describe "Any decorator test" do
|
|
43
|
+
it_is_a_decorator_test
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
describe Object do
|
|
47
|
+
it_is_not_a_decorator_test
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
describe "Nope" do
|
|
51
|
+
it_is_not_a_decorator_test
|
|
52
|
+
end
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
require 'minitest_helper'
|
|
2
|
+
|
|
3
|
+
def it_does_not_leak_view_context
|
|
4
|
+
2.times do
|
|
5
|
+
it "has an independent view context" do
|
|
6
|
+
refute_equal :leaked, Draper::ViewContext.current
|
|
7
|
+
Draper::ViewContext.current = :leaked
|
|
8
|
+
end
|
|
9
|
+
end
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
describe "A decorator test" do
|
|
13
|
+
it_does_not_leak_view_context
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
describe "A controller test" do
|
|
17
|
+
tests Class.new(ActionController::Base)
|
|
18
|
+
|
|
19
|
+
it_does_not_leak_view_context
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
describe "A mailer test" do
|
|
23
|
+
it_does_not_leak_view_context
|
|
24
|
+
end
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
require 'test_helper'
|
|
2
|
+
|
|
3
|
+
if defined?(Devise)
|
|
4
|
+
class DeviseTest < Draper::TestCase
|
|
5
|
+
def test_sign_in_a_real_user
|
|
6
|
+
user = User.new
|
|
7
|
+
sign_in user
|
|
8
|
+
|
|
9
|
+
assert_same user, helper.current_user
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def test_sign_in_a_mock_user
|
|
13
|
+
user = Object.new
|
|
14
|
+
sign_in :user, user
|
|
15
|
+
|
|
16
|
+
assert_same user, helper.current_user
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def test_sign_in_a_real_admin
|
|
20
|
+
admin = Admin.new
|
|
21
|
+
sign_in admin
|
|
22
|
+
|
|
23
|
+
assert_same admin, helper.current_admin
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def test_sign_in_a_mock_admin
|
|
27
|
+
admin = Object.new
|
|
28
|
+
sign_in :admin, admin
|
|
29
|
+
|
|
30
|
+
assert_same admin, helper.current_admin
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def test_sign_out_a_real_user
|
|
34
|
+
user = User.new
|
|
35
|
+
sign_in user
|
|
36
|
+
sign_out user
|
|
37
|
+
|
|
38
|
+
assert helper.current_user.nil?
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def test_sign_out_a_mock_user
|
|
42
|
+
user = Object.new
|
|
43
|
+
sign_in :user, user
|
|
44
|
+
sign_out :user
|
|
45
|
+
|
|
46
|
+
assert helper.current_user.nil?
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def test_sign_out_without_a_user
|
|
50
|
+
sign_out :user
|
|
51
|
+
|
|
52
|
+
assert helper.current_user.nil?
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def test_backwards_compatibility
|
|
56
|
+
user = Object.new
|
|
57
|
+
ActiveSupport::Deprecation.silence do
|
|
58
|
+
sign_in user
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
assert_same user, helper.current_user
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
end
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
require 'test_helper'
|
|
2
|
+
|
|
3
|
+
class HelpersTest < Draper::TestCase
|
|
4
|
+
def test_access_helpers_through_helper
|
|
5
|
+
assert_equal "<p>Help!</p>", helper.content_tag(:p, "Help!")
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
def test_access_helpers_through_helpers
|
|
9
|
+
assert_equal "<p>Help!</p>", helpers.content_tag(:p, "Help!")
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def test_access_helpers_through_h
|
|
13
|
+
assert_equal "<p>Help!</p>", h.content_tag(:p, "Help!")
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def test_same_helper_object_as_decorators
|
|
17
|
+
decorator = Draper::Decorator.new(Object.new)
|
|
18
|
+
|
|
19
|
+
assert_same decorator.helpers, helpers
|
|
20
|
+
end
|
|
21
|
+
end
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
require 'test_helper'
|
|
2
|
+
|
|
3
|
+
def it_does_not_leak_view_context
|
|
4
|
+
2.times do |n|
|
|
5
|
+
define_method("test_has_independent_view_context_#{n}") do
|
|
6
|
+
refute_equal :leaked, Draper::ViewContext.current
|
|
7
|
+
Draper::ViewContext.current = :leaked
|
|
8
|
+
end
|
|
9
|
+
end
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
class DecoratorTest < Draper::TestCase
|
|
13
|
+
it_does_not_leak_view_context
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
class ControllerTest < ActionController::TestCase
|
|
17
|
+
tests Class.new(ActionController::Base)
|
|
18
|
+
|
|
19
|
+
it_does_not_leak_view_context
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
class MailerTest < ActionMailer::TestCase
|
|
23
|
+
it_does_not_leak_view_context
|
|
24
|
+
end
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
require 'spec_helper'
|
|
2
|
+
require 'ammeter/init'
|
|
2
3
|
|
|
3
4
|
# Generators are not automatically loaded by Rails
|
|
4
5
|
require 'generators/decorator/decorator_generator'
|
|
@@ -15,8 +16,7 @@ describe Rails::Generators::DecoratorGenerator do
|
|
|
15
16
|
describe 'app/decorators/your_model_decorator.rb' do
|
|
16
17
|
subject { file('app/decorators/your_model_decorator.rb') }
|
|
17
18
|
it { should exist }
|
|
18
|
-
it { should contain "class YourModelDecorator < Draper::
|
|
19
|
-
it { should contain "decorates :your_model" }
|
|
19
|
+
it { should contain "class YourModelDecorator < Draper::Decorator" }
|
|
20
20
|
end
|
|
21
21
|
end
|
|
22
22
|
|
|
@@ -31,15 +31,15 @@ describe Rails::Generators::DecoratorGenerator do
|
|
|
31
31
|
end
|
|
32
32
|
|
|
33
33
|
context 'parent decorator' do
|
|
34
|
-
describe 'decorator
|
|
34
|
+
describe 'decorator inherited from Draper::Decorator' do
|
|
35
35
|
before { run_generator ["YourModel"] }
|
|
36
36
|
|
|
37
37
|
subject { file('app/decorators/your_model_decorator.rb') }
|
|
38
38
|
it { should exist }
|
|
39
|
-
it { should contain "class YourModelDecorator < Draper::
|
|
39
|
+
it { should contain "class YourModelDecorator < Draper::Decorator" }
|
|
40
40
|
end
|
|
41
41
|
|
|
42
|
-
describe "decorator
|
|
42
|
+
describe "decorator inherited from ApplicationDecorator if it's present" do
|
|
43
43
|
before do
|
|
44
44
|
class ApplicationDecorator; end
|
|
45
45
|
run_generator ["YourModel"]
|
|
@@ -81,7 +81,7 @@ describe Rails::Generators::DecoratorGenerator do
|
|
|
81
81
|
describe 'test/decorators/YourModel_decorator_test.rb' do
|
|
82
82
|
subject { file('test/decorators/your_model_decorator_test.rb') }
|
|
83
83
|
it { should exist }
|
|
84
|
-
it { should contain "class YourModelDecoratorTest <
|
|
84
|
+
it { should contain "class YourModelDecoratorTest < Draper::TestCase" }
|
|
85
85
|
end
|
|
86
86
|
end
|
|
87
87
|
|
|
@@ -91,7 +91,48 @@ describe Rails::Generators::DecoratorGenerator do
|
|
|
91
91
|
describe 'test/decorators/your_model_decorator_test.rb' do
|
|
92
92
|
subject { file('test/decorators/namespace/your_model_decorator_test.rb') }
|
|
93
93
|
it { should exist }
|
|
94
|
-
it { should contain "class Namespace::YourModelDecoratorTest <
|
|
94
|
+
it { should contain "class Namespace::YourModelDecoratorTest < Draper::TestCase" }
|
|
95
95
|
end
|
|
96
96
|
end
|
|
97
|
+
|
|
98
|
+
context 'using minitest-rails' do
|
|
99
|
+
before { run_generator ["YourModel", "-t=mini_test"] }
|
|
100
|
+
|
|
101
|
+
describe 'test/decorators/your_model_decorator_test.rb' do
|
|
102
|
+
subject { file('test/decorators/your_model_decorator_test.rb') }
|
|
103
|
+
it { should exist }
|
|
104
|
+
it { should contain "class YourModelDecoratorTest < Draper::TestCase" }
|
|
105
|
+
end
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
context 'using minitest-rails with namespaced model' do
|
|
109
|
+
before { run_generator ["Namespace::YourModel", "-t=mini_test"] }
|
|
110
|
+
|
|
111
|
+
describe 'test/decorators/your_model_decorator_test.rb' do
|
|
112
|
+
subject { file('test/decorators/namespace/your_model_decorator_test.rb') }
|
|
113
|
+
it { should exist }
|
|
114
|
+
it { should contain "class Namespace::YourModelDecoratorTest < Draper::TestCase" }
|
|
115
|
+
end
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
context 'using minitest-rails with spec syntax' do
|
|
119
|
+
before { run_generator ["YourModel", "-t=mini_test", "--spec"] }
|
|
120
|
+
|
|
121
|
+
describe 'test/decorators/your_model_decorator_test.rb' do
|
|
122
|
+
subject { file('test/decorators/your_model_decorator_test.rb') }
|
|
123
|
+
it { should exist }
|
|
124
|
+
it { should contain "describe YourModelDecorator" }
|
|
125
|
+
end
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
context 'using minitest-rails with spec syntax with namespaced model' do
|
|
129
|
+
before { run_generator ["Namespace::YourModel", "-t=mini_test", "--spec"] }
|
|
130
|
+
|
|
131
|
+
describe 'test/decorators/your_model_decorator_test.rb' do
|
|
132
|
+
subject { file('test/decorators/namespace/your_model_decorator_test.rb') }
|
|
133
|
+
it { should exist }
|
|
134
|
+
it { should contain "describe Namespace::YourModelDecorator" }
|
|
135
|
+
end
|
|
136
|
+
end
|
|
137
|
+
|
|
97
138
|
end
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
require 'support/dummy_app'
|
|
3
|
+
require 'support/matchers/have_text'
|
|
4
|
+
|
|
5
|
+
app = DummyApp.new(ENV["RAILS_ENV"])
|
|
6
|
+
spec_types = {
|
|
7
|
+
view: ["/posts/1", "PostsController"],
|
|
8
|
+
mailer: ["/posts/1/mail", "PostMailer"]
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
app.start_server do
|
|
12
|
+
spec_types.each do |type, (path, controller)|
|
|
13
|
+
page = app.get(path)
|
|
14
|
+
|
|
15
|
+
describe "in a #{type}" do
|
|
16
|
+
it "runs in the correct environment" do
|
|
17
|
+
expect(page).to have_text(app.environment).in("#environment")
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
it "uses the correct view context controller" do
|
|
21
|
+
expect(page).to have_text(controller).in("#controller")
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
it "can use built-in helpers" do
|
|
25
|
+
expect(page).to have_text("Once upon a...").in("#truncated")
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
it "can use built-in private helpers" do
|
|
29
|
+
# Nokogiri unescapes text!
|
|
30
|
+
expect(page).to have_text("<script>danger</script>").in("#html_escaped")
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
it "can use user-defined helpers from app/helpers" do
|
|
34
|
+
expect(page).to have_text("Hello, world!").in("#hello_world")
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
it "can use user-defined helpers from the controller" do
|
|
38
|
+
expect(page).to have_text("Goodnight, moon!").in("#goodnight_moon")
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
it "can use path helpers with a model" do
|
|
42
|
+
expect(page).to have_text("/en/posts/1").in("#path_with_model")
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
it "can use path helpers with an id" do
|
|
46
|
+
expect(page).to have_text("/en/posts/1").in("#path_with_id")
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
it "can use url helpers with a model" do
|
|
50
|
+
expect(page).to have_text("http://www.example.com:12345/en/posts/1").in("#url_with_model")
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
it "can use url helpers with an id" do
|
|
54
|
+
expect(page).to have_text("http://www.example.com:12345/en/posts/1").in("#url_with_id")
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
end
|