Skip to content
This repository has been archived by the owner on Dec 12, 2021. It is now read-only.

Add ability to namespace a resource for nifty:scaffold #63

Open
9 commits merged into
base: master
Choose a base branch
from
Jan 17, 2011
Open
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
24 changes: 1 addition & 23 deletions .rvmrc
Original file line number Diff line number Diff line change
@@ -1,23 +1 @@
#!/usr/bin/env bash

# adapted from: http://rvm.beginrescueend.com/workflow/rvmrc/

ruby_string="1.9.2"
gemset_name="nifty-generators"

if rvm list strings | grep -q "${ruby_string}" ; then

# Load or create the specified environment
if [[ -d "${rvm_path:-$HOME/.rvm}/environments" \
&& -s "${rvm_path:-$HOME/.rvm}/environments/${ruby_string}@${gemset_name}" ]] ; then
\. "${rvm_path:-$HOME/.rvm}/environments/${ruby_string}@${gemset_name}"
else
rvm --create "${ruby_string}@${gemset_name}"
fi

else

# Notify the user to install the desired interpreter before proceeding.
echo "${ruby_string} was not found, please run 'rvm install ${ruby_string}' and then cd back into the project directory."

fi
rvm use 1.9.2@nifty-generators --create
7 changes: 7 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
0.4.6 (March 26, 2011)

* fixing form block in HAML (thanks apocalyptiq)

* fixing instance variable setting in HAML (thanks nhocki)


0.4.5 (February 14, 2011)

* fixing user validation tests in nifty:authentication (thanks apocalyptiq) - issue #87
Expand Down
2 changes: 1 addition & 1 deletion nifty-generators.gemspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Gem::Specification.new do |s|
s.name = "nifty-generators"
s.version = "0.4.5"
s.version = "0.4.6"
s.author = "Ryan Bates"
s.email = "[email protected]"
s.homepage = "http://github.com/ryanb/nifty-generators"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
%p== Don't have an account? #{link_to "Sign up!", signup_path}

<%- if options[:authlogic] -%>
- form_for @<%= session_singular_name %> do |f|
= form_for @<%= session_singular_name %> do |f|
= f.error_messages
%p
= f.label :username
Expand Down
157 changes: 104 additions & 53 deletions rails_generators/nifty_scaffold/nifty_scaffold_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,51 +43,71 @@ def initialize(runtime_args, runtime_options = {})
def manifest
record do |m|
unless options[:skip_model]
m.directory "app/models"
m.template "model.rb", "app/models/#{singular_name}.rb"
destination = "app/models/#{model_name.underscore}.rb"
m.directory File.dirname(destination)
m.template "model.rb", destination
unless options[:skip_migration]
m.migration_template "migration.rb", "db/migrate", :migration_file_name => "create_#{plural_name}"
m.migration_template "migration.rb", "db/migrate", :migration_file_name => "create_#{model_name.underscore.pluralize.gsub('/','_')}"
end

if rspec?
m.directory "spec/models"
m.template "tests/#{test_framework}/model.rb", "spec/models/#{singular_name}_spec.rb"
m.directory "spec/fixtures"
m.template "fixtures.yml", "spec/fixtures/#{plural_name}.yml"
destination = "spec/models/#{model_name.underscore}_spec.rb"
m.directory File.dirname(destination)
m.template "tests/#{test_framework}/model.rb", destination
destination = "spec/fixtures/#{plural_model_name}.yml"
m.directory File.dirname(destination)
m.template "fixtures.yml", destination
else
m.directory "test/unit"
m.template "tests/#{test_framework}/model.rb", "test/unit/#{singular_name}_test.rb"
m.directory "test/fixtures"
m.template "fixtures.yml", "test/fixtures/#{plural_name}.yml"
destination = "test/unit/#{model_name.underscore}_test.rb"
m.directory File.dirname(destination)
m.template "tests/#{test_framework}/model.rb", destination
destination = "test/fixtures/#{plural_model_name}.yml"
m.directory File.dirname(destination)
m.template "fixtures.yml", destination
end
end

unless options[:skip_controller]
m.directory "app/controllers"
m.template "controller.rb", "app/controllers/#{plural_name}_controller.rb"
destination = "app/controllers/#{resource_path}_controller.rb"
m.directory File.dirname(destination)
m.template "controller.rb", destination

m.directory "app/helpers"
m.template "helper.rb", "app/helpers/#{plural_name}_helper.rb"
destination = "app/helpers/#{resource_path}_helper.rb"
m.directory File.dirname(destination)
m.template "helper.rb", destination

m.directory "app/views/#{plural_name}"
m.directory "app/views/#{resource_path}"
controller_actions.each do |action|
if File.exist? source_path("views/#{view_language}/#{action}.html.#{view_language}")
m.template "views/#{view_language}/#{action}.html.#{view_language}", "app/views/#{plural_name}/#{action}.html.#{view_language}"
m.template "views/#{view_language}/#{action}.html.#{view_language}", "app/views/#{resource_path}/#{action}.html.#{view_language}"
end
end

if form_partial?
m.template "views/#{view_language}/_form.html.#{view_language}", "app/views/#{plural_name}/_form.html.#{view_language}"
m.template "views/#{view_language}/_form.html.#{view_language}", "app/views/#{resource_path}/_form.html.#{view_language}"
end

m.route_resources plural_name
sentinel = 'ActionController::Routing::Routes.draw do |map|'
namespaces = resource_path.split('/')
resource = namespaces.pop
route = namespaces.reverse.inject("map.resources :#{resource}") { |acc, namespace|
"map.namespace(:#{namespace}){|map| #{acc} }"
}
logger.route route
unless options[:pretend]
m.gsub_file 'config/routes.rb', /(#{Regexp.escape(sentinel)})/mi do |match|
"#{match}\n #{route}\n"
end
end

if rspec?
m.directory "spec/controllers"
m.template "tests/#{test_framework}/controller.rb", "spec/controllers/#{plural_name}_controller_spec.rb"
destination = "spec/controllers/#{resource_path}_controller_spec.rb"
m.directory File.dirname(destination)
m.template "tests/#{test_framework}/controller.rb", destination
else
m.directory "test/functional"
m.template "tests/#{test_framework}/controller.rb", "test/functional/#{plural_name}_controller_test.rb"
destination = "test/functional/#{resource_path}_controller_test.rb"
m.directory File.dirname(destination)
m.template "tests/#{test_framework}/controller.rb", destination
end
end
end
Expand All @@ -109,20 +129,58 @@ def actions?(*names)
names.all? { |n| action? n.to_s }
end

def singular_name
name.underscore
def resource_path
name.underscore.pluralize
end

def plural_name
name.underscore.pluralize
def singular_model_name
model_name.underscore.gsub('/', '_')
end

def plural_model_name
singular_model_name.pluralize
end

def class_name
name.camelize
def model_name
if options[:namespace_model]
name.camelize
else
name.split('::').last.camelize
end
end

def plural_class_name
plural_name.camelize
def items_path
if action? :index
"#{resource_path.gsub('/', '_')}_path"
else
"root_path"
end
end

def items_url
if action? :index
"#{resource_path.gsub('/', '_')}_url"
else
"root_url"
end
end

def item_path(options = {})
if action?(:show) || options[:action]
name = options[:instance_variable] ? "@#{singular_model_name}" : singular_model_name
if %w(new edit).include? options[:action].to_s
"#{options[:action].to_s}_#{resource_path.singularize.gsub('/', '_')}_path(#{name})"
else
if resource_path.include?('/') && !self.options[:namespace_model]
namespace = resource_path.split('/')[0..-2]
"[ :#{namespace.join(', :')}, #{name} ]"
else
name
end
end
else
items_path
end
end

def controller_methods(dir_name)
Expand All @@ -143,40 +201,32 @@ def render_form
end
end

def items_path(suffix = 'path')
if action? :index
"#{plural_name}_#{suffix}"
else
"root_#{suffix}"
end
end

def item_path(suffix = 'path')
if action? :show
"@#{singular_name}"
else
items_path(suffix)
end
end

def item_path_for_spec(suffix = 'path')
if action? :show
"#{singular_name}_#{suffix}(assigns[:#{singular_name}])"
"#{resource_path.singularize.gsub('/', '_')}_#{suffix}(assigns[:#{singular_model_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
"#{singular_name}_#{suffix}(assigns(:#{singular_name}))"
"#{resource_path.singularize.gsub('/', '_')}_#{suffix}(assigns(:#{singular_model_name}))"
else
items_path(suffix)
if suffix == 'path'
items_path
else
items_url
end
end
end

def model_columns_for_attributes
class_name.constantize.columns.reject do |column|
model_name.constantize.columns.reject do |column|
column.name.to_s =~ /^(id|created_at|updated_at)$/
end
end
Expand Down Expand Up @@ -206,6 +256,7 @@ def add_options!(opt)
opt.on("--skip-migration", "Don't generate migration file for model.") { |v| options[:skip_migration] = v }
opt.on("--skip-timestamps", "Don't add timestamps to migration file.") { |v| options[:skip_timestamps] = v }
opt.on("--skip-controller", "Don't generate controller, helper, or views.") { |v| options[:skip_controller] = v }
opt.on("--namespace-model", "If the resource is namespaced, include the model in the namespace.") { |v| options[:namespace_model] = v }
opt.on("--invert", "Generate all controller actions except these mentioned.") { |v| options[:invert] = v }
opt.on("--haml", "Generate HAML views instead of ERB.") { |v| options[:haml] = v }
opt.on("--testunit", "Use test/unit for test files.") { options[:test_framework] = :testunit }
Expand All @@ -215,7 +266,7 @@ def add_options!(opt)

# is there a better way to do this? Perhaps with const_defined?
def model_exists?
File.exist? destination_path("app/models/#{singular_name}.rb")
File.exist? destination_path("app/models/#{model_name.underscore}.rb")
end

def read_template(relative_path)
Expand Down
6 changes: 3 additions & 3 deletions rails_generators/nifty_scaffold/templates/actions/create.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
def create
@<%= singular_name %> = <%= class_name %>.new(params[:<%= singular_name %>])
if @<%= singular_name %>.save
@<%= singular_model_name %> = <%= model_name %>.new(params[:<%= singular_model_name %>])
if @<%= singular_model_name %>.save
flash[:notice] = "Successfully created <%= name.underscore.humanize.downcase %>."
redirect_to <%= item_path('url') %>
redirect_to <%= item_path :instance_variable => true %>
else
render :action => 'new'
end
Expand Down
6 changes: 3 additions & 3 deletions rails_generators/nifty_scaffold/templates/actions/destroy.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
def destroy
@<%= singular_name %> = <%= class_name %>.find(params[:id])
@<%= singular_name %>.destroy
@<%= singular_model_name %> = <%= model_name %>.find(params[:id])
@<%= singular_model_name %>.destroy
flash[:notice] = "Successfully destroyed <%= name.underscore.humanize.downcase %>."
redirect_to <%= items_path('url') %>
redirect_to <%= items_url %>
end
2 changes: 1 addition & 1 deletion rails_generators/nifty_scaffold/templates/actions/edit.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
def edit
@<%= singular_name %> = <%= class_name %>.find(params[:id])
@<%= singular_model_name %> = <%= model_name %>.find(params[:id])
end
2 changes: 1 addition & 1 deletion rails_generators/nifty_scaffold/templates/actions/index.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
def index
@<%= plural_name %> = <%= class_name %>.all
@<%= plural_model_name %> = <%= model_name %>.all
end
2 changes: 1 addition & 1 deletion rails_generators/nifty_scaffold/templates/actions/new.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
def new
@<%= singular_name %> = <%= class_name %>.new
@<%= singular_model_name %> = <%= model_name %>.new
end
2 changes: 1 addition & 1 deletion rails_generators/nifty_scaffold/templates/actions/show.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
def show
@<%= singular_name %> = <%= class_name %>.find(params[:id])
@<%= singular_model_name %> = <%= model_name %>.find(params[:id])
end
6 changes: 3 additions & 3 deletions rails_generators/nifty_scaffold/templates/actions/update.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
def update
@<%= singular_name %> = <%= class_name %>.find(params[:id])
if @<%= singular_name %>.update_attributes(params[:<%= singular_name %>])
@<%= singular_model_name %> = <%= model_name %>.find(params[:id])
if @<%= singular_model_name %>.update_attributes(params[:<%= singular_model_name %>])
flash[:notice] = "Successfully updated <%= name.underscore.humanize.downcase %>."
redirect_to <%= item_path('url') %>
redirect_to <%= item_path :instance_variable => true %>
else
render :action => 'edit'
end
Expand Down
2 changes: 1 addition & 1 deletion rails_generators/nifty_scaffold/templates/controller.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
class <%= plural_class_name %>Controller < ApplicationController
class <%= resource_path.camelize %>Controller < ApplicationController
<%= controller_methods :actions %>
end
2 changes: 1 addition & 1 deletion rails_generators/nifty_scaffold/templates/helper.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
module <%= plural_class_name %>Helper
module <%= resource_path.camelize %>Helper
end
6 changes: 3 additions & 3 deletions rails_generators/nifty_scaffold/templates/migration.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class Create<%= plural_class_name %> < ActiveRecord::Migration
class Create<%= model_name.pluralize.delete('::') %> < ActiveRecord::Migration
def self.up
create_table :<%= plural_name %> do |t|
create_table :<%= plural_model_name %> do |t|
<%- for attribute in attributes -%>
t.<%= attribute.type %> :<%= attribute.name %>
<%- end -%>
Expand All @@ -11,6 +11,6 @@ def self.up
end

def self.down
drop_table :<%= plural_name %>
drop_table :<%= plural_model_name %>
end
end
3 changes: 2 additions & 1 deletion rails_generators/nifty_scaffold/templates/model.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
class <%= class_name %> < ActiveRecord::Base
class <%= model_name %> < ActiveRecord::Base
<%= "set_table_name :#{plural_model_name}" if model_name.include?('::') %>
attr_accessible <%= attributes.map { |a| ":#{a.name}" }.join(", ") %>
end
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
it "create action should render new template when model is invalid" do
<%= class_name %>.any_instance.stubs(:valid?).returns(false)
<%= model_name %>.any_instance.stubs(:valid?).returns(false)
post :create
response.should render_template(:new)
end

it "create action should redirect when model is valid" do
<%= class_name %>.any_instance.stubs(:valid?).returns(true)
<%= model_name %>.any_instance.stubs(:valid?).returns(true)
post :create
response.should redirect_to(<%= item_path_for_spec('url') %>)
end
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
it "destroy action should destroy model and redirect to index action" do
<%= singular_name %> = <%= class_name %>.first
delete :destroy, :id => <%= singular_name %>
response.should redirect_to(<%= items_path('url') %>)
<%= class_name %>.exists?(<%= singular_name %>.id).should be_false
<%= singular_model_name %> = <%= model_name %>.first
delete :destroy, :id => <%= singular_model_name %>
response.should redirect_to(<%= items_url %>)
<%= model_name %>.exists?(<%= singular_model_name %>.id).should be_false
end
Loading