ApplicationDecorator generator (#796)

* un-skip test and remove unused class definition

* fix build

* add InstallGenerator

* re-namespace generators

* remove module namespacing

* update README with new generator command

* inherit from Base instead of NamedBase

* add inheritance line to Generators section of README

* call #copy_file instead of #template

* remove skip functionality

* refactor once-used variable

* specify application_decorator.rb file instead of class

* move decorator generator back to rails namespace

* revert README change

* specify decorator class

* re-word template help text

* remove unnecessary describe block

* remove delegate_all from application_decorator template

* edit README
This commit is contained in:
C.J. Howard 2017-04-05 13:43:27 -05:00 committed by Cliff Braton
parent 9490a57812
commit a1d854a7f4
5 changed files with 48 additions and 8 deletions

View File

@ -139,6 +139,12 @@ end
### Generators
To create an `ApplicationDecorator` that all generated decorators inherit from, run...
```
rails generate draper:install
```
When you have Draper installed and generate a controller...
```

View File

@ -0,0 +1,14 @@
module Draper
module Generators
class InstallGenerator < Rails::Generators::Base
source_root File.expand_path('../templates', __FILE__)
desc 'Creates an ApplicationDecorator, if none exists.'
def create_application_decorator
file = 'application_decorator.rb'
copy_file file, "app/decorators/#{file}"
end
end
end
end

View File

@ -0,0 +1,8 @@
class ApplicationDecorator < Draper::Decorator
# Define methods for all decorated objects.
# Helpers are accessed through `helpers` (aka `h`). For example:
#
# def percent_amount
# h.number_to_percentage object.amount, precision: 2
# end
end

View File

@ -1,6 +1,6 @@
module Rails
module Generators
class DecoratorGenerator < NamedBase
class DecoratorGenerator < NamedBase
source_root File.expand_path("../templates", __FILE__)
check_class_collision suffix: "Decorator"
@ -24,13 +24,6 @@ module Rails
end
end
end
# Rails 3.0.X compatibility, stolen from https://github.com/jnunemaker/mongomapper/pull/385/files#L1R32
unless methods.include?(:module_namespacing)
def module_namespacing
yield if block_given?
end
end
end
end
end

View File

@ -0,0 +1,19 @@
require 'spec_helper'
require 'dummy/config/environment'
require 'ammeter/init'
require 'generators/draper/install_generator'
describe Draper::Generators::InstallGenerator do
destination File.expand_path('../tmp', __FILE__)
before { prepare_destination }
after(:all) { FileUtils.rm_rf destination_root }
describe 'the application decorator' do
subject { file('app/decorators/application_decorator.rb') }
before { run_generator }
it { is_expected.to contain 'class ApplicationDecorator' }
end
end