Fix ApplicationDecorator usage in generator

Closes #456
This commit is contained in:
Andrew Haines 2013-02-05 11:24:40 +00:00
parent 55210bae17
commit e0df75d719
2 changed files with 100 additions and 108 deletions

View File

@ -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)

View File

@ -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