zoo-generators 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +2 -2
- data/Rakefile +1 -1
- data/features/zoo_authentication.feature +13 -13
- data/lib/generators/zoo/authentication/USAGE +1 -1
- data/lib/generators/zoo/authentication/authentication_generator.rb +12 -12
- data/lib/generators/zoo/authentication/templates/controller_authentication.rb +2 -2
- data/lib/generators/zoo/authentication/templates/sessions_controller.rb +2 -2
- data/lib/generators/zoo/authentication/templates/tests/rspec/user.rb +12 -12
- data/lib/generators/zoo/authentication/templates/tests/rspec/users_controller.rb +5 -5
- data/lib/generators/zoo/authentication/templates/tests/shoulda/user.rb +12 -12
- data/lib/generators/zoo/authentication/templates/tests/shoulda/users_controller.rb +5 -5
- data/lib/generators/zoo/authentication/templates/tests/testunit/user.rb +12 -12
- data/lib/generators/zoo/authentication/templates/tests/testunit/users_controller.rb +5 -5
- data/lib/generators/zoo/authentication/templates/user.rb +4 -4
- data/lib/generators/zoo/authentication/templates/users_controller.rb +3 -3
- data/lib/generators/zoo/config/config_generator.rb +1 -1
- data/lib/generators/zoo/layout/layout_generator.rb +1 -1
- data/lib/generators/zoo/layout/templates/error_messages_helper.rb +3 -3
- data/lib/generators/zoo/layout/templates/layout.html.haml +2 -2
- data/lib/generators/zoo/scaffold/scaffold_generator.rb +13 -13
- data/lib/generators/zoo/scaffold/templates/actions/create.rb +1 -1
- data/lib/generators/zoo/scaffold/templates/actions/destroy.rb +1 -1
- data/lib/generators/zoo/scaffold/templates/tests/rspec/actions/destroy.rb +1 -1
- data/lib/generators/zoo/scaffold/templates/tests/rspec/actions/edit.rb +1 -1
- data/lib/generators/zoo/scaffold/templates/tests/rspec/actions/show.rb +1 -1
- data/lib/generators/zoo/scaffold/templates/tests/rspec/actions/update.rb +2 -2
- data/lib/generators/zoo/scaffold/templates/tests/shoulda/actions/destroy.rb +1 -1
- data/lib/generators/zoo/scaffold/templates/tests/shoulda/actions/edit.rb +1 -1
- data/lib/generators/zoo/scaffold/templates/tests/shoulda/actions/show.rb +1 -1
- data/lib/generators/zoo/scaffold/templates/tests/shoulda/actions/update.rb +2 -2
- data/lib/generators/zoo/scaffold/templates/tests/testunit/actions/destroy.rb +1 -1
- data/lib/generators/zoo/scaffold/templates/tests/testunit/actions/edit.rb +1 -1
- data/lib/generators/zoo/scaffold/templates/tests/testunit/actions/show.rb +1 -1
- data/lib/generators/zoo/scaffold/templates/tests/testunit/actions/update.rb +2 -2
- data/lib/generators/zoo/scaffold/templates/views/haml/_form.html.haml +1 -1
- data/lib/generators/zoo/scaffold/templates/views/haml/edit.html.haml +1 -1
- data/lib/generators/zoo/scaffold/templates/views/haml/index.html.haml +4 -4
- data/lib/generators/zoo/scaffold/templates/views/haml/show.html.haml +3 -3
- metadata +1 -2
- data/lib/generators/zoo/layout/templates/layout.html.erb +0 -19
data/README.rdoc
CHANGED
@@ -9,7 +9,7 @@ A collection of useful Rails generator scripts for scaffolding, layout files, au
|
|
9
9
|
|
10
10
|
Add the gem to your Gemfile.
|
11
11
|
|
12
|
-
gem "zoo-generators",
|
12
|
+
gem "zoo-generators", group : :development
|
13
13
|
|
14
14
|
Then you can run any of the included generators.
|
15
15
|
|
@@ -54,7 +54,7 @@ Add the attribute to the attr_accessible line in the model.
|
|
54
54
|
|
55
55
|
Some generators default redirecting to the root_url. Set this in your routes.rb file like this (substituting your controller name).
|
56
56
|
|
57
|
-
root :
|
57
|
+
root to: "home#index"
|
58
58
|
|
59
59
|
|
60
60
|
<b>I get a missing database error.</b>
|
data/Rakefile
CHANGED
@@ -5,7 +5,7 @@ Feature: Zoo Authentication Generator
|
|
5
5
|
|
6
6
|
Scenario: Generate default authentication
|
7
7
|
Given a new Rails app
|
8
|
-
When I insert "root :
|
8
|
+
When I insert "root to: 'users#new'" into "config/routes.rb" after line 1
|
9
9
|
And I run "rails g zoo:authentication"
|
10
10
|
Then I should see the following files
|
11
11
|
| app/models/user.rb |
|
@@ -25,13 +25,13 @@ Feature: Zoo Authentication Generator
|
|
25
25
|
And I should see the following in file "config/routes.rb"
|
26
26
|
| resources :sessions |
|
27
27
|
| resources :users |
|
28
|
-
| match 'login' => 'sessions#new',
|
29
|
-
| match 'logout' => 'sessions#destroy',
|
30
|
-
| match 'signup' => 'users#new',
|
31
|
-
| match 'user/edit' => 'users#edit',
|
28
|
+
| match 'login' => 'sessions#new', as : :login |
|
29
|
+
| match 'logout' => 'sessions#destroy', as : :logout |
|
30
|
+
| match 'signup' => 'users#new', as : :signup |
|
31
|
+
| match 'user/edit' => 'users#edit', as : :edit_current_user |
|
32
32
|
And I should see "include ControllerAuthentication" in file "app/controllers/application_controller.rb"
|
33
|
-
And I should see "gem "mocha",
|
34
|
-
And I should see "gem "bcrypt-ruby", :
|
33
|
+
And I should see "gem "mocha", group : :test" in file "Gemfile"
|
34
|
+
And I should see "gem "bcrypt-ruby", require: "bcrypt"" in file "Gemfile"
|
35
35
|
When I run "rails g zoo:layout -f"
|
36
36
|
And I run "rake db:migrate"
|
37
37
|
And I run "bundle install"
|
@@ -39,7 +39,7 @@ Feature: Zoo Authentication Generator
|
|
39
39
|
|
40
40
|
Scenario: Generate named authentication
|
41
41
|
Given a new Rails app
|
42
|
-
When I insert "root :
|
42
|
+
When I insert "root to: 'accounts#new'" into "config/routes.rb" after line 1
|
43
43
|
And I run "rails g zoo:authentication Account CurrentSession"
|
44
44
|
Then I should see the following files
|
45
45
|
| app/models/account.rb |
|
@@ -57,10 +57,10 @@ Feature: Zoo Authentication Generator
|
|
57
57
|
And I should see the following in file "config/routes.rb"
|
58
58
|
| resources :current_sessions |
|
59
59
|
| resources :accounts |
|
60
|
-
| match 'login' => 'current_sessions#new',
|
61
|
-
| match 'logout' => 'current_sessions#destroy',
|
62
|
-
| match 'signup' => 'accounts#new',
|
63
|
-
| match 'account/edit' => 'accounts#edit',
|
60
|
+
| match 'login' => 'current_sessions#new', as : :login |
|
61
|
+
| match 'logout' => 'current_sessions#destroy', as : :logout |
|
62
|
+
| match 'signup' => 'accounts#new', as : :signup |
|
63
|
+
| match 'account/edit' => 'accounts#edit', as : :edit_current_account |
|
64
64
|
When I run "rails g zoo:layout -f"
|
65
65
|
And I run "rake db:migrate"
|
66
66
|
And I run "bundle install"
|
@@ -68,7 +68,7 @@ Feature: Zoo Authentication Generator
|
|
68
68
|
|
69
69
|
Scenario: Generate named authentication with rspec
|
70
70
|
Given a new Rails app
|
71
|
-
When I insert "root :
|
71
|
+
When I insert "root to: 'accounts#new'" into "config/routes.rb" after line 1
|
72
72
|
And I run "rails g zoo:authentication Account CurrentSession --rspec"
|
73
73
|
Then I should see the following files
|
74
74
|
| spec/models/account_spec.rb |
|
@@ -41,6 +41,6 @@ Methods:
|
|
41
41
|
You can also restrict unregistered users from accessing a controller using
|
42
42
|
a before filter. For example.
|
43
43
|
|
44
|
-
before_filter :login_required, :
|
44
|
+
before_filter :login_required, except: [:index, :show]
|
45
45
|
|
46
46
|
See the generated file lib/authentication.rb for details.
|
@@ -6,17 +6,17 @@ module Zoo
|
|
6
6
|
class AuthenticationGenerator < Base
|
7
7
|
include Rails::Generators::Migration
|
8
8
|
|
9
|
-
argument :user_name, :
|
10
|
-
argument :session_name, :
|
9
|
+
argument :user_name, type: :string, default: 'user', banner: 'user_name'
|
10
|
+
argument :session_name, type: :string, default: 'session', banner: 'sessions_controller_name'
|
11
11
|
|
12
|
-
class_option :testunit, :
|
13
|
-
class_option :rspec, :
|
14
|
-
class_option :shoulda, :
|
12
|
+
class_option :testunit, desc: 'Use test/unit for test files.', group: 'Test framework', type: :boolean
|
13
|
+
class_option :rspec, desc: 'Use RSpec for test files.', group: 'Test framework', type: :boolean
|
14
|
+
class_option :shoulda, desc: 'Use shoulda for test files.', group: 'Test framework', type: :boolean
|
15
15
|
|
16
16
|
def add_gems
|
17
17
|
add_gem "haml-rails"
|
18
|
-
add_gem "bcrypt-ruby", :
|
19
|
-
add_gem "mocha", :
|
18
|
+
add_gem "bcrypt-ruby", require: "bcrypt"
|
19
|
+
add_gem "mocha", group: :test
|
20
20
|
end
|
21
21
|
|
22
22
|
def create_model_files
|
@@ -47,10 +47,10 @@ module Zoo
|
|
47
47
|
def create_routes
|
48
48
|
route "resources #{user_plural_name.to_sym.inspect}"
|
49
49
|
route "resources #{session_plural_name.to_sym.inspect}"
|
50
|
-
route "match 'login' => '#{session_plural_name}#new',
|
51
|
-
route "match 'logout' => '#{session_plural_name}#destroy',
|
52
|
-
route "match 'signup' => '#{user_plural_name}#new',
|
53
|
-
route "match '#{user_singular_name}/edit' => '#{user_plural_name}#edit',
|
50
|
+
route "match 'login' => '#{session_plural_name}#new', as : :login"
|
51
|
+
route "match 'logout' => '#{session_plural_name}#destroy', as : :logout"
|
52
|
+
route "match 'signup' => '#{user_plural_name}#new', as : :signup"
|
53
|
+
route "match '#{user_singular_name}/edit' => '#{user_plural_name}#edit', as : :edit_current_#{user_singular_name}"
|
54
54
|
end
|
55
55
|
|
56
56
|
def create_migration
|
@@ -129,7 +129,7 @@ module Zoo
|
|
129
129
|
elsif options.shoulda?
|
130
130
|
return @test_framework = :shoulda
|
131
131
|
else
|
132
|
-
return @test_framework = File.exist?(destination_path('spec')) ?
|
132
|
+
return @test_framework = File.exist?(destination_path('spec')) ? rspec: :testunit
|
133
133
|
end
|
134
134
|
end
|
135
135
|
|
@@ -14,7 +14,7 @@
|
|
14
14
|
# You can also restrict unregistered users from accessing a controller using
|
15
15
|
# a before filter. For example.
|
16
16
|
#
|
17
|
-
# before_filter :login_required, :
|
17
|
+
# before_filter :login_required, except: [:index, :show]
|
18
18
|
module ControllerAuthentication
|
19
19
|
def self.included(controller)
|
20
20
|
controller.send :helper_method, :current_<%= user_singular_name %>, :logged_in?, :redirect_to_target_or_default
|
@@ -31,7 +31,7 @@ module ControllerAuthentication
|
|
31
31
|
def login_required
|
32
32
|
unless logged_in?
|
33
33
|
store_target_location
|
34
|
-
redirect_to login_url, :
|
34
|
+
redirect_to login_url, alert: "You must first log in or sign up before accessing this page."
|
35
35
|
end
|
36
36
|
end
|
37
37
|
|
@@ -6,7 +6,7 @@ class <%= session_plural_class_name %>Controller < ApplicationController
|
|
6
6
|
<%= user_singular_name %> = <%= user_class_name %>.authenticate(params[:login], params[:password])
|
7
7
|
if <%= user_singular_name %>
|
8
8
|
session[:<%= user_singular_name %>_id] = <%= user_singular_name %>.id
|
9
|
-
redirect_to_target_or_default root_url, :
|
9
|
+
redirect_to_target_or_default root_url, notice: "Logged in successfully."
|
10
10
|
else
|
11
11
|
flash.now[:alert] = "Invalid login or password."
|
12
12
|
render :new
|
@@ -15,6 +15,6 @@ class <%= session_plural_class_name %>Controller < ApplicationController
|
|
15
15
|
|
16
16
|
def destroy
|
17
17
|
session[:<%= user_singular_name %>_id] = nil
|
18
|
-
redirect_to root_url, :
|
18
|
+
redirect_to root_url, notice: "You have been logged out."
|
19
19
|
end
|
20
20
|
end
|
@@ -18,33 +18,33 @@ describe <%= user_class_name %> do
|
|
18
18
|
end
|
19
19
|
|
20
20
|
it "should require username" do
|
21
|
-
new_<%= user_singular_name %>(:
|
21
|
+
new_<%= user_singular_name %>(username: '').should have(1).error_on(:username)
|
22
22
|
end
|
23
23
|
|
24
24
|
it "should require password" do
|
25
|
-
new_<%= user_singular_name %>(:
|
25
|
+
new_<%= user_singular_name %>(password: '').should have(1).error_on(:password)
|
26
26
|
end
|
27
27
|
|
28
28
|
it "should require well formed email" do
|
29
|
-
new_<%= user_singular_name %>(:
|
29
|
+
new_<%= user_singular_name %>(email: 'foo@bar@example.com').should have(1).error_on(:email)
|
30
30
|
end
|
31
31
|
|
32
32
|
it "should validate uniqueness of email" do
|
33
|
-
new_<%= user_singular_name %>(:
|
34
|
-
new_<%= user_singular_name %>(:
|
33
|
+
new_<%= user_singular_name %>(email: 'bar@example.com').save!
|
34
|
+
new_<%= user_singular_name %>(email: 'bar@example.com').should have(1).error_on(:email)
|
35
35
|
end
|
36
36
|
|
37
37
|
it "should validate uniqueness of username" do
|
38
|
-
new_<%= user_singular_name %>(:
|
39
|
-
new_<%= user_singular_name %>(:
|
38
|
+
new_<%= user_singular_name %>(username: 'uniquename').save!
|
39
|
+
new_<%= user_singular_name %>(username: 'uniquename').should have(1).error_on(:username)
|
40
40
|
end
|
41
41
|
|
42
42
|
it "should not allow odd characters in username" do
|
43
|
-
new_<%= user_singular_name %>(:
|
43
|
+
new_<%= user_singular_name %>(username: 'odd ^&(@)').should have(1).error_on(:username)
|
44
44
|
end
|
45
45
|
|
46
46
|
it "should validate password is longer than 3 characters" do
|
47
|
-
new_<%= user_singular_name %>(:
|
47
|
+
new_<%= user_singular_name %>(password: 'bad').should have(1).error_on(:password)
|
48
48
|
end
|
49
49
|
|
50
50
|
it "should require matching password confirmation" do
|
@@ -59,13 +59,13 @@ describe <%= user_class_name %> do
|
|
59
59
|
end
|
60
60
|
|
61
61
|
it "should authenticate by username" do
|
62
|
-
<%= user_singular_name %> = new_<%= user_singular_name %>(:
|
62
|
+
<%= user_singular_name %> = new_<%= user_singular_name %>(username: 'foobar', password: 'secret')
|
63
63
|
<%= user_singular_name %>.save!
|
64
64
|
<%= user_class_name %>.authenticate('foobar', 'secret').should == <%= user_singular_name %>
|
65
65
|
end
|
66
66
|
|
67
67
|
it "should authenticate by email" do
|
68
|
-
<%= user_singular_name %> = new_<%= user_singular_name %>(:
|
68
|
+
<%= user_singular_name %> = new_<%= user_singular_name %>(email: 'foo@bar.com', password: 'secret')
|
69
69
|
<%= user_singular_name %>.save!
|
70
70
|
<%= user_class_name %>.authenticate('foo@bar.com', 'secret').should == <%= user_singular_name %>
|
71
71
|
end
|
@@ -75,7 +75,7 @@ describe <%= user_class_name %> do
|
|
75
75
|
end
|
76
76
|
|
77
77
|
it "should not authenticate bad password" do
|
78
|
-
new_<%= user_singular_name %>(:
|
78
|
+
new_<%= user_singular_name %>(username: 'foobar', password: 'secret').save!
|
79
79
|
<%= user_class_name %>.authenticate('foobar', 'badpassword').should be_nil
|
80
80
|
end
|
81
81
|
end
|
@@ -23,32 +23,32 @@ describe <%= user_plural_class_name %>Controller do
|
|
23
23
|
end
|
24
24
|
|
25
25
|
it "edit action should redirect when not logged in" do
|
26
|
-
get :edit, :
|
26
|
+
get :edit, id: "ignored"
|
27
27
|
response.should redirect_to(login_url)
|
28
28
|
end
|
29
29
|
|
30
30
|
it "edit action should render edit template" do
|
31
31
|
@controller.stubs(:current_<%= user_singular_name %>).returns(<%= user_class_name %>.first)
|
32
|
-
get :edit, :
|
32
|
+
get :edit, id: "ignored"
|
33
33
|
response.should render_template(:edit)
|
34
34
|
end
|
35
35
|
|
36
36
|
it "update action should redirect when not logged in" do
|
37
|
-
put :update, :
|
37
|
+
put :update, id: "ignored"
|
38
38
|
response.should redirect_to(login_url)
|
39
39
|
end
|
40
40
|
|
41
41
|
it "update action should render edit template when <%= user_singular_name %> is invalid" do
|
42
42
|
@controller.stubs(:current_<%= user_singular_name %>).returns(<%= user_class_name %>.first)
|
43
43
|
<%= user_class_name %>.any_instance.stubs(:valid?).returns(false)
|
44
|
-
put :update, :
|
44
|
+
put :update, id: "ignored"
|
45
45
|
response.should render_template(:edit)
|
46
46
|
end
|
47
47
|
|
48
48
|
it "update action should redirect when <%= user_singular_name %> is valid" do
|
49
49
|
@controller.stubs(:current_<%= user_singular_name %>).returns(<%= user_class_name %>.first)
|
50
50
|
<%= user_class_name %>.any_instance.stubs(:valid?).returns(true)
|
51
|
-
put :update, :
|
51
|
+
put :update, id: "ignored"
|
52
52
|
response.should redirect_to(root_url)
|
53
53
|
end
|
54
54
|
end
|
@@ -20,33 +20,33 @@ class <%= user_class_name %>Test < ActiveSupport::TestCase
|
|
20
20
|
end
|
21
21
|
|
22
22
|
should "require username" do
|
23
|
-
assert_equal ["can't be blank"], new_<%= user_singular_name %>(:
|
23
|
+
assert_equal ["can't be blank"], new_<%= user_singular_name %>(username: '').errors[:username]
|
24
24
|
end
|
25
25
|
|
26
26
|
should "require password" do
|
27
|
-
assert_equal ["can't be blank"], new_<%= user_singular_name %>(:
|
27
|
+
assert_equal ["can't be blank"], new_<%= user_singular_name %>(password: '').errors[:password]
|
28
28
|
end
|
29
29
|
|
30
30
|
should "require well formed email" do
|
31
|
-
assert_equal ["is invalid"], new_<%= user_singular_name %>(:
|
31
|
+
assert_equal ["is invalid"], new_<%= user_singular_name %>(email: 'foo@bar@example.com').errors[:email]
|
32
32
|
end
|
33
33
|
|
34
34
|
should "validate uniqueness of email" do
|
35
|
-
new_<%= user_singular_name %>(:
|
36
|
-
assert_equal ["has already been taken"], new_<%= user_singular_name %>(:
|
35
|
+
new_<%= user_singular_name %>(email: 'bar@example.com').save!
|
36
|
+
assert_equal ["has already been taken"], new_<%= user_singular_name %>(email: 'bar@example.com').errors[:email]
|
37
37
|
end
|
38
38
|
|
39
39
|
should "validate uniqueness of username" do
|
40
|
-
new_<%= user_singular_name %>(:
|
41
|
-
assert_equal ["has already been taken"], new_<%= user_singular_name %>(:
|
40
|
+
new_<%= user_singular_name %>(username: 'uniquename').save!
|
41
|
+
assert_equal ["has already been taken"], new_<%= user_singular_name %>(username: 'uniquename').errors[:username]
|
42
42
|
end
|
43
43
|
|
44
44
|
should "not allow odd characters in username" do
|
45
|
-
assert_equal ["should only contain letters, numbers, or .-_@"], new_<%= user_singular_name %>(:
|
45
|
+
assert_equal ["should only contain letters, numbers, or .-_@"], new_<%= user_singular_name %>(username: 'odd ^&(@)').errors[:username]
|
46
46
|
end
|
47
47
|
|
48
48
|
should "validate password is longer than 3 characters" do
|
49
|
-
assert_equal ["is too short (minimum is 4 characters)"], new_<%= user_singular_name %>(:
|
49
|
+
assert_equal ["is too short (minimum is 4 characters)"], new_<%= user_singular_name %>(password: 'bad').errors[:password]
|
50
50
|
end
|
51
51
|
|
52
52
|
should "require matching password confirmation" do
|
@@ -61,13 +61,13 @@ class <%= user_class_name %>Test < ActiveSupport::TestCase
|
|
61
61
|
end
|
62
62
|
|
63
63
|
should "authenticate by username" do
|
64
|
-
<%= user_singular_name %> = new_<%= user_singular_name %>(:
|
64
|
+
<%= user_singular_name %> = new_<%= user_singular_name %>(username: 'foobar', password: 'secret')
|
65
65
|
<%= user_singular_name %>.save!
|
66
66
|
assert_equal <%= user_singular_name %>, <%= user_class_name %>.authenticate('foobar', 'secret')
|
67
67
|
end
|
68
68
|
|
69
69
|
should "authenticate by email" do
|
70
|
-
<%= user_singular_name %> = new_<%= user_singular_name %>(:
|
70
|
+
<%= user_singular_name %> = new_<%= user_singular_name %>(email: 'foo@bar.com', password: 'secret')
|
71
71
|
<%= user_singular_name %>.save!
|
72
72
|
assert_equal <%= user_singular_name %>, <%= user_class_name %>.authenticate('foo@bar.com', 'secret')
|
73
73
|
end
|
@@ -77,7 +77,7 @@ class <%= user_class_name %>Test < ActiveSupport::TestCase
|
|
77
77
|
end
|
78
78
|
|
79
79
|
should "not authenticate bad password" do
|
80
|
-
new_<%= user_singular_name %>(:
|
80
|
+
new_<%= user_singular_name %>(username: 'foobar', password: 'secret').save!
|
81
81
|
assert_nil <%= user_class_name %>.authenticate('foobar', 'badpassword')
|
82
82
|
end
|
83
83
|
end
|
@@ -25,34 +25,34 @@ class <%= user_plural_class_name %>ControllerTest < ActionController::TestCase
|
|
25
25
|
|
26
26
|
context "edit action" do
|
27
27
|
should "redirect when not logged in" do
|
28
|
-
get :edit, :
|
28
|
+
get :edit, id: "ignored"
|
29
29
|
assert_redirected_to login_url
|
30
30
|
end
|
31
31
|
|
32
32
|
should "render edit template" do
|
33
33
|
@controller.stubs(:current_<%= user_singular_name %>).returns(<%= user_class_name %>.first)
|
34
|
-
get :edit, :
|
34
|
+
get :edit, id: "ignored"
|
35
35
|
assert_template 'edit'
|
36
36
|
end
|
37
37
|
end
|
38
38
|
|
39
39
|
context "update action" do
|
40
40
|
should "redirect when not logged in" do
|
41
|
-
put :update, :
|
41
|
+
put :update, id: "ignored"
|
42
42
|
assert_redirected_to login_url
|
43
43
|
end
|
44
44
|
|
45
45
|
should "render edit template when <%= user_singular_name %> is invalid" do
|
46
46
|
@controller.stubs(:current_<%= user_singular_name %>).returns(<%= user_class_name %>.first)
|
47
47
|
<%= user_class_name %>.any_instance.stubs(:valid?).returns(false)
|
48
|
-
put :update, :
|
48
|
+
put :update, id: "ignored"
|
49
49
|
assert_template 'edit'
|
50
50
|
end
|
51
51
|
|
52
52
|
should "redirect when <%= user_singular_name %> is valid" do
|
53
53
|
@controller.stubs(:current_<%= user_singular_name %>).returns(<%= user_class_name %>.first)
|
54
54
|
<%= user_class_name %>.any_instance.stubs(:valid?).returns(true)
|
55
|
-
put :update, :
|
55
|
+
put :update, id: "ignored"
|
56
56
|
assert_redirected_to root_url
|
57
57
|
end
|
58
58
|
end
|
@@ -21,33 +21,33 @@ class <%= user_class_name %>Test < ActiveSupport::TestCase
|
|
21
21
|
end
|
22
22
|
|
23
23
|
def test_require_username
|
24
|
-
assert_equal ["can't be blank"], new_<%= user_singular_name %>(:
|
24
|
+
assert_equal ["can't be blank"], new_<%= user_singular_name %>(username: '').errors[:username]
|
25
25
|
end
|
26
26
|
|
27
27
|
def test_require_password
|
28
|
-
assert_equal ["can't be blank"], new_<%= user_singular_name %>(:
|
28
|
+
assert_equal ["can't be blank"], new_<%= user_singular_name %>(password: '').errors[:password]
|
29
29
|
end
|
30
30
|
|
31
31
|
def test_require_well_formed_email
|
32
|
-
assert_equal ["is invalid"], new_<%= user_singular_name %>(:
|
32
|
+
assert_equal ["is invalid"], new_<%= user_singular_name %>(email: 'foo@bar@example.com').errors[:email]
|
33
33
|
end
|
34
34
|
|
35
35
|
def test_validate_uniqueness_of_email
|
36
|
-
new_<%= user_singular_name %>(:
|
37
|
-
assert_equal ["has already been taken"], new_<%= user_singular_name %>(:
|
36
|
+
new_<%= user_singular_name %>(email: 'bar@example.com').save!
|
37
|
+
assert_equal ["has already been taken"], new_<%= user_singular_name %>(email: 'bar@example.com').errors[:email]
|
38
38
|
end
|
39
39
|
|
40
40
|
def test_validate_uniqueness_of_username
|
41
|
-
new_<%= user_singular_name %>(:
|
42
|
-
assert_equal ["has already been taken"], new_<%= user_singular_name %>(:
|
41
|
+
new_<%= user_singular_name %>(username: 'uniquename').save!
|
42
|
+
assert_equal ["has already been taken"], new_<%= user_singular_name %>(username: 'uniquename').errors[:username]
|
43
43
|
end
|
44
44
|
|
45
45
|
def test_validate_odd_characters_in_username
|
46
|
-
assert_equal ["should only contain letters, numbers, or .-_@"], new_<%= user_singular_name %>(:
|
46
|
+
assert_equal ["should only contain letters, numbers, or .-_@"], new_<%= user_singular_name %>(username: 'odd ^&(@)').errors[:username]
|
47
47
|
end
|
48
48
|
|
49
49
|
def test_validate_password_length
|
50
|
-
assert_equal ["is too short (minimum is 4 characters)"], new_<%= user_singular_name %>(:
|
50
|
+
assert_equal ["is too short (minimum is 4 characters)"], new_<%= user_singular_name %>(password: 'bad').errors[:password]
|
51
51
|
end
|
52
52
|
|
53
53
|
def test_require_matching_password_confirmation
|
@@ -63,14 +63,14 @@ class <%= user_class_name %>Test < ActiveSupport::TestCase
|
|
63
63
|
|
64
64
|
def test_authenticate_by_username
|
65
65
|
<%= user_class_name %>.delete_all
|
66
|
-
<%= user_singular_name %> = new_<%= user_singular_name %>(:
|
66
|
+
<%= user_singular_name %> = new_<%= user_singular_name %>(username: 'foobar', password: 'secret')
|
67
67
|
<%= user_singular_name %>.save!
|
68
68
|
assert_equal <%= user_singular_name %>, <%= user_class_name %>.authenticate('foobar', 'secret')
|
69
69
|
end
|
70
70
|
|
71
71
|
def test_authenticate_by_email
|
72
72
|
<%= user_class_name %>.delete_all
|
73
|
-
<%= user_singular_name %> = new_<%= user_singular_name %>(:
|
73
|
+
<%= user_singular_name %> = new_<%= user_singular_name %>(email: 'foo@bar.com', password: 'secret')
|
74
74
|
<%= user_singular_name %>.save!
|
75
75
|
assert_equal <%= user_singular_name %>, <%= user_class_name %>.authenticate('foo@bar.com', 'secret')
|
76
76
|
end
|
@@ -81,7 +81,7 @@ class <%= user_class_name %>Test < ActiveSupport::TestCase
|
|
81
81
|
|
82
82
|
def test_authenticate_bad_password
|
83
83
|
<%= user_class_name %>.delete_all
|
84
|
-
new_<%= user_singular_name %>(:
|
84
|
+
new_<%= user_singular_name %>(username: 'foobar', password: 'secret').save!
|
85
85
|
assert_nil <%= user_class_name %>.authenticate('foobar', 'badpassword')
|
86
86
|
end
|
87
87
|
end
|
@@ -20,32 +20,32 @@ class <%= user_plural_class_name %>ControllerTest < ActionController::TestCase
|
|
20
20
|
end
|
21
21
|
|
22
22
|
def test_edit_without_user
|
23
|
-
get :edit, :
|
23
|
+
get :edit, id: "ignored"
|
24
24
|
assert_redirected_to login_url
|
25
25
|
end
|
26
26
|
|
27
27
|
def test_edit
|
28
28
|
@controller.stubs(:current_<%= user_singular_name %>).returns(<%= user_class_name %>.first)
|
29
|
-
get :edit, :
|
29
|
+
get :edit, id: "ignored"
|
30
30
|
assert_template 'edit'
|
31
31
|
end
|
32
32
|
|
33
33
|
def test_update_without_user
|
34
|
-
put :update, :
|
34
|
+
put :update, id: "ignored"
|
35
35
|
assert_redirected_to login_url
|
36
36
|
end
|
37
37
|
|
38
38
|
def test_update_invalid
|
39
39
|
@controller.stubs(:current_<%= user_singular_name %>).returns(<%= user_class_name %>.first)
|
40
40
|
<%= user_class_name %>.any_instance.stubs(:valid?).returns(false)
|
41
|
-
put :update, :
|
41
|
+
put :update, id: "ignored"
|
42
42
|
assert_template 'edit'
|
43
43
|
end
|
44
44
|
|
45
45
|
def test_update_valid
|
46
46
|
@controller.stubs(:current_<%= user_singular_name %>).returns(<%= user_class_name %>.first)
|
47
47
|
<%= user_class_name %>.any_instance.stubs(:valid?).returns(true)
|
48
|
-
put :update, :
|
48
|
+
put :update, id: "ignored"
|
49
49
|
assert_redirected_to root_url
|
50
50
|
end
|
51
51
|
end
|
@@ -8,11 +8,11 @@ class <%= user_class_name %> < ActiveRecord::Base
|
|
8
8
|
|
9
9
|
validates_presence_of :username
|
10
10
|
validates_uniqueness_of :username, :email, :allow_blank => true
|
11
|
-
validates_format_of :username, :
|
12
|
-
validates_format_of :email, :
|
13
|
-
validates_presence_of
|
11
|
+
validates_format_of :username, with: /^[-\w\._@]+$/i, :allow_blank => true, message: "should only contain letters, numbers, or .-_@"
|
12
|
+
validates_format_of :email, with: /^[-a-z0-9_+\.]+\@([-a-z0-9]+\.)+[a-z0-9]{2,4}$/i
|
13
|
+
validates_presence_of password, on: :create
|
14
14
|
validates_confirmation_of :password
|
15
|
-
validates_length_of :password, :
|
15
|
+
validates_length_of :password, minimum: 4, :allow_blank => true
|
16
16
|
|
17
17
|
# login can be either username or email address
|
18
18
|
def self.authenticate(login, pass)
|
@@ -1,5 +1,5 @@
|
|
1
1
|
class <%= user_plural_class_name %>Controller < ApplicationController
|
2
|
-
before_filter :login_required, :
|
2
|
+
before_filter :login_required, except: [:new, :create]
|
3
3
|
|
4
4
|
def new
|
5
5
|
@<%= user_singular_name %> = <%= user_class_name %>.new
|
@@ -9,7 +9,7 @@ class <%= user_plural_class_name %>Controller < ApplicationController
|
|
9
9
|
@<%= user_singular_name %> = <%= user_class_name %>.new(params[:<%= user_singular_name %>])
|
10
10
|
if @<%= user_singular_name %>.save
|
11
11
|
session[:<%= user_singular_name %>_id] = @<%= user_singular_name %>.id
|
12
|
-
redirect_to root_url, :
|
12
|
+
redirect_to root_url, notice: "Thank you for signing up! You are now logged in."
|
13
13
|
else
|
14
14
|
render :new
|
15
15
|
end
|
@@ -22,7 +22,7 @@ class <%= user_plural_class_name %>Controller < ApplicationController
|
|
22
22
|
def update
|
23
23
|
@<%= user_singular_name %> = current_<%= user_singular_name %>
|
24
24
|
if @<%= user_singular_name %>.update_attributes(params[:<%= user_singular_name %>])
|
25
|
-
redirect_to root_url, :
|
25
|
+
redirect_to root_url, notice: "Your profile has been updated."
|
26
26
|
else
|
27
27
|
render :edit
|
28
28
|
end
|
@@ -3,7 +3,7 @@ require 'generators/zoo'
|
|
3
3
|
module Zoo
|
4
4
|
module Generators
|
5
5
|
class ConfigGenerator < Base
|
6
|
-
argument
|
6
|
+
argument config_name, type: :string, default: 'app', banner: 'config_name'
|
7
7
|
|
8
8
|
def create_config
|
9
9
|
template "load_config.rb", "config/initializers/load_#{file_name}_config.rb"
|
@@ -3,7 +3,7 @@ require 'generators/zoo'
|
|
3
3
|
module Zoo
|
4
4
|
module Generators
|
5
5
|
class LayoutGenerator < Base
|
6
|
-
argument :layout_name, :
|
6
|
+
argument :layout_name, type: :string, default: 'application', banner: 'layout_name'
|
7
7
|
|
8
8
|
# def add_gems
|
9
9
|
# add_gem "haml-rails"
|
@@ -2,11 +2,11 @@ module ErrorMessagesHelper
|
|
2
2
|
# Render error messages for the given objects. The :message and :header_message options are allowed.
|
3
3
|
def error_messages_for(*objects)
|
4
4
|
options = objects.extract_options!
|
5
|
-
options[:header_message] ||= I18n.t(:"activerecord.errors.header", :
|
6
|
-
options[:message] ||= I18n.t(:"activerecord.errors.message", :
|
5
|
+
options[:header_message] ||= I18n.t(:"activerecord.errors.header", default: "Invalid Fields")
|
6
|
+
options[:message] ||= I18n.t(:"activerecord.errors.message", default: "Correct the following errors and try again.")
|
7
7
|
messages = objects.compact.map { |o| o.errors.full_messages }.flatten
|
8
8
|
unless messages.empty?
|
9
|
-
content_tag(:div, :
|
9
|
+
content_tag(:div, class: "error_messages") do
|
10
10
|
list_items = messages.map { |msg| content_tag(:li, msg.html_safe) }
|
11
11
|
content_tag(:h2, options[:header_message].html_safe) + content_tag(:p, options[:message].html_safe) + content_tag(:ul, list_items.join.html_safe)
|
12
12
|
end
|
@@ -4,7 +4,7 @@
|
|
4
4
|
%head
|
5
5
|
%title
|
6
6
|
= yield(:title) || "Untitled"
|
7
|
-
%meta{"http-equiv"=>"Content-Type", :
|
7
|
+
%meta{"http-equiv"=>"Content-Type", content: "text/html; charset=utf-8"}/
|
8
8
|
= stylesheet_link_tag "<%= file_name %>"
|
9
9
|
= javascript_include_tag :defaults
|
10
10
|
= csrf_meta_tag
|
@@ -13,7 +13,7 @@
|
|
13
13
|
%body
|
14
14
|
#container
|
15
15
|
- flash.each do |name, msg|
|
16
|
-
= content_tag :div, msg, :
|
16
|
+
= content_tag :div, msg, id: "flash_#{name}"
|
17
17
|
|
18
18
|
- if show_title?
|
19
19
|
%h1= yield(:title)
|
@@ -8,18 +8,18 @@ module Zoo
|
|
8
8
|
include Rails::Generators::Migration
|
9
9
|
no_tasks { attr_accessor :scaffold_name, :model_attributes, :controller_actions }
|
10
10
|
|
11
|
-
argument :scaffold_name, :
|
12
|
-
argument :args_for_c_m, :
|
13
|
-
|
14
|
-
class_option :skip_model, :
|
15
|
-
class_option :skip_migration, :
|
16
|
-
class_option :skip_timestamps, :
|
17
|
-
class_option :skip_controller, :
|
18
|
-
class_option :invert, :
|
19
|
-
class_option :namespace_model, :
|
20
|
-
class_option :testunit, :
|
21
|
-
class_option :rspec, :
|
22
|
-
class_option :shoulda, :
|
11
|
+
argument :scaffold_name, type: :string, required: true, banner: 'ModelName'
|
12
|
+
argument :args_for_c_m, type: :array, default: [], banner: 'controller_actions and model:attributes'
|
13
|
+
|
14
|
+
class_option :skip_model, desc: 'Don\'t generate a model or migration file.', type: :boolean
|
15
|
+
class_option :skip_migration, desc: 'Dont generate migration file for model.', type: :boolean
|
16
|
+
class_option :skip_timestamps, desc: 'Don\'t add timestamps to migration file.', type: :boolean
|
17
|
+
class_option :skip_controller, desc: 'Don\'t generate controller, helper, or views.', type: :boolean
|
18
|
+
class_option :invert, desc: 'Generate all controller actions except these mentioned.', type: :boolean
|
19
|
+
class_option :namespace_model, desc: 'If the resource is namespaced, include the model in the namespace.', type: :boolean
|
20
|
+
class_option :testunit, desc: 'Use test/unit for test files.', group: 'Test framework', type: :boolean
|
21
|
+
class_option :rspec, desc: 'Use RSpec for test files.', group: 'Test framework', type: :boolean
|
22
|
+
class_option :shoulda, desc: 'Use shoulda for test files.', group: 'Test framework', type: :boolean
|
23
23
|
|
24
24
|
def initialize(*args, &block)
|
25
25
|
super
|
@@ -66,7 +66,7 @@ module Zoo
|
|
66
66
|
def add_gems
|
67
67
|
add_gem "haml-rails"
|
68
68
|
add_gem "formtastic"
|
69
|
-
add_gem "mocha", :
|
69
|
+
add_gem "mocha", group: :test
|
70
70
|
end
|
71
71
|
|
72
72
|
def create_model
|
@@ -1,7 +1,7 @@
|
|
1
1
|
def create
|
2
2
|
@<%= instance_name %> = <%= class_name %>.new(params[:<%= instance_name %>])
|
3
3
|
if @<%= instance_name %>.save
|
4
|
-
redirect_to <%= item_url %>, :
|
4
|
+
redirect_to <%= item_url %>, notice: "Successfully created <%= class_name.underscore.humanize.downcase %>."
|
5
5
|
else
|
6
6
|
render :new
|
7
7
|
end
|
@@ -1,5 +1,5 @@
|
|
1
1
|
def destroy
|
2
2
|
@<%= instance_name %> = <%= class_name %>.find(params[:id])
|
3
3
|
@<%= instance_name %>.destroy
|
4
|
-
redirect_to <%= items_url %>, :
|
4
|
+
redirect_to <%= items_url %>, notice: "Successfully destroyed <%= class_name.underscore.humanize.downcase %>."
|
5
5
|
end
|
@@ -1,6 +1,6 @@
|
|
1
1
|
it "destroy action should destroy model and redirect to index action" do
|
2
2
|
<%= instance_name %> = <%= class_name %>.first
|
3
|
-
delete :destroy, :
|
3
|
+
delete :destroy, id: <%= instance_name %>
|
4
4
|
response.should redirect_to(<%= items_url %>)
|
5
5
|
<%= class_name %>.exists?(<%= instance_name %>.id).should be_false
|
6
6
|
end
|
@@ -1,11 +1,11 @@
|
|
1
1
|
it "update action should render edit template when model is invalid" do
|
2
2
|
<%= class_name %>.any_instance.stubs(:valid?).returns(false)
|
3
|
-
put :update, :
|
3
|
+
put :update, id: <%= class_name %>.first
|
4
4
|
response.should render_template(:edit)
|
5
5
|
end
|
6
6
|
|
7
7
|
it "update action should redirect when model is valid" do
|
8
8
|
<%= class_name %>.any_instance.stubs(:valid?).returns(true)
|
9
|
-
put :update, :
|
9
|
+
put :update, id: <%= class_name %>.first
|
10
10
|
response.should redirect_to(<%= item_path_for_spec('url') %>)
|
11
11
|
end
|
@@ -1,7 +1,7 @@
|
|
1
1
|
context "destroy action" do
|
2
2
|
should "destroy model and redirect to index action" do
|
3
3
|
<%= instance_name %> = <%= class_name %>.first
|
4
|
-
delete :destroy, :
|
4
|
+
delete :destroy, id: <%= instance_name %>
|
5
5
|
assert_redirected_to <%= items_url %>
|
6
6
|
assert !<%= class_name %>.exists?(<%= instance_name %>.id)
|
7
7
|
end
|
@@ -1,13 +1,13 @@
|
|
1
1
|
context "update action" do
|
2
2
|
should "render edit template when model is invalid" do
|
3
3
|
<%= class_name %>.any_instance.stubs(:valid?).returns(false)
|
4
|
-
put :update, :
|
4
|
+
put :update, id: <%= class_name %>.first
|
5
5
|
assert_template 'edit'
|
6
6
|
end
|
7
7
|
|
8
8
|
should "redirect when model is valid" do
|
9
9
|
<%= class_name %>.any_instance.stubs(:valid?).returns(true)
|
10
|
-
put :update, :
|
10
|
+
put :update, id: <%= class_name %>.first
|
11
11
|
assert_redirected_to <%= item_path_for_test('url') %>
|
12
12
|
end
|
13
13
|
end
|
@@ -1,11 +1,11 @@
|
|
1
1
|
def test_update_invalid
|
2
2
|
<%= class_name %>.any_instance.stubs(:valid?).returns(false)
|
3
|
-
put :update, :
|
3
|
+
put :update, id: <%= class_name %>.first
|
4
4
|
assert_template 'edit'
|
5
5
|
end
|
6
6
|
|
7
7
|
def test_update_valid
|
8
8
|
<%= class_name %>.any_instance.stubs(:valid?).returns(true)
|
9
|
-
put :update, :
|
9
|
+
put :update, id: <%= class_name %>.first
|
10
10
|
assert_redirected_to <%= item_path_for_test('url') %>
|
11
11
|
end
|
@@ -14,12 +14,12 @@
|
|
14
14
|
%td= link_to 'Show', <%= item_path %>
|
15
15
|
<%- end -%>
|
16
16
|
<%- if action? :edit -%>
|
17
|
-
%td= link_to 'Edit', <%= item_path :
|
17
|
+
%td= link_to 'Edit', <%= item_path action: :edit %>
|
18
18
|
<%- end -%>
|
19
19
|
<%- if action? :destroy -%>
|
20
|
-
%td= link_to 'Destroy', <%= item_path %>, :
|
20
|
+
%td= link_to 'Destroy', <%= item_path %>, confirm: 'Are you sure?', method: :delete
|
21
21
|
<%- end -%>
|
22
22
|
|
23
|
-
<%- if
|
24
|
-
%p= link_to "New <%= singular_name.titleize %>", <%= item_path :
|
23
|
+
<%- if action? :new -%>
|
24
|
+
%p= link_to "New <%= singular_name.titleize %>", <%= item_path action: :new %>
|
25
25
|
<%- end -%>
|
@@ -2,17 +2,17 @@
|
|
2
2
|
|
3
3
|
<%- for attribute in model_attributes -%>
|
4
4
|
%p
|
5
|
-
%strong <%= attribute.human_name.titleize
|
5
|
+
%strong <%= attribute.human_name.titleize %>
|
6
6
|
= @<%= instance_name %>.<%= attribute.name %>
|
7
7
|
<%- end -%>
|
8
8
|
|
9
9
|
%p
|
10
10
|
<%- if action? :edit -%>
|
11
|
-
= link_to "Edit", <%= item_path :
|
11
|
+
= link_to "Edit", <%= item_path action: :edit, instance_variable: true %>
|
12
12
|
|
|
13
13
|
<%- end -%>
|
14
14
|
<%- if action? :destroy -%>
|
15
|
-
= link_to "Destroy", <%= item_path :
|
15
|
+
= link_to "Destroy", <%= item_path instance_variable: true %>, confirm: 'Are you sure?', method: :delete
|
16
16
|
|
|
17
17
|
<%- end -%>
|
18
18
|
<%- if action? :index -%>
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: zoo-generators
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.1.
|
5
|
+
version: 0.1.1
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Mikael Henriksson
|
@@ -144,7 +144,6 @@ files:
|
|
144
144
|
- lib/generators/zoo/config/USAGE
|
145
145
|
- lib/generators/zoo/layout/layout_generator.rb
|
146
146
|
- lib/generators/zoo/layout/templates/error_messages_helper.rb
|
147
|
-
- lib/generators/zoo/layout/templates/layout.html.erb
|
148
147
|
- lib/generators/zoo/layout/templates/layout.html.haml
|
149
148
|
- lib/generators/zoo/layout/templates/layout_helper.rb
|
150
149
|
- lib/generators/zoo/layout/templates/stylesheet.css
|
@@ -1,19 +0,0 @@
|
|
1
|
-
<!DOCTYPE html>
|
2
|
-
<html>
|
3
|
-
<head>
|
4
|
-
<title><%%= content_for?(:title) ? yield(:title) : "Untitled" %></title>
|
5
|
-
<%%= stylesheet_link_tag "<%= file_name %>" %>
|
6
|
-
<%%= javascript_include_tag :defaults %>
|
7
|
-
<%%= csrf_meta_tag %>
|
8
|
-
<%%= yield(:head) %>
|
9
|
-
</head>
|
10
|
-
<body>
|
11
|
-
<div id="container">
|
12
|
-
<%% flash.each do |name, msg| %>
|
13
|
-
<%%= content_tag :div, msg, :id => "flash_#{name}" %>
|
14
|
-
<%% end %>
|
15
|
-
<%%= content_tag :h1, yield(:title) if show_title? %>
|
16
|
-
<%%= yield %>
|
17
|
-
</div>
|
18
|
-
</body>
|
19
|
-
</html>
|