unittk-nifty-generators 0.1.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (75) hide show
  1. data/CHANGELOG +43 -0
  2. data/LICENSE +20 -0
  3. data/Manifest +73 -0
  4. data/README +71 -0
  5. data/TODO +7 -0
  6. data/lib/nifty_generators.rb +3 -0
  7. data/nifty-generators.gemspec +51 -0
  8. data/rails_generators/nifty_config/USAGE +23 -0
  9. data/rails_generators/nifty_config/nifty_config_generator.rb +32 -0
  10. data/rails_generators/nifty_config/templates/config.yml +8 -0
  11. data/rails_generators/nifty_config/templates/load_config.rb +2 -0
  12. data/rails_generators/nifty_layout/USAGE +25 -0
  13. data/rails_generators/nifty_layout/nifty_layout_generator.rb +44 -0
  14. data/rails_generators/nifty_layout/templates/helper.rb +23 -0
  15. data/rails_generators/nifty_layout/templates/layout.html.erb +22 -0
  16. data/rails_generators/nifty_layout/templates/layout.html.haml +19 -0
  17. data/rails_generators/nifty_layout/templates/stylesheet.css +80 -0
  18. data/rails_generators/nifty_layout/templates/stylesheet.sass +66 -0
  19. data/rails_generators/nifty_scaffold/USAGE +51 -0
  20. data/rails_generators/nifty_scaffold/nifty_scaffold_generator.rb +224 -0
  21. data/rails_generators/nifty_scaffold/templates/actions/create.rb +9 -0
  22. data/rails_generators/nifty_scaffold/templates/actions/destroy.rb +6 -0
  23. data/rails_generators/nifty_scaffold/templates/actions/edit.rb +3 -0
  24. data/rails_generators/nifty_scaffold/templates/actions/index.rb +3 -0
  25. data/rails_generators/nifty_scaffold/templates/actions/new.rb +3 -0
  26. data/rails_generators/nifty_scaffold/templates/actions/show.rb +3 -0
  27. data/rails_generators/nifty_scaffold/templates/actions/update.rb +9 -0
  28. data/rails_generators/nifty_scaffold/templates/controller.rb +3 -0
  29. data/rails_generators/nifty_scaffold/templates/fixtures.yml +9 -0
  30. data/rails_generators/nifty_scaffold/templates/helper.rb +2 -0
  31. data/rails_generators/nifty_scaffold/templates/migration.rb +16 -0
  32. data/rails_generators/nifty_scaffold/templates/model.rb +2 -0
  33. data/rails_generators/nifty_scaffold/templates/tests/rspec/actions/create.rb +11 -0
  34. data/rails_generators/nifty_scaffold/templates/tests/rspec/actions/destroy.rb +6 -0
  35. data/rails_generators/nifty_scaffold/templates/tests/rspec/actions/edit.rb +4 -0
  36. data/rails_generators/nifty_scaffold/templates/tests/rspec/actions/index.rb +4 -0
  37. data/rails_generators/nifty_scaffold/templates/tests/rspec/actions/new.rb +4 -0
  38. data/rails_generators/nifty_scaffold/templates/tests/rspec/actions/show.rb +4 -0
  39. data/rails_generators/nifty_scaffold/templates/tests/rspec/actions/update.rb +11 -0
  40. data/rails_generators/nifty_scaffold/templates/tests/rspec/controller.rb +8 -0
  41. data/rails_generators/nifty_scaffold/templates/tests/rspec/model.rb +7 -0
  42. data/rails_generators/nifty_scaffold/templates/tests/shoulda/actions/create.rb +13 -0
  43. data/rails_generators/nifty_scaffold/templates/tests/shoulda/actions/destroy.rb +8 -0
  44. data/rails_generators/nifty_scaffold/templates/tests/shoulda/actions/edit.rb +6 -0
  45. data/rails_generators/nifty_scaffold/templates/tests/shoulda/actions/index.rb +6 -0
  46. data/rails_generators/nifty_scaffold/templates/tests/shoulda/actions/new.rb +6 -0
  47. data/rails_generators/nifty_scaffold/templates/tests/shoulda/actions/show.rb +6 -0
  48. data/rails_generators/nifty_scaffold/templates/tests/shoulda/actions/update.rb +13 -0
  49. data/rails_generators/nifty_scaffold/templates/tests/shoulda/controller.rb +5 -0
  50. data/rails_generators/nifty_scaffold/templates/tests/shoulda/model.rb +7 -0
  51. data/rails_generators/nifty_scaffold/templates/tests/testunit/actions/create.rb +11 -0
  52. data/rails_generators/nifty_scaffold/templates/tests/testunit/actions/destroy.rb +6 -0
  53. data/rails_generators/nifty_scaffold/templates/tests/testunit/actions/edit.rb +4 -0
  54. data/rails_generators/nifty_scaffold/templates/tests/testunit/actions/index.rb +4 -0
  55. data/rails_generators/nifty_scaffold/templates/tests/testunit/actions/new.rb +4 -0
  56. data/rails_generators/nifty_scaffold/templates/tests/testunit/actions/show.rb +4 -0
  57. data/rails_generators/nifty_scaffold/templates/tests/testunit/actions/update.rb +11 -0
  58. data/rails_generators/nifty_scaffold/templates/tests/testunit/controller.rb +5 -0
  59. data/rails_generators/nifty_scaffold/templates/tests/testunit/model.rb +7 -0
  60. data/rails_generators/nifty_scaffold/templates/views/erb/_form.html.erb +10 -0
  61. data/rails_generators/nifty_scaffold/templates/views/erb/edit.html.erb +14 -0
  62. data/rails_generators/nifty_scaffold/templates/views/erb/index.html.erb +29 -0
  63. data/rails_generators/nifty_scaffold/templates/views/erb/new.html.erb +7 -0
  64. data/rails_generators/nifty_scaffold/templates/views/erb/show.html.erb +20 -0
  65. data/rails_generators/nifty_scaffold/templates/views/haml/_form.html.haml +11 -0
  66. data/rails_generators/nifty_scaffold/templates/views/haml/edit.html.haml +14 -0
  67. data/rails_generators/nifty_scaffold/templates/views/haml/index.html.haml +25 -0
  68. data/rails_generators/nifty_scaffold/templates/views/haml/new.html.haml +7 -0
  69. data/rails_generators/nifty_scaffold/templates/views/haml/show.html.haml +20 -0
  70. data/tasks/deployment.rake +2 -0
  71. data/test/test_helper.rb +117 -0
  72. data/test/test_nifty_config_generator.rb +37 -0
  73. data/test/test_nifty_layout_generator.rb +42 -0
  74. data/test/test_nifty_scaffold_generator.rb +532 -0
  75. metadata +140 -0
@@ -0,0 +1,29 @@
1
+ <%% title "<%= plural_name.titleize %>" %>
2
+
3
+ <table>
4
+ <tr>
5
+ <%- for attribute in attributes -%>
6
+ <th><%= attribute.column.human_name.titleize %></th>
7
+ <%- end -%>
8
+ </tr>
9
+ <%% for <%= singular_name %> in @<%= plural_name %> %>
10
+ <tr>
11
+ <%- for attribute in attributes -%>
12
+ <td><%%=h <%= singular_name %>.<%= attribute.name %> %></td>
13
+ <%- end -%>
14
+ <%- if action? :show -%>
15
+ <td><%%= link_to "Show", <%= singular_name %> %></td>
16
+ <%- end -%>
17
+ <%- if action? :edit -%>
18
+ <td><%%= link_to "Edit", edit_<%= singular_name %>_path(<%= singular_name %>) %></td>
19
+ <%- end -%>
20
+ <%- if action? :destroy -%>
21
+ <td><%%= link_to "Destroy", <%= singular_name %>, :confirm => 'Are you sure?', :method => :delete %></td>
22
+ <%- end -%>
23
+ </tr>
24
+ <%% end %>
25
+ </table>
26
+
27
+ <%- if action? :new -%>
28
+ <p><%%= link_to "New <%= singular_name.titleize %>", new_<%= singular_name %>_path %></p>
29
+ <%- end -%>
@@ -0,0 +1,7 @@
1
+ <%% title "New <%= singular_name.titleize %>" %>
2
+
3
+ <%= render_form %>
4
+
5
+ <%- if action? :index -%>
6
+ <p><%%= link_to "Back to List", <%= plural_name %>_path %></p>
7
+ <%- end -%>
@@ -0,0 +1,20 @@
1
+ <%% title "<%= singular_name.titleize %>" %>
2
+
3
+ <%- for attribute in attributes -%>
4
+ <p>
5
+ <strong><%= attribute.column.human_name.titleize %>:</strong>
6
+ <%%=h @<%= singular_name %>.<%= attribute.name %> %>
7
+ </p>
8
+ <%- end -%>
9
+
10
+ <p>
11
+ <%- if action? :edit -%>
12
+ <%%= link_to "Edit", edit_<%= singular_name %>_path(@<%= singular_name %>) %> |
13
+ <%- end -%>
14
+ <%- if action? :destroy -%>
15
+ <%%= link_to "Destroy", @<%= singular_name %>, :confirm => 'Are you sure?', :method => :delete %> |
16
+ <%- end -%>
17
+ <%- if action? :index -%>
18
+ <%%= link_to "View All", <%= plural_name %>_path %>
19
+ <%- end -%>
20
+ </p>
@@ -0,0 +1,11 @@
1
+ = error_messages_for :<%= singular_name %>
2
+
3
+ - form_for @<%= singular_name %> do |f|
4
+ <%- for attribute in attributes -%>
5
+ %p
6
+ =f.label :<%=attribute.name %>
7
+ %br
8
+ = f.<%= attribute.field_type %> :<%= attribute.name %>
9
+ <%- end -%>
10
+ %p
11
+ = f.submit "Submit"
@@ -0,0 +1,14 @@
1
+ - title "Edit <%= singular_name.titleize %>"
2
+
3
+ <%= render_form %>
4
+
5
+ <%- if actions? :show, :index -%>
6
+ %p
7
+ <%- if action? :show -%>
8
+ = link_to "Edit", edit_<%= singular_name %>_path(@<%= singular_name %>)
9
+ |
10
+ <%- end -%>
11
+ <%- if action? :index -%>
12
+ = link_to "View All", <%= plural_name %>_path
13
+ <%- end -%>
14
+ <%- end -%>
@@ -0,0 +1,25 @@
1
+ - title "<%= plural_name.titleize %>"
2
+
3
+ %table
4
+ %tr
5
+ <%- for attribute in attributes -%>
6
+ %th <%= attribute.column.human_name %>
7
+ <%- end -%>
8
+ - for <%= singular_name %> in @<%= plural_name %>
9
+ %tr
10
+ <%- for attribute in attributes -%>
11
+ %td=& <%= singular_name %>.<%= attribute.name %>
12
+ <%- end -%>
13
+ <%- if action? :show -%>
14
+ %td= link_to 'Show', <%= singular_name %>
15
+ <%- end -%>
16
+ <%- if action? :edit -%>
17
+ %td= link_to 'Edit', edit_<%= singular_name %>_path(<%= singular_name %>)
18
+ <%- end -%>
19
+ <%- if action? :destroy -%>
20
+ %td= link_to 'Destroy', <%= singular_name %>, :confirm => 'Are you sure?', :method => :delete
21
+ <%- end -%>
22
+
23
+ <%- if actions? :new -%>
24
+ %p= link_to "New <%= singular_name.titleize %>", new_<%= singular_name %>_path
25
+ <%- end -%>
@@ -0,0 +1,7 @@
1
+ - title "New <%= singular_name.titleize %>"
2
+
3
+ <%= render_form %>
4
+
5
+ <%- if action? :index -%>
6
+ %p= link_to "Back to List", <%= plural_name %>_path
7
+ <%- end -%>
@@ -0,0 +1,20 @@
1
+ - title "<%= singular_name.titleize %>"
2
+
3
+ <%- for attribute in attributes -%>
4
+ %p
5
+ %strong <%= attribute.column.human_name.titleize %>:
6
+ =& @<%= singular_name %>.<%= attribute.name %>
7
+ <%- end -%>
8
+
9
+ %p
10
+ <%- if action? :edit -%>
11
+ = link_to "Edit", edit_<%= singular_name %>_path(@<%= singular_name %>)
12
+ |
13
+ <%- end -%>
14
+ <%- if action? :destroy -%>
15
+ = link_to "Destroy", @<%= singular_name %>, :confirm => 'Are you sure?', :method => :delete
16
+ |
17
+ <%- end -%>
18
+ <%- if action? :index -%>
19
+ = link_to "View All", <%= plural_name %>_path
20
+ <%- end -%>
@@ -0,0 +1,2 @@
1
+ desc "Build the manifest and gemspec files."
2
+ task :build => [:build_manifest, :build_gemspec]
@@ -0,0 +1,117 @@
1
+ require 'test/unit'
2
+
3
+ # Must set before requiring generator libs.
4
+ TMP_ROOT = File.dirname(__FILE__) + "/tmp" unless defined?(TMP_ROOT)
5
+ PROJECT_NAME = "myproject" unless defined?(PROJECT_NAME)
6
+ app_root = File.join(TMP_ROOT, PROJECT_NAME)
7
+ if defined?(APP_ROOT)
8
+ APP_ROOT.replace(app_root)
9
+ else
10
+ APP_ROOT = app_root
11
+ end
12
+ if defined?(RAILS_ROOT)
13
+ RAILS_ROOT.replace(app_root)
14
+ else
15
+ RAILS_ROOT = app_root
16
+ end
17
+
18
+ require 'rubygems'
19
+ gem 'rails', '2.0.2' # getting a Rails.configuration error with 2.1
20
+ require 'rubigen' # gem install rubigen
21
+ require 'rubigen/helpers/generator_test_helper'
22
+ require 'rails_generator'
23
+ require 'shoulda' # gem install Shoulda
24
+ require 'mocha'
25
+
26
+ module NiftyGenerators
27
+ module TestHelper
28
+ include RubiGen::GeneratorTestHelper
29
+
30
+ def setup
31
+ bare_setup
32
+ end
33
+
34
+ def teardown
35
+ bare_teardown
36
+ end
37
+
38
+ protected
39
+
40
+ def run_rails_generator(generator, *args)
41
+ options = args.pop if args.last.kind_of? Hash
42
+ options ||= {}
43
+ run_generator(generator.to_s, args, generator_sources, options.reverse_merge(:quiet => true))
44
+ end
45
+
46
+ def generator_sources
47
+ [RubiGen::PathSource.new(:test, File.join(File.dirname(__FILE__), "..", "rails_generators"))]
48
+ end
49
+ end
50
+
51
+ module ShouldaAdditions
52
+ def rails_generator(*args)
53
+ setup do
54
+ run_rails_generator(*args)
55
+ end
56
+ end
57
+
58
+ def should_generate_file(file, &block)
59
+ should "generate file #{file}" do
60
+ yield("foo") if block_given?
61
+ assert_generated_file(file)
62
+ end
63
+ end
64
+
65
+ def should_not_generate_file(file)
66
+ should "not generate file #{file}" do
67
+ assert !File.exists?("#{APP_ROOT}/#{file}"),"The file '#{file}' should not exist"
68
+ end
69
+ end
70
+ end
71
+ end
72
+
73
+ class Thoughtbot::Shoulda::Context
74
+ include NiftyGenerators::ShouldaAdditions
75
+ end
76
+
77
+ # Mock out what we need from AR::Base.
78
+ module ActiveRecord
79
+ class Base
80
+ class << self
81
+ attr_accessor :pluralize_table_names, :columns
82
+
83
+ def add_column(name, type = :string)
84
+ returning ActiveRecord::ConnectionAdapters::Column.new(name, nil) do |column|
85
+ column.type = type
86
+ @columns ||= []
87
+ @columns << column
88
+ end
89
+ end
90
+ end
91
+ self.pluralize_table_names = true
92
+ end
93
+
94
+ module ConnectionAdapters
95
+ class Column
96
+ attr_accessor :name, :default, :type, :limit, :null, :sql_type, :precision, :scale
97
+
98
+ def initialize(name, default, sql_type = nil)
99
+ @name=name
100
+ @default=default
101
+ @type=@sql_type=sql_type
102
+ end
103
+
104
+ def human_name
105
+ @name.humanize
106
+ end
107
+ end
108
+ end
109
+ end
110
+
111
+ # And what we need from ActionView
112
+ module ActionView
113
+ module Helpers
114
+ module ActiveRecordHelper; end
115
+ class InstanceTag; end
116
+ end
117
+ end
@@ -0,0 +1,37 @@
1
+ require File.join(File.dirname(__FILE__), "test_helper.rb")
2
+
3
+ class TestNiftyLayoutGenerator < Test::Unit::TestCase
4
+ include NiftyGenerators::TestHelper
5
+
6
+ # Some generator-related assertions:
7
+ # assert_generated_file(name, &block) # block passed the file contents
8
+ # assert_directory_exists(name)
9
+ # assert_generated_class(name, &block)
10
+ # assert_generated_module(name, &block)
11
+ # assert_generated_test_for(name, &block)
12
+ # The assert_generated_(class|module|test_for) &block is passed the body of the class/module within the file
13
+ # assert_has_method(body, *methods) # check that the body has a list of methods (methods with parentheses not supported yet)
14
+ #
15
+ # Other helper methods are:
16
+ # app_root_files - put this in teardown to show files generated by the test method (e.g. p app_root_files)
17
+ # bare_setup - place this in setup method to create the APP_ROOT folder for each test
18
+ # bare_teardown - place this in teardown method to destroy the TMP_ROOT or APP_ROOT folder after each test
19
+
20
+ context "generator without name" do
21
+ rails_generator :nifty_config
22
+ should_generate_file 'config/app_config.yml'
23
+ should_generate_file 'config/initializers/load_app_config.rb'
24
+ end
25
+
26
+ context "generator with name" do
27
+ rails_generator :nifty_config, "FooBar"
28
+ should_generate_file 'config/foo_bar_config.yml'
29
+
30
+ should "use the name as a constant in the initializer" do
31
+ assert_generated_file 'config/initializers/load_foo_bar_config.rb' do |body|
32
+ assert_match "config/foo_bar_config.yml", body
33
+ assert_match "FOO_BAR_CONFIG = ", body
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,42 @@
1
+ require File.join(File.dirname(__FILE__), "test_helper.rb")
2
+
3
+ class TestNiftyLayoutGenerator < Test::Unit::TestCase
4
+ include NiftyGenerators::TestHelper
5
+
6
+ # Some generator-related assertions:
7
+ # assert_generated_file(name, &block) # block passed the file contents
8
+ # assert_directory_exists(name)
9
+ # assert_generated_class(name, &block)
10
+ # assert_generated_module(name, &block)
11
+ # assert_generated_test_for(name, &block)
12
+ # The assert_generated_(class|module|test_for) &block is passed the body of the class/module within the file
13
+ # assert_has_method(body, *methods) # check that the body has a list of methods (methods with parentheses not supported yet)
14
+ #
15
+ # Other helper methods are:
16
+ # app_root_files - put this in teardown to show files generated by the test method (e.g. p app_root_files)
17
+ # bare_setup - place this in setup method to create the APP_ROOT folder for each test
18
+ # bare_teardown - place this in teardown method to destroy the TMP_ROOT or APP_ROOT folder after each test
19
+
20
+ context "generator without name" do
21
+ rails_generator :nifty_layout
22
+ should_generate_file 'app/views/layouts/application.html.erb'
23
+ end
24
+
25
+ context "generator with name" do
26
+ rails_generator :nifty_layout, "foobar"
27
+ should_generate_file 'app/views/layouts/foobar.html.erb'
28
+ should_generate_file 'public/stylesheets/foobar.css'
29
+ should_generate_file 'app/helpers/layout_helper.rb'
30
+ end
31
+
32
+ context "generator with CamelCase name" do
33
+ rails_generator :nifty_layout, "FooBar"
34
+ should_generate_file 'app/views/layouts/foo_bar.html.erb'
35
+ end
36
+
37
+ context "generator with haml option" do
38
+ rails_generator :nifty_layout, "foobar", :haml => true
39
+ should_generate_file 'app/views/layouts/foobar.html.haml'
40
+ should_generate_file 'public/stylesheets/sass/foobar.sass'
41
+ end
42
+ end