diff --git a/lib/generators/draper/decorator/decorator_generator.rb b/lib/generators/draper/decorator/decorator_generator.rb index 41110ca..5f9e9e1 100644 --- a/lib/generators/draper/decorator/decorator_generator.rb +++ b/lib/generators/draper/decorator/decorator_generator.rb @@ -1,5 +1,5 @@ module Draper - class DecoratorGenerator < Rails::Generators::NamedBase + class DecoratorGenerator < Rails::Generators::Base desc <<-DESC Description: Generate a decorator for the given model. @@ -8,14 +8,36 @@ module Draper "spec/decorators/article_decorator_spec" DESC + argument :resource_name, :type => :string + class_option "test-framework", :type => :string, :default => "rspec", :aliases => "-t", :desc => "Test framework to be invoked" + source_root File.expand_path('../templates', __FILE__) DECORATORS_ROOT = 'app/decorators/' def build_model_decorator - template 'decorator.rb', "#{DECORATORS_ROOT}#{singular_name}_decorator.rb" + template 'decorator.rb', "#{DECORATORS_ROOT}#{resource_name.singularize}_decorator.rb" + end +# + def build_decorator_tests + case options["test-framework"] + when "rspec" + build_decorator_spec + when "test_unit" + build_decorator_test + end + end + + private + def build_decorator_spec + empty_directory 'spec/decorators' + template 'decorator_spec.rb', File.join('spec/decorators', "#{resource_name.singularize}_decorator_spec.rb") + end + + def build_decorator_test + empty_directory 'test/decorators/' + template 'decorator_test.rb', File.join('test/decorators', "#{resource_name.singularize}_decorator_test.rb") end - hook_for :test_framework end end diff --git a/lib/generators/draper/decorator/templates/decorator.rb b/lib/generators/draper/decorator/templates/decorator.rb index c60bed6..beeeb88 100644 --- a/lib/generators/draper/decorator/templates/decorator.rb +++ b/lib/generators/draper/decorator/templates/decorator.rb @@ -1,5 +1,5 @@ -class <%= singular_name.camelize %>Decorator < ApplicationDecorator - decorates :<%= singular_name.to_sym %> +class <%= resource_name.singularize.camelize %>Decorator < ApplicationDecorator + decorates :<%= resource_name.singularize.to_sym %> # Accessing Helpers # You can access any helper via a proxy diff --git a/lib/generators/rspec/templates/decorator_spec.rb b/lib/generators/draper/decorator/templates/decorator_spec.rb similarity index 59% rename from lib/generators/rspec/templates/decorator_spec.rb rename to lib/generators/draper/decorator/templates/decorator_spec.rb index adb4015..78556cc 100644 --- a/lib/generators/rspec/templates/decorator_spec.rb +++ b/lib/generators/draper/decorator/templates/decorator_spec.rb @@ -1,5 +1,5 @@ require 'spec_helper' -describe <%= singular_name.camelize %>Decorator do +describe <%= resource_name.singularize.camelize %>Decorator do before { ApplicationController.new.set_current_view_context } end diff --git a/lib/generators/test_unit/templates/decorator_test.rb b/lib/generators/draper/decorator/templates/decorator_test.rb similarity index 63% rename from lib/generators/test_unit/templates/decorator_test.rb rename to lib/generators/draper/decorator/templates/decorator_test.rb index dce4bf1..fd2f64d 100644 --- a/lib/generators/test_unit/templates/decorator_test.rb +++ b/lib/generators/draper/decorator/templates/decorator_test.rb @@ -1,6 +1,6 @@ require 'test_helper' -class <%= singular_name.camelize %>DecoratorTest < ActiveSupport::TestCase +class <%= resource_name.singularize.camelize %>DecoratorTest < ActiveSupport::TestCase def setup ApplicationController.new.set_current_view_context end diff --git a/lib/generators/rspec/decorator_generator.rb b/lib/generators/rspec/decorator_generator.rb deleted file mode 100644 index fc75fbc..0000000 --- a/lib/generators/rspec/decorator_generator.rb +++ /dev/null @@ -1,11 +0,0 @@ -module Rspec - class DecoratorGenerator < ::Rails::Generators::NamedBase - source_root File.expand_path('../templates', __FILE__) - - SPEC_ROOT = 'spec/decorators/' - - def build_model_and_application_decorator_specs - template 'decorator_spec.rb', "#{SPEC_ROOT}#{singular_name}_decorator_spec.rb" - end - end -end diff --git a/lib/generators/test_unit/decorator_generator.rb b/lib/generators/test_unit/decorator_generator.rb deleted file mode 100644 index 20c0f9e..0000000 --- a/lib/generators/test_unit/decorator_generator.rb +++ /dev/null @@ -1,11 +0,0 @@ -module TestUnit - class DecoratorGenerator < ::Rails::Generators::NamedBase - source_root File.expand_path('../templates', __FILE__) - - TEST_ROOT = 'test/decorators/' - - def build_model_decorator_tests - template 'decorator_test.rb', "#{TEST_ROOT}#{singular_name}_decorator_test.rb" - end - end -end diff --git a/spec/generators/draper/decorator/decorator_generator_spec.rb b/spec/generators/draper/decorator/decorator_generator_spec.rb index fb696b6..4c79d3a 100644 --- a/spec/generators/draper/decorator/decorator_generator_spec.rb +++ b/spec/generators/draper/decorator/decorator_generator_spec.rb @@ -9,6 +9,50 @@ describe Draper::DecoratorGenerator do before { prepare_destination } + context 'decorator context' do + before { run_generator ["product"] } + + describe 'app/decorators/product_decorator.rb' do + subject { file('app/decorators/product_decorator.rb') } + it { should exist } + it { should contain "class ProductDecorator < ApplicationDecorator" } + end + end + + context 'default test framework' do + before { run_generator ["product"] } + + describe 'spec/decorators/product_decorator_spec.rb' do + subject { file('spec/decorators/product_decorator_spec.rb') } + it { should exist } + it { should contain "describe ProductDecorator" } + end + end + + context 'using rspec' do + before { run_generator ["product", "-t=rspec"] } + + describe 'spec/decorators/product_decorator_spec.rb' do + subject { file('spec/decorators/product_decorator_spec.rb') } + it { should exist } + it { should contain "describe ProductDecorator" } + end + end + + context 'using rspec' do + before { run_generator ["product", "-t=test_unit"] } + + describe 'test/decorators/product_decorator_test.rb' do + subject { file('test/decorators/product_decorator_test.rb') } + it { should exist } + it { should contain "class ProductDecoratorTest < ActiveSupport::TestCase" } + end + end + +end + + +=begin describe 'no arguments' do before { run_generator %w(products) } @@ -17,6 +61,43 @@ describe Draper::DecoratorGenerator do it { should exist } it { should contain "class ProductsDecorator < ApplicationDecorator" } end - end -end + + + context 'simple' do + before { run_generator %w(products) } + + describe 'app/decorators/products_decorator.rb' do + subject { file('app/decorators/products_decorator.rb') } + it { should exist } + it { should contain "class ProductsDecorator < ApplicationDecorator" } + end + end + + + + + + context 'using rspec' do + + describe 'app/decorators/products_decorator.rb' do + subject { file('app/decorators/products_decorator.rb') } + it { should exist } + it { should contain "class ProductsDecorator < ApplicationDecorator" } + end + + shared_examples_for "ApplicationDecoratorGenerator" do + describe 'app/decorators/application_decorator.rb' do + subject { file('app/decorators/application_decorator.rb') } + it { should exist } + it { should contain "class ApplicationDecorator < Draper::Base" } + end + end + + describe 'spec/decorators/application_decorator_spec.rb' do + subject { file('spec/decorators/application_decorator_spec.rb') } + it { should exist } + it { should contain "describe ApplicationDecorator do" } + end + end +=end \ No newline at end of file diff --git a/spec/generators/rspec/decorator_generator_spec.rb b/spec/generators/rspec/decorator_generator_spec.rb deleted file mode 100644 index e7b19fb..0000000 --- a/spec/generators/rspec/decorator_generator_spec.rb +++ /dev/null @@ -1,22 +0,0 @@ -require 'spec_helper' - -# Generators are not automatically loaded by Rails -require 'generators/rspec/decorator_generator' - -describe Rspec::DecoratorGenerator do - # Tell the generator where to put its output (what it thinks of as Rails.root) - destination File.expand_path("../../../../tmp", __FILE__) - - before { prepare_destination } - - describe 'no arguments' do - before { run_generator %w(products) } - - describe 'spec/decorators/products_decorator_spec.rb' do - subject { file('spec/decorators/products_decorator_spec.rb') } - it { should exist } - it { should contain "describe ProductsDecorator" } - end - - end -end diff --git a/spec/generators/test_unit/decorator_generator_spec.rb b/spec/generators/test_unit/decorator_generator_spec.rb deleted file mode 100644 index 7b19cad..0000000 --- a/spec/generators/test_unit/decorator_generator_spec.rb +++ /dev/null @@ -1,22 +0,0 @@ -require 'spec_helper' - -# Generators are not automatically loaded by Rails -require 'generators/test_unit/decorator_generator' - -describe TestUnit::DecoratorGenerator do - # Tell the generator where to put its output (what it thinks of as Rails.root) - destination File.expand_path("../../../../tmp", __FILE__) - - before { prepare_destination } - - describe 'no arguments' do - before { run_generator %w(products) } - - describe 'test/decorators/products_decorator_test.rb' do - subject { file('test/decorators/products_decorator_test.rb') } - it { should exist } - it { should contain "class ProductsDecoratorTest < ActiveSupport::TestCase" } - end - - end -end