From fa4f2ebbd5b5fd6960263f4ef24f40933f8269ec Mon Sep 17 00:00:00 2001 From: Benny Wong Date: Sun, 16 Oct 2011 11:16:28 -0400 Subject: [PATCH] Add a RSpec generator and hook to create default decorator specs --- .../draper/decorator/decorator_generator.rb | 4 ++- lib/generators/rspec/decorator_generator.rb | 17 +++++++++++ .../templates/application_decorator_spec.rb | 5 ++++ .../rspec/templates/decorator_spec.rb | 5 ++++ .../rspec/decorator_generator_spec.rb | 28 +++++++++++++++++++ 5 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 lib/generators/rspec/decorator_generator.rb create mode 100644 lib/generators/rspec/templates/application_decorator_spec.rb create mode 100644 lib/generators/rspec/templates/decorator_spec.rb create mode 100644 spec/generators/rspec/decorator_generator_spec.rb diff --git a/lib/generators/draper/decorator/decorator_generator.rb b/lib/generators/draper/decorator/decorator_generator.rb index 71e3b86..cf602ea 100644 --- a/lib/generators/draper/decorator/decorator_generator.rb +++ b/lib/generators/draper/decorator/decorator_generator.rb @@ -13,5 +13,7 @@ module Draper end template 'decorator.rb', "#{DECORATORS_ROOT}#{singular_name}_decorator.rb" end + + hook_for :test_framework end -end \ No newline at end of file +end diff --git a/lib/generators/rspec/decorator_generator.rb b/lib/generators/rspec/decorator_generator.rb new file mode 100644 index 0000000..3d7109c --- /dev/null +++ b/lib/generators/rspec/decorator_generator.rb @@ -0,0 +1,17 @@ +module Rspec + class DecoratorGenerator < ::Rails::Generators::NamedBase + 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 +end diff --git a/lib/generators/rspec/templates/application_decorator_spec.rb b/lib/generators/rspec/templates/application_decorator_spec.rb new file mode 100644 index 0000000..7f800dd --- /dev/null +++ b/lib/generators/rspec/templates/application_decorator_spec.rb @@ -0,0 +1,5 @@ +require 'spec_helper' + +describe ApplicationDecorator do + before { ApplicationController.new.set_current_view_context } +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..adb4015 --- /dev/null +++ b/lib/generators/rspec/templates/decorator_spec.rb @@ -0,0 +1,5 @@ +require 'spec_helper' + +describe <%= singular_name.camelize %>Decorator do + before { ApplicationController.new.set_current_view_context } +end diff --git a/spec/generators/rspec/decorator_generator_spec.rb b/spec/generators/rspec/decorator_generator_spec.rb new file mode 100644 index 0000000..48d42a7 --- /dev/null +++ b/spec/generators/rspec/decorator_generator_spec.rb @@ -0,0 +1,28 @@ +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/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 } + it { should contain "describe ProductsDecorator" } + end + + end +end