controller_scaffolding 0.0.52

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.
Files changed (116) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.rdoc +3 -0
  4. data/Rakefile +32 -0
  5. data/lib/controller_scaffolding.rb +7 -0
  6. data/lib/controller_scaffolding/add_generator.rb +8 -0
  7. data/lib/controller_scaffolding/version.rb +3 -0
  8. data/lib/generators/assets/javascripts/ext_index_nav.js +14 -0
  9. data/lib/generators/assets/stylesheets/controller_scaffolding.css.scss +131 -0
  10. data/lib/generators/assets/stylesheets/ext_form_submit.css.scss +0 -0
  11. data/lib/generators/assets/stylesheets/ext_index_nav.css.scss +0 -0
  12. data/lib/generators/controller/concerns/ext_form_submit.rb +17 -0
  13. data/lib/generators/controller/concerns/ext_index_nav.rb +19 -0
  14. data/lib/generators/controller/controller_scaffolding_generator.rb +121 -0
  15. data/lib/generators/haml/controller/controller_scaffolding_generator.rb +166 -0
  16. data/lib/generators/haml/controller/partials/_flash_messages.html.haml +2 -0
  17. data/lib/generators/haml/controller/partials/_pagination.html.haml +10 -0
  18. data/lib/generators/haml/controller/partials/_validation_errors.html.haml +5 -0
  19. data/lib/templates/haml/controller/_form.html.haml +17 -0
  20. data/lib/templates/haml/controller/edit.html.haml +2 -0
  21. data/lib/templates/haml/controller/index.html.haml +37 -0
  22. data/lib/templates/haml/controller/new.html.haml +2 -0
  23. data/lib/templates/haml/controller/view.html.haml +4 -0
  24. data/lib/templates/rails/controller/controller.rb +16 -0
  25. data/test/controller_scaffolding_test.rb +7 -0
  26. data/test/dummy/README.rdoc +28 -0
  27. data/test/dummy/Rakefile +6 -0
  28. data/test/dummy/app/assets/javascripts/application.js +13 -0
  29. data/test/dummy/app/assets/stylesheets/application.css +15 -0
  30. data/test/dummy/app/controllers/application_controller.rb +6 -0
  31. data/test/dummy/app/helpers/application_helper.rb +2 -0
  32. data/test/dummy/app/models/person.rb +3 -0
  33. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  34. data/test/dummy/bin/bundle +3 -0
  35. data/test/dummy/bin/rails +4 -0
  36. data/test/dummy/bin/rake +4 -0
  37. data/test/dummy/config.ru +4 -0
  38. data/test/dummy/config/application.rb +30 -0
  39. data/test/dummy/config/boot.rb +5 -0
  40. data/test/dummy/config/database.yml +25 -0
  41. data/test/dummy/config/environment.rb +5 -0
  42. data/test/dummy/config/environments/development.rb +39 -0
  43. data/test/dummy/config/environments/production.rb +83 -0
  44. data/test/dummy/config/environments/test.rb +39 -0
  45. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  46. data/test/dummy/config/initializers/cookies_serializer.rb +3 -0
  47. data/test/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  48. data/test/dummy/config/initializers/inflections.rb +16 -0
  49. data/test/dummy/config/initializers/mime_types.rb +4 -0
  50. data/test/dummy/config/initializers/session_store.rb +3 -0
  51. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  52. data/test/dummy/config/locales/en.yml +25 -0
  53. data/test/dummy/config/routes.rb +56 -0
  54. data/test/dummy/config/secrets.yml +22 -0
  55. data/test/dummy/db/development.sqlite3 +0 -0
  56. data/test/dummy/db/migrate/20140711034542_create_people.rb +14 -0
  57. data/test/dummy/db/schema.rb +27 -0
  58. data/test/dummy/db/test.sqlite3 +0 -0
  59. data/test/dummy/lib/tasks/sample_data.rake +18 -0
  60. data/test/dummy/log/development.log +5691 -0
  61. data/test/dummy/log/test.log +81562 -0
  62. data/test/dummy/public/404.html +67 -0
  63. data/test/dummy/public/422.html +67 -0
  64. data/test/dummy/public/500.html +66 -0
  65. data/test/dummy/public/favicon.ico +0 -0
  66. data/test/dummy/spec/factories/factory.rb +12 -0
  67. data/test/dummy/spec/requests/people_spec.rb +227 -0
  68. data/test/dummy/spec/spec_helper.rb +42 -0
  69. data/test/dummy/test/test_helper.rb +15 -0
  70. data/test/dummy/tmp/cache/assets/development/sass/4cc2210c6c3c0fe94891da849ffe7911fc420445/controller_scaffolding.css.scssc +0 -0
  71. data/test/dummy/tmp/cache/assets/development/sass/4cc2210c6c3c0fe94891da849ffe7911fc420445/ext_form_submit.css.scssc +0 -0
  72. data/test/dummy/tmp/cache/assets/development/sass/4cc2210c6c3c0fe94891da849ffe7911fc420445/ext_index_nav.css.scssc +0 -0
  73. data/test/dummy/tmp/cache/assets/development/sprockets/12b145c6de8e8a781d4120a5f29229de +0 -0
  74. data/test/dummy/tmp/cache/assets/development/sprockets/13fe41fee1fe35b49d145bcc06610705 +0 -0
  75. data/test/dummy/tmp/cache/assets/development/sprockets/1406702d7228a8c1a717fc485390c025 +0 -0
  76. data/test/dummy/tmp/cache/assets/development/sprockets/2f5173deea6c795b8fdde723bb4b63af +0 -0
  77. data/test/dummy/tmp/cache/assets/development/sprockets/357970feca3ac29060c1e3861e2c0953 +0 -0
  78. data/test/dummy/tmp/cache/assets/development/sprockets/5e3a16a7a65151928708d6db358c759b +0 -0
  79. data/test/dummy/tmp/cache/assets/development/sprockets/653fdbb8934cfc4411579c8628e7162d +0 -0
  80. data/test/dummy/tmp/cache/assets/development/sprockets/705d80fa05052d5d6f269e75d7a11374 +0 -0
  81. data/test/dummy/tmp/cache/assets/development/sprockets/7761773b21e56b187ef1ef0faed07f4c +0 -0
  82. data/test/dummy/tmp/cache/assets/development/sprockets/8c0b04415ee487129c0d2bc8b098fb8c +0 -0
  83. data/test/dummy/tmp/cache/assets/development/sprockets/8e608c74df0c65a289387e25a3881ef6 +0 -0
  84. data/test/dummy/tmp/cache/assets/development/sprockets/965093127c5e679a2ffe5f562d7b4662 +0 -0
  85. data/test/dummy/tmp/cache/assets/development/sprockets/aee47d2b394353b1f027d1265ce94bec +0 -0
  86. data/test/dummy/tmp/cache/assets/development/sprockets/cffd775d018f68ce5dba1ee0d951a994 +0 -0
  87. data/test/dummy/tmp/cache/assets/development/sprockets/d10aa50bf278b2fcf5727421f422083f +0 -0
  88. data/test/dummy/tmp/cache/assets/development/sprockets/d771ace226fc8215a3572e0aa35bb0d6 +0 -0
  89. data/test/dummy/tmp/cache/assets/development/sprockets/d9a48b188723fa8a03d0f673046b0fc7 +0 -0
  90. data/test/dummy/tmp/cache/assets/development/sprockets/d9ad301878886b21cb9b44509bb894ee +0 -0
  91. data/test/dummy/tmp/cache/assets/development/sprockets/dd3286d35fd64e2653a6197e1a119fe3 +0 -0
  92. data/test/dummy/tmp/cache/assets/development/sprockets/e1096456df20861d666f73523b3a56d1 +0 -0
  93. data/test/dummy/tmp/cache/assets/development/sprockets/e6f3190c03c1c2b869d131e25d38eb7b +0 -0
  94. data/test/dummy/tmp/cache/assets/development/sprockets/e8b18160729bab32f8368174bf32a9ce +0 -0
  95. data/test/dummy/tmp/cache/assets/development/sprockets/eb8ff0c9bd29ca1463e355de4819639b +0 -0
  96. data/test/dummy/tmp/cache/assets/development/sprockets/f7cbd26ba1d28d48de824f0e94586655 +0 -0
  97. data/test/dummy/tmp/cache/assets/test/sass/4cc2210c6c3c0fe94891da849ffe7911fc420445/controller_scaffolding.css.scssc +0 -0
  98. data/test/dummy/tmp/cache/assets/test/sprockets/13fe41fee1fe35b49d145bcc06610705 +0 -0
  99. data/test/dummy/tmp/cache/assets/test/sprockets/2f5173deea6c795b8fdde723bb4b63af +0 -0
  100. data/test/dummy/tmp/cache/assets/test/sprockets/357970feca3ac29060c1e3861e2c0953 +0 -0
  101. data/test/dummy/tmp/cache/assets/test/sprockets/5e3a16a7a65151928708d6db358c759b +0 -0
  102. data/test/dummy/tmp/cache/assets/test/sprockets/7761773b21e56b187ef1ef0faed07f4c +0 -0
  103. data/test/dummy/tmp/cache/assets/test/sprockets/8c0b04415ee487129c0d2bc8b098fb8c +0 -0
  104. data/test/dummy/tmp/cache/assets/test/sprockets/965093127c5e679a2ffe5f562d7b4662 +0 -0
  105. data/test/dummy/tmp/cache/assets/test/sprockets/cffd775d018f68ce5dba1ee0d951a994 +0 -0
  106. data/test/dummy/tmp/cache/assets/test/sprockets/d771ace226fc8215a3572e0aa35bb0d6 +0 -0
  107. data/test/dummy/tmp/cache/assets/test/sprockets/d9a48b188723fa8a03d0f673046b0fc7 +0 -0
  108. data/test/dummy/tmp/cache/assets/test/sprockets/d9ad301878886b21cb9b44509bb894ee +0 -0
  109. data/test/dummy/tmp/cache/assets/test/sprockets/e1096456df20861d666f73523b3a56d1 +0 -0
  110. data/test/dummy/tmp/cache/assets/test/sprockets/e8b18160729bab32f8368174bf32a9ce +0 -0
  111. data/test/dummy/tmp/cache/assets/test/sprockets/f7cbd26ba1d28d48de824f0e94586655 +0 -0
  112. data/test/dummy/tmp/pids/server.pid +1 -0
  113. data/test/lib/generators/controller_scaffolding_generator_test.rb +108 -0
  114. data/test/lib/generators/ext_controller_scaffolding_generator_test.rb +52 -0
  115. data/test/test_helper.rb +15 -0
  116. metadata +431 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 86b332872eea33fdd28316b626d3d31cbb7aac3a
4
+ data.tar.gz: 301ab35b6841d8977a693216949307dc01aa0f7f
5
+ SHA512:
6
+ metadata.gz: 94c624469193929bbba55002ba240c963cdf569187e40102ecd907870909b6fca2db322836f8ea3fe896f8e8a830f3d3f2636f21667af432eba2386e04d383b7
7
+ data.tar.gz: b9d684512bb3078b9b860b7cd3c0d52631fe94c9cc94af28f6c3d309fa6db83c10aad0ff0fc7eec21f865d3be8d90050c1c7d15e60f06a687a01a196fb9b6205
@@ -0,0 +1,20 @@
1
+ Copyright 2014 YOURNAME
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,3 @@
1
+ = ControllerScaffolding
2
+
3
+ This project rocks and uses MIT-LICENSE.
@@ -0,0 +1,32 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'ControllerScaffolding'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.rdoc')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+
18
+
19
+
20
+ Bundler::GemHelper.install_tasks
21
+
22
+ require 'rake/testtask'
23
+
24
+ Rake::TestTask.new(:test) do |t|
25
+ t.libs << 'lib'
26
+ t.libs << 'test'
27
+ t.pattern = 'test/**/*_test.rb'
28
+ t.verbose = false
29
+ end
30
+
31
+
32
+ task default: :test
@@ -0,0 +1,7 @@
1
+ module ControllerScaffolding
2
+ require "haml-rails"
3
+ require "will_paginate"
4
+ require 'jquery-rails'
5
+ require "factory_girl_rails" if Rails.env == 'test'
6
+ require 'controller_scaffolding/add_generator' if defined?(Rails)
7
+ end
@@ -0,0 +1,8 @@
1
+ module ControllerScaffolding
2
+ class AddGenerator < Rails::Railtie
3
+ generators do
4
+ require "generators/controller/controller_scaffolding_generator"
5
+ require "generators/haml/controller/controller_scaffolding_generator"
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,3 @@
1
+ module ControllerScaffolding
2
+ VERSION = "0.0.52"
3
+ end
@@ -0,0 +1,14 @@
1
+ function set_per_page(sel) {
2
+ url = updateQueryStringParameter($(location).attr('href'), "per_page", $(sel).val())
3
+ window.location = updateQueryStringParameter(url, "page", "1")
4
+ }
5
+ function updateQueryStringParameter(uri, key, value) {
6
+ var re = new RegExp("([?&])" + key + "=.*?(&|$)", "i");
7
+ var separator = uri.indexOf('?') !== -1 ? "&" : "?";
8
+ if (uri.match(re)) {
9
+ return uri.replace(re, '$1' + key + "=" + value + '$2');
10
+ }
11
+ else {
12
+ return uri + separator + key + "=" + value;
13
+ }
14
+ }
@@ -0,0 +1,131 @@
1
+
2
+ $gray-lighter: lighten(#000, 80%);
3
+ $gray-light: lighten(#000, 46.7%);
4
+ $table-bg-color: white;
5
+ $border-color: #CBE6E6;
6
+ $link-color: #704942;
7
+
8
+ /*Mixins*/
9
+ @mixin tblbasic {
10
+ width: 750px;
11
+ background-color: $table-bg-color;
12
+ border-radius: 5px;
13
+ border: solid 2px $border-color;
14
+ border-collapse: inherit;
15
+ border-spacing: 0px;
16
+ }
17
+
18
+ /*for forms*/
19
+ table.outer {
20
+ @include tblbasic;
21
+ padding: 20px;
22
+ background-color: $gray-lighter;
23
+ td { padding: 5px; }
24
+ }
25
+
26
+ /*for lists*/
27
+ table.outer-list {
28
+ @include tblbasic;
29
+ margin: 15px;
30
+ tr:nth-child(even) {
31
+ background-color: $gray-lighter;
32
+ }
33
+ thead {
34
+ tr {
35
+ background-color: $gray-light;
36
+ th {
37
+ padding: 5px 0px 5px 5px; /*top right bottom left*/
38
+ border-bottom: solid 2px $border-color;
39
+ }
40
+ }
41
+
42
+ }
43
+ td { padding: 5px; }
44
+ }
45
+
46
+ table.page-top {
47
+ padding: 20px;
48
+ width: 800px;
49
+ border-radius: 5px;
50
+ td { padding: 5px; }
51
+ }
52
+
53
+ .center-align { text-align: center; }
54
+
55
+ .link_col { width: 30px; }
56
+
57
+ a {
58
+ color: $link-color;
59
+ text-decoration: none;
60
+ }
61
+
62
+ /*body style borrowed from bootsrap*/
63
+ body {
64
+ font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
65
+ font-size: 14px;
66
+ line-height: 1.428571429;
67
+ color: #333333;
68
+ background-color: white;
69
+ }
70
+
71
+ /*alert styles borrowed from bootstrap*/
72
+ .alert {
73
+ padding: 8px 35px 8px 14px;
74
+ margin-bottom: 20px;
75
+ text-shadow: 0 1px 0 rgba(255, 255, 255, 0.5);
76
+ background-color: #fcf8e3;
77
+ border: 1px solid #fbeed5;
78
+ -webkit-border-radius: 4px;
79
+ -moz-border-radius: 4px;
80
+ border-radius: 4px;
81
+ }
82
+ .alert,
83
+ .alert h4 {
84
+ color: #c09853;
85
+ }
86
+ .alert h4 {
87
+ margin: 0;
88
+ }
89
+ .alert .close {
90
+ position: relative;
91
+ top: -2px;
92
+ right: -21px;
93
+ line-height: 20px;
94
+ }
95
+ .alert-success {
96
+ background-color: #dff0d8;
97
+ border-color: #d6e9c6;
98
+ color: #468847;
99
+ }
100
+ .alert-success h4 {
101
+ color: #468847;
102
+ }
103
+ .alert-danger,
104
+ .alert-error {
105
+ background-color: #f2dede;
106
+ border-color: #eed3d7;
107
+ color: #b94a48;
108
+ }
109
+ .alert-danger h4,
110
+ .alert-error h4 {
111
+ color: #b94a48;
112
+ }
113
+ .alert-info {
114
+ background-color: #d9edf7;
115
+ border-color: #bce8f1;
116
+ color: #3a87ad;
117
+ }
118
+ .alert-info h4 {
119
+ color: #3a87ad;
120
+ }
121
+ .alert-block {
122
+ padding-top: 14px;
123
+ padding-bottom: 14px;
124
+ }
125
+ .alert-block > p,
126
+ .alert-block > ul {
127
+ margin-bottom: 0;
128
+ }
129
+ .alert-block p + p {
130
+ margin-top: 5px;
131
+ }
@@ -0,0 +1,17 @@
1
+ module ExtFormSubmit
2
+ def redir_url()
3
+ if params[:btn_reload]
4
+ :back
5
+ else
6
+ eval("#{params[:controller]}_url")
7
+ end
8
+ end
9
+
10
+ def flash_alert(obj)
11
+ err_str = view_context.render_for_controller("validation_errors", {:obj => obj})
12
+ msg = ("Unable to #{action_name} #{obj.class} for the following" +
13
+ " " + view_context.pluralize(obj.errors.full_messages.length, "reason") +
14
+ ":#{err_str}") unless obj.errors.messages.size < 1
15
+ flash.now[:error] = msg.html_safe
16
+ end
17
+ end
@@ -0,0 +1,19 @@
1
+ module ExtIndexNav
2
+
3
+ def setup_pagination(obj_class=controller_name.classify.constantize)
4
+ @page = params[:page] || 1
5
+
6
+ case params[:per_page]
7
+ when nil
8
+ @per_page = 15
9
+ when "All"
10
+ @per_page = obj_class.count
11
+ @selected_val = "All"
12
+ else
13
+ @per_page = params[:per_page]
14
+ end
15
+ end
16
+
17
+
18
+
19
+ end
@@ -0,0 +1,121 @@
1
+ #CONTROLLER GENERATOR
2
+
3
+ require 'rails/generators/generated_attribute'
4
+
5
+ module Rails
6
+ module Generators
7
+ ##################### Generators module methods #####################
8
+ RAILS_ADDED_COLS = %w(id created_at updated_at)
9
+
10
+ #TODO...There has GOT to be a better way to do this (column name gets listed first if it contains the word "name")
11
+ ATTR_SORT_PROC =
12
+ proc do |a, b|
13
+ if a =~ /name/
14
+ 1
15
+ elsif b =~ /name/
16
+ -1
17
+ elsif a =~ /email/
18
+ 1
19
+ elsif b =~ /email/
20
+ -1
21
+ else
22
+ 0
23
+ end
24
+ end
25
+
26
+ def attr_cols(table_name)
27
+ #return an array of the columns we are interested in allowing the user to change...
28
+ # as GeneratedAttribute objects
29
+ acs = table_name.classify.constantize.columns
30
+ .reject{ |col| RAILS_ADDED_COLS.include?(col.name) }
31
+ .sort(&ATTR_SORT_PROC)
32
+ .map { |ac| GeneratedAttribute.new(ac.name, ac.type)}
33
+ end
34
+
35
+ module_function :attr_cols
36
+ #######################################################################
37
+
38
+ class ControllerScaffoldingGenerator < NamedBase
39
+ argument :actions, type: :array, default: [], banner: "action action"
40
+ class_option :ext_index_nav, :type => :boolean, :default => true, :desc => "Include extended index page features."
41
+ class_option :ext_form_submit, :type => :boolean, :default => true, :desc => "Include extended form submission features."
42
+ check_class_collision suffix: "Controller"
43
+
44
+ #Note: This needs to be set Outside of any methods
45
+ source_paths << [File.expand_path('../../../templates/rails/controller', __FILE__)]
46
+
47
+ def check_for_model
48
+ begin
49
+ table_name.classify.constantize #throws runtime if model doesn't exist
50
+ rescue
51
+ raise Thor::Error,
52
+ "Cannot run controller scaffold for model (#{table_name}) that doesn't yet exist."
53
+ end
54
+ end
55
+
56
+ def create_controller_file
57
+ template 'controller.rb', File.join('app/controllers', class_path, "#{file_name}_controller.rb")
58
+ end
59
+
60
+ def add_routes
61
+ #TODO Handle nested resources here (look at namespace_ladder in scaffold generators)
62
+ route "resources :#{plural_table_name.to_sym}"
63
+ end
64
+
65
+ hook_for :template_engine, :assets, :test_framework, :helper,
66
+ #================================ P R I V A T E =================================
67
+ private
68
+ def generate_action_code(action, ext_index=true, ext_form_submit=true)
69
+ case action
70
+ when "index"
71
+ if ext_index
72
+ %Q`setup_pagination
73
+ @#{plural_table_name} = #{class_name.singularize}.all
74
+ .paginate(per_page: @per_page, page: @page)`
75
+ else
76
+ %Q`@#{plural_table_name} = #{class_name.singularize}.all`
77
+ end
78
+ when "edit"
79
+ %Q`@#{table_name.singularize} = #{class_name.singularize}.find(params[:id])`
80
+ when "new"
81
+ %Q`@#{table_name.singularize} = #{class_name.singularize}.new`
82
+ when "update"
83
+ redir_call = ext_form_submit ? "redir_url" : "edit_#{table_name.singularize}_url(@#{table_name.singularize})"
84
+ %Q`@#{table_name.singularize} = #{class_name.singularize}.find(params[:id])
85
+ if @#{table_name.singularize}.update_attributes(#{table_name.singularize}_params)
86
+ flash[:success] = "Updated #{table_name.singularize.humanize} successfully"
87
+ redirect_to #{redir_call}
88
+ else
89
+ flash_alert(@#{table_name.singularize})
90
+ render :edit
91
+ end`
92
+ when "create"
93
+ redir_call = ext_form_submit ? "redir_url" : "new_#{table_name.singularize}_url(@#{table_name.singularize})"
94
+ %Q`@#{table_name.singularize} = #{class_name.singularize}.new(#{table_name.singularize}_params)
95
+ if @#{table_name.singularize}.save
96
+ flash[:success] = "Created #{table_name.singularize.humanize} successfully"
97
+ redirect_to #{redir_call}
98
+ else
99
+ flash_alert(@#{table_name.singularize})
100
+ render :new
101
+ end`
102
+ when "destroy"
103
+ %Q`@#{table_name.singularize} = #{class_name.singularize}.find(params[:id])
104
+ if @#{table_name.singularize}.destroy
105
+ flash[:success] = "Deleted #{table_name.singularize.humanize} successfully"
106
+ else
107
+ flash[:error] = "Unable to delete #{table_name.singularize.humanize}"
108
+ end
109
+ redirect_to #{table_name.pluralize}_url`
110
+ end
111
+ end
112
+
113
+ def generate_strong_params_def
114
+ %Q`def #{table_name.singularize}_params
115
+ params.required(:#{table_name.singularize}).permit(#{Rails::Generators::attr_cols(table_name).map { |col| col.name.to_sym}})
116
+ end`
117
+ end
118
+ end
119
+ end
120
+
121
+ end
@@ -0,0 +1,166 @@
1
+ #TEMPLATE GENERATOR & FILE COPIER
2
+
3
+ require 'rails/generators/erb/controller/controller_generator'
4
+ require 'rails/generators/erb/scaffold/scaffold_generator'
5
+
6
+ module Haml
7
+ module Generators
8
+ class ControllerScaffoldingGenerator < Erb::Generators::ControllerGenerator
9
+ argument :actions, type: :array, default: [], banner: "action action"
10
+ class_option :ext_index_nav, :type => :boolean, :default => true, :desc => "Include extended index page features."
11
+ class_option :ext_form_submit, :type => :boolean, :default => true, :desc => "Include extended form submission features."
12
+
13
+ source_paths << File.expand_path('../../../../templates/haml/controller', __FILE__)
14
+ def copy_view_files #do NOT change the name of this method
15
+ # it must be overriding an existing one in a parent class
16
+ base_path = File.join("app/views", class_path, file_name)
17
+ empty_directory base_path
18
+ @attr_cols = ::Rails::Generators::attr_cols(table_name)
19
+ (actions - %w(create update destroy)).each do |action|
20
+ @action = action
21
+ formats.each do |format|
22
+ @path = File.join(base_path, filename_with_extensions(action, format))
23
+ set_template(@action, @path)
24
+ end
25
+ end
26
+ end
27
+
28
+ def gen_form_partial
29
+ #Create _form partial?
30
+ base_path = File.join("app/views", class_path, file_name)
31
+ unless (actions & %w(edit new)).empty? #Remember that "&" is Array#intersect
32
+ @path = File.join(base_path, filename_with_extensions("_form", format))
33
+ set_template("_form", @path)
34
+ end
35
+ end
36
+
37
+ #TODO: Couldn't get the tests to work with relative paths being passed to the
38
+ # inject_into_file calls...it wants to make them relative to the test's destination directory
39
+
40
+ def handle_ext_index
41
+ #Extended index functionality?
42
+ if options.ext_index_nav?
43
+ copy_controller_concern("ext_index_nav.rb")
44
+ inject_into_file "#{::Rails.root.to_s}/app/controllers/application_controller.rb",
45
+ after: "class ApplicationController < ActionController::Base\n" do
46
+ "\ninclude ExtIndexNav\n\n"
47
+ end
48
+ copy_partial("_pagination")
49
+ add_pagination_to_locale_file
50
+ copy_ext_index_js
51
+ inject_into_file "#{::Rails.root.to_s}/app/assets/javascripts/application.js",
52
+ before: "\n//= require_tree ." do
53
+ "\n//= require jquery\n//= require jquery_ujs"
54
+ end
55
+ end
56
+ end
57
+
58
+ def handle_ext_form
59
+ #extended form submission functionality?
60
+ if options.ext_form_submit?
61
+ copy_controller_concern("ext_form_submit.rb")
62
+
63
+ inject_into_file "#{::Rails.root.to_s}/app/controllers/application_controller.rb",
64
+ after: "class ApplicationController < ActionController::Base\n" do
65
+ "\ninclude ExtFormSubmit\n\n"
66
+ end
67
+ copy_partial("_flash_messages")
68
+ inject_into_file "#{::Rails.root.to_s}/app/views/layouts/application.html.erb",
69
+ before: "<%= yield %>\n" do
70
+ "\n<%= render 'flash_messages' %>\n"
71
+ end
72
+ copy_partial("_validation_errors")
73
+ inject_into_file "#{::Rails.root.to_s}/app/helpers/application_helper.rb",
74
+ after: "module ApplicationHelper\n" do
75
+ "\ndef render_for_controller(partial, local_vars)
76
+ render(:partial => partial, :locals => local_vars).html_safe
77
+ end\n"
78
+ end
79
+ end
80
+ end
81
+
82
+ def copy_stylesheet
83
+ if options.ext_form_submit? || options.ext_index_nav?
84
+ source_paths << File.expand_path('../../../../generators/assets/stylesheets', __FILE__)
85
+ base_path = "app/assets/stylesheets"
86
+ path = File.join(base_path, 'controller_scaffolding.css.scss')
87
+ copy_file('controller_scaffolding.css.scss', path) if file_action(path)
88
+ end
89
+ end
90
+
91
+
92
+ #================================= P R O T E C T E D =================================
93
+ protected
94
+ def handler
95
+ :haml
96
+ end
97
+ #================================= P R I V A T E =====================================
98
+ private
99
+
100
+ def set_template(action, path)
101
+ template filename_with_extensions(action.to_sym, format), path
102
+ rescue Thor::Error => e
103
+ say("Falling back to the 'view.html.haml' template because #{action}.html.haml doesn't exist",
104
+ :magenta) unless options.quiet?
105
+ template filename_with_extensions(:view, format), @path
106
+ end
107
+
108
+ def copy_partial(file)
109
+ source_paths << File.expand_path('../partials', __FILE__)
110
+ base_path = "app/views/application"
111
+ path = File.join(base_path, filename_with_extensions( file, format))
112
+ copy_file(filename_with_extensions(file, format), path) if file_action(path)
113
+ end
114
+
115
+ def copy_controller_concern(file_w_ext)
116
+ source_paths << File.expand_path('../../../../generators/controller/concerns', __FILE__)
117
+ base_path = "app/controllers/concerns"
118
+ path = File.join(base_path, file_w_ext)
119
+ copy_file(file_w_ext, path) if file_action(path)
120
+ end
121
+
122
+ def copy_ext_index_js
123
+ source_paths << File.expand_path('../../../../generators/assets/javascripts', __FILE__)
124
+ base_path = "app/assets/javascripts"
125
+ path = File.join(base_path, 'ext_index_nav.js')
126
+ copy_file('ext_index_nav.js', path) if file_action(path)
127
+ end
128
+
129
+ def add_pagination_to_locale_file
130
+ inject_into_file "#{::Rails.root.to_s}/config/locales/en.yml",
131
+ after: "\nen:\n" do
132
+ %Q{\n
133
+ will_paginate:
134
+ models:
135
+ people:
136
+ zero: People
137
+ one: Person
138
+ other: People
139
+ page_entries_info:
140
+ multi_page_html: "Displaying <b>%{from}&nbsp;-&nbsp;%{to}</b> of <b>%{count}</b> %{model}"
141
+ single_page_html:
142
+ zero: "No %{model} found"
143
+ one: "Displaying <b>1</b> %{model}"
144
+ other: "Displaying <b>all&nbsp;%{count}</b> %{model}"
145
+ }
146
+ end
147
+ end
148
+
149
+ def file_action(path)
150
+ return true if options.force?
151
+ if behavior == :revoke
152
+ verb = "Remove"
153
+ elsif File.exists?(path)
154
+ verb = "Overwrite existing"
155
+ else
156
+ return true
157
+ end
158
+ return yes?("#{verb} shared file #{path}? (y or n)", :yellow)
159
+ end
160
+
161
+ def inc_link?(name)
162
+ actions.include?(name)
163
+ end
164
+ end
165
+ end
166
+ end