draper 4.0.4 → 4.0.5
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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +6 -0
- data/README.md +3 -6
- data/lib/draper/collection_decorator.rb +0 -6
- data/lib/draper/compatibility/broadcastable.rb +24 -0
- data/lib/draper/compatibility/global_id.rb +9 -5
- data/lib/draper/decoratable.rb +5 -0
- data/lib/draper/decorator.rb +2 -18
- data/lib/draper/railtie.rb +6 -1
- data/lib/draper/version.rb +1 -1
- metadata +4 -232
- data/.github/PULL_REQUEST_TEMPLATE.md +0 -24
- data/.github/workflows/ci.yml +0 -71
- data/.gitignore +0 -18
- data/Gemfile +0 -46
- data/bin/bundle +0 -114
- data/bin/rake +0 -29
- data/draper.gemspec +0 -34
- data/spec/draper/collection_decorator_spec.rb +0 -291
- data/spec/draper/configuration_spec.rb +0 -49
- data/spec/draper/decoratable/equality_spec.rb +0 -10
- data/spec/draper/decoratable_spec.rb +0 -235
- data/spec/draper/decorated_association_spec.rb +0 -82
- data/spec/draper/decorates_assigned_spec.rb +0 -71
- data/spec/draper/decorator_spec.rb +0 -872
- data/spec/draper/draper_spec.rb +0 -25
- data/spec/draper/factory_spec.rb +0 -247
- data/spec/draper/finders_spec.rb +0 -166
- data/spec/draper/helper_proxy_spec.rb +0 -61
- data/spec/draper/lazy_helpers_spec.rb +0 -21
- data/spec/draper/query_methods/load_strategy_spec.rb +0 -26
- data/spec/draper/query_methods_spec.rb +0 -80
- data/spec/draper/undecorate_chain_spec.rb +0 -20
- data/spec/draper/undecorate_spec.rb +0 -19
- data/spec/draper/view_context/build_strategy_spec.rb +0 -115
- data/spec/draper/view_context_spec.rb +0 -182
- data/spec/draper/view_helpers_spec.rb +0 -8
- data/spec/dummy/.rspec +0 -1
- data/spec/dummy/Rakefile +0 -7
- data/spec/dummy/app/assets/config/manifest.js +0 -3
- data/spec/dummy/app/controllers/application_controller.rb +0 -2
- data/spec/dummy/app/controllers/base_controller.rb +0 -4
- data/spec/dummy/app/controllers/localized_urls.rb +0 -5
- data/spec/dummy/app/controllers/posts_controller.rb +0 -20
- data/spec/dummy/app/decorators/comment_decorator.rb +0 -13
- data/spec/dummy/app/decorators/mongoid_post_decorator.rb +0 -2
- data/spec/dummy/app/decorators/post_decorator.rb +0 -60
- data/spec/dummy/app/helpers/application_helper.rb +0 -5
- data/spec/dummy/app/jobs/publish_post_job.rb +0 -7
- data/spec/dummy/app/mailers/application_mailer.rb +0 -3
- data/spec/dummy/app/mailers/post_mailer.rb +0 -19
- data/spec/dummy/app/models/admin.rb +0 -3
- data/spec/dummy/app/models/application_record.rb +0 -3
- data/spec/dummy/app/models/comment.rb +0 -3
- data/spec/dummy/app/models/mongoid_post.rb +0 -3
- data/spec/dummy/app/models/post.rb +0 -7
- data/spec/dummy/app/models/user.rb +0 -3
- data/spec/dummy/app/views/layouts/application.html.erb +0 -11
- data/spec/dummy/app/views/post_mailer/decorated_email.html.erb +0 -1
- data/spec/dummy/app/views/posts/_post.html.erb +0 -42
- data/spec/dummy/app/views/posts/show.html.erb +0 -1
- data/spec/dummy/bin/rails +0 -4
- data/spec/dummy/config/application.rb +0 -39
- data/spec/dummy/config/boot.rb +0 -5
- data/spec/dummy/config/database.yml +0 -25
- data/spec/dummy/config/environment.rb +0 -5
- data/spec/dummy/config/environments/development.rb +0 -78
- data/spec/dummy/config/environments/production.rb +0 -102
- data/spec/dummy/config/environments/test.rb +0 -69
- data/spec/dummy/config/initializers/backtrace_silencers.rb +0 -7
- data/spec/dummy/config/initializers/draper.rb +0 -5
- data/spec/dummy/config/initializers/filter_parameter_logging.rb +0 -4
- data/spec/dummy/config/initializers/inflections.rb +0 -15
- data/spec/dummy/config/initializers/mime_types.rb +0 -5
- data/spec/dummy/config/initializers/secret_token.rb +0 -8
- data/spec/dummy/config/initializers/session_store.rb +0 -8
- data/spec/dummy/config/locales/en.yml +0 -5
- data/spec/dummy/config/mongoid.yml +0 -142
- data/spec/dummy/config/routes.rb +0 -9
- data/spec/dummy/config/storage.yml +0 -7
- data/spec/dummy/config.ru +0 -4
- data/spec/dummy/db/migrate/20121019115657_create_posts.rb +0 -8
- data/spec/dummy/db/migrate/20240907041839_create_comments.rb +0 -9
- data/spec/dummy/db/schema.rb +0 -28
- data/spec/dummy/db/seeds.rb +0 -2
- data/spec/dummy/fast_spec/post_decorator_spec.rb +0 -37
- data/spec/dummy/lib/tasks/test.rake +0 -16
- data/spec/dummy/log/.gitkeep +0 -0
- data/spec/dummy/public/404.html +0 -26
- data/spec/dummy/public/422.html +0 -26
- data/spec/dummy/public/500.html +0 -25
- data/spec/dummy/public/favicon.ico +0 -0
- data/spec/dummy/script/rails +0 -6
- data/spec/dummy/spec/decorators/active_model_serializers_spec.rb +0 -12
- data/spec/dummy/spec/decorators/devise_spec.rb +0 -55
- data/spec/dummy/spec/decorators/helpers_spec.rb +0 -21
- data/spec/dummy/spec/decorators/post_decorator_spec.rb +0 -64
- data/spec/dummy/spec/decorators/spec_type_spec.rb +0 -7
- data/spec/dummy/spec/decorators/view_context_spec.rb +0 -22
- data/spec/dummy/spec/jobs/publish_post_job_spec.rb +0 -11
- data/spec/dummy/spec/mailers/post_mailer_spec.rb +0 -25
- data/spec/dummy/spec/models/application_spec.rb +0 -7
- data/spec/dummy/spec/models/mongoid_post_spec.rb +0 -8
- data/spec/dummy/spec/models/post_spec.rb +0 -51
- data/spec/dummy/spec/rails_helper.rb +0 -69
- data/spec/dummy/spec/shared_examples/decoratable.rb +0 -22
- data/spec/dummy/spec/spec_helper.rb +0 -93
- data/spec/dummy/test/decorators/minitest/devise_test.rb +0 -55
- data/spec/dummy/test/decorators/minitest/helpers_test.rb +0 -21
- data/spec/dummy/test/decorators/minitest/spec_type_test.rb +0 -52
- data/spec/dummy/test/decorators/minitest/view_context_test.rb +0 -24
- data/spec/dummy/test/decorators/test_unit/devise_test.rb +0 -55
- data/spec/dummy/test/decorators/test_unit/helpers_test.rb +0 -21
- data/spec/dummy/test/decorators/test_unit/view_context_test.rb +0 -24
- data/spec/dummy/test/minitest_helper.rb +0 -2
- data/spec/dummy/test/test_helper.rb +0 -3
- data/spec/generators/controller/controller_generator_spec.rb +0 -23
- data/spec/generators/decorator/decorator_generator_spec.rb +0 -93
- data/spec/generators/install/install_generator_spec.rb +0 -19
- data/spec/integration/integration_spec.rb +0 -70
- data/spec/performance/active_record.rb +0 -4
- data/spec/performance/benchmark.rb +0 -55
- data/spec/performance/decorators.rb +0 -45
- data/spec/performance/models.rb +0 -20
- data/spec/spec_helper.rb +0 -50
- data/spec/support/dummy_app.rb +0 -85
- data/spec/support/matchers/have_text.rb +0 -50
- data/spec/support/shared_examples/decoratable_equality.rb +0 -40
- data/spec/support/shared_examples/view_helpers.rb +0 -39
|
@@ -1,55 +0,0 @@
|
|
|
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
|
-
end
|
|
55
|
-
end
|
|
@@ -1,21 +0,0 @@
|
|
|
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
|
|
@@ -1,24 +0,0 @@
|
|
|
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
|
-
subject{ 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,23 +0,0 @@
|
|
|
1
|
-
require 'spec_helper'
|
|
2
|
-
require 'dummy/config/environment'
|
|
3
|
-
require 'ammeter/init'
|
|
4
|
-
require 'generators/controller_override'
|
|
5
|
-
require 'generators/rails/decorator_generator'
|
|
6
|
-
SimpleCov.command_name 'test:generator'
|
|
7
|
-
|
|
8
|
-
describe Rails::Generators::ControllerGenerator do
|
|
9
|
-
destination File.expand_path("../tmp", __FILE__)
|
|
10
|
-
|
|
11
|
-
before { prepare_destination }
|
|
12
|
-
after(:all) { FileUtils.rm_rf destination_root }
|
|
13
|
-
|
|
14
|
-
describe "the generated decorator" do
|
|
15
|
-
subject { file("app/decorators/your_model_decorator.rb") }
|
|
16
|
-
|
|
17
|
-
describe "naming" do
|
|
18
|
-
before { run_generator %w(YourModels) }
|
|
19
|
-
|
|
20
|
-
it { is_expected.to contain "class YourModelDecorator" }
|
|
21
|
-
end
|
|
22
|
-
end
|
|
23
|
-
end
|
|
@@ -1,93 +0,0 @@
|
|
|
1
|
-
require 'spec_helper'
|
|
2
|
-
require 'dummy/config/environment'
|
|
3
|
-
require 'ammeter/init'
|
|
4
|
-
require 'generators/rails/decorator_generator'
|
|
5
|
-
|
|
6
|
-
describe Rails::Generators::DecoratorGenerator do
|
|
7
|
-
destination File.expand_path("../tmp", __FILE__)
|
|
8
|
-
|
|
9
|
-
before { prepare_destination }
|
|
10
|
-
after(:all) { FileUtils.rm_rf destination_root }
|
|
11
|
-
|
|
12
|
-
describe "the generated decorator" do
|
|
13
|
-
subject { file("app/decorators/your_model_decorator.rb") }
|
|
14
|
-
|
|
15
|
-
describe "naming" do
|
|
16
|
-
before { run_generator %w(YourModel) }
|
|
17
|
-
|
|
18
|
-
it { is_expected.to contain "class YourModelDecorator" }
|
|
19
|
-
end
|
|
20
|
-
|
|
21
|
-
describe "namespacing" do
|
|
22
|
-
subject { file("app/decorators/namespace/your_model_decorator.rb") }
|
|
23
|
-
before { run_generator %w(Namespace::YourModel) }
|
|
24
|
-
|
|
25
|
-
it { is_expected.to contain "class Namespace::YourModelDecorator" }
|
|
26
|
-
end
|
|
27
|
-
|
|
28
|
-
describe "inheritance" do
|
|
29
|
-
context "by default" do
|
|
30
|
-
before { run_generator %w(YourModel) }
|
|
31
|
-
|
|
32
|
-
it { is_expected.to contain "class YourModelDecorator < Draper::Decorator" }
|
|
33
|
-
end
|
|
34
|
-
|
|
35
|
-
context "with the --parent option" do
|
|
36
|
-
before { run_generator %w(YourModel --parent=FooDecorator) }
|
|
37
|
-
|
|
38
|
-
it { is_expected.to contain "class YourModelDecorator < FooDecorator" }
|
|
39
|
-
end
|
|
40
|
-
|
|
41
|
-
context "with an ApplicationDecorator" do
|
|
42
|
-
before do
|
|
43
|
-
allow_any_instance_of(Object).to receive(:require).and_call_original
|
|
44
|
-
allow_any_instance_of(Object).to receive(:require).with("application_decorator").and_return(
|
|
45
|
-
stub_const "ApplicationDecorator", Class.new
|
|
46
|
-
)
|
|
47
|
-
end
|
|
48
|
-
|
|
49
|
-
before { run_generator %w(YourModel) }
|
|
50
|
-
|
|
51
|
-
it { is_expected.to contain "class YourModelDecorator < ApplicationDecorator" }
|
|
52
|
-
end
|
|
53
|
-
end
|
|
54
|
-
end
|
|
55
|
-
|
|
56
|
-
context "with -t=rspec" do
|
|
57
|
-
describe "the generated spec" do
|
|
58
|
-
subject { file("spec/decorators/your_model_decorator_spec.rb") }
|
|
59
|
-
|
|
60
|
-
describe "naming" do
|
|
61
|
-
before { run_generator %w(YourModel -t=rspec) }
|
|
62
|
-
|
|
63
|
-
it { is_expected.to contain "describe YourModelDecorator" }
|
|
64
|
-
end
|
|
65
|
-
|
|
66
|
-
describe "namespacing" do
|
|
67
|
-
subject { file("spec/decorators/namespace/your_model_decorator_spec.rb") }
|
|
68
|
-
before { run_generator %w(Namespace::YourModel -t=rspec) }
|
|
69
|
-
|
|
70
|
-
it { is_expected.to contain "describe Namespace::YourModelDecorator" }
|
|
71
|
-
end
|
|
72
|
-
end
|
|
73
|
-
end
|
|
74
|
-
|
|
75
|
-
context "with -t=test_unit" do
|
|
76
|
-
describe "the generated test" do
|
|
77
|
-
subject { file("test/decorators/your_model_decorator_test.rb") }
|
|
78
|
-
|
|
79
|
-
describe "naming" do
|
|
80
|
-
before { run_generator %w(YourModel -t=test_unit) }
|
|
81
|
-
|
|
82
|
-
it { is_expected.to contain "class YourModelDecoratorTest < Draper::TestCase" }
|
|
83
|
-
end
|
|
84
|
-
|
|
85
|
-
describe "namespacing" do
|
|
86
|
-
subject { file("test/decorators/namespace/your_model_decorator_test.rb") }
|
|
87
|
-
before { run_generator %w(Namespace::YourModel -t=test_unit) }
|
|
88
|
-
|
|
89
|
-
it { is_expected.to contain "class Namespace::YourModelDecoratorTest < Draper::TestCase" }
|
|
90
|
-
end
|
|
91
|
-
end
|
|
92
|
-
end
|
|
93
|
-
end
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
require 'spec_helper'
|
|
2
|
-
require 'dummy/config/environment'
|
|
3
|
-
require 'ammeter/init'
|
|
4
|
-
require 'generators/draper/install_generator'
|
|
5
|
-
|
|
6
|
-
describe Draper::Generators::InstallGenerator do
|
|
7
|
-
destination File.expand_path('../tmp', __FILE__)
|
|
8
|
-
|
|
9
|
-
before { prepare_destination }
|
|
10
|
-
after(:all) { FileUtils.rm_rf destination_root }
|
|
11
|
-
|
|
12
|
-
describe 'the application decorator' do
|
|
13
|
-
subject { file('app/decorators/application_decorator.rb') }
|
|
14
|
-
|
|
15
|
-
before { run_generator }
|
|
16
|
-
|
|
17
|
-
it { is_expected.to contain 'class ApplicationDecorator' }
|
|
18
|
-
end
|
|
19
|
-
end
|
|
@@ -1,70 +0,0 @@
|
|
|
1
|
-
require 'spec_helper'
|
|
2
|
-
require 'support/dummy_app'
|
|
3
|
-
require 'support/matchers/have_text'
|
|
4
|
-
SimpleCov.command_name 'test:integration'
|
|
5
|
-
|
|
6
|
-
app = DummyApp.new(ENV["RAILS_ENV"])
|
|
7
|
-
spec_types = {
|
|
8
|
-
view: ["/posts/1", "PostsController"],
|
|
9
|
-
mailer: ["/posts/1/mail", "PostMailer"]
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
app.start_server do
|
|
13
|
-
spec_types.each do |type, (path, controller)|
|
|
14
|
-
page = app.get(path)
|
|
15
|
-
|
|
16
|
-
describe "in a #{type}" do
|
|
17
|
-
it "runs in the correct environment" do
|
|
18
|
-
expect(page).to have_text(app.environment).in("#environment")
|
|
19
|
-
end
|
|
20
|
-
|
|
21
|
-
it "uses the correct view context controller" do
|
|
22
|
-
expect(page).to have_text(controller).in("#controller")
|
|
23
|
-
end
|
|
24
|
-
|
|
25
|
-
it "can use built-in helpers" do
|
|
26
|
-
expect(page).to have_text("Once upon a...").in("#truncated")
|
|
27
|
-
end
|
|
28
|
-
|
|
29
|
-
it "can use built-in private helpers" do
|
|
30
|
-
# Nokogiri unescapes text!
|
|
31
|
-
expect(page).to have_text("<script>danger</script>").in("#html_escaped")
|
|
32
|
-
end
|
|
33
|
-
|
|
34
|
-
it "can use user-defined helpers from app/helpers" do
|
|
35
|
-
expect(page).to have_text("Hello, world!").in("#hello_world")
|
|
36
|
-
end
|
|
37
|
-
|
|
38
|
-
it "can use user-defined helpers from the controller" do
|
|
39
|
-
expect(page).to have_text("Goodnight, moon!").in("#goodnight_moon")
|
|
40
|
-
end
|
|
41
|
-
|
|
42
|
-
# _path helpers aren't available in mailers
|
|
43
|
-
if type == :view
|
|
44
|
-
it "can be passed to path helpers" do
|
|
45
|
-
expect(page).to have_text("/en/posts/1").in("#path_with_decorator")
|
|
46
|
-
end
|
|
47
|
-
|
|
48
|
-
it "can use path helpers with a model" do
|
|
49
|
-
expect(page).to have_text("/en/posts/1").in("#path_with_model")
|
|
50
|
-
end
|
|
51
|
-
|
|
52
|
-
it "can use path helpers with an id" do
|
|
53
|
-
expect(page).to have_text("/en/posts/1").in("#path_with_id")
|
|
54
|
-
end
|
|
55
|
-
end
|
|
56
|
-
|
|
57
|
-
it "can be passed to url helpers" do
|
|
58
|
-
expect(page).to have_text("http://www.example.com:12345/en/posts/1").in("#url_with_decorator")
|
|
59
|
-
end
|
|
60
|
-
|
|
61
|
-
it "can use url helpers with a model" do
|
|
62
|
-
expect(page).to have_text("http://www.example.com:12345/en/posts/1").in("#url_with_model")
|
|
63
|
-
end
|
|
64
|
-
|
|
65
|
-
it "can use url helpers with an id" do
|
|
66
|
-
expect(page).to have_text("http://www.example.com:12345/en/posts/1").in("#url_with_id")
|
|
67
|
-
end
|
|
68
|
-
end
|
|
69
|
-
end
|
|
70
|
-
end
|
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
require 'rubygems'
|
|
2
|
-
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)
|
|
3
|
-
require 'bundler/setup' if File.exist?(ENV['BUNDLE_GEMFILE'])
|
|
4
|
-
Bundler.require :default
|
|
5
|
-
|
|
6
|
-
require "benchmark"
|
|
7
|
-
require "draper"
|
|
8
|
-
require "./performance/models"
|
|
9
|
-
require "./performance/decorators"
|
|
10
|
-
|
|
11
|
-
Benchmark.bm do |bm|
|
|
12
|
-
puts "\n[ Exclusivelly using #method_missing for model delegation ]"
|
|
13
|
-
[ 1_000, 10_000, 100_000 ].each do |i|
|
|
14
|
-
puts "\n[ #{i} ]"
|
|
15
|
-
bm.report("#new ") do
|
|
16
|
-
i.times do |n|
|
|
17
|
-
ProductDecorator.decorate(Product.new)
|
|
18
|
-
end
|
|
19
|
-
end
|
|
20
|
-
|
|
21
|
-
bm.report("#hello_world ") do
|
|
22
|
-
i.times do |n|
|
|
23
|
-
ProductDecorator.decorate(Product.new).hello_world
|
|
24
|
-
end
|
|
25
|
-
end
|
|
26
|
-
|
|
27
|
-
bm.report("#sample_class_method ") do
|
|
28
|
-
i.times do |n|
|
|
29
|
-
ProductDecorator.decorate(Product.new).class.sample_class_method
|
|
30
|
-
end
|
|
31
|
-
end
|
|
32
|
-
end
|
|
33
|
-
|
|
34
|
-
puts "\n[ Defining methods on method_missing first hit ]"
|
|
35
|
-
[ 1_000, 10_000, 100_000 ].each do |i|
|
|
36
|
-
puts "\n[ #{i} ]"
|
|
37
|
-
bm.report("#new ") do
|
|
38
|
-
i.times do |n|
|
|
39
|
-
FastProductDecorator.decorate(FastProduct.new)
|
|
40
|
-
end
|
|
41
|
-
end
|
|
42
|
-
|
|
43
|
-
bm.report("#hello_world ") do
|
|
44
|
-
i.times do |n|
|
|
45
|
-
FastProductDecorator.decorate(FastProduct.new).hello_world
|
|
46
|
-
end
|
|
47
|
-
end
|
|
48
|
-
|
|
49
|
-
bm.report("#sample_class_method ") do
|
|
50
|
-
i.times do |n|
|
|
51
|
-
FastProductDecorator.decorate(FastProduct.new).class.sample_class_method
|
|
52
|
-
end
|
|
53
|
-
end
|
|
54
|
-
end
|
|
55
|
-
end
|
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
require "./performance/models"
|
|
2
|
-
class ProductDecorator < Draper::Decorator
|
|
3
|
-
|
|
4
|
-
def awesome_title
|
|
5
|
-
"Awesome Title"
|
|
6
|
-
end
|
|
7
|
-
|
|
8
|
-
# Original #method_missing
|
|
9
|
-
def method_missing(method, *args, &block)
|
|
10
|
-
if allow?(method)
|
|
11
|
-
begin
|
|
12
|
-
model.send(method, *args, &block)
|
|
13
|
-
rescue NoMethodError
|
|
14
|
-
super
|
|
15
|
-
end
|
|
16
|
-
else
|
|
17
|
-
super
|
|
18
|
-
end
|
|
19
|
-
end
|
|
20
|
-
|
|
21
|
-
end
|
|
22
|
-
|
|
23
|
-
class FastProductDecorator < Draper::Decorator
|
|
24
|
-
|
|
25
|
-
def awesome_title
|
|
26
|
-
"Awesome Title"
|
|
27
|
-
end
|
|
28
|
-
|
|
29
|
-
# Modified #method_missing
|
|
30
|
-
def method_missing(method, *args, &block)
|
|
31
|
-
if allow?(method)
|
|
32
|
-
begin
|
|
33
|
-
self.class.send :define_method, method do |*args, &block|
|
|
34
|
-
model.send(method, *args, &block)
|
|
35
|
-
end
|
|
36
|
-
self.send(method, *args, &block)
|
|
37
|
-
rescue NoMethodError
|
|
38
|
-
super
|
|
39
|
-
end
|
|
40
|
-
else
|
|
41
|
-
super
|
|
42
|
-
end
|
|
43
|
-
end
|
|
44
|
-
|
|
45
|
-
end
|
data/spec/performance/models.rb
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
require "./performance/active_record"
|
|
2
|
-
class Product < ActiveRecord::Base
|
|
3
|
-
def self.sample_class_method
|
|
4
|
-
"sample class method"
|
|
5
|
-
end
|
|
6
|
-
|
|
7
|
-
def hello_world
|
|
8
|
-
"Hello, World"
|
|
9
|
-
end
|
|
10
|
-
end
|
|
11
|
-
|
|
12
|
-
class FastProduct < ActiveRecord::Base
|
|
13
|
-
def self.sample_class_method
|
|
14
|
-
"sample class method"
|
|
15
|
-
end
|
|
16
|
-
|
|
17
|
-
def hello_world
|
|
18
|
-
"Hello, World"
|
|
19
|
-
end
|
|
20
|
-
end
|
data/spec/spec_helper.rb
DELETED
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
require 'simplecov'
|
|
2
|
-
SimpleCov.start do
|
|
3
|
-
add_filter 'spec'
|
|
4
|
-
add_group 'Draper', 'lib/draper'
|
|
5
|
-
add_group 'Generators', 'lib/generators'
|
|
6
|
-
end
|
|
7
|
-
|
|
8
|
-
require 'bundler/setup'
|
|
9
|
-
require 'draper'
|
|
10
|
-
require 'action_controller'
|
|
11
|
-
require 'action_controller/test_case'
|
|
12
|
-
|
|
13
|
-
RSpec.configure do |config|
|
|
14
|
-
config.expect_with(:rspec) {|c| c.syntax = :expect}
|
|
15
|
-
config.order = :random
|
|
16
|
-
config.mock_with :rspec do |mocks|
|
|
17
|
-
mocks.yield_receiver_to_any_instance_implementation_blocks = true
|
|
18
|
-
end
|
|
19
|
-
end
|
|
20
|
-
|
|
21
|
-
class Model; include Draper::Decoratable; end
|
|
22
|
-
|
|
23
|
-
class Product < Model; end
|
|
24
|
-
class SpecialProduct < Product; end
|
|
25
|
-
class Other < Model; end
|
|
26
|
-
class Person < Model; end
|
|
27
|
-
class Child < Person; end
|
|
28
|
-
class ProductDecorator < Draper::Decorator; end
|
|
29
|
-
class ProductsDecorator < Draper::CollectionDecorator; end
|
|
30
|
-
|
|
31
|
-
class OtherDecorator < Draper::Decorator; end
|
|
32
|
-
|
|
33
|
-
module Namespaced
|
|
34
|
-
class Product < Model; end
|
|
35
|
-
class ProductDecorator < Draper::Decorator; end
|
|
36
|
-
ProductsDecorator = Class.new(Draper::CollectionDecorator)
|
|
37
|
-
class OtherDecorator < Draper::Decorator; end
|
|
38
|
-
end
|
|
39
|
-
|
|
40
|
-
ApplicationController = Class.new(ActionController::Base)
|
|
41
|
-
CustomController = Class.new(ActionController::Base)
|
|
42
|
-
|
|
43
|
-
# After each example, revert changes made to the class
|
|
44
|
-
def protect_class(klass)
|
|
45
|
-
before { stub_const klass.name, Class.new(klass) }
|
|
46
|
-
end
|
|
47
|
-
|
|
48
|
-
def protect_module(mod)
|
|
49
|
-
before { stub_const mod.name, mod.dup }
|
|
50
|
-
end
|
data/spec/support/dummy_app.rb
DELETED
|
@@ -1,85 +0,0 @@
|
|
|
1
|
-
require 'socket'
|
|
2
|
-
require 'net/http'
|
|
3
|
-
|
|
4
|
-
# Adapted from code by Jon Leighton
|
|
5
|
-
# https://github.com/jonleighton/focused_controller/blob/ec7ccf1/test/acceptance/app_test.rb
|
|
6
|
-
|
|
7
|
-
class DummyApp
|
|
8
|
-
|
|
9
|
-
def initialize(environment)
|
|
10
|
-
raise ArgumentError, "Environment must be development or production" unless ["development", "production"].include?(environment.to_s)
|
|
11
|
-
@environment = environment
|
|
12
|
-
end
|
|
13
|
-
|
|
14
|
-
attr_reader :environment
|
|
15
|
-
|
|
16
|
-
def url
|
|
17
|
-
"http://#{localhost}:#{port}"
|
|
18
|
-
end
|
|
19
|
-
|
|
20
|
-
def get(path)
|
|
21
|
-
Net::HTTP.get(URI(url + path))
|
|
22
|
-
end
|
|
23
|
-
|
|
24
|
-
def within_app(&block)
|
|
25
|
-
Dir.chdir(root, &block)
|
|
26
|
-
end
|
|
27
|
-
|
|
28
|
-
def start_server
|
|
29
|
-
within_app do
|
|
30
|
-
IO.popen("bundle exec rails s -e #{@environment} -p #{port} 2>&1") do |out|
|
|
31
|
-
start = Time.now
|
|
32
|
-
started = false
|
|
33
|
-
output = ""
|
|
34
|
-
timeout = 60.0
|
|
35
|
-
|
|
36
|
-
while !started && !out.eof? && Time.now - start <= timeout
|
|
37
|
-
output << read_output(out)
|
|
38
|
-
sleep 0.1
|
|
39
|
-
|
|
40
|
-
begin
|
|
41
|
-
TCPSocket.new(localhost, port)
|
|
42
|
-
rescue Errno::ECONNREFUSED
|
|
43
|
-
else
|
|
44
|
-
started = true
|
|
45
|
-
end
|
|
46
|
-
end
|
|
47
|
-
|
|
48
|
-
raise "Server failed to start:\n#{output}" unless started
|
|
49
|
-
|
|
50
|
-
yield
|
|
51
|
-
|
|
52
|
-
Process.kill("KILL", out.pid)
|
|
53
|
-
File.delete("tmp/pids/server.pid") if File.exist?("tmp/pids/server.pid")
|
|
54
|
-
end
|
|
55
|
-
end
|
|
56
|
-
end
|
|
57
|
-
|
|
58
|
-
private
|
|
59
|
-
|
|
60
|
-
def root
|
|
61
|
-
File.expand_path("../../dummy", __FILE__)
|
|
62
|
-
end
|
|
63
|
-
|
|
64
|
-
def localhost
|
|
65
|
-
"127.0.0.1"
|
|
66
|
-
end
|
|
67
|
-
|
|
68
|
-
def port
|
|
69
|
-
@port ||= begin
|
|
70
|
-
server = TCPServer.new(localhost, 0)
|
|
71
|
-
server.addr[1]
|
|
72
|
-
ensure
|
|
73
|
-
server.close if server
|
|
74
|
-
end
|
|
75
|
-
end
|
|
76
|
-
|
|
77
|
-
def read_output(stream)
|
|
78
|
-
read = IO.select([stream], [], [stream], 0.1)
|
|
79
|
-
output = ""
|
|
80
|
-
loop { output << stream.read_nonblock(1024) } if read
|
|
81
|
-
output
|
|
82
|
-
rescue Errno::EAGAIN, Errno::EWOULDBLOCK, EOFError
|
|
83
|
-
output
|
|
84
|
-
end
|
|
85
|
-
end
|
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
require 'capybara'
|
|
2
|
-
|
|
3
|
-
module HaveTextMatcher
|
|
4
|
-
def have_text(text)
|
|
5
|
-
HaveText.new(text)
|
|
6
|
-
end
|
|
7
|
-
|
|
8
|
-
class HaveText
|
|
9
|
-
def initialize(text)
|
|
10
|
-
@text = text
|
|
11
|
-
end
|
|
12
|
-
|
|
13
|
-
def in(css)
|
|
14
|
-
@css = css
|
|
15
|
-
self
|
|
16
|
-
end
|
|
17
|
-
|
|
18
|
-
def matches?(subject)
|
|
19
|
-
@subject = Capybara.string(subject)
|
|
20
|
-
|
|
21
|
-
@subject.has_css?(@css || "*", text: @text)
|
|
22
|
-
end
|
|
23
|
-
|
|
24
|
-
def failure_message
|
|
25
|
-
"expected to find #{@text.inspect} #{within}"
|
|
26
|
-
end
|
|
27
|
-
|
|
28
|
-
def failure_message_when_negated
|
|
29
|
-
"expected not to find #{@text.inspect} #{within}"
|
|
30
|
-
end
|
|
31
|
-
|
|
32
|
-
private
|
|
33
|
-
|
|
34
|
-
def within
|
|
35
|
-
if @css && @subject.has_css?(@css)
|
|
36
|
-
"within\n#{@subject.find(@css).native}"
|
|
37
|
-
else
|
|
38
|
-
"#{inside} within\n#{@subject.native}"
|
|
39
|
-
end
|
|
40
|
-
end
|
|
41
|
-
|
|
42
|
-
def inside
|
|
43
|
-
@css ? "inside #{@css.inspect}" : "anywhere"
|
|
44
|
-
end
|
|
45
|
-
end
|
|
46
|
-
end
|
|
47
|
-
|
|
48
|
-
RSpec.configure do |config|
|
|
49
|
-
config.include HaveTextMatcher
|
|
50
|
-
end
|
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
shared_examples_for "decoration-aware #==" do |subject|
|
|
2
|
-
it "is true for itself" do
|
|
3
|
-
expect(subject == subject).to be_truthy
|
|
4
|
-
end
|
|
5
|
-
|
|
6
|
-
it "is false for another object" do
|
|
7
|
-
expect(subject == Object.new).to be_falsey
|
|
8
|
-
end
|
|
9
|
-
|
|
10
|
-
it "is true for a decorated version of itself" do
|
|
11
|
-
decorated = double(object: subject, decorated?: true)
|
|
12
|
-
|
|
13
|
-
expect(subject == decorated).to be_truthy
|
|
14
|
-
end
|
|
15
|
-
|
|
16
|
-
it "is false for a decorated other object" do
|
|
17
|
-
decorated = double(object: Object.new, decorated?: true)
|
|
18
|
-
|
|
19
|
-
expect(subject == decorated).to be_falsey
|
|
20
|
-
end
|
|
21
|
-
|
|
22
|
-
it "is false for a decoratable object with a `object` association" do
|
|
23
|
-
decoratable = double(object: subject, decorated?: false)
|
|
24
|
-
|
|
25
|
-
expect(subject == decoratable).to be_falsey
|
|
26
|
-
end
|
|
27
|
-
|
|
28
|
-
it "is false for an undecoratable object with a `object` association" do
|
|
29
|
-
undecoratable = double(object: subject)
|
|
30
|
-
|
|
31
|
-
expect(subject == undecoratable).to be_falsey
|
|
32
|
-
end
|
|
33
|
-
|
|
34
|
-
it "is true for a multiply-decorated version of itself" do
|
|
35
|
-
decorated = double(object: subject, decorated?: true)
|
|
36
|
-
redecorated = double(object: decorated, decorated?: true)
|
|
37
|
-
|
|
38
|
-
expect(subject == redecorated).to be_truthy
|
|
39
|
-
end
|
|
40
|
-
end
|