very_nifty_generators 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/LICENSE +20 -0
- data/README.rdoc +17 -0
- data/lib/generators/nifty/authentication/authentication_generator.rb +152 -0
- data/lib/generators/nifty/authentication/templates/authentication.rb +61 -0
- data/lib/generators/nifty/authentication/templates/authlogic_session.rb +2 -0
- data/lib/generators/nifty/authentication/templates/migration.rb +20 -0
- data/lib/generators/nifty/authentication/templates/sessions_controller.rb +45 -0
- data/lib/generators/nifty/authentication/templates/sessions_helper.rb +2 -0
- data/lib/generators/nifty/authentication/templates/tests/rspec/sessions_controller.rb +39 -0
- data/lib/generators/nifty/authentication/templates/tests/rspec/user.rb +83 -0
- data/lib/generators/nifty/authentication/templates/tests/rspec/users_controller.rb +26 -0
- data/lib/generators/nifty/authentication/templates/tests/shoulda/sessions_controller.rb +40 -0
- data/lib/generators/nifty/authentication/templates/tests/shoulda/user.rb +85 -0
- data/lib/generators/nifty/authentication/templates/tests/shoulda/users_controller.rb +27 -0
- data/lib/generators/nifty/authentication/templates/tests/testunit/sessions_controller.rb +36 -0
- data/lib/generators/nifty/authentication/templates/tests/testunit/user.rb +88 -0
- data/lib/generators/nifty/authentication/templates/tests/testunit/users_controller.rb +23 -0
- data/lib/generators/nifty/authentication/templates/user.rb +42 -0
- data/lib/generators/nifty/authentication/templates/users_controller.rb +18 -0
- data/lib/generators/nifty/authentication/templates/users_helper.rb +2 -0
- data/lib/generators/nifty/config/config_generator.rb +29 -0
- data/lib/generators/nifty/layout/layout_generator.rb +28 -0
- data/lib/generators/nifty/layout/templates/helper.rb +22 -0
- data/lib/generators/nifty/scaffold/scaffold_generator.rb +247 -0
- data/lib/generators/nifty/scaffold/templates/actions/create.rb +9 -0
- data/lib/generators/nifty/scaffold/templates/actions/destroy.rb +6 -0
- data/lib/generators/nifty/scaffold/templates/actions/edit.rb +3 -0
- data/lib/generators/nifty/scaffold/templates/actions/index.rb +3 -0
- data/lib/generators/nifty/scaffold/templates/actions/new.rb +3 -0
- data/lib/generators/nifty/scaffold/templates/actions/show.rb +3 -0
- data/lib/generators/nifty/scaffold/templates/actions/update.rb +9 -0
- data/lib/generators/nifty/scaffold/templates/controller.rb +3 -0
- data/lib/generators/nifty/scaffold/templates/helper.rb +2 -0
- data/lib/generators/nifty/scaffold/templates/migration.rb +16 -0
- data/lib/generators/nifty/scaffold/templates/model.rb +3 -0
- data/lib/generators/nifty/scaffold/templates/tests/rspec/actions/create.rb +11 -0
- data/lib/generators/nifty/scaffold/templates/tests/rspec/actions/destroy.rb +6 -0
- data/lib/generators/nifty/scaffold/templates/tests/rspec/actions/edit.rb +4 -0
- data/lib/generators/nifty/scaffold/templates/tests/rspec/actions/index.rb +4 -0
- data/lib/generators/nifty/scaffold/templates/tests/rspec/actions/new.rb +4 -0
- data/lib/generators/nifty/scaffold/templates/tests/rspec/actions/show.rb +4 -0
- data/lib/generators/nifty/scaffold/templates/tests/rspec/actions/update.rb +11 -0
- data/lib/generators/nifty/scaffold/templates/tests/rspec/controller.rb +8 -0
- data/lib/generators/nifty/scaffold/templates/tests/rspec/model.rb +7 -0
- data/lib/generators/nifty/scaffold/templates/tests/shoulda/actions/create.rb +13 -0
- data/lib/generators/nifty/scaffold/templates/tests/shoulda/actions/destroy.rb +8 -0
- data/lib/generators/nifty/scaffold/templates/tests/shoulda/actions/edit.rb +6 -0
- data/lib/generators/nifty/scaffold/templates/tests/shoulda/actions/index.rb +6 -0
- data/lib/generators/nifty/scaffold/templates/tests/shoulda/actions/new.rb +6 -0
- data/lib/generators/nifty/scaffold/templates/tests/shoulda/actions/show.rb +6 -0
- data/lib/generators/nifty/scaffold/templates/tests/shoulda/actions/update.rb +13 -0
- data/lib/generators/nifty/scaffold/templates/tests/shoulda/controller.rb +5 -0
- data/lib/generators/nifty/scaffold/templates/tests/shoulda/model.rb +7 -0
- data/lib/generators/nifty/scaffold/templates/tests/testunit/actions/create.rb +11 -0
- data/lib/generators/nifty/scaffold/templates/tests/testunit/actions/destroy.rb +6 -0
- data/lib/generators/nifty/scaffold/templates/tests/testunit/actions/edit.rb +4 -0
- data/lib/generators/nifty/scaffold/templates/tests/testunit/actions/index.rb +4 -0
- data/lib/generators/nifty/scaffold/templates/tests/testunit/actions/new.rb +4 -0
- data/lib/generators/nifty/scaffold/templates/tests/testunit/actions/show.rb +4 -0
- data/lib/generators/nifty/scaffold/templates/tests/testunit/actions/update.rb +11 -0
- data/lib/generators/nifty/scaffold/templates/tests/testunit/controller.rb +5 -0
- data/lib/generators/nifty/scaffold/templates/tests/testunit/model.rb +7 -0
- data/lib/generators/nifty.rb +15 -0
- data/spec/spec_helper.rb +9 -0
- data/spec/very_nifty_generators_spec.rb +7 -0
- metadata +128 -0
@@ -0,0 +1,88 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class <%= user_class_name %>Test < ActiveSupport::TestCase
|
4
|
+
<%- unless options.authlogic? -%>
|
5
|
+
def new_<%= user_singular_name %>(attributes = {})
|
6
|
+
attributes[:username] ||= 'foo'
|
7
|
+
attributes[:email] ||= 'foo@example.com'
|
8
|
+
attributes[:password] ||= 'abc123'
|
9
|
+
attributes[:password_confirmation] ||= attributes[:password]
|
10
|
+
<%= user_singular_name %> = <%= user_class_name %>.new(attributes)
|
11
|
+
<%= user_singular_name %>.valid? # run validations
|
12
|
+
<%= user_singular_name %>
|
13
|
+
end
|
14
|
+
|
15
|
+
def setup
|
16
|
+
<%= user_class_name %>.delete_all
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_valid
|
20
|
+
assert new_<%= user_singular_name %>.valid?
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_require_username
|
24
|
+
assert new_<%= user_singular_name %>(:username => '').errors.on(:username)
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_require_password
|
28
|
+
assert new_<%= user_singular_name %>(:password => '').errors.on(:password)
|
29
|
+
end
|
30
|
+
|
31
|
+
def test_require_well_formed_email
|
32
|
+
assert new_<%= user_singular_name %>(:email => 'foo@bar@example.com').errors.on(:email)
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_validate_uniqueness_of_email
|
36
|
+
new_<%= user_singular_name %>(:email => 'bar@example.com').save!
|
37
|
+
assert new_<%= user_singular_name %>(:email => 'bar@example.com').errors.on(:email)
|
38
|
+
end
|
39
|
+
|
40
|
+
def test_validate_uniqueness_of_username
|
41
|
+
new_<%= user_singular_name %>(:username => 'uniquename').save!
|
42
|
+
assert new_<%= user_singular_name %>(:username => 'uniquename').errors.on(:username)
|
43
|
+
end
|
44
|
+
|
45
|
+
def test_validate_odd_characters_in_username
|
46
|
+
assert new_<%= user_singular_name %>(:username => 'odd ^&(@)').errors.on(:username)
|
47
|
+
end
|
48
|
+
|
49
|
+
def test_validate_password_length
|
50
|
+
assert new_<%= user_singular_name %>(:password => 'bad').errors.on(:password)
|
51
|
+
end
|
52
|
+
|
53
|
+
def test_require_matching_password_confirmation
|
54
|
+
assert new_<%= user_singular_name %>(:password_confirmation => 'nonmatching').errors.on(:password)
|
55
|
+
end
|
56
|
+
|
57
|
+
def test_generate_password_hash_and_salt_on_create
|
58
|
+
<%= user_singular_name %> = new_<%= user_singular_name %>
|
59
|
+
<%= user_singular_name %>.save!
|
60
|
+
assert <%= user_singular_name %>.password_hash
|
61
|
+
assert <%= user_singular_name %>.password_salt
|
62
|
+
end
|
63
|
+
|
64
|
+
def test_authenticate_by_username
|
65
|
+
<%= user_class_name %>.delete_all
|
66
|
+
<%= user_singular_name %> = new_<%= user_singular_name %>(:username => 'foobar', :password => 'secret')
|
67
|
+
<%= user_singular_name %>.save!
|
68
|
+
assert_equal <%= user_singular_name %>, <%= user_class_name %>.authenticate('foobar', 'secret')
|
69
|
+
end
|
70
|
+
|
71
|
+
def test_authenticate_by_email
|
72
|
+
<%= user_class_name %>.delete_all
|
73
|
+
<%= user_singular_name %> = new_<%= user_singular_name %>(:email => 'foo@bar.com', :password => 'secret')
|
74
|
+
<%= user_singular_name %>.save!
|
75
|
+
assert_equal <%= user_singular_name %>, <%= user_class_name %>.authenticate('foo@bar.com', 'secret')
|
76
|
+
end
|
77
|
+
|
78
|
+
def test_authenticate_bad_username
|
79
|
+
assert_nil <%= user_class_name %>.authenticate('nonexisting', 'secret')
|
80
|
+
end
|
81
|
+
|
82
|
+
def test_authenticate_bad_password
|
83
|
+
<%= user_class_name %>.delete_all
|
84
|
+
new_<%= user_singular_name %>(:username => 'foobar', :password => 'secret').save!
|
85
|
+
assert_nil <%= user_class_name %>.authenticate('foobar', 'badpassword')
|
86
|
+
end
|
87
|
+
<%- end -%>
|
88
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class <%= user_plural_class_name %>ControllerTest < ActionController::TestCase
|
4
|
+
def test_new
|
5
|
+
get :new
|
6
|
+
assert_template 'new'
|
7
|
+
end
|
8
|
+
|
9
|
+
def test_create_invalid
|
10
|
+
<%= user_class_name %>.any_instance.stubs(:valid?).returns(false)
|
11
|
+
post :create
|
12
|
+
assert_template 'new'
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_create_valid
|
16
|
+
<%= user_class_name %>.any_instance.stubs(:valid?).returns(true)
|
17
|
+
post :create
|
18
|
+
assert_redirected_to root_url
|
19
|
+
<%- unless options.authlogic? -%>
|
20
|
+
assert_equal assigns['<%= user_singular_name %>'].id, session['<%= user_singular_name %>_id']
|
21
|
+
<%- end -%>
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
class <%= user_class_name %> < ActiveRecord::Base
|
2
|
+
<%- if options.authlogic? -%>
|
3
|
+
acts_as_authentic
|
4
|
+
<%- else -%>
|
5
|
+
# new columns need to be added here to be writable through mass assignment
|
6
|
+
attr_accessible :username, :email, :password, :password_confirmation
|
7
|
+
|
8
|
+
attr_accessor :password
|
9
|
+
before_save :prepare_password
|
10
|
+
|
11
|
+
validates_presence_of :username
|
12
|
+
validates_uniqueness_of :username, :email, :allow_blank => true
|
13
|
+
validates_format_of :username, :with => /^[-\w\._@]+$/i, :allow_blank => true, :message => "should only contain letters, numbers, or .-_@"
|
14
|
+
validates_format_of :email, :with => /^[-a-z0-9_+\.]+\@([-a-z0-9]+\.)+[a-z0-9]{2,4}$/i
|
15
|
+
validates_presence_of :password, :on => :create
|
16
|
+
validates_confirmation_of :password
|
17
|
+
validates_length_of :password, :minimum => 4, :allow_blank => true
|
18
|
+
|
19
|
+
# login can be either username or email address
|
20
|
+
def self.authenticate(login, pass)
|
21
|
+
<%= user_singular_name %> = find_by_username(login) || find_by_email(login)
|
22
|
+
return <%= user_singular_name %> if <%= user_singular_name %> && <%= user_singular_name %>.matching_password?(pass)
|
23
|
+
end
|
24
|
+
|
25
|
+
def matching_password?(pass)
|
26
|
+
self.password_hash == encrypt_password(pass)
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
|
31
|
+
def prepare_password
|
32
|
+
unless password.blank?
|
33
|
+
self.password_salt = Digest::SHA1.hexdigest([Time.now, rand].join)
|
34
|
+
self.password_hash = encrypt_password(password)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def encrypt_password(pass)
|
39
|
+
Digest::SHA1.hexdigest([pass, password_salt].join)
|
40
|
+
end
|
41
|
+
<%- end -%>
|
42
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
class <%= user_plural_class_name %>Controller < ApplicationController
|
2
|
+
def new
|
3
|
+
@<%= user_singular_name %> = <%= user_class_name %>.new
|
4
|
+
end
|
5
|
+
|
6
|
+
def create
|
7
|
+
@<%= user_singular_name %> = <%= user_class_name %>.new(params[:<%= user_singular_name %>])
|
8
|
+
if @<%= user_singular_name %>.save
|
9
|
+
<%- unless options[:authlogic] -%>
|
10
|
+
session[:<%= user_singular_name %>_id] = @<%= user_singular_name %>.id
|
11
|
+
<%- end -%>
|
12
|
+
flash[:notice] = "Thank you for signing up! You are now logged in."
|
13
|
+
redirect_to root_url
|
14
|
+
else
|
15
|
+
render :action => 'new'
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'generators/nifty'
|
2
|
+
|
3
|
+
module Nifty
|
4
|
+
module Generators
|
5
|
+
class ConfigGenerator < Base
|
6
|
+
argument :config_name, :type => :string, :default => 'app'
|
7
|
+
|
8
|
+
def create_files
|
9
|
+
initializer("load_#{file_name}_config.rb") do
|
10
|
+
<<EOF
|
11
|
+
raw_config = File.read(RAILS_ROOT + "/config/#{file_name}_config.yml")
|
12
|
+
#{constant_name}_CONFIG = YAML.load(raw_config)[RAILS_ENV].symbolize_keys
|
13
|
+
EOF
|
14
|
+
end
|
15
|
+
copy_file 'config.yml', "config/#{file_name}_config.yml"
|
16
|
+
end
|
17
|
+
|
18
|
+
no_tasks do
|
19
|
+
def file_name
|
20
|
+
config_name.underscore
|
21
|
+
end
|
22
|
+
|
23
|
+
def constant_name
|
24
|
+
config_name.underscore.upcase
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'generators/nifty'
|
2
|
+
|
3
|
+
module Nifty
|
4
|
+
module Generators
|
5
|
+
class LayoutGenerator < Base
|
6
|
+
argument :layout_name, :type => :string, :default => 'application', :banner => 'layout_name'
|
7
|
+
|
8
|
+
class_option :haml, :desc => 'Generate HAML for view, and SASS for stylesheet.', :type => :boolean
|
9
|
+
|
10
|
+
def create_layout
|
11
|
+
if options.haml?
|
12
|
+
template 'layout.html.haml', "app/views/layouts/#{file_name}.html.haml"
|
13
|
+
copy_file 'stylesheet.sass', "public/stylesheets/sass/#{file_name}.sass"
|
14
|
+
else
|
15
|
+
template 'layout.html.erb', "app/views/layouts/#{file_name}.html.erb"
|
16
|
+
copy_file 'stylesheet.css', "public/stylesheets/#{file_name}.css"
|
17
|
+
end
|
18
|
+
copy_file 'helper.rb', 'app/helpers/layout_helper.rb'
|
19
|
+
end
|
20
|
+
|
21
|
+
no_tasks do
|
22
|
+
def file_name
|
23
|
+
layout_name.underscore
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# These helper methods can be called in your template to set variables to be used in the layout
|
2
|
+
# This module should be included in all views globally,
|
3
|
+
# to do so you may need to add this line to your ApplicationController
|
4
|
+
# helper :layout
|
5
|
+
module LayoutHelper
|
6
|
+
def title(page_title, show_title = true)
|
7
|
+
@_content_for[:title] = page_title.to_s
|
8
|
+
@show_title = show_title
|
9
|
+
end
|
10
|
+
|
11
|
+
def show_title?
|
12
|
+
@show_title
|
13
|
+
end
|
14
|
+
|
15
|
+
def stylesheet(*args)
|
16
|
+
content_for(:head) { stylesheet_link_tag(*args) }
|
17
|
+
end
|
18
|
+
|
19
|
+
def javascript(*args)
|
20
|
+
content_for(:head) { javascript_include_tag(*args) }
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,247 @@
|
|
1
|
+
require 'generators/nifty'
|
2
|
+
require 'rails/generators/migration'
|
3
|
+
|
4
|
+
module Nifty
|
5
|
+
module Generators
|
6
|
+
class ScaffoldGenerator < Base
|
7
|
+
include Rails::Generators::Migration
|
8
|
+
no_tasks { attr_accessor :model_name, :model_attributes, :controller_actions }
|
9
|
+
|
10
|
+
argument :model_name, :type => :string, :required => true, :banner => 'ModelName'
|
11
|
+
argument :args_for_c_m, :type => :array, :default => [], :banner => 'controller_actions and model:attributes'
|
12
|
+
|
13
|
+
class_option :skip_model, :desc => 'Don\'t generate a model or migration file.', :type => :boolean
|
14
|
+
class_option :skip_migration, :desc => 'Dont generate migration file for model.', :type => :boolean
|
15
|
+
class_option :skip_timestamps, :desc => 'Don\'t add timestamps to migration file.', :type => :boolean
|
16
|
+
class_option :skip_controller, :desc => 'Don\'t generate controller, helper, or views.', :type => :boolean
|
17
|
+
class_option :invert, :desc => 'Generate all controller actions except these mentioned.', :type => :boolean
|
18
|
+
class_option :haml, :desc => 'Generate HAML views instead of ERB.', :type => :boolean
|
19
|
+
|
20
|
+
class_option :testunit, :desc => 'Use test/unit for test files.', :group => 'Test framework',
|
21
|
+
:type => :boolean
|
22
|
+
class_option :rspec, :desc => 'Use RSpec for test files.', :group => 'Test framework',
|
23
|
+
:type => :boolean
|
24
|
+
class_option :shoulda, :desc => 'Use shoulda for test files.', :group => 'Test framework',
|
25
|
+
:type => :boolean
|
26
|
+
|
27
|
+
def initialize(*args, &block)
|
28
|
+
super
|
29
|
+
|
30
|
+
@controller_actions = []
|
31
|
+
@model_attributes = []
|
32
|
+
|
33
|
+
args_for_c_m.each do |arg|
|
34
|
+
if arg == '!'
|
35
|
+
options[:invert] = true
|
36
|
+
elsif arg.include?(':')
|
37
|
+
@model_attributes << Rails::Generators::GeneratedAttribute.new(*arg.split(':'))
|
38
|
+
else
|
39
|
+
@controller_actions << arg
|
40
|
+
@controller_actions << 'create' if arg == 'new'
|
41
|
+
@controller_actions << 'update' if arg == 'edit'
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
@controller_actions.uniq!
|
46
|
+
@model_attributes.uniq!
|
47
|
+
|
48
|
+
if options.invert? || @controller_actions.empty?
|
49
|
+
@controller_actions = all_actions - @controller_actions
|
50
|
+
end
|
51
|
+
|
52
|
+
if @model_attributes.empty?
|
53
|
+
options[:skip_model] = true # default to skipping model if no attributes passed
|
54
|
+
if model_exists?
|
55
|
+
model_columns_for_attributes.each do |column|
|
56
|
+
@model_attributes << Rails::Generators::GeneratedAttribute.new(column.name.to_s, column.type.to_s)
|
57
|
+
end
|
58
|
+
else
|
59
|
+
@model_attributes << Rails::Generators::GeneratedAttribute.new('name', 'string')
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
def create_model
|
65
|
+
unless options.skip_model?
|
66
|
+
template 'model.rb', "app/models/#{singular_name}.rb"
|
67
|
+
if options.rspec?
|
68
|
+
template "tests/rspec/model.rb", "spec/models/#{singular_name}_spec.rb"
|
69
|
+
template 'fixtures.yml', "spec/fixtures/#{plural_name}.yml"
|
70
|
+
else
|
71
|
+
template "tests/#{test_framework}/model.rb", "test/unit/#{singular_name}_test.rb"
|
72
|
+
template 'fixtures.yml', "test/fixtures/#{plural_name}.yml"
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
def create_migration
|
78
|
+
unless options.skip_model? || options.skip_migration?
|
79
|
+
migration_template 'migration.rb', "db/migrate/create_#{plural_name}.rb"
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
def create_controller
|
84
|
+
unless options.skip_controller?
|
85
|
+
template 'controller.rb', "app/controllers/#{plural_name}_controller.rb"
|
86
|
+
|
87
|
+
template 'helper.rb', "app/helpers/#{plural_name}_helper.rb"
|
88
|
+
|
89
|
+
controller_actions.each do |action|
|
90
|
+
if %w[index show new edit].include?(action) # Actions with templates
|
91
|
+
template "views/#{view_language}/#{action}.html.#{view_language}", "app/views/#{plural_name}/#{action}.html.#{view_language}"
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
if form_partial?
|
96
|
+
template "views/#{view_language}/_form.html.#{view_language}", "app/views/#{plural_name}/_form.html.#{view_language}"
|
97
|
+
end
|
98
|
+
|
99
|
+
route "resources #{plural_name.to_sym.inspect}"
|
100
|
+
|
101
|
+
if options.rspec?
|
102
|
+
template "tests/#{test_framework}/controller.rb", "spec/controllers/#{plural_name}_controller_spec.rb"
|
103
|
+
else
|
104
|
+
template "tests/#{test_framework}/controller.rb", "test/functional/#{plural_name}_controller_test.rb"
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
no_tasks do
|
110
|
+
def form_partial?
|
111
|
+
actions? :new, :edit
|
112
|
+
end
|
113
|
+
|
114
|
+
def all_actions
|
115
|
+
%w[index show new create edit update destroy]
|
116
|
+
end
|
117
|
+
|
118
|
+
def action?(name)
|
119
|
+
controller_actions.include? name.to_s
|
120
|
+
end
|
121
|
+
|
122
|
+
def actions?(*names)
|
123
|
+
names.all? { |name| action? name }
|
124
|
+
end
|
125
|
+
|
126
|
+
def singular_name
|
127
|
+
model_name.underscore
|
128
|
+
end
|
129
|
+
|
130
|
+
def plural_name
|
131
|
+
model_name.underscore.pluralize
|
132
|
+
end
|
133
|
+
|
134
|
+
def class_name
|
135
|
+
model_name.camelize
|
136
|
+
end
|
137
|
+
|
138
|
+
def plural_class_name
|
139
|
+
plural_name.camelize
|
140
|
+
end
|
141
|
+
|
142
|
+
def controller_methods(dir_name)
|
143
|
+
controller_actions.map do |action|
|
144
|
+
read_template("#{dir_name}/#{action}.rb")
|
145
|
+
end.join(" \n").strip
|
146
|
+
end
|
147
|
+
|
148
|
+
def render_form
|
149
|
+
if form_partial?
|
150
|
+
if options.haml?
|
151
|
+
"= render 'form'"
|
152
|
+
else
|
153
|
+
"<%= render 'form' %>"
|
154
|
+
end
|
155
|
+
else
|
156
|
+
read_template("views/#{view_language}/_form.html.#{view_language}")
|
157
|
+
end
|
158
|
+
end
|
159
|
+
|
160
|
+
def items_path(suffix = 'path')
|
161
|
+
if action? :index
|
162
|
+
"#{plural_name}_#{suffix}"
|
163
|
+
else
|
164
|
+
"root_#{suffix}"
|
165
|
+
end
|
166
|
+
end
|
167
|
+
|
168
|
+
def item_path(suffix = 'path')
|
169
|
+
if action? :show
|
170
|
+
"@#{singular_name}"
|
171
|
+
else
|
172
|
+
items_path(suffix)
|
173
|
+
end
|
174
|
+
end
|
175
|
+
|
176
|
+
def item_path_for_spec(suffix = 'path')
|
177
|
+
if action? :show
|
178
|
+
"#{singular_name}_#{suffix}(assigns[:#{singular_name}])"
|
179
|
+
else
|
180
|
+
items_path(suffix)
|
181
|
+
end
|
182
|
+
end
|
183
|
+
|
184
|
+
def item_path_for_test(suffix = 'path')
|
185
|
+
if action? :show
|
186
|
+
"#{singular_name}_#{suffix}(assigns(:#{singular_name}))"
|
187
|
+
else
|
188
|
+
items_path(suffix)
|
189
|
+
end
|
190
|
+
end
|
191
|
+
|
192
|
+
def model_columns_for_attributes
|
193
|
+
class_name.constantize.columns.reject do |column|
|
194
|
+
column.name.to_s =~ /^(id|created_at|updated_at)$/
|
195
|
+
end
|
196
|
+
end
|
197
|
+
end
|
198
|
+
|
199
|
+
protected
|
200
|
+
|
201
|
+
no_tasks do
|
202
|
+
def view_language
|
203
|
+
options.haml? ? 'haml' : 'erb'
|
204
|
+
end
|
205
|
+
|
206
|
+
def test_framework
|
207
|
+
return @test_framework if defined?(@test_framework)
|
208
|
+
if options.testunit?
|
209
|
+
return @test_framework = :testunit
|
210
|
+
elsif options.rspec?
|
211
|
+
return @test_framework = :rspec
|
212
|
+
elsif options.shoulda?
|
213
|
+
return @test_framework = :shoulda
|
214
|
+
else
|
215
|
+
return @test_framework = default_test_framework
|
216
|
+
end
|
217
|
+
end
|
218
|
+
|
219
|
+
def default_test_framework
|
220
|
+
File.exist?(destination_path("spec")) ? :rspec : :testunit
|
221
|
+
end
|
222
|
+
|
223
|
+
def model_exists?
|
224
|
+
File.exist? destination_path("app/models/#{singular_name}.rb")
|
225
|
+
end
|
226
|
+
|
227
|
+
def read_template(relative_path)
|
228
|
+
ERB.new(File.read(find_in_source_paths(relative_path)), nil, '-').result(binding)
|
229
|
+
end
|
230
|
+
|
231
|
+
def destination_path(path)
|
232
|
+
File.join(destination_root, path)
|
233
|
+
end
|
234
|
+
|
235
|
+
# FIXME: Should be proxied to ActiveRecord::Generators::Base
|
236
|
+
# Implement the required interface for Rails::Generators::Migration.
|
237
|
+
def next_migration_number(dirname) #:nodoc:
|
238
|
+
if ActiveRecord::Base.timestamped_migrations
|
239
|
+
Time.now.utc.strftime("%Y%m%d%H%M%S")
|
240
|
+
else
|
241
|
+
"%.3d" % (current_migration_number(dirname) + 1)
|
242
|
+
end
|
243
|
+
end
|
244
|
+
end
|
245
|
+
end
|
246
|
+
end
|
247
|
+
end
|
@@ -0,0 +1,9 @@
|
|
1
|
+
def create
|
2
|
+
@<%= singular_name %> = <%= class_name %>.new(params[:<%= singular_name %>])
|
3
|
+
if @<%= singular_name %>.save
|
4
|
+
flash[:notice] = "Successfully created <%= model_name.underscore.humanize.downcase %>."
|
5
|
+
redirect_to <%= item_path('url') %>
|
6
|
+
else
|
7
|
+
render :action => 'new'
|
8
|
+
end
|
9
|
+
end
|
@@ -0,0 +1,9 @@
|
|
1
|
+
def update
|
2
|
+
@<%= singular_name %> = <%= class_name %>.find(params[:id])
|
3
|
+
if @<%= singular_name %>.update_attributes(params[:<%= singular_name %>])
|
4
|
+
flash[:notice] = "Successfully updated <%= model_name.underscore.humanize.downcase %>."
|
5
|
+
redirect_to <%= item_path('url') %>
|
6
|
+
else
|
7
|
+
render :action => 'edit'
|
8
|
+
end
|
9
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
class Create<%= plural_class_name %> < ActiveRecord::Migration
|
2
|
+
def self.up
|
3
|
+
create_table :<%= plural_name %> do |t|
|
4
|
+
<%- for attribute in model_attributes -%>
|
5
|
+
t.<%= attribute.type %> :<%= attribute.name %>
|
6
|
+
<%- end -%>
|
7
|
+
<%- unless options[:skip_timestamps] -%>
|
8
|
+
t.timestamps
|
9
|
+
<%- end -%>
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.down
|
14
|
+
drop_table :<%= plural_name %>
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
it "create action should render new template when model is invalid" do
|
2
|
+
<%= class_name %>.any_instance.stubs(:valid?).returns(false)
|
3
|
+
post :create
|
4
|
+
response.should render_template(:new)
|
5
|
+
end
|
6
|
+
|
7
|
+
it "create action should redirect when model is valid" do
|
8
|
+
<%= class_name %>.any_instance.stubs(:valid?).returns(true)
|
9
|
+
post :create
|
10
|
+
response.should redirect_to(<%= item_path_for_spec('url') %>)
|
11
|
+
end
|
@@ -0,0 +1,6 @@
|
|
1
|
+
it "destroy action should destroy model and redirect to index action" do
|
2
|
+
<%= singular_name %> = <%= class_name %>.first
|
3
|
+
delete :destroy, :id => <%= singular_name %>
|
4
|
+
response.should redirect_to(<%= items_path('url') %>)
|
5
|
+
<%= class_name %>.exists?(<%= singular_name %>.id).should be_false
|
6
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
it "update action should render edit template when model is invalid" do
|
2
|
+
<%= class_name %>.any_instance.stubs(:valid?).returns(false)
|
3
|
+
put :update, :id => <%= class_name %>.first
|
4
|
+
response.should render_template(:edit)
|
5
|
+
end
|
6
|
+
|
7
|
+
it "update action should redirect when model is valid" do
|
8
|
+
<%= class_name %>.any_instance.stubs(:valid?).returns(true)
|
9
|
+
put :update, :id => <%= class_name %>.first
|
10
|
+
response.should redirect_to(<%= item_path_for_spec('url') %>)
|
11
|
+
end
|