From cd95a16d14ea2003c1d8452369071ac87d493c9b Mon Sep 17 00:00:00 2001 From: Vasiliy Ermolovich Date: Sat, 12 May 2012 02:17:12 +0300 Subject: [PATCH] use ApplicationDecorator if it's present, closes #190 --- .../decorator/decorator_generator.rb | 2 +- .../decorator/templates/decorator.rb | 2 +- .../decorator/decorator_generator_spec.rb | 23 ++++++++++++++++++- 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/lib/generators/decorator/decorator_generator.rb b/lib/generators/decorator/decorator_generator.rb index eb5963f..c590b04 100644 --- a/lib/generators/decorator/decorator_generator.rb +++ b/lib/generators/decorator/decorator_generator.rb @@ -17,7 +17,7 @@ module Rails def parent_class_name if options[:parent] options[:parent] - elsif defined?(:ApplicationDecorator) + elsif defined?(ApplicationDecorator) "ApplicationDecorator" else "Draper::Base" diff --git a/lib/generators/decorator/templates/decorator.rb b/lib/generators/decorator/templates/decorator.rb index 1ef2a31..e3d3a8b 100644 --- a/lib/generators/decorator/templates/decorator.rb +++ b/lib/generators/decorator/templates/decorator.rb @@ -1,5 +1,5 @@ <% module_namespacing do -%> -class <%= class_name %>Decorator < ApplicationDecorator +class <%= class_name %>Decorator < <%= parent_class_name %> decorates :<%= singular_name %> # Accessing Helpers diff --git a/spec/generators/decorator/decorator_generator_spec.rb b/spec/generators/decorator/decorator_generator_spec.rb index 04418a7..79336ff 100644 --- a/spec/generators/decorator/decorator_generator_spec.rb +++ b/spec/generators/decorator/decorator_generator_spec.rb @@ -15,7 +15,7 @@ describe Rails::Generators::DecoratorGenerator do describe 'app/decorators/your_model_decorator.rb' do subject { file('app/decorators/your_model_decorator.rb') } it { should exist } - it { should contain "class YourModelDecorator < ApplicationDecorator" } + it { should contain "class YourModelDecorator < Draper::Base" } it { should contain "decorates :your_model" } end end @@ -30,6 +30,27 @@ describe Rails::Generators::DecoratorGenerator do end end + context 'parent decorator' do + describe 'decorator inhereted from Draper::Base' do + before { run_generator ["YourModel"] } + + subject { file('app/decorators/your_model_decorator.rb') } + it { should exist } + it { should contain "class YourModelDecorator < Draper::Base" } + end + + describe "decorator inhereted from ApplicationDecorator if it's present" do + before do + class ApplicationDecorator; end + run_generator ["YourModel"] + end + + subject { file('app/decorators/your_model_decorator.rb') } + it { should exist } + it { should contain "class YourModelDecorator < ApplicationDecorator" } + end + end + context 'using rspec' do before { run_generator ["YourModel", "-t=rspec"] }