From 7d6789444bc80d56b365211b50309d3f8100aade Mon Sep 17 00:00:00 2001 From: Alexandre Angelim Date: Wed, 30 Nov 2011 13:36:35 -0200 Subject: [PATCH] Splitting draper:decorator into draper:install and draper:decorator, so I won't lose all my decorators when trying to destroy one of them (again!). - run rails g draper:install to create draper directories (app/decorators and spec/decorators), along with application_decorator files. - use rails g draper:install -t=test_unit to select unit tests instead of rspec. --- .../draper/decorator/decorator_generator.rb | 16 ++++--- .../draper/install/install_generator.rb | 39 ++++++++++++++++ .../templates/application_decorator.rb | 0 .../templates/application_decorator_spec.rb | 0 .../templates/application_decorator_test.rb | 0 lib/generators/rspec/decorator_generator.rb | 6 --- .../test_unit/decorator_generator.rb | 8 +--- .../decorator/decorator_generator_spec.rb | 6 --- .../draper/install/install_generator_spec.rb | 46 +++++++++++++++++++ .../rspec/decorator_generator_spec.rb | 6 --- .../test_unit/decorator_generator_spec.rb | 6 --- 11 files changed, 95 insertions(+), 38 deletions(-) create mode 100644 lib/generators/draper/install/install_generator.rb rename lib/generators/draper/{decorator => install}/templates/application_decorator.rb (100%) rename lib/generators/{rspec => draper/install}/templates/application_decorator_spec.rb (100%) rename lib/generators/{test_unit => draper/install}/templates/application_decorator_test.rb (100%) create mode 100644 spec/generators/draper/install/install_generator_spec.rb diff --git a/lib/generators/draper/decorator/decorator_generator.rb b/lib/generators/draper/decorator/decorator_generator.rb index cf602ea..363f0f2 100644 --- a/lib/generators/draper/decorator/decorator_generator.rb +++ b/lib/generators/draper/decorator/decorator_generator.rb @@ -1,16 +1,18 @@ module Draper class DecoratorGenerator < Rails::Generators::NamedBase + desc <<-DESC + Description: + Generate a decorator for the given model. + Example: rails g draper:decorator Article + generates: "app/decorators/article_decorator" + "spec/decorators/article_decorator_spec" + DESC + source_root File.expand_path('../templates', __FILE__) DECORATORS_ROOT = 'app/decorators/' - APPLICATION_DECORATOR = 'application_decorator.rb' - APPLICATION_DECORATOR_PATH = DECORATORS_ROOT + APPLICATION_DECORATOR - def build_model_and_application_decorators - empty_directory "app/decorators" - unless File.exists?(APPLICATION_DECORATOR_PATH) - template APPLICATION_DECORATOR, APPLICATION_DECORATOR_PATH - end + def build_model_decorator template 'decorator.rb', "#{DECORATORS_ROOT}#{singular_name}_decorator.rb" end diff --git a/lib/generators/draper/install/install_generator.rb b/lib/generators/draper/install/install_generator.rb new file mode 100644 index 0000000..02d3e2d --- /dev/null +++ b/lib/generators/draper/install/install_generator.rb @@ -0,0 +1,39 @@ +module Draper + class InstallGenerator < Rails::Generators::Base + + desc <<-DESC + Description: + Generate application and spec decorators in your application. + DESC + + class_option "test-framework", :type => :string, :default => "rspec", :aliases => "-t", :desc => "Test framework to be invoked" + + source_root File.expand_path('../templates', __FILE__) + + def build_application_decorator + empty_directory 'app/decorators' + template 'application_decorator.rb', File.join('app/decorators', 'application_decorator.rb') + end + + def build_decorator_tests + case options["test-framework"] + when "rspec" + build_application_decorator_spec + when "test_unit" + build_application_decorator_test + end + end + + private + def build_application_decorator_spec + empty_directory 'spec/decorators' + template 'application_decorator_spec.rb', File.join('spec/decorators', 'application_decorator_spec.rb') + end + + def build_application_decorator_test + empty_directory 'test/decorators/' + template 'application_decorator_test.rb', File.join('test/decorators', 'application_decorator_test.rb') + end + + end +end \ No newline at end of file diff --git a/lib/generators/draper/decorator/templates/application_decorator.rb b/lib/generators/draper/install/templates/application_decorator.rb similarity index 100% rename from lib/generators/draper/decorator/templates/application_decorator.rb rename to lib/generators/draper/install/templates/application_decorator.rb diff --git a/lib/generators/rspec/templates/application_decorator_spec.rb b/lib/generators/draper/install/templates/application_decorator_spec.rb similarity index 100% rename from lib/generators/rspec/templates/application_decorator_spec.rb rename to lib/generators/draper/install/templates/application_decorator_spec.rb diff --git a/lib/generators/test_unit/templates/application_decorator_test.rb b/lib/generators/draper/install/templates/application_decorator_test.rb similarity index 100% rename from lib/generators/test_unit/templates/application_decorator_test.rb rename to lib/generators/draper/install/templates/application_decorator_test.rb diff --git a/lib/generators/rspec/decorator_generator.rb b/lib/generators/rspec/decorator_generator.rb index 3d7109c..fc75fbc 100644 --- a/lib/generators/rspec/decorator_generator.rb +++ b/lib/generators/rspec/decorator_generator.rb @@ -3,14 +3,8 @@ module Rspec source_root File.expand_path('../templates', __FILE__) SPEC_ROOT = 'spec/decorators/' - APPLICATION_DECORATOR_SPEC = 'application_decorator_spec.rb' - APPLICATION_DECORATOR_SPEC_PATH = SPEC_ROOT + APPLICATION_DECORATOR_SPEC def build_model_and_application_decorator_specs - empty_directory SPEC_ROOT - unless File.exists?(APPLICATION_DECORATOR_SPEC_PATH) - template APPLICATION_DECORATOR_SPEC, APPLICATION_DECORATOR_SPEC_PATH - end template 'decorator_spec.rb', "#{SPEC_ROOT}#{singular_name}_decorator_spec.rb" end end diff --git a/lib/generators/test_unit/decorator_generator.rb b/lib/generators/test_unit/decorator_generator.rb index 7a553c0..20c0f9e 100644 --- a/lib/generators/test_unit/decorator_generator.rb +++ b/lib/generators/test_unit/decorator_generator.rb @@ -3,14 +3,8 @@ module TestUnit source_root File.expand_path('../templates', __FILE__) TEST_ROOT = 'test/decorators/' - APPLICATION_DECORATOR_TEST = 'application_decorator_test.rb' - APPLICATION_DECORATOR_TEST_PATH = TEST_ROOT + APPLICATION_DECORATOR_TEST - def build_model_and_application_decorator_tests - empty_directory TEST_ROOT - unless File.exists?(APPLICATION_DECORATOR_TEST_PATH) - template APPLICATION_DECORATOR_TEST, APPLICATION_DECORATOR_TEST_PATH - end + def build_model_decorator_tests template 'decorator_test.rb', "#{TEST_ROOT}#{singular_name}_decorator_test.rb" end end diff --git a/spec/generators/draper/decorator/decorator_generator_spec.rb b/spec/generators/draper/decorator/decorator_generator_spec.rb index 9c2817b..fb696b6 100644 --- a/spec/generators/draper/decorator/decorator_generator_spec.rb +++ b/spec/generators/draper/decorator/decorator_generator_spec.rb @@ -12,12 +12,6 @@ describe Draper::DecoratorGenerator do describe 'no arguments' do before { run_generator %w(products) } - 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 - describe 'app/decorators/products_decorator.rb' do subject { file('app/decorators/products_decorator.rb') } it { should exist } diff --git a/spec/generators/draper/install/install_generator_spec.rb b/spec/generators/draper/install/install_generator_spec.rb new file mode 100644 index 0000000..eaaa4eb --- /dev/null +++ b/spec/generators/draper/install/install_generator_spec.rb @@ -0,0 +1,46 @@ +require 'spec_helper' + +# Generators are not automatically loaded by Rails +require 'generators/draper/install/install_generator' + +describe Draper::InstallGenerator 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 } + + context 'using rspec' do + before { run_generator } + + 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 + + context "using test_unit" do + before { run_generator ["", "-t=test_unit"] } + + it_should_behave_like "ApplicationDecoratorGenerator" + + describe 'spec/decorators/application_decorator_spec.rb' do + subject { file('spec/decorators/application_decorator_spec.rb') } + it { should_not exist } + end + + describe 'spec/decorators/application_decorator_test.rb' do + subject { file('test/decorators/application_decorator_test.rb') } + it { should exist } + end + end + +end diff --git a/spec/generators/rspec/decorator_generator_spec.rb b/spec/generators/rspec/decorator_generator_spec.rb index 48d42a7..e7b19fb 100644 --- a/spec/generators/rspec/decorator_generator_spec.rb +++ b/spec/generators/rspec/decorator_generator_spec.rb @@ -12,12 +12,6 @@ describe Rspec::DecoratorGenerator do describe 'no arguments' do before { run_generator %w(products) } - 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 - describe 'spec/decorators/products_decorator_spec.rb' do subject { file('spec/decorators/products_decorator_spec.rb') } it { should exist } diff --git a/spec/generators/test_unit/decorator_generator_spec.rb b/spec/generators/test_unit/decorator_generator_spec.rb index 7d71f04..7b19cad 100644 --- a/spec/generators/test_unit/decorator_generator_spec.rb +++ b/spec/generators/test_unit/decorator_generator_spec.rb @@ -12,12 +12,6 @@ describe TestUnit::DecoratorGenerator do describe 'no arguments' do before { run_generator %w(products) } - describe 'test/decorators/application_decorator_test.rb' do - subject { file('test/decorators/application_decorator_test.rb') } - it { should exist } - it { should contain "class ApplicationDecoratorTest < ActiveSupport::TestCase" } - end - describe 'test/decorators/products_decorator_test.rb' do subject { file('test/decorators/products_decorator_test.rb') } it { should exist }