From 49f01d90867a58f89505e10c426ec56337029f00 Mon Sep 17 00:00:00 2001 From: Vasiliy Ermolovich Date: Sat, 28 Apr 2012 22:26:57 +0300 Subject: [PATCH] rework generators --- .../draper/decorator/decorator_generator.rb | 46 ------------- .../decorator/templates/decorator_spec.rb | 4 -- lib/generators/draper/decorator_generator.rb | 21 ++++++ .../draper/install/install_generator.rb | 39 ----------- lib/generators/draper/install_generator.rb | 20 ++++++ .../templates/application_decorator.rb | 0 .../templates/application_decorator_spec.rb | 0 .../templates/application_decorator_test.rb | 0 .../{decorator => }/templates/decorator.rb | 4 +- lib/generators/rails/decorator_generator.rb | 15 ----- lib/generators/rspec/decorator_generator.rb | 9 +++ .../rspec/templates/decorator_spec.rb | 4 ++ .../test_unit/decorator_generator.rb | 9 +++ .../templates/decorator_test.rb | 2 +- .../decorator/decorator_generator_spec.rb | 67 +------------------ .../draper/install/install_generator_spec.rb | 8 ++- spec/spec_helper.rb | 2 + 17 files changed, 76 insertions(+), 174 deletions(-) delete mode 100644 lib/generators/draper/decorator/decorator_generator.rb delete mode 100644 lib/generators/draper/decorator/templates/decorator_spec.rb create mode 100644 lib/generators/draper/decorator_generator.rb delete mode 100644 lib/generators/draper/install/install_generator.rb create mode 100644 lib/generators/draper/install_generator.rb rename lib/generators/draper/{install => }/templates/application_decorator.rb (100%) rename lib/generators/draper/{install => }/templates/application_decorator_spec.rb (100%) rename lib/generators/draper/{install => }/templates/application_decorator_test.rb (100%) rename lib/generators/draper/{decorator => }/templates/decorator.rb (86%) delete mode 100644 lib/generators/rails/decorator_generator.rb create mode 100644 lib/generators/rspec/decorator_generator.rb create mode 100644 lib/generators/rspec/templates/decorator_spec.rb create mode 100644 lib/generators/test_unit/decorator_generator.rb rename lib/generators/{draper/decorator => test_unit}/templates/decorator_test.rb (63%) diff --git a/lib/generators/draper/decorator/decorator_generator.rb b/lib/generators/draper/decorator/decorator_generator.rb deleted file mode 100644 index ff4face..0000000 --- a/lib/generators/draper/decorator/decorator_generator.rb +++ /dev/null @@ -1,46 +0,0 @@ -module Draper - class DecoratorGenerator < Rails::Generators::Base - 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 - - 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}#{decorator_name}_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', "#{decorator_name}_decorator_spec.rb") - end - - def build_decorator_test - empty_directory 'test/decorators/' - template 'decorator_test.rb', File.join('test/decorators', "#{decorator_name}_decorator_test.rb") - end - - def decorator_name - resource_name.underscore.singularize - end - end -end diff --git a/lib/generators/draper/decorator/templates/decorator_spec.rb b/lib/generators/draper/decorator/templates/decorator_spec.rb deleted file mode 100644 index b4677f0..0000000 --- a/lib/generators/draper/decorator/templates/decorator_spec.rb +++ /dev/null @@ -1,4 +0,0 @@ -require 'spec_helper' - -describe <%= resource_name.singularize.camelize %>Decorator do -end diff --git a/lib/generators/draper/decorator_generator.rb b/lib/generators/draper/decorator_generator.rb new file mode 100644 index 0000000..890c2ce --- /dev/null +++ b/lib/generators/draper/decorator_generator.rb @@ -0,0 +1,21 @@ +module Draper + module Generators + class DecoratorGenerator < Rails::Generators::NamedBase + source_root File.expand_path('../templates', __FILE__) + + 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 + + def create_decorator_file + template 'decorator.rb', File.join('app/decorators', "#{singular_name}_decorator.rb") + end + + hook_for :test_framework + end + end +end diff --git a/lib/generators/draper/install/install_generator.rb b/lib/generators/draper/install/install_generator.rb deleted file mode 100644 index ee18425..0000000 --- a/lib/generators/draper/install/install_generator.rb +++ /dev/null @@ -1,39 +0,0 @@ -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 diff --git a/lib/generators/draper/install_generator.rb b/lib/generators/draper/install_generator.rb new file mode 100644 index 0000000..d4dfe86 --- /dev/null +++ b/lib/generators/draper/install_generator.rb @@ -0,0 +1,20 @@ +module Draper + module Generators + class InstallGenerator < Rails::Generators::Base + source_root File.expand_path('../templates', __FILE__) + + desc <<-DESC + Description: + Generate application and spec decorators in your application. + DESC + + def create_decorator_file + template 'application_decorator.rb', File.join('app/decorators', 'application_decorator.rb') + end + + hook_for :test_framework, :as => :decorator do |test_framework| + invoke test_framework, ['application'] + end + end + end +end diff --git a/lib/generators/draper/install/templates/application_decorator.rb b/lib/generators/draper/templates/application_decorator.rb similarity index 100% rename from lib/generators/draper/install/templates/application_decorator.rb rename to lib/generators/draper/templates/application_decorator.rb diff --git a/lib/generators/draper/install/templates/application_decorator_spec.rb b/lib/generators/draper/templates/application_decorator_spec.rb similarity index 100% rename from lib/generators/draper/install/templates/application_decorator_spec.rb rename to lib/generators/draper/templates/application_decorator_spec.rb diff --git a/lib/generators/draper/install/templates/application_decorator_test.rb b/lib/generators/draper/templates/application_decorator_test.rb similarity index 100% rename from lib/generators/draper/install/templates/application_decorator_test.rb rename to lib/generators/draper/templates/application_decorator_test.rb diff --git a/lib/generators/draper/decorator/templates/decorator.rb b/lib/generators/draper/templates/decorator.rb similarity index 86% rename from lib/generators/draper/decorator/templates/decorator.rb rename to lib/generators/draper/templates/decorator.rb index 59303ea..0f49a46 100644 --- a/lib/generators/draper/decorator/templates/decorator.rb +++ b/lib/generators/draper/templates/decorator.rb @@ -1,5 +1,5 @@ -class <%= resource_name.singularize.camelize %>Decorator < ApplicationDecorator - decorates :<%= resource_name.singularize.underscore.to_sym %> +class <%= singular_name.camelize %>Decorator < ApplicationDecorator + decorates :<%= singular_name %> # Accessing Helpers # You can access any helper via a proxy diff --git a/lib/generators/rails/decorator_generator.rb b/lib/generators/rails/decorator_generator.rb deleted file mode 100644 index 572a47d..0000000 --- a/lib/generators/rails/decorator_generator.rb +++ /dev/null @@ -1,15 +0,0 @@ -require File.expand_path('../../draper/decorator/decorator_generator.rb', __FILE__) -class Rails::DecoratorGenerator < Draper::DecoratorGenerator - - source_root File.expand_path('../../draper/decorator/templates', __FILE__) - - class_option :invoke_after_finished, :type => :string, :description => "Generator to invoke when finished" - - def build_model_and_application_decorators - super - if self.options[:invoke_after_finished] - Rails::Generators.invoke(self.options[:invoke_after_finished], [@name, @_initializer.first[1..-1]]) - end - end - -end diff --git a/lib/generators/rspec/decorator_generator.rb b/lib/generators/rspec/decorator_generator.rb new file mode 100644 index 0000000..8373634 --- /dev/null +++ b/lib/generators/rspec/decorator_generator.rb @@ -0,0 +1,9 @@ +module Rspec + class DecoratorGenerator < ::Rails::Generators::NamedBase + source_root File.expand_path('../templates', __FILE__) + + def create_spec_file + template 'decorator_spec.rb', File.join('spec/decorators', "#{singular_name}_decorator_spec.rb") + end + end +end diff --git a/lib/generators/rspec/templates/decorator_spec.rb b/lib/generators/rspec/templates/decorator_spec.rb new file mode 100644 index 0000000..eb4eedc --- /dev/null +++ b/lib/generators/rspec/templates/decorator_spec.rb @@ -0,0 +1,4 @@ +require 'spec_helper' + +describe <%= singular_name.camelize %>Decorator do +end diff --git a/lib/generators/test_unit/decorator_generator.rb b/lib/generators/test_unit/decorator_generator.rb new file mode 100644 index 0000000..d2ab0c8 --- /dev/null +++ b/lib/generators/test_unit/decorator_generator.rb @@ -0,0 +1,9 @@ +module TestUnit + class DecoratorGenerator < ::Rails::Generators::NamedBase + source_root File.expand_path('../templates', __FILE__) + + def create_test_file + template 'decorator_test.rb', File.join('test/decorators', "#{singular_name}_decorator_test.rb") + end + end +end diff --git a/lib/generators/draper/decorator/templates/decorator_test.rb b/lib/generators/test_unit/templates/decorator_test.rb similarity index 63% rename from lib/generators/draper/decorator/templates/decorator_test.rb rename to lib/generators/test_unit/templates/decorator_test.rb index 656be86..42a1ef1 100644 --- a/lib/generators/draper/decorator/templates/decorator_test.rb +++ b/lib/generators/test_unit/templates/decorator_test.rb @@ -1,6 +1,6 @@ require 'test_helper' -class <%= resource_name.singularize.camelize %>DecoratorTest < ActiveSupport::TestCase +class <%= singular_name.camelize %>DecoratorTest < ActiveSupport::TestCase def setup ApplicationController.new.set_current_view_context end diff --git a/spec/generators/draper/decorator/decorator_generator_spec.rb b/spec/generators/draper/decorator/decorator_generator_spec.rb index 1d6ffb2..c035b98 100644 --- a/spec/generators/draper/decorator/decorator_generator_spec.rb +++ b/spec/generators/draper/decorator/decorator_generator_spec.rb @@ -1,9 +1,9 @@ require 'spec_helper' # Generators are not automatically loaded by Rails -require 'generators/draper/decorator/decorator_generator' +require 'generators/draper/decorator_generator' -describe Draper::DecoratorGenerator do +describe Draper::Generators::DecoratorGenerator do # Tell the generator where to put its output (what it thinks of as Rails.root) destination File.expand_path("../../../../../tmp", __FILE__) @@ -21,17 +21,7 @@ describe Draper::DecoratorGenerator do end context 'decorator name' do - before { run_generator ["YourModel"] } - - 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 'default test framework' do - before { run_generator ["YourModel"] } + before { run_generator ["YourModel", '-t=rspec'] } describe 'spec/decorators/your_model_decorator_spec.rb' do subject { file('spec/decorators/your_model_decorator_spec.rb') } @@ -60,54 +50,3 @@ describe Draper::DecoratorGenerator do end end end - - -=begin - describe 'no arguments' 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 '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 diff --git a/spec/generators/draper/install/install_generator_spec.rb b/spec/generators/draper/install/install_generator_spec.rb index 3cd09a3..c8e92d9 100644 --- a/spec/generators/draper/install/install_generator_spec.rb +++ b/spec/generators/draper/install/install_generator_spec.rb @@ -1,16 +1,18 @@ require 'spec_helper' # Generators are not automatically loaded by Rails -require 'generators/draper/install/install_generator' +require 'generators/draper/install_generator' -describe Draper::InstallGenerator do +describe Draper::Generators::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 } + before do + run_generator ['', "-t=rspec"] + end shared_examples_for "ApplicationDecoratorGenerator" do describe 'app/decorators/application_decorator.rb' do diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 6b4e82a..5088f43 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,5 +1,7 @@ require 'rubygems' require 'bundler/setup' +require 'rails' + Bundler.require require './spec/support/samples/active_record.rb'