zoo-generators 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (98) hide show
  1. data/CHANGELOG +198 -0
  2. data/Gemfile +2 -0
  3. data/LICENSE +20 -0
  4. data/README.rdoc +96 -0
  5. data/Rakefile +10 -0
  6. data/features/step_definitions/common_steps.rb +62 -0
  7. data/features/step_definitions/rails_setup_steps.rb +6 -0
  8. data/features/support/env.rb +6 -0
  9. data/features/support/matchers.rb +7 -0
  10. data/features/zoo_authentication.feature +83 -0
  11. data/features/zoo_config.feature +17 -0
  12. data/features/zoo_layout.feature +13 -0
  13. data/features/zoo_scaffold.feature +84 -0
  14. data/lib/generators/zoo.rb +28 -0
  15. data/lib/generators/zoo/authentication/USAGE +46 -0
  16. data/lib/generators/zoo/authentication/authentication_generator.rb +151 -0
  17. data/lib/generators/zoo/authentication/templates/controller_authentication.rb +48 -0
  18. data/lib/generators/zoo/authentication/templates/factory.rb +6 -0
  19. data/lib/generators/zoo/authentication/templates/fixtures.yml +12 -0
  20. data/lib/generators/zoo/authentication/templates/migration.rb +15 -0
  21. data/lib/generators/zoo/authentication/templates/sessions_controller.rb +20 -0
  22. data/lib/generators/zoo/authentication/templates/sessions_helper.rb +2 -0
  23. data/lib/generators/zoo/authentication/templates/tests/rspec/sessions_controller.rb +26 -0
  24. data/lib/generators/zoo/authentication/templates/tests/rspec/user.rb +81 -0
  25. data/lib/generators/zoo/authentication/templates/tests/rspec/users_controller.rb +54 -0
  26. data/lib/generators/zoo/authentication/templates/tests/shoulda/sessions_controller.rb +26 -0
  27. data/lib/generators/zoo/authentication/templates/tests/shoulda/user.rb +83 -0
  28. data/lib/generators/zoo/authentication/templates/tests/shoulda/users_controller.rb +59 -0
  29. data/lib/generators/zoo/authentication/templates/tests/testunit/sessions_controller.rb +22 -0
  30. data/lib/generators/zoo/authentication/templates/tests/testunit/user.rb +87 -0
  31. data/lib/generators/zoo/authentication/templates/tests/testunit/users_controller.rb +51 -0
  32. data/lib/generators/zoo/authentication/templates/user.rb +35 -0
  33. data/lib/generators/zoo/authentication/templates/users_controller.rb +30 -0
  34. data/lib/generators/zoo/authentication/templates/users_helper.rb +2 -0
  35. data/lib/generators/zoo/authentication/templates/views/haml/_form.html.haml +16 -0
  36. data/lib/generators/zoo/authentication/templates/views/haml/edit.html.haml +3 -0
  37. data/lib/generators/zoo/authentication/templates/views/haml/login.html.haml +13 -0
  38. data/lib/generators/zoo/authentication/templates/views/haml/signup.html.haml +5 -0
  39. data/lib/generators/zoo/config/USAGE +23 -0
  40. data/lib/generators/zoo/config/config_generator.rb +24 -0
  41. data/lib/generators/zoo/config/templates/config.yml +8 -0
  42. data/lib/generators/zoo/config/templates/load_config.rb +2 -0
  43. data/lib/generators/zoo/layout/USAGE +25 -0
  44. data/lib/generators/zoo/layout/layout_generator.rb +26 -0
  45. data/lib/generators/zoo/layout/templates/error_messages_helper.rb +23 -0
  46. data/lib/generators/zoo/layout/templates/layout.html.erb +19 -0
  47. data/lib/generators/zoo/layout/templates/layout.html.haml +21 -0
  48. data/lib/generators/zoo/layout/templates/layout_helper.rb +22 -0
  49. data/lib/generators/zoo/layout/templates/stylesheet.css +83 -0
  50. data/lib/generators/zoo/layout/templates/stylesheet.sass +73 -0
  51. data/lib/generators/zoo/scaffold/USAGE +51 -0
  52. data/lib/generators/zoo/scaffold/scaffold_generator.rb +316 -0
  53. data/lib/generators/zoo/scaffold/templates/actions/create.rb +8 -0
  54. data/lib/generators/zoo/scaffold/templates/actions/destroy.rb +5 -0
  55. data/lib/generators/zoo/scaffold/templates/actions/edit.rb +3 -0
  56. data/lib/generators/zoo/scaffold/templates/actions/index.rb +3 -0
  57. data/lib/generators/zoo/scaffold/templates/actions/new.rb +3 -0
  58. data/lib/generators/zoo/scaffold/templates/actions/show.rb +3 -0
  59. data/lib/generators/zoo/scaffold/templates/actions/update.rb +8 -0
  60. data/lib/generators/zoo/scaffold/templates/controller.rb +3 -0
  61. data/lib/generators/zoo/scaffold/templates/factory.rb +5 -0
  62. data/lib/generators/zoo/scaffold/templates/fixtures.yml +9 -0
  63. data/lib/generators/zoo/scaffold/templates/helper.rb +2 -0
  64. data/lib/generators/zoo/scaffold/templates/migration.rb +16 -0
  65. data/lib/generators/zoo/scaffold/templates/model.rb +4 -0
  66. data/lib/generators/zoo/scaffold/templates/tests/rspec/actions/create.rb +11 -0
  67. data/lib/generators/zoo/scaffold/templates/tests/rspec/actions/destroy.rb +6 -0
  68. data/lib/generators/zoo/scaffold/templates/tests/rspec/actions/edit.rb +4 -0
  69. data/lib/generators/zoo/scaffold/templates/tests/rspec/actions/index.rb +4 -0
  70. data/lib/generators/zoo/scaffold/templates/tests/rspec/actions/new.rb +4 -0
  71. data/lib/generators/zoo/scaffold/templates/tests/rspec/actions/show.rb +4 -0
  72. data/lib/generators/zoo/scaffold/templates/tests/rspec/actions/update.rb +11 -0
  73. data/lib/generators/zoo/scaffold/templates/tests/rspec/controller.rb +8 -0
  74. data/lib/generators/zoo/scaffold/templates/tests/rspec/model.rb +7 -0
  75. data/lib/generators/zoo/scaffold/templates/tests/shoulda/actions/create.rb +13 -0
  76. data/lib/generators/zoo/scaffold/templates/tests/shoulda/actions/destroy.rb +8 -0
  77. data/lib/generators/zoo/scaffold/templates/tests/shoulda/actions/edit.rb +6 -0
  78. data/lib/generators/zoo/scaffold/templates/tests/shoulda/actions/index.rb +6 -0
  79. data/lib/generators/zoo/scaffold/templates/tests/shoulda/actions/new.rb +6 -0
  80. data/lib/generators/zoo/scaffold/templates/tests/shoulda/actions/show.rb +6 -0
  81. data/lib/generators/zoo/scaffold/templates/tests/shoulda/actions/update.rb +13 -0
  82. data/lib/generators/zoo/scaffold/templates/tests/shoulda/controller.rb +5 -0
  83. data/lib/generators/zoo/scaffold/templates/tests/shoulda/model.rb +7 -0
  84. data/lib/generators/zoo/scaffold/templates/tests/testunit/actions/create.rb +11 -0
  85. data/lib/generators/zoo/scaffold/templates/tests/testunit/actions/destroy.rb +6 -0
  86. data/lib/generators/zoo/scaffold/templates/tests/testunit/actions/edit.rb +4 -0
  87. data/lib/generators/zoo/scaffold/templates/tests/testunit/actions/index.rb +4 -0
  88. data/lib/generators/zoo/scaffold/templates/tests/testunit/actions/new.rb +4 -0
  89. data/lib/generators/zoo/scaffold/templates/tests/testunit/actions/show.rb +4 -0
  90. data/lib/generators/zoo/scaffold/templates/tests/testunit/actions/update.rb +11 -0
  91. data/lib/generators/zoo/scaffold/templates/tests/testunit/controller.rb +5 -0
  92. data/lib/generators/zoo/scaffold/templates/tests/testunit/model.rb +7 -0
  93. data/lib/generators/zoo/scaffold/templates/views/haml/_form.html.haml +9 -0
  94. data/lib/generators/zoo/scaffold/templates/views/haml/edit.html.haml +14 -0
  95. data/lib/generators/zoo/scaffold/templates/views/haml/index.html.haml +25 -0
  96. data/lib/generators/zoo/scaffold/templates/views/haml/new.html.haml +7 -0
  97. data/lib/generators/zoo/scaffold/templates/views/haml/show.html.haml +20 -0
  98. metadata +243 -0
@@ -0,0 +1,51 @@
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
+ assert_equal assigns['<%= user_singular_name %>'].id, session['<%= user_singular_name %>_id']
20
+ end
21
+
22
+ def test_edit_without_user
23
+ get :edit, :id => "ignored"
24
+ assert_redirected_to login_url
25
+ end
26
+
27
+ def test_edit
28
+ @controller.stubs(:current_<%= user_singular_name %>).returns(<%= user_class_name %>.first)
29
+ get :edit, :id => "ignored"
30
+ assert_template 'edit'
31
+ end
32
+
33
+ def test_update_without_user
34
+ put :update, :id => "ignored"
35
+ assert_redirected_to login_url
36
+ end
37
+
38
+ def test_update_invalid
39
+ @controller.stubs(:current_<%= user_singular_name %>).returns(<%= user_class_name %>.first)
40
+ <%= user_class_name %>.any_instance.stubs(:valid?).returns(false)
41
+ put :update, :id => "ignored"
42
+ assert_template 'edit'
43
+ end
44
+
45
+ def test_update_valid
46
+ @controller.stubs(:current_<%= user_singular_name %>).returns(<%= user_class_name %>.first)
47
+ <%= user_class_name %>.any_instance.stubs(:valid?).returns(true)
48
+ put :update, :id => "ignored"
49
+ assert_redirected_to root_url
50
+ end
51
+ end
@@ -0,0 +1,35 @@
1
+ class <%= user_class_name %> < ActiveRecord::Base
2
+
3
+ # new columns need to be added here to be writable through mass assignment
4
+ attr_accessible :username, :email, :password, :password_confirmation
5
+
6
+ attr_accessor :password
7
+ before_save :prepare_password
8
+
9
+ validates_presence_of :username
10
+ validates_uniqueness_of :username, :email, :allow_blank => true
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
+ validates_confirmation_of :password
15
+ validates_length_of :password, :minimum => 4, :allow_blank => true
16
+
17
+ # login can be either username or email address
18
+ def self.authenticate(login, pass)
19
+ <%= user_singular_name %> = find_by_username(login) || find_by_email(login)
20
+ return <%= user_singular_name %> if <%= user_singular_name %> && <%= user_singular_name %>.password_hash == <%= user_singular_name %>.encrypt_password(pass)
21
+ end
22
+
23
+ def encrypt_password(pass)
24
+ BCrypt::Engine.hash_secret(pass, password_salt)
25
+ end
26
+
27
+ private
28
+
29
+ def prepare_password
30
+ unless password.blank?
31
+ self.password_salt = BCrypt::Engine.generate_salt
32
+ self.password_hash = encrypt_password(password)
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,30 @@
1
+ class <%= user_plural_class_name %>Controller < ApplicationController
2
+ before_filter :login_required, :except => [:new, :create]
3
+
4
+ def new
5
+ @<%= user_singular_name %> = <%= user_class_name %>.new
6
+ end
7
+
8
+ def create
9
+ @<%= user_singular_name %> = <%= user_class_name %>.new(params[:<%= user_singular_name %>])
10
+ if @<%= user_singular_name %>.save
11
+ session[:<%= user_singular_name %>_id] = @<%= user_singular_name %>.id
12
+ redirect_to root_url, :notice => "Thank you for signing up! You are now logged in."
13
+ else
14
+ render :new
15
+ end
16
+ end
17
+
18
+ def edit
19
+ @<%= user_singular_name %> = current_<%= user_singular_name %>
20
+ end
21
+
22
+ def update
23
+ @<%= user_singular_name %> = current_<%= user_singular_name %>
24
+ if @<%= user_singular_name %>.update_attributes(params[:<%= user_singular_name %>])
25
+ redirect_to root_url, :notice => "Your profile has been updated."
26
+ else
27
+ render :edit
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,2 @@
1
+ module <%= user_plural_class_name %>Helper
2
+ end
@@ -0,0 +1,16 @@
1
+ = form_for @<%= user_singular_name %> do |f|
2
+ = f.error_messages
3
+ .field
4
+ = f.label :username
5
+ = f.text_field :username
6
+ .field
7
+ = f.label :email, "Email Address"
8
+ = f.text_field :email
9
+ .field
10
+ = f.label :password
11
+ = f.password_field :password
12
+ .field
13
+ = f.label :password_confirmation, "Confirm Password"
14
+ = f.password_field :password_confirmation
15
+ .actions
16
+ = f.submit (@<%= user_singular_name %>.new_record? ? "Sign up" : "Update")
@@ -0,0 +1,3 @@
1
+ = title "Sign up"
2
+
3
+ = render "form"
@@ -0,0 +1,13 @@
1
+ - title "Log in"
2
+
3
+ %p== Don't have an account? #{link_to "Sign up!", signup_path}
4
+
5
+ = form_tag <%= session_plural_name %>_path do
6
+ .field
7
+ = label_tag :login, "Username or Email Address"
8
+ = text_field_tag :login, params[:login]
9
+ .field
10
+ = label_tag :password
11
+ = password_field_tag :password
12
+ .actions
13
+ = submit_tag "Log in"
@@ -0,0 +1,5 @@
1
+ - title "Sign up"
2
+
3
+ %p== Already have an account? #{link_to "Log in", login_path}.
4
+
5
+ = render "form"
@@ -0,0 +1,23 @@
1
+ Description:
2
+ The zoo_config generator creates YAML file in your config
3
+ directory and an initializer to load this config. The config has a
4
+ separate section for each environment. This is a great place to put
5
+ any config settings you don't want in your app.
6
+
7
+ The config is loaded into a constant called APP_CONFIG by default,
8
+ this changes depending on the name you choose to pass the generator.
9
+ Use this constant to access the config settings like this.
10
+
11
+ APP_CONFIG[:some_setting]
12
+
13
+
14
+ Examples:
15
+ rails generate zoo:config
16
+
17
+ Config: config/app_config.yml
18
+ Initializer: config/initializers/load_app_config.rb
19
+
20
+ rails generate zoo:config passwords
21
+
22
+ Config: config/passwords_config.yml
23
+ Initializer: config/initializers/load_passwords_config.rb
@@ -0,0 +1,24 @@
1
+ require 'generators/zoo'
2
+
3
+ module Zoo
4
+ module Generators
5
+ class ConfigGenerator < Base
6
+ argument :config_name, :type => :string, :default => 'app', :banner => 'config_name'
7
+
8
+ def create_config
9
+ template "load_config.rb", "config/initializers/load_#{file_name}_config.rb"
10
+ copy_file "config.yml", "config/#{file_name}_config.yml"
11
+ end
12
+
13
+ private
14
+
15
+ def file_name
16
+ config_name.underscore
17
+ end
18
+
19
+ def constant_name
20
+ config_name.underscore.upcase
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,8 @@
1
+ development:
2
+ domain: localhost:3000
3
+
4
+ test:
5
+ domain: test.host
6
+
7
+ production:
8
+ domain: example.com
@@ -0,0 +1,2 @@
1
+ raw_config = File.read("#{Rails.root}/config/<%= file_name %>_config.yml")
2
+ <%= constant_name %>_CONFIG = YAML.load(raw_config)[Rails.env].symbolize_keys
@@ -0,0 +1,25 @@
1
+ Description:
2
+ The zoo:layout generator creates a basic layout, stylesheet and
3
+ helper which will give some structure to a starting Rails app.
4
+
5
+ The generator takes one argument which will be the name of the
6
+ layout and stylesheet files. If no argument is passed then it defaults
7
+ to "application".
8
+
9
+ The helper module includes some methods which can be called in any
10
+ template or partial to set variables to be used in the layout, such as
11
+ page title and javascript/stylesheet includes.
12
+
13
+ Examples:
14
+ rails generate zoo:layout
15
+
16
+ Layout: app/views/layouts/application.html.haml
17
+ Stylesheet: public/stylesheets/application.css
18
+ Helper: app/helpers/layout_helper.rb
19
+
20
+
21
+ rails generate zoo:layout admin
22
+
23
+ Layout: app/views/layouts/admin.html.haml
24
+ Stylesheet: public/stylesheets/admin.css
25
+ Helper: app/helpers/layout_helper.rb
@@ -0,0 +1,26 @@
1
+ require 'generators/zoo'
2
+
3
+ module Zoo
4
+ module Generators
5
+ class LayoutGenerator < Base
6
+ argument :layout_name, :type => :string, :default => 'application', :banner => 'layout_name'
7
+
8
+ # def add_gems
9
+ # add_gem "haml-rails"
10
+ # end
11
+
12
+ def create_layout
13
+ template 'layout.html.haml', "app/views/layouts/#{file_name}.html.haml"
14
+ copy_file 'stylesheet.sass', "public/stylesheets/sass/#{file_name}.sass"
15
+ copy_file 'layout_helper.rb', 'app/helpers/layout_helper.rb'
16
+ copy_file 'error_messages_helper.rb', 'app/helpers/error_messages_helper.rb'
17
+ end
18
+
19
+ private
20
+
21
+ def file_name
22
+ layout_name.underscore
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,23 @@
1
+ module ErrorMessagesHelper
2
+ # Render error messages for the given objects. The :message and :header_message options are allowed.
3
+ def error_messages_for(*objects)
4
+ options = objects.extract_options!
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
+ messages = objects.compact.map { |o| o.errors.full_messages }.flatten
8
+ unless messages.empty?
9
+ content_tag(:div, :class => "error_messages") do
10
+ list_items = messages.map { |msg| content_tag(:li, msg.html_safe) }
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
+ end
13
+ end
14
+ end
15
+
16
+ module FormBuilderAdditions
17
+ def error_messages(options = {})
18
+ @template.error_messages_for(@object, options)
19
+ end
20
+ end
21
+ end
22
+
23
+ ActionView::Helpers::FormBuilder.send(:include, ErrorMessagesHelper::FormBuilderAdditions)
@@ -0,0 +1,19 @@
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>
@@ -0,0 +1,21 @@
1
+ !!!
2
+ %html
3
+
4
+ %head
5
+ %title
6
+ = yield(:title) || "Untitled"
7
+ %meta{"http-equiv"=>"Content-Type", :content=>"text/html; charset=utf-8"}/
8
+ = stylesheet_link_tag "<%= file_name %>"
9
+ = javascript_include_tag :defaults
10
+ = csrf_meta_tag
11
+ = yield(:head)
12
+
13
+ %body
14
+ #container
15
+ - flash.each do |name, msg|
16
+ = content_tag :div, msg, :id => "flash_#{name}"
17
+
18
+ - if show_title?
19
+ %h1= yield(:title)
20
+
21
+ = yield
@@ -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) { h(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,83 @@
1
+ html, body {
2
+ background-color: #4B7399;
3
+ font-family: Verdana, Helvetica, Arial;
4
+ font-size: 14px;
5
+ }
6
+
7
+ a img {
8
+ border: none;
9
+ }
10
+
11
+ a {
12
+ color: #0000FF;
13
+ }
14
+
15
+ .clear {
16
+ clear: both;
17
+ height: 0;
18
+ overflow: hidden;
19
+ }
20
+
21
+ #container {
22
+ width: 75%;
23
+ margin: 0 auto;
24
+ background-color: #FFF;
25
+ padding: 20px 40px;
26
+ border: solid 1px black;
27
+ margin-top: 20px;
28
+ }
29
+
30
+ #flash_notice, #flash_error, #flash_alert {
31
+ padding: 5px 8px;
32
+ margin: 10px 0;
33
+ }
34
+
35
+ #flash_notice {
36
+ background-color: #CFC;
37
+ border: solid 1px #6C6;
38
+ }
39
+
40
+ #flash_error, #flash_alert {
41
+ background-color: #FCC;
42
+ border: solid 1px #C66;
43
+ }
44
+
45
+ .error_messages {
46
+ width: 400px;
47
+ border: 2px solid #CF0000;
48
+ padding: 0px;
49
+ padding-bottom: 12px;
50
+ margin-bottom: 20px;
51
+ background-color: #f0f0f0;
52
+ font-size: 12px;
53
+ }
54
+
55
+ .error_messages h2 {
56
+ text-align: left;
57
+ font-weight: bold;
58
+ padding: 5px 10px;
59
+ font-size: 12px;
60
+ margin: 0;
61
+ background-color: #c00;
62
+ color: #fff;
63
+ }
64
+
65
+ .error_messages p {
66
+ margin: 8px 10px;
67
+ }
68
+
69
+ .error_messages ul {
70
+ margin: 0;
71
+ }
72
+
73
+ .field_with_errors {
74
+ display: inline;
75
+ }
76
+
77
+ form .field, form .actions {
78
+ margin: 10px 0;
79
+ }
80
+
81
+ form label {
82
+ display: block;
83
+ }