Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ApplicationDecorator generator #796

Merged
merged 26 commits into from
Apr 5, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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...

```
Expand Down
14 changes: 14 additions & 0 deletions lib/generators/draper/install_generator.rb
Original file line number Diff line number Diff line change
@@ -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
8 changes: 8 additions & 0 deletions lib/generators/draper/templates/application_decorator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class ApplicationDecorator < Draper::Decorator
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed delegate_all from this template. The belief is that certain issues and bad practices may stem from having this line in all decorators by default. Implementers should be encouraged to whitelist their delegations to minimize this risk.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree completely. It's best not to assume functionality at the application level. I'd rather the user add this line themselves that way they are forced to understand what it does.

Copy link
Contributor

@syguer syguer Apr 5, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@chuck-john @codebycliff
I agree your opinion too :)
However, how do you think about leaving as comment to notice function of delegate_all? And additionally, if you agree with me, I want to add include Draper::LazyHelpers as comment too.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's an interesting idea. If we left in any commented out stuff, I'd want to include all draper functionality that can be used (e.g. decorates_finders) with documentation explaining. I think I'd like to wait for this though for the time being unless you feel strongly about it.

# 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
9 changes: 1 addition & 8 deletions lib/generators/rails/decorator_generator.rb
Original file line number Diff line number Diff line change
@@ -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"

Expand All @@ -24,13 +24,6 @@ def parent_class_name
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
19 changes: 19 additions & 0 deletions spec/generators/install/install_generator_spec.rb
Original file line number Diff line number Diff line change
@@ -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