-
Notifications
You must be signed in to change notification settings - Fork 61
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
[Review Requested] Add scaffold_controller generator #83
Closed
Closed
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
6d3b382
Add scaffold_controller generator
ianks cf96053
Add tests for scaffold_controller generator
ianks 7ce9926
Create collection on scaffold_generator
ianks 407c6f4
Refactor namespace for Railtie.generators
ianks 36a87d2
Fix tests, only use generator on Rails 4+
ianks b6744d3
Add module namespacing to representers
ianks dbc9873
Include Representable in representer
ianks 79b7bcc
Fix naming bug in collection_representer template
ianks 48bb8fe
Fix tests for collection representer
ianks 851c430
Do not test generators for Rails < 3.2
ianks File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,4 +7,5 @@ Gemfile*.lock | |
pkg/* | ||
test/dummy/log/* | ||
test/dummy/tmp/* | ||
test/tmp/* | ||
.rvmrc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,16 @@ | ||
require "bundler/gem_tasks" | ||
|
||
require 'rake/testtask' | ||
require "rails/version" | ||
|
||
task :default => [:test] | ||
|
||
Rake::TestTask.new(:test) do |test| | ||
test.libs << 'test' | ||
test.test_files = FileList['test/*_test.rb'] | ||
|
||
# Do not test generators for Rails < 3.2 | ||
test.test_files = FileList['test/*_test.rb'].reject do |file| | ||
file.include?('generator') && Rails::VERSION::STRING < '3.2' | ||
end | ||
|
||
test.verbose = true | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,4 @@ gemspec :path => '../' | |
|
||
gem 'railties', '~> 3.0.11' | ||
gem 'activerecord', '~> 3.0.11' | ||
gem "minitest", "~> 4.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,3 +6,4 @@ gemspec :path => '../' | |
|
||
gem 'railties', '~> 3.2.13' | ||
gem 'activerecord', '~> 3.2.13' | ||
gem "minitest", "~> 4.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
module Rails | ||
module Generators | ||
class CollectionRepresenterGenerator < Rails::Generators::NamedBase | ||
source_root File.expand_path("../templates", __FILE__) | ||
|
||
argument :properties, :type => :array, :default => [], | ||
:banner => "property[:class[:extend]] property[:class[:extend]]" | ||
|
||
class_option :format, :default => :json, :banner => "--format=JSON", | ||
:desc => "Use different formats JSON, JSON::HAL or XML" | ||
|
||
def generate_representer_file | ||
template('collection_representer.rb', file_path) | ||
end | ||
|
||
private | ||
|
||
def format | ||
options[:format].upcase | ||
end | ||
|
||
def property_options | ||
PropertyBuilder.new(properties) | ||
end | ||
|
||
def file_path | ||
base_path = 'app/representers' | ||
File.join(base_path, class_path, "#{file_name.pluralize}_representer.rb") | ||
end | ||
|
||
class PropertyBuilder | ||
include Enumerable | ||
|
||
def initialize(properties) | ||
@raw = properties | ||
end | ||
|
||
def each(&block) | ||
properties_with_options.each(&block) | ||
end | ||
|
||
private | ||
|
||
def properties_with_options | ||
properties.map do |(name, klass, representer)| | ||
p = [name_option(name)] | ||
p << hash_option(:class, klass) | ||
p << hash_option(:extend, representer) | ||
|
||
p.compact.join(', ') | ||
end | ||
end | ||
|
||
def name_option(name) | ||
return unless name | ||
"property :#{name}" | ||
end | ||
|
||
def hash_option(key, value) | ||
return unless key && value | ||
":#{key} => #{value.classify}" | ||
end | ||
|
||
def properties | ||
@raw.map { |p| p.split(':') } | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
require 'rails/generators/named_base' | ||
require 'rails/generators/rails/scaffold_controller/scaffold_controller_generator' | ||
require 'rails/generators/resource_helpers' | ||
|
||
module Rails | ||
module Generators | ||
class ScaffoldControllerGenerator | ||
source_root File.expand_path('../templates', __FILE__) | ||
|
||
hook_for :representer, default: true | ||
hook_for :collection_representer, default: true | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<% module_namespacing do -%> | ||
module <%= class_name.pluralize %>Representer | ||
include Representable::JSON::Collection | ||
|
||
items extend: <%= class_name %>Representer, class: <%= class_name %> | ||
end | ||
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<% if namespaced? -%> | ||
require_dependency "<%= namespaced_file_path %>/application_controller" | ||
|
||
<% end -%> | ||
<% module_namespacing do -%> | ||
class <%= controller_class_name %>Controller < ApplicationController | ||
include Roar::Rails::ControllerAdditions | ||
respond_to :json | ||
|
||
before_action :set_<%= singular_table_name %>, only: [:show, :edit, :update, :destroy] | ||
|
||
def index | ||
@<%= plural_table_name %> = <%= orm_class.all(class_name) %> | ||
|
||
respond_with @<%= plural_table_name %>, :represent_with => <%= controller_class_name %>Representer | ||
end | ||
|
||
def show | ||
respond_with @<%= singular_table_name %>, :represent_with => <%= class_name %>Representer | ||
end | ||
|
||
def create | ||
@<%= singular_table_name %> = consume! <%= class_name %>.new, :represent_with => <%= class_name %>Representer | ||
@<%= singular_table_name %>.save | ||
|
||
respond_with @<%= singular_table_name %>, :represent_with => <%= class_name %>Representer | ||
end | ||
|
||
def update | ||
consume! @<%= singular_table_name %>, :represent_with => <%= class_name %>Representer | ||
@<%= singular_table_name %>.save | ||
|
||
respond_with @<%= singular_table_name %>, :represent_with => <%= class_name %>Representer | ||
end | ||
|
||
def destroy | ||
@<%= orm_instance.destroy %> | ||
|
||
head :no_content | ||
end | ||
|
||
private | ||
|
||
def set_<%= singular_table_name %> | ||
@<%= singular_table_name %> = <%= orm_class.find(class_name, "params[:id]") %> | ||
end | ||
end | ||
<% end -%> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
<% module_namespacing do -%> | ||
module <%= class_name %>Representer | ||
include Roar::Representer::<%= format %> | ||
<% property_options.each do |property| %> | ||
<%= property -%> | ||
<% end %> | ||
end | ||
<% end -%> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
require 'test_helper' | ||
require 'rails/generators' | ||
require 'generators/rails/collection_representer_generator' | ||
|
||
class CollectionRepresenterGeneratorTest < Rails::Generators::TestCase | ||
destination File.join(Rails.root, "tmp") | ||
setup :prepare_destination | ||
tests Rails::Generators::CollectionRepresenterGenerator | ||
|
||
test "create a representer with correct class_name" do | ||
run_generator %w(singer) | ||
|
||
assert_file representer_path('singer'), /module SingersRepresenter/ | ||
end | ||
|
||
test "create a representer with collection support" do | ||
run_generator %w(singer) | ||
|
||
assert_file representer_path('singer'), /include Representable::JSON::Collection/ | ||
end | ||
|
||
test "extend the correct representer" do | ||
run_generator %w(singer) | ||
|
||
assert_file representer_path('singer'), /items extend: SingerRepresenter, class: Singer/ | ||
end | ||
|
||
def representer_path(name) | ||
"app/representers/#{name.pluralize}_representer.rb" | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
require 'test_helper' | ||
require 'rails/generators/test_case' | ||
|
||
require 'generators/rails/scaffold_controller_generator' | ||
|
||
class ScaffoldControllerGeneratorTest < Rails::Generators::TestCase | ||
if Rails::VERSION::STRING >= "4.0" | ||
tests Rails::Generators::ScaffoldControllerGenerator | ||
arguments %w(Post title body:text) | ||
destination File.expand_path('../tmp', __FILE__) | ||
setup :prepare_destination | ||
|
||
test 'controller content' do | ||
run_generator | ||
|
||
assert_file 'app/controllers/posts_controller.rb' do |content| | ||
assert_instance_method :index, content do |m| | ||
assert_match /@posts = Post\.all/, m | ||
assert_match /respond_with @posts, :represent_with => PostsRepresenter/, m | ||
end | ||
|
||
assert_instance_method :show, content do |m| | ||
assert_match /respond_with @post, :represent_with => PostRepresenter/, m | ||
end | ||
|
||
assert_instance_method :create, content do |m| | ||
assert_match /@post = consume! Post\.new, :represent_with => PostRepresenter/, m | ||
assert_match /@post\.save/, m | ||
assert_match /respond_with @post, :represent_with => PostRepresenter/, m | ||
end | ||
|
||
assert_instance_method :update, content do |m| | ||
assert_match /consume! @post, :represent_with => PostRepresenter/, m | ||
assert_match /@post\.save/, m | ||
assert_match /respond_with @post, :represent_with => PostRepresenter/, m | ||
end | ||
|
||
assert_instance_method :destroy, content do |m| | ||
assert_match /@post\.destroy/, m | ||
assert_match /head :no_content/, m | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we go the
Controller::represents
way here and set this on the class level instead of using:represent_with
in every action? This option was meant to be a work-around if you need to customise an edge case.Do you use
::represents
at all?