Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

draper:decorator defaults to rspec too #131

Merged
merged 3 commits into from

2 participants

This page is out of date. Refresh to see the latest.
View
28 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 @@ class DecoratorGenerator < Rails::Generators::NamedBase
"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
View
4 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
View
2  ...erators/rspec/templates/decorator_spec.rb → ...per/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
View
2  ...ors/test_unit/templates/decorator_test.rb → ...per/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
View
11 lib/generators/rspec/decorator_generator.rb
@@ -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
View
11 lib/generators/test_unit/decorator_generator.rb
@@ -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
View
83 spec/generators/draper/decorator/decorator_generator_spec.rb
@@ -9,6 +9,50 @@
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 @@
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
+=end
View
22 spec/generators/rspec/decorator_generator_spec.rb
@@ -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
View
22 spec/generators/test_unit/decorator_generator_spec.rb
@@ -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
Something went wrong with that request. Please try again.