From e0df75d719b83143d48823e01283cfcb867d41cf Mon Sep 17 00:00:00 2001 From: Andrew Haines Date: Tue, 5 Feb 2013 11:24:40 +0000 Subject: [PATCH] Fix ApplicationDecorator usage in generator Closes #456 --- .../decorator/decorator_generator.rb | 18 +- .../decorator/decorator_generator_spec.rb | 190 +++++++++--------- 2 files changed, 100 insertions(+), 108 deletions(-) diff --git a/lib/generators/decorator/decorator_generator.rb b/lib/generators/decorator/decorator_generator.rb index 802b11e..fb200f7 100644 --- a/lib/generators/decorator/decorator_generator.rb +++ b/lib/generators/decorator/decorator_generator.rb @@ -2,9 +2,9 @@ module Rails module Generators class DecoratorGenerator < NamedBase source_root File.expand_path("../templates", __FILE__) - check_class_collision :suffix => "Decorator" + check_class_collision suffix: "Decorator" - class_option :parent, :type => :string, :desc => "The parent class for the generated decorator" + class_option :parent, type: :string, desc: "The parent class for the generated decorator" def create_decorator_file template 'decorator.rb', File.join('app/decorators', class_path, "#{file_name}_decorator.rb") @@ -15,16 +15,16 @@ module Rails private def parent_class_name - if options[:parent] - options[:parent] - elsif defined?(ApplicationDecorator) - "ApplicationDecorator" - else - "Draper::Decorator" + options.fetch("parent") do + begin + require 'application_decorator' + ApplicationDecorator + rescue LoadError + "Draper::Decorator" + end end end - # Rails 3.0.X compatibility, stolen from https://github.com/jnunemaker/mongomapper/pull/385/files#L1R32 unless methods.include?(:module_namespacing) def module_namespacing(&block) diff --git a/spec/generators/decorator/decorator_generator_spec.rb b/spec/generators/decorator/decorator_generator_spec.rb index 054b890..daf7f29 100644 --- a/spec/generators/decorator/decorator_generator_spec.rb +++ b/spec/generators/decorator/decorator_generator_spec.rb @@ -1,137 +1,129 @@ require 'spec_helper' require 'ammeter/init' - -# Generators are not automatically loaded by Rails require 'generators/decorator/decorator_generator' describe Rails::Generators::DecoratorGenerator do - # Tell the generator where to put its output (what it thinks of as Rails.root) - destination File.expand_path("../../../../../tmp", __FILE__) + destination File.expand_path("../tmp", __FILE__) before { prepare_destination } + #after(:all) { FileUtils.rm_rf destination_root } - context 'decorator context' do - before { run_generator ["YourModel"] } + describe "the generated decorator" do + subject { file("app/decorators/your_model_decorator.rb") } - describe 'app/decorators/your_model_decorator.rb' do - subject { file('app/decorators/your_model_decorator.rb') } - it { should exist } - it { should contain "class YourModelDecorator < Draper::Decorator" } - end - end + describe "naming" do + before { run_generator %w(YourModel) } - context 'decorator name' do - before { run_generator ["YourModel", '-t=rspec'] } - - describe 'spec/decorators/your_model_decorator_spec.rb' do - subject { file('spec/decorators/your_model_decorator_spec.rb') } - it { should exist } - it { should contain "describe YourModelDecorator" } - end - end - - context 'parent decorator' do - describe 'decorator inherited from Draper::Decorator' do - before { run_generator ["YourModel"] } - - subject { file('app/decorators/your_model_decorator.rb') } - it { should exist } - it { should contain "class YourModelDecorator < Draper::Decorator" } + it { should contain "class YourModelDecorator" } end - describe "decorator inherited from ApplicationDecorator if it's present" do - before do - class ApplicationDecorator; end - run_generator ["YourModel"] + describe "namespacing" do + subject { file("app/decorators/namespace/your_model_decorator.rb") } + before { run_generator %w(Namespace::YourModel) } + + it { should contain "class Namespace::YourModelDecorator" } + end + + describe "inheritance" do + context "by default" do + before { run_generator %w(YourModel) } + + it { should contain "class YourModelDecorator < Draper::Decorator" } end - after do - Object.send(:remove_const, :ApplicationDecorator) + context "with the --parent option" do + before { run_generator %w(YourModel --parent=FooDecorator) } + + it { should contain "class YourModelDecorator < FooDecorator" } end - subject { file('app/decorators/your_model_decorator.rb') } - it { should exist } - it { should contain "class YourModelDecorator < ApplicationDecorator" } + context "with an ApplicationDecorator" do + before do + Object.any_instance.stub(:require).with("application_decorator").and_return do + stub_const "ApplicationDecorator", Class.new + end + end + + before { run_generator %w(YourModel) } + + it { should contain "class YourModelDecorator < ApplicationDecorator" } + end end end - context 'using rspec' do - before { run_generator ["YourModel", "-t=rspec"] } + context "with -t=rspec" do + describe "the generated spec" do + subject { file("spec/decorators/your_model_decorator_spec.rb") } - describe 'spec/decorators/your_model_decorator_spec.rb' do - subject { file('spec/decorators/your_model_decorator_spec.rb') } - it { should exist } - it { should contain "describe YourModelDecorator" } + describe "naming" do + before { run_generator %w(YourModel -t=rspec) } + + it { should contain "describe YourModelDecorator" } + end + + describe "namespacing" do + subject { file("spec/decorators/namespace/your_model_decorator_spec.rb") } + before { run_generator %w(Namespace::YourModel -t=rspec) } + + it { should contain "describe Namespace::YourModelDecorator" } + end end end - context 'using rspec with namespaced model' do - before { run_generator ["Namespace::YourModel", "-t=rspec"] } + context "with -t=test_unit" do + describe "the generated test" do + subject { file("test/decorators/your_model_decorator_test.rb") } - describe 'spec/decorators/your_model_decorator_spec.rb' do - subject { file('spec/decorators/namespace/your_model_decorator_spec.rb') } - it { should exist } - it { should contain "describe Namespace::YourModelDecorator" } + describe "naming" do + before { run_generator %w(YourModel -t=test_unit) } + + it { should contain "class YourModelDecoratorTest < Draper::TestCase" } + end + + describe "namespacing" do + subject { file("test/decorators/namespace/your_model_decorator_test.rb") } + before { run_generator %w(Namespace::YourModel -t=test_unit) } + + it { should contain "class Namespace::YourModelDecoratorTest < Draper::TestCase" } + end end end - context 'using test-unit' do - before { run_generator ["YourModel", "-t=test_unit"] } + context "with -t=mini_test" do + describe "the generated test" do + subject { file("test/decorators/your_model_decorator_test.rb") } - describe 'test/decorators/YourModel_decorator_test.rb' do - subject { file('test/decorators/your_model_decorator_test.rb') } - it { should exist } - it { should contain "class YourModelDecoratorTest < Draper::TestCase" } + describe "naming" do + before { run_generator %w(YourModel -t=mini_test) } + + it { should contain "class YourModelDecoratorTest < Draper::TestCase" } + end + + describe "namespacing" do + subject { file("test/decorators/namespace/your_model_decorator_test.rb") } + before { run_generator %w(Namespace::YourModel -t=mini_test) } + + it { should contain "class Namespace::YourModelDecoratorTest < Draper::TestCase" } + end end end - context 'using test-unit with namespaced model' do - before { run_generator ["Namespace::YourModel", "-t=test_unit"] } + context "with -t=mini_test --spec" do + describe "the generated test" do + subject { file("test/decorators/your_model_decorator_test.rb") } - describe 'test/decorators/your_model_decorator_test.rb' do - subject { file('test/decorators/namespace/your_model_decorator_test.rb') } - it { should exist } - it { should contain "class Namespace::YourModelDecoratorTest < Draper::TestCase" } - end - end + describe "naming" do + before { run_generator %w(YourModel -t=mini_test --spec) } - context 'using minitest-rails' do - before { run_generator ["YourModel", "-t=mini_test"] } + it { should contain "describe YourModelDecorator" } + end - describe 'test/decorators/your_model_decorator_test.rb' do - subject { file('test/decorators/your_model_decorator_test.rb') } - it { should exist } - it { should contain "class YourModelDecoratorTest < Draper::TestCase" } - end - end + describe "namespacing" do + subject { file("test/decorators/namespace/your_model_decorator_test.rb") } + before { run_generator %w(Namespace::YourModel -t=mini_test --spec) } - context 'using minitest-rails with namespaced model' do - before { run_generator ["Namespace::YourModel", "-t=mini_test"] } - - describe 'test/decorators/your_model_decorator_test.rb' do - subject { file('test/decorators/namespace/your_model_decorator_test.rb') } - it { should exist } - it { should contain "class Namespace::YourModelDecoratorTest < Draper::TestCase" } - end - end - - context 'using minitest-rails with spec syntax' do - before { run_generator ["YourModel", "-t=mini_test", "--spec"] } - - describe 'test/decorators/your_model_decorator_test.rb' do - subject { file('test/decorators/your_model_decorator_test.rb') } - it { should exist } - it { should contain "describe YourModelDecorator" } - end - end - - context 'using minitest-rails with spec syntax with namespaced model' do - before { run_generator ["Namespace::YourModel", "-t=mini_test", "--spec"] } - - describe 'test/decorators/your_model_decorator_test.rb' do - subject { file('test/decorators/namespace/your_model_decorator_test.rb') } - it { should exist } - it { should contain "describe Namespace::YourModelDecorator" } + it { should contain "describe Namespace::YourModelDecorator" } + end end end