Skip to content

Commit

Permalink
Make a namespaced resource to default to not namespace the model. The
Browse files Browse the repository at this point in the history
model can be namespaced with --namespace-model.
  • Loading branch information
Douglas Meyer committed Jan 1, 2011
1 parent ed7f3f4 commit 7dabe27
Show file tree
Hide file tree
Showing 20 changed files with 130 additions and 58 deletions.
14 changes: 13 additions & 1 deletion features/nifty_scaffold.feature
Expand Up @@ -43,16 +43,28 @@ Feature: Nifty Scaffold Generator
Given a new Rails app
When I run "rails g nifty:scaffold Admin::User name:string"
Then I should see the following files
| app/models/admin/user.rb |
| app/models/user.rb |
| app/controllers/admin/users_controller.rb |
| app/helpers/admin/users_helper.rb |
| app/views/admin/users/index.html.erb |
| app/views/admin/users/show.html.erb |
| app/views/admin/users/new.html.erb |
| app/views/admin/users/edit.html.erb |
| db/migrate |
And I should see "class User" in file "app/models/user.rb"
And I should not see "set_table_name :" in file "app/models/user.rb"
And I should see "namespace(:admin){ resources :users }" in file "config/routes.rb"
When I run "rails g nifty:layout -f"
And I run "rake db:migrate"
And I should successfully run "rails g nifty:scaffold Admin::User -f"
Then I should successfully run "rake test"

Scenario: Generate scaffold with a namespaced model
Given a new Rails app
When I run "rails g nifty:scaffold Admin::User name:string --namespace_model"
Then I should see "class Admin::User" in file "app/models/admin/user.rb"
And I should see "set_table_name :admin_users" in file "app/models/admin/user.rb"
When I run "rails g nifty:layout -f"
And I run "rake db:migrate"
And I should successfully run "rails g nifty:scaffold Admin::User -f --namespace_model"
Then I should successfully run "rake test"
6 changes: 6 additions & 0 deletions features/step_definitions/common_steps.rb
Expand Up @@ -25,6 +25,12 @@
File.readlines(path).join.should include(content)
end

Then /^I should not see "(.*)" in file "([^\"]*)"$/ do |content, short_path|
path = File.join(@current_directory, short_path)
File.should exist(path)
File.readlines(path).join.should_not include(content)
end

Then /^I should see the following files$/ do |table|
table.raw.flatten.each do |path|
File.should exist(File.join(@current_directory, path))
Expand Down
100 changes: 77 additions & 23 deletions lib/generators/nifty/scaffold/scaffold_generator.rb
Expand Up @@ -6,16 +6,17 @@ module Nifty
module Generators
class ScaffoldGenerator < Base
include Rails::Generators::Migration
no_tasks { attr_accessor :model_name, :model_attributes, :controller_actions }
no_tasks { attr_accessor :scaffold_name, :model_attributes, :controller_actions }

argument :model_name, :type => :string, :required => true, :banner => 'ModelName'
argument :scaffold_name, :type => :string, :required => true, :banner => 'ModelName'
argument :args_for_c_m, :type => :array, :default => [], :banner => 'controller_actions and model:attributes'

class_option :skip_model, :desc => 'Don\'t generate a model or migration file.', :type => :boolean
class_option :skip_migration, :desc => 'Dont generate migration file for model.', :type => :boolean
class_option :skip_timestamps, :desc => 'Don\'t add timestamps to migration file.', :type => :boolean
class_option :skip_controller, :desc => 'Don\'t generate controller, helper, or views.', :type => :boolean
class_option :invert, :desc => 'Generate all controller actions except these mentioned.', :type => :boolean
class_option :namespace_model, :desc => 'If the resource is namespaced, include the model in the namespace.', :type => :boolean
class_option :haml, :desc => 'Generate HAML views instead of ERB.', :type => :boolean

class_option :testunit, :desc => 'Use test/unit for test files.', :group => 'Test framework', :type => :boolean
Expand All @@ -28,6 +29,7 @@ def initialize(*args, &block)
@controller_actions = []
@model_attributes = []
@skip_model = options.skip_model?
@namespace_model = options.namespace_model?
@invert_actions = options.invert?

args_for_c_m.each do |arg|
Expand Down Expand Up @@ -67,20 +69,20 @@ def add_gems

def create_model
unless @skip_model
template 'model.rb', "app/models/#{singular_name}.rb"
template 'model.rb', "app/models/#{model_path}.rb"
if test_framework == :rspec
template "tests/rspec/model.rb", "spec/models/#{singular_name}_spec.rb"
template 'fixtures.yml', "spec/fixtures/#{plural_name}.yml"
template "tests/rspec/model.rb", "spec/models/#{model_path}_spec.rb"
template 'fixtures.yml', "spec/fixtures/#{model_path.pluralize}.yml"
else
template "tests/#{test_framework}/model.rb", "test/unit/#{singular_name}_test.rb"
template 'fixtures.yml', "test/fixtures/#{plural_name}.yml"
template "tests/#{test_framework}/model.rb", "test/unit/#{model_path}_test.rb"
template 'fixtures.yml', "test/fixtures/#{model_path.pluralize}.yml"
end
end
end

def create_migration
unless @skip_model || options.skip_migration?
migration_template 'migration.rb', "db/migrate/create_#{instances_name}.rb"
migration_template 'migration.rb', "db/migrate/create_#{model_path.pluralize.gsub('/', '_')}.rb"
end
end

Expand Down Expand Up @@ -133,27 +135,41 @@ def actions?(*names)
end

def singular_name
model_name.underscore
scaffold_name.underscore
end

def plural_name
model_name.underscore.pluralize
scaffold_name.underscore.pluralize
end

def table_name
plural_name.gsub('/', '_')
if scaffold_name.include?('::') && @namespace_model
plural_name.gsub('/', '_')
end
end

def class_name
model_name.camelize
if @namespace_model
scaffold_name.camelize
else
scaffold_name.split('::').last.camelize
end
end

def model_path
class_name.underscore
end

def plural_class_name
plural_name.camelize
end

def instance_name
singular_name.gsub('/','_')
if @namespace_model
singular_name.gsub('/','_')
else
singular_name.split('/').last
end
end

def instances_name
Expand All @@ -178,35 +194,73 @@ def render_form
end
end

def items_path(suffix = 'path')
def item_resource
scaffold_name.underscore.gsub('/','_')
end

def items_path
if action? :index
"#{instances_name}_#{suffix}"
"#{item_resource.pluralize}_path"
else
"root_path"
end
end

def item_path(options = {})
if action? :show
name = options[:instance_variable] ? "@#{instance_name}" : instance_name
if %w(new edit).include? options[:action].to_s
"#{options[:action].to_s}_#{item_resource}_path(#{name})"
else
if scaffold_name.include?('::') && !@namespace_model
namespace = singular_name.split('/')[0..-2]
"[ :#{namespace.join(', :')}, #{name} ]"
else
name
end
end
else
"root_#{suffix}"
items_path
end
end

def item_path(suffix = 'path')
def item_url
if action? :show
"@#{instance_name}"
item_resource + '_url'
else
items_path(suffix)
items_url
end
end

def items_url
if action? :index
item_resource.pluralize + '_url'
else
"root_url"
end
end

def item_path_for_spec(suffix = 'path')
if action? :show
"#{instance_name}_#{suffix}(assigns[:#{instance_name}])"
"#{item_resource}_#{suffix}(assigns[:#{instance_name}])"
else
items_path(suffix)
if suffix == 'path'
items_path
else
items_url
end
end
end

def item_path_for_test(suffix = 'path')
if action? :show
"#{instance_name}_#{suffix}(assigns(:#{instance_name}))"
"#{item_resource}_#{suffix}(assigns(:#{instance_name}))"
else
items_path(suffix)
if suffix == 'path'
items_path
else
items_url
end
end
end

Expand Down
4 changes: 2 additions & 2 deletions lib/generators/nifty/scaffold/templates/actions/create.rb
@@ -1,8 +1,8 @@
def create
@<%= instance_name %> = <%= class_name %>.new(params[:<%= instance_name %>])
if @<%= instance_name %>.save
flash[:notice] = "Successfully created <%= model_name.underscore.humanize.downcase %>."
redirect_to <%= item_path('url') %>
flash[:notice] = "Successfully created <%= class_name.underscore.humanize.downcase %>."
redirect_to <%= item_path :instance_variable => true %>
else
render :action => 'new'
end
Expand Down
4 changes: 2 additions & 2 deletions lib/generators/nifty/scaffold/templates/actions/destroy.rb
@@ -1,6 +1,6 @@
def destroy
@<%= instance_name %> = <%= class_name %>.find(params[:id])
@<%= instance_name %>.destroy
flash[:notice] = "Successfully destroyed <%= model_name.underscore.humanize.downcase %>."
redirect_to <%= items_path('url') %>
flash[:notice] = "Successfully destroyed <%= class_name.underscore.humanize.downcase %>."
redirect_to <%= items_url %>
end
4 changes: 2 additions & 2 deletions lib/generators/nifty/scaffold/templates/actions/update.rb
@@ -1,8 +1,8 @@
def update
@<%= instance_name %> = <%= class_name %>.find(params[:id])
if @<%= instance_name %>.update_attributes(params[:<%= instance_name %>])
flash[:notice] = "Successfully updated <%= model_name.underscore.humanize.downcase %>."
redirect_to <%= item_path('url') %>
flash[:notice] = "Successfully updated <%= class_name.underscore.humanize.downcase %>."
redirect_to <%= item_url %>
else
render :action => 'edit'
end
Expand Down
6 changes: 3 additions & 3 deletions lib/generators/nifty/scaffold/templates/migration.rb
@@ -1,6 +1,6 @@
class Create<%= plural_class_name.delete('::') %> < ActiveRecord::Migration
class Create<%= class_name.pluralize.delete('::') %> < ActiveRecord::Migration
def self.up
create_table <%= table_name.to_sym.inspect %> do |t|
create_table :<%= table_name || plural_name.split('/').last %> do |t|
<%- for attribute in model_attributes -%>
t.<%= attribute.type %> :<%= attribute.name %>
<%- end -%>
Expand All @@ -11,6 +11,6 @@ def self.up
end
def self.down
drop_table :<%= table_name %>
drop_table :<%= table_name || plural_name.split('/').last %>
end
end
@@ -1,6 +1,6 @@
it "destroy action should destroy model and redirect to index action" do
<%= instance_name %> = <%= class_name %>.first
delete :destroy, :id => <%= instance_name %>
response.should redirect_to(<%= items_path('url') %>)
response.should redirect_to(<%= items_url %>)
<%= class_name %>.exists?(<%= instance_name %>.id).should be_false
end
Expand Up @@ -2,7 +2,7 @@
should "destroy model and redirect to index action" do
<%= instance_name %> = <%= class_name %>.first
delete :destroy, :id => <%= instance_name %>
assert_redirected_to <%= items_path('url') %>
assert_redirected_to <%= items_url %>
assert !<%= class_name %>.exists?(<%= instance_name %>.id)
end
end
@@ -1,6 +1,6 @@
def test_destroy
<%= instance_name %> = <%= class_name %>.first
delete :destroy, :id => <%= instance_name %>
assert_redirected_to <%= items_path('url') %>
assert_redirected_to <%= items_url %>
assert !<%= class_name %>.exists?(<%= instance_name %>.id)
end
@@ -1,4 +1,4 @@
<%%= form_for @<%= instance_name %> do |f| %>
<%%= form_for <%= item_path :instance_variable => true %> do |f| %>
<%%= f.error_messages %>
<%- for attribute in model_attributes -%>
<p>
Expand Down
Expand Up @@ -5,10 +5,10 @@
<%- if actions? :show, :index -%>
<p>
<%- if action? :show -%>
<%%= link_to "Show", @<%= instance_name %> %> |
<%%= link_to "Show", <%= item_path :instance_variable => true %> %> |
<%- end -%>
<%- if action? :index -%>
<%%= link_to "View All", <%= instances_name %>_path %>
<%%= link_to "View All", <%= items_path %> %>
<%- end -%>
</p>
<%- end -%>
Expand Up @@ -12,18 +12,18 @@
<td><%%= <%= instance_name %>.<%= attribute.name %> %></td>
<%- end -%>
<%- if action? :show -%>
<td><%%= link_to "Show", <%= instance_name %> %></td>
<td><%%= link_to "Show", <%= item_path %> %></td>
<%- end -%>
<%- if action? :edit -%>
<td><%%= link_to "Edit", edit_<%= instance_name %>_path(<%= instance_name %>) %></td>
<td><%%= link_to "Edit", <%= item_path :action => :edit %> %></td>
<%- end -%>
<%- if action? :destroy -%>
<td><%%= link_to "Destroy", <%= instance_name %>, :confirm => 'Are you sure?', :method => :delete %></td>
<td><%%= link_to "Destroy", <%= item_path %>, :confirm => 'Are you sure?', :method => :delete %></td>
<%- end -%>
</tr>
<%% end %>
</table>

<%- if action? :new -%>
<p><%%= link_to "New <%= singular_name.titleize %>", new_<%= instance_name %>_path %></p>
<p><%%= link_to "New <%= singular_name.titleize %>", <%= item_path :action => :new %> %></p>
<%- end -%>
Expand Up @@ -3,5 +3,5 @@
<%= render_form %>
<%- if action? :index -%>
<p><%%= link_to "Back to List", <%= instances_name %>_path %></p>
<p><%%= link_to "Back to List", <%= items_path %> %></p>
<%- end -%>
Expand Up @@ -9,12 +9,12 @@

<p>
<%- if action? :edit -%>
<%%= link_to "Edit", edit_<%= instance_name %>_path(@<%= instance_name %>) %> |
<%%= link_to "Edit", <%= item_path :action => :edit, :instance_variable => true %> %> |
<%- end -%>
<%- if action? :destroy -%>
<%%= link_to "Destroy", @<%= instance_name %>, :confirm => 'Are you sure?', :method => :delete %> |
<%%= link_to "Destroy", <%= item_path :instance_variable => true %>, :confirm => 'Are you sure?', :method => :delete %> |
<%- end -%>
<%- if action? :index -%>
<%%= link_to "View All", <%= instance_name %>_path %>
<%%= link_to "View All", <%= items_path %> %>
<%- end -%>
</p>
@@ -1,4 +1,4 @@
- form_for @<%= instance_name %> do |f|
- form_for <%= item_path %> do |f|
= f.error_messages
<%- for attribute in model_attributes -%>
%p
Expand Down
Expand Up @@ -5,10 +5,10 @@
<%- if actions? :show, :index -%>
%p
<%- if action? :show -%>
= link_to "Show", <%= instance_name %>_path(@<%= instance_name %>)
= link_to "Show", <%= item_path %>
|
<%- end -%>
<%- if action? :index -%>
= link_to "View All", <%= instances_name %>_path
= link_to "View All", <%= items_path %>
<%- end -%>
<%- end -%>

0 comments on commit 7dabe27

Please sign in to comment.