beautiful_scaffold 1.0.0.pre → 2.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (91) hide show
  1. checksums.yaml +5 -5
  2. data/.gitignore +2 -0
  3. data/CHANGELOG +73 -1
  4. data/Gemfile +25 -17
  5. data/README.rdoc +45 -28
  6. data/Rakefile +29 -0
  7. data/beautiful_scaffold.gemspec +7 -7
  8. data/lib/beautiful_scaffold/version.rb +3 -0
  9. data/lib/generators/USAGE +3 -1
  10. data/lib/generators/beautiful_cancancan_generator.rb +51 -0
  11. data/lib/generators/beautiful_jointable_generator.rb +41 -8
  12. data/lib/generators/beautiful_locale_generator.rb +112 -88
  13. data/lib/generators/beautiful_migration_generator.rb +14 -20
  14. data/lib/generators/beautiful_scaffold_common_methods.rb +92 -25
  15. data/lib/generators/beautiful_scaffold_generator.rb +164 -101
  16. data/lib/generators/beautiful_sorcery_generator.rb +137 -0
  17. data/lib/generators/beautiful_storage_generator.rb +61 -0
  18. data/lib/generators/templates/app/assets/javascripts/application-bs.js +7 -7
  19. data/lib/generators/templates/app/assets/javascripts/beautiful_scaffold.js +70 -47
  20. data/lib/generators/templates/app/assets/javascripts/bootstrap-datetimepicker-for-beautiful-scaffold.js +23 -42
  21. data/lib/generators/templates/app/assets/javascripts/fixed_menu.js +3 -1
  22. data/lib/generators/templates/app/assets/javascripts/jstree.min.js +6 -0
  23. data/lib/generators/templates/app/assets/stylesheets/application-bs.css +14 -6
  24. data/lib/generators/templates/app/assets/stylesheets/beautiful-scaffold.css.scss +38 -3
  25. data/lib/generators/templates/app/assets/stylesheets/themes/default-dark/32px.png +0 -0
  26. data/lib/generators/templates/app/assets/stylesheets/themes/default-dark/40px.png +0 -0
  27. data/lib/generators/templates/app/assets/stylesheets/themes/default-dark/style.scss +1146 -0
  28. data/lib/generators/templates/app/assets/stylesheets/themes/default-dark/throbber.gif +0 -0
  29. data/lib/generators/templates/app/assets/stylesheets/themes/default/32px.png +0 -0
  30. data/lib/generators/templates/app/assets/stylesheets/themes/default/40px.png +0 -0
  31. data/lib/generators/templates/app/assets/stylesheets/themes/default/style.scss +1102 -0
  32. data/lib/generators/templates/app/assets/stylesheets/themes/default/throbber.gif +0 -0
  33. data/lib/generators/templates/app/controllers/base.rb +31 -14
  34. data/lib/generators/templates/app/controllers/master_base.rb +55 -47
  35. data/lib/generators/templates/app/controllers/user_sessions_controller.rb +20 -0
  36. data/lib/generators/templates/app/helpers/beautiful_helper.rb +122 -72
  37. data/lib/generators/templates/app/helpers/model_helper.rb +14 -3
  38. data/lib/generators/templates/app/initializers/link_renderer.rb +23 -18
  39. data/lib/generators/templates/app/initializers/ransack.rb +16 -0
  40. data/lib/generators/templates/app/locales/beautiful_scaffold.en.yml +8 -0
  41. data/lib/generators/templates/app/locales/beautiful_scaffold.fr.yml +9 -1
  42. data/lib/generators/templates/app/locales/beautiful_scaffold.ja.yml +9 -2
  43. data/lib/generators/templates/app/mailers/user_mailer.rb +13 -0
  44. data/lib/generators/templates/app/models/ability.rb +39 -0
  45. data/lib/generators/templates/app/models/concerns/caption_concern.rb +1 -1
  46. data/lib/generators/templates/app/models/user.rb +11 -0
  47. data/lib/generators/templates/app/views/_form.html.erb +4 -1
  48. data/lib/generators/templates/app/views/_form_habtm_tag.html.erb +6 -2
  49. data/lib/generators/templates/app/views/_mass_inserting.html.erb +40 -31
  50. data/lib/generators/templates/app/views/_modal_columns.html.erb +4 -4
  51. data/lib/generators/templates/app/views/edit.html.erb +1 -3
  52. data/lib/generators/templates/app/views/index.html.erb +84 -58
  53. data/lib/generators/templates/app/views/layout.html.erb +36 -47
  54. data/lib/generators/templates/app/views/login_logout/user_mailer/activation_needed_email.en.html.erb +16 -0
  55. data/lib/generators/templates/app/views/login_logout/user_mailer/activation_needed_email.en.text.erb +9 -0
  56. data/lib/generators/templates/app/views/login_logout/user_mailer/activation_needed_email.fr.html.erb +16 -0
  57. data/lib/generators/templates/app/views/login_logout/user_mailer/activation_needed_email.fr.text.erb +8 -0
  58. data/lib/generators/templates/app/views/login_logout/user_mailer/activation_success_email.en.html.erb +19 -0
  59. data/lib/generators/templates/app/views/login_logout/user_mailer/activation_success_email.en.text.erb +8 -0
  60. data/lib/generators/templates/app/views/login_logout/user_mailer/activation_success_email.fr.html.erb +19 -0
  61. data/lib/generators/templates/app/views/login_logout/user_mailer/activation_success_email.fr.text.erb +8 -0
  62. data/lib/generators/templates/app/views/login_logout/user_sessions/_form.html.erb +15 -0
  63. data/lib/generators/templates/app/views/login_logout/user_sessions/new.html.erb +3 -0
  64. data/lib/generators/templates/app/views/new.html.erb +0 -2
  65. data/lib/generators/templates/app/views/partials/_forget_password.html.erb +2 -2
  66. data/lib/generators/templates/app/views/partials/_form_field.html.erb +7 -7
  67. data/lib/generators/templates/app/views/partials/_index_batch.html.erb +1 -1
  68. data/lib/generators/templates/app/views/partials/_index_column.html.erb +4 -4
  69. data/lib/generators/templates/app/views/partials/_index_header.html.erb +1 -1
  70. data/lib/generators/templates/app/views/partials/_index_search.html.erb +2 -2
  71. data/lib/generators/templates/app/views/partials/_index_search_default_fields.html.erb +1 -1
  72. data/lib/generators/templates/app/views/partials/_login_logout_register.html.erb +7 -0
  73. data/lib/generators/templates/app/views/partials/_show_field.html.erb +3 -3
  74. data/lib/generators/templates/app/views/show.html.erb +2 -2
  75. data/lib/generators/templates/app/views/treeview.html.erb +8 -5
  76. metadata +41 -26
  77. data/lib/generators/beautiful_devisecancan_generator.rb +0 -128
  78. data/lib/generators/templates/app/assets/javascripts/bootstrap-datepicker.js +0 -834
  79. data/lib/generators/templates/app/assets/javascripts/bootstrap-timepicker.js +0 -797
  80. data/lib/generators/templates/app/assets/javascripts/jquery.jstree.js +0 -4551
  81. data/lib/generators/templates/app/assets/stylesheets/bootstrap_and_overrides.css.less +0 -40
  82. data/lib/generators/templates/app/assets/stylesheets/datepicker.css +0 -224
  83. data/lib/generators/templates/app/assets/stylesheets/reset.css +0 -48
  84. data/lib/generators/templates/app/assets/stylesheets/themes/default/d.gif +0 -0
  85. data/lib/generators/templates/app/assets/stylesheets/themes/default/d.png +0 -0
  86. data/lib/generators/templates/app/assets/stylesheets/themes/default/style.css +0 -74
  87. data/lib/generators/templates/app/assets/stylesheets/timepicker.css +0 -89
  88. data/lib/generators/templates/app/views/partials/_register_form.html.erb +0 -44
  89. data/lib/generators/templates/app/views/partials/_sign_in_form.html.erb +0 -18
  90. data/lib/generators/templates/app/views/partials/_sign_in_sign_out.html.erb +0 -19
  91. data/lib/generators/templates/lib/custom_failure.rb +0 -15
@@ -1,6 +1,6 @@
1
1
  # encoding : utf-8
2
2
  class BeautifulScaffoldGenerator < Rails::Generators::Base
3
- require 'beautiful_scaffold_common_methods'
3
+ require_relative 'beautiful_scaffold_common_methods'
4
4
  include BeautifulScaffoldCommonMethods
5
5
 
6
6
  # Resources
@@ -13,19 +13,20 @@ class BeautifulScaffoldGenerator < Rails::Generators::Base
13
13
 
14
14
  source_root File.expand_path('../templates', __FILE__)
15
15
 
16
- argument :model, :type => :string, :desc => "Name of model (downcase singular)"
17
- argument :myattributes, :type => :array, :default => [], :banner => "field:type field:type"
18
-
19
- class_option :namespace, :default => nil
20
- class_option :donttouchgem, :default => nil
16
+ argument :model_opt, type: :string, desc: "Name of model singular downcase (ex: family)"
17
+ argument :myattributes, type: :array, default: [], banner: "field:type field:type"
18
+
19
+ class_option :namespace, default: nil
20
+ class_option :donttouchgem, default: nil
21
+ class_option :mountable_engine, default: nil
21
22
 
22
23
  def install_gems
23
- if options[:donttouchgem].blank? then
24
+ if options[:donttouchgem].blank?
24
25
  require_gems
25
26
  end
26
27
 
27
28
  #inside Rails.root do # Bug ?!
28
- Bundler.with_clean_env do
29
+ Bundler.with_unbundled_env do
29
30
  run "bundle install"
30
31
  end
31
32
  end
@@ -35,7 +36,7 @@ class BeautifulScaffoldGenerator < Rails::Generators::Base
35
36
  @fulltext_field = []
36
37
  myattributes.each{ |attr|
37
38
  a,t = attr.split(':')
38
- if ['wysiwyg'].include?(t) then
39
+ if ['wysiwyg'].include?(t)
39
40
  # _typetext = {html|text}
40
41
  # _fulltext = text without any code
41
42
  @fulltext_field << [a + '_typetext', 'string'].join(':')
@@ -45,88 +46,149 @@ class BeautifulScaffoldGenerator < Rails::Generators::Base
45
46
  end
46
47
 
47
48
  def mimetype
48
- if not File.exist?("app/controllers/beautiful_controller.rb") then
49
- inject_into_file("config/initializers/mime_types.rb", 'Mime::Type.register_alias "application/pdf", :pdf' + "\n", :before => "# Be sure to restart your server when you modify this file." )
49
+ if !File.exist?("app/controllers/beautiful_controller.rb")
50
+ if File.exist?("config/initializers/mime_types.rb") # For mountable engine
51
+ inject_into_file("config/initializers/mime_types.rb", 'Mime::Type.register_alias "application/pdf", :pdf' + "\n", :before => "# Be sure to restart your server when you modify this file." )
52
+ else
53
+ puts "============> Engine : You must add `Mime::Type.register_alias \"application/pdf\", :pdf` to your config/initializers/mime_types.rb main app !"
54
+ end
50
55
  end
51
56
  end
52
-
57
+
53
58
  def generate_assets
54
59
  stylesheetspath = "app/assets/stylesheets/"
55
-
60
+ stylesheetspath_dest = "#{stylesheetspath}#{engine_name}"
61
+
56
62
  # Css
57
- reset = "#{stylesheetspath}reset.css"
58
63
  bc_css = [
59
- "#{stylesheetspath}application-bs.css",
60
- "#{stylesheetspath}datepicker.css",
61
- "#{stylesheetspath}timepicker.css",
62
- "#{stylesheetspath}beautiful-scaffold.css.scss",
63
- "#{stylesheetspath}tagit-dark-grey.css",
64
- "#{stylesheetspath}colorpicker.css",
65
- "#{stylesheetspath}bootstrap-wysihtml5.css"
64
+ "beautiful-scaffold.css.scss",
65
+ "tagit-dark-grey.css",
66
+ "colorpicker.css",
67
+ "bootstrap-wysihtml5.css"
66
68
  ]
67
-
69
+
68
70
  javascriptspath = "app/assets/javascripts/"
69
-
71
+ javascriptspath_dest = "#{javascriptspath}#{engine_name}"
72
+
73
+ bc_css.each do |path|
74
+ copy_file "#{stylesheetspath}#{path}", "#{stylesheetspath_dest}#{path}"
75
+ end
76
+ copy_file "#{stylesheetspath}application-bs.css", "#{stylesheetspath_dest}application-bs.scss"
77
+
78
+ # Jstree theme
79
+ directory "#{stylesheetspath}themes", "#{stylesheetspath}#{engine_name}themes"
80
+
81
+ if !engine_name.blank?
82
+ ['beautiful-scaffold',
83
+ 'tagit-dark-grey',
84
+ 'colorpicker',
85
+ 'bootstrap-wysihtml5'].each do |fileassets|
86
+ gsub_file File.join(stylesheetspath_dest, "application-bs.scss"), " *= require #{fileassets}", " *= require #{engine_name}#{fileassets}"
87
+ end
88
+
89
+ # Issue otherwise
90
+ gsub_file File.join(stylesheetspath_dest, "application-bs.scss"), '@import "tempusdominus-bootstrap-4.css";', '@import "../tempusdominus-bootstrap-4.css";'
91
+ gsub_file File.join(stylesheetspath_dest, "application-bs.scss"), 'require themes/default/style', "require #{engine_name}themes/default/style"
92
+
93
+ # treeview
94
+ gsub_file File.join(stylesheetspath_dest, 'themes', 'default', 'style.scss'), 'asset-url("themes', "asset-url(\"#{engine_name}themes"
95
+ gsub_file File.join(stylesheetspath_dest, 'themes', 'default-dark', 'style.scss'), 'asset-url("themes', "asset-url(\"#{engine_name}themes"
96
+ end
97
+
70
98
  # Js
71
99
  bc_js = [
72
- "#{javascriptspath}application-bs.js",
73
- "#{javascriptspath}beautiful_scaffold.js",
74
- "#{javascriptspath}bootstrap-datepicker.js",
75
- "#{javascriptspath}bootstrap-datetimepicker-for-beautiful-scaffold.js",
76
- "#{javascriptspath}bootstrap-timepicker.js",
77
- "#{javascriptspath}jquery.jstree.js",
78
- "#{javascriptspath}jquery-barcode.js",
79
- "#{javascriptspath}tagit.js",
80
- "#{javascriptspath}bootstrap-colorpicker.js",
81
- "#{javascriptspath}a-wysihtml5-0.3.0.min.js",
82
- "#{javascriptspath}bootstrap-wysihtml5.js",
83
- "#{javascriptspath}fixed_menu.js"
100
+ "application-bs.js",
101
+ "beautiful_scaffold.js",
102
+ "bootstrap-datetimepicker-for-beautiful-scaffold.js",
103
+ "jquery-barcode.js",
104
+ "jstree.min.js",
105
+ "tagit.js",
106
+ "bootstrap-colorpicker.js",
107
+ "a-wysihtml5-0.3.0.min.js",
108
+ "bootstrap-wysihtml5.js",
109
+ "fixed_menu.js"
84
110
  ]
85
111
 
86
- [reset, bc_css, bc_js].flatten.each{ |path|
87
- copy_file path, path
112
+ [bc_js].flatten.each{ |path|
113
+ copy_file "#{javascriptspath}#{path}", "#{javascriptspath_dest}#{path}"
88
114
  }
89
115
 
90
- # Jstree theme
91
- directory "app/assets/stylesheets/themes", "app/assets/stylesheets/themes"
116
+ if !engine_name.blank?
117
+ ['a-wysihtml5-0.3.0.min',
118
+ 'bootstrap-colorpicker',
119
+ 'bootstrap-datetimepicker-for-beautiful-scaffold',
120
+ 'bootstrap-wysihtml5',
121
+ 'tagit.js',
122
+ 'jstree.min.js',
123
+ 'jquery-barcode',
124
+ 'beautiful_scaffold',
125
+ 'fixed_menu'].each do |fileassets|
126
+ gsub_file File.join(javascriptspath_dest, "application-bs.js"), "//= require #{fileassets}", "//= require #{engine_name}#{fileassets}"
127
+ end
128
+ end
92
129
 
93
130
  # Images
94
131
  dir_image = "app/assets/images"
95
- directory dir_image, dir_image
96
-
97
- # Old method
98
- #generate("bootstrap:install","static")
99
- # New method
100
- copy_file "#{stylesheetspath}bootstrap_and_overrides.css.less",
101
- "#{stylesheetspath}bootstrap_and_overrides.css.less"
132
+ dir_image_dest = "app/assets/images/#{engine_opt}"
133
+ directory dir_image, dir_image_dest
102
134
 
103
135
  # Precompile BS assets
104
- inject_into_file("config/initializers/assets.rb", "Rails.application.config.assets.precompile += ['application-bs.css','application-bs.js']", after: /\z/m)
136
+ path_to_assets_rb = "config/initializers/assets.rb"
137
+ if !File.exist?(path_to_assets_rb) && !engine_name.blank? # Engine
138
+ path_to_assets_rb = File.join("test", "dummy", "config/initializers/assets.rb")
139
+ end
140
+
141
+ append_to_file(path_to_assets_rb, "Rails.application.config.assets.precompile += ['#{engine_name}application-bs.css','#{engine_name}application-bs.js']")
142
+ if !engine_name.blank?
143
+ manifest_prefix = "#{engine_opt}_"
144
+ else
145
+ manifest_prefix = ""
146
+ end
147
+ #append_to_file("app/assets/config/#{manifest_prefix}manifest.js", '//= link_directory ../stylesheets/faq .css')
148
+ append_to_file("app/assets/config/#{manifest_prefix}manifest.js", '//= link_directory ../javascripts .js')
105
149
  end
106
-
150
+
107
151
  def generate_layout
108
- template "app/views/layout.html.erb", "app/views/layouts/beautiful_layout.html.erb"
109
- if not File.exist?("app/views/layouts/_beautiful_menu.html.erb") then
110
- template "app/views/_beautiful_menu.html.erb", "app/views/layouts/_beautiful_menu.html.erb"
152
+ template "app/views/layout.html.erb", "app/views/layouts/#{engine_name}beautiful_layout.html.erb"
153
+
154
+ gsub_file "app/views/layouts/#{engine_name}beautiful_layout.html.erb", '"layouts/beautiful_menu"', "\"layouts/#{engine_name}beautiful_menu\""
155
+
156
+ if !File.exist?("app/views/layouts/#{engine_name}_beautiful_menu.html.erb")
157
+ template "app/views/_beautiful_menu.html.erb", "app/views/layouts/#{engine_name}_beautiful_menu.html.erb"
111
158
  end
112
159
 
113
- empty_directory "app/views/beautiful"
114
- template "app/views/dashboard.html.erb", "app/views/beautiful/dashboard.html.erb"
115
- copy_file "app/views/_modal_columns.html.erb", "app/views/layouts/_modal_columns.html.erb"
116
- copy_file "app/views/_mass_inserting.html.erb", "app/views/layouts/_mass_inserting.html.erb"
117
-
118
- inject_into_file("app/views/layouts/_beautiful_menu.html.erb",'
119
- <li class="<%= "active" if params[:controller] == "' + namespace_for_url + model.pluralize + '" %>">
120
- <%= link_to ' + i18n_t_m_p(model) + '.capitalize, ' + namespace_for_route + model.pluralize + '_path %>
121
- </li>', :after => "<!-- Beautiful Scaffold Menu Do Not Touch This -->")
160
+ empty_directory "app/views/#{engine_name}beautiful"
161
+ template "app/views/dashboard.html.erb", "app/views/#{engine_name}beautiful/dashboard.html.erb"
162
+ copy_file "app/views/_modal_columns.html.erb", "app/views/layouts/#{engine_name}_modal_columns.html.erb"
163
+ copy_file "app/views/_mass_inserting.html.erb", "app/views/layouts/#{engine_name}_mass_inserting.html.erb"
164
+
165
+ action_ctrl = "#{namespace_for_url}#{model.pluralize}"
166
+
167
+ inject_into_file("app/views/layouts/#{engine_name}_beautiful_menu.html.erb",
168
+ "\n" + '<%= link_to ' + i18n_t_m_p(model) + '.capitalize, ' + namespace_for_route + model.pluralize + '_path, class: "nav-link #{(params[:controller] == "' + action_ctrl + '" ? "active" : "")}" %>',
169
+ :after => "<!-- Beautiful Scaffold Menu Do Not Touch This -->")
122
170
  end
123
171
 
124
172
  def generate_model
125
173
  generate("model", "#{model} #{beautiful_attr_to_rails_attr.join(' ')} #{@fulltext_field.join(' ')}")
174
+ directory "app/models/concerns", "app/models/concerns/#{engine_name}"
175
+
176
+ copy_file "app/models/pdf_report.rb", "app/models/#{engine_name}pdf_report.rb"
177
+
178
+ if !engine_name.blank?
179
+ ['caption_concern', 'default_sorting_concern','fulltext_concern'].each do |f|
180
+ path_to_the_concern = "app/models/concerns/#{engine_name}#{f}.rb"
181
+ inject_into_file path_to_the_concern, "module #{engine_camel}\n", before: "module #{f.camelcase}"
182
+ append_to_file path_to_the_concern, "\nend #endofmodule \n"
183
+ end
126
184
 
127
- directory "app/models/concerns", "app/models/concerns"
185
+ path_to_the_pdf_report = "app/models/#{engine_name}pdf_report.rb"
186
+ inject_into_file path_to_the_pdf_report, "module #{engine_camel}\n", before: "class PdfReport"
187
+ append_to_file path_to_the_pdf_report, "\nend #endofmodule \n"
188
+ end
128
189
 
129
- inject_into_file("app/models/#{model}.rb",'
190
+ gsub_file "app/models/#{engine_name}#{model}.rb", 'ActiveRecord::Base', 'ApplicationRecord' # Rails 4 -> 5
191
+ inject_into_file("app/models/#{engine_name}#{model}.rb",'
130
192
 
131
193
  include DefaultSortingConcern
132
194
  include FulltextConcern
@@ -140,53 +202,62 @@ class BeautifulScaffoldGenerator < Rails::Generators::Base
140
202
  return ' + attributes_without_type.map{ |attr| ":#{attr}" }.join(",") + '
141
203
  end', :after => "class #{model_camelize} < ApplicationRecord")
142
204
 
143
- copy_file "app/models/pdf_report.rb", "app/models/pdf_report.rb"
144
205
  end
145
206
 
146
207
  def add_to_model
147
- # Add relation
148
- myattributes.each{ |attr|
149
- a,t = attr.split(':')
150
- if ['references', 'reference'].include?(t) then
151
- begin
152
- inject_into_file("app/models/#{a}.rb", "\n has_many :#{model_pluralize}, :dependent => :nullify", :after => "ApplicationRecord")
153
- rescue
154
- end
155
- end
156
- }
208
+ add_relation
157
209
  end
158
210
 
159
211
  def generate_controller
160
- copy_file "app/controllers/master_base.rb", "app/controllers/beautiful_controller.rb"
161
- dirs = ['app', 'controllers', options[:namespace]].compact
212
+ beautiful_ctrl_path = "app/controllers/#{engine_name}beautiful_controller.rb"
213
+ copy_file "app/controllers/master_base.rb", beautiful_ctrl_path
214
+ # beautiful_controller in the context of engine
215
+ if !engine_name.empty?
216
+ inject_into_file beautiful_ctrl_path, "module #{engine_camel}\n", before: "class BeautifulController"
217
+ #gsub_file beautiful_ctrl_path, '< ApplicationController', "< ::#{engine_camel}::ApplicationController" # Rails 4 -> 5 'BeautifulController < ApplicationController'
218
+ append_to_file beautiful_ctrl_path, "end #endofmodule \n"
219
+
220
+ gsub_file beautiful_ctrl_path, 'layout "beautiful_layout"', "layout \"#{engine_name}beautiful_layout\""
221
+ end
222
+ dirs = ['app', 'controllers', engine_name, options[:namespace]].compact
162
223
  # Avoid to remove app/controllers directory (https://github.com/rivsc/Beautiful-Scaffold/issues/6)
163
- empty_directory File.join(dirs) if not options[:namespace].blank?
164
- template "app/controllers/base.rb", File.join([dirs, "#{model_pluralize}_controller.rb"].flatten)
224
+ empty_directory File.join(dirs) if !options[:namespace].blank?
225
+ dest_ctrl_file = File.join([dirs, "#{model_pluralize}_controller.rb"].flatten)
226
+ template "app/controllers/base.rb", dest_ctrl_file
165
227
  end
166
-
228
+
167
229
  def generate_helper
168
- copy_file "app/helpers/beautiful_helper.rb", "app/helpers/beautiful_helper.rb"
169
- dirs = ['app', 'helpers', options[:namespace]].compact
230
+ dest_bs_helper_file = "app/helpers/#{engine_name}beautiful_helper.rb"
231
+ template "app/helpers/beautiful_helper.rb", dest_bs_helper_file
232
+
233
+ dirs = ['app', 'helpers', engine_name, options[:namespace]].compact
170
234
  empty_directory File.join(dirs)
171
- template "app/helpers/model_helper.rb", File.join([dirs, "#{model_pluralize}_helper.rb"].flatten)
235
+ dest_helper_file = File.join([dirs, "#{model_pluralize}_helper.rb"].flatten)
236
+ template "app/helpers/model_helper.rb", dest_helper_file
172
237
  end
173
238
 
174
239
  def generate_views
175
- namespacedirs = ["app", "views", options[:namespace]].compact
240
+ namespacedirs = ["app", "views", engine_name, options[:namespace]].compact
176
241
  empty_directory File.join(namespacedirs)
177
-
242
+
178
243
  dirs = [namespacedirs, model_pluralize]
179
244
  empty_directory File.join(dirs)
180
-
245
+
181
246
  [available_views, 'treeview'].flatten.each do |view|
182
247
  filename = view + ".html.erb"
183
248
  current_template_path = File.join([dirs, filename].flatten)
184
249
  empty_template_path = File.join(["app", "views", filename].flatten)
185
-
186
250
  template empty_template_path, current_template_path
251
+
252
+ gsub_file current_template_path, '"layouts/modal_columns"', "\"layouts/#{engine_name}modal_columns\""
253
+ gsub_file current_template_path, '"layouts/mass_inserting"', "\"layouts/#{engine_name}mass_inserting\""
187
254
  end
188
255
 
189
- copy_file "app/views/_form_habtm_tag.html.erb", "app/views/layouts/_form_habtm_tag.html.erb"
256
+ copy_file "app/views/_form_habtm_tag.html.erb", "app/views/layouts/#{engine_name}_form_habtm_tag.html.erb"
257
+ end
258
+
259
+ def install_ransack_intializer
260
+ copy_file "app/initializers/ransack.rb", "config/initializers/ransack.rb"
190
261
  end
191
262
 
192
263
  def install_willpaginate_renderer_for_bootstrap
@@ -194,19 +265,15 @@ class BeautifulScaffoldGenerator < Rails::Generators::Base
194
265
  end
195
266
 
196
267
  def routes
197
- routes_in_text = File.read("config/routes.rb")
198
-
199
- if not routes_in_text[/beautiful#dashboard/] and not routes_in_text[/beautiful#select_fields/] then
200
-
201
- myroute = <<EOF
202
- root :to => 'beautiful#dashboard'
203
- match ':model_sym/select_fields' => 'beautiful#select_fields', :via => [:get, :post]
268
+ myroute = <<EOF
269
+ root :to => 'beautiful#dashboard'
270
+ match ':model_sym/select_fields' => 'beautiful#select_fields', as: :select_fields, via: [:get, :post]
204
271
 
205
272
  concern :bs_routes do
206
273
  collection do
207
274
  post :batch
208
275
  get :treeview
209
- match :search_and_filter, :action => :index, :as => :search, :via => [:get, :post]
276
+ match :search_and_filter, action: :index, as: :search, via: [:get, :post]
210
277
  end
211
278
  member do
212
279
  post :treeview_update
@@ -216,16 +283,12 @@ root :to => 'beautiful#dashboard'
216
283
  # Add route with concerns: :bs_routes here # Do not remove
217
284
  EOF
218
285
 
219
- route(myroute)
220
- end
221
-
222
- search_namespace = namespace_alone + "/" if not namespace_alone.blank?
223
- search_namespace ||= ""
286
+ inject_into_file("config/routes.rb", myroute, :after => "routes.draw do\n")
224
287
 
225
288
  myroute = "\n "
226
- myroute += "namespace :#{namespace_alone} do\n " if not namespace_alone.blank?
289
+ myroute += "namespace :#{namespace_alone} do\n " if !namespace_alone.blank?
227
290
  myroute += "resources :#{model_pluralize}, concerns: :bs_routes\n "
228
- myroute += "end\n" if not namespace_alone.blank?
291
+ myroute += "end\n" if !namespace_alone.blank?
229
292
 
230
293
  inject_into_file("config/routes.rb", myroute, :after => ":bs_routes here # Do not remove")
231
294
  end
@@ -0,0 +1,137 @@
1
+ # encoding : utf-8
2
+ class BeautifulSorceryGenerator < Rails::Generators::Base
3
+ require_relative 'beautiful_scaffold_common_methods'
4
+ include BeautifulScaffoldCommonMethods
5
+
6
+ source_root File.expand_path('../templates', __FILE__)
7
+
8
+ #argument :model, :type => :string, :desc => "Name of model (ex: User)"
9
+
10
+ def install_sorcery
11
+ model = "User"
12
+ view_path = "app/views/"
13
+
14
+ if !File.read('Gemfile').include?("sorcery")
15
+ gem("sorcery", "0.16.0")
16
+ end
17
+
18
+ Bundler.with_unbundled_env do
19
+ run "bundle install"
20
+ end
21
+
22
+ raise "Model must be specified" if model.blank?
23
+
24
+ # Install sorcery
25
+ generate("sorcery:install", "remember_me reset_password user_activation brute_force_protection external --model #{model}")
26
+
27
+ # If exist users migration just add columns
28
+ create_user_migration = Dir.glob("db/migrate/*create_users.rb").first
29
+ if create_user_migration
30
+ already_email = File.read(create_user_migration).include?(":email")
31
+ sorcery_core_file = Dir.glob("db/migrate/*sorcery_core.rb").first
32
+ File.open(sorcery_core_file, "w+") do |f|
33
+ f.write("class SorceryCore < ActiveRecord::Migration[6.1]
34
+ def change
35
+ #{(already_email ? '' : 'add_column :users, :email, :string')}
36
+ add_column :users, :crypted_password, :string
37
+ add_column :users, :salt, :string
38
+
39
+ #{(already_email ? '' : 'add_index :users, :email, unique: true')}
40
+ end
41
+ end")
42
+ end
43
+ end
44
+
45
+ # Generate mailer
46
+ copy_file("app/mailers/user_mailer.rb")
47
+
48
+ # Install controllers
49
+ copy_file("app/controllers/user_sessions_controller.rb")
50
+
51
+ # ===== Controller
52
+ inject_into_file("app/controllers/users_controller.rb",
53
+ "\n
54
+ skip_before_action :require_login, only: [:new, :create, :activate]
55
+ \n", after: "< BeautifulController")
56
+
57
+ inject_into_file("app/controllers/users_controller.rb",
58
+ "def activate
59
+ if @user = User.load_from_activation_token(params[:id])
60
+ @user.activate!
61
+ redirect_to(login_path, :notice => 'User was successfully activated.')
62
+ else
63
+ not_authenticated
64
+ end
65
+ end\n\n ", before: "private")
66
+
67
+ # ====== Model
68
+ # Add password & password_confirmation in model
69
+ inject_into_file("app/models/user.rb",
70
+ "\n
71
+ validates :password, length: { minimum: 3 }, if: -> { new_record? || changes[:crypted_password] }
72
+ validates :password, confirmation: true, if: -> { new_record? || changes[:crypted_password] }
73
+ validates :password_confirmation, presence: true, if: -> { new_record? || changes[:crypted_password] }
74
+
75
+ before_update :setup_activation, if: -> { email_changed? }
76
+ after_update :send_activation_needed_email!, if: -> { previous_changes['email'].present? }\n",
77
+ after: "ApplicationRecord")
78
+
79
+ inject_into_file("app/models/user.rb", ":password,:password_confirmation,", :after => "def self.permitted_attributes\n return ")
80
+
81
+ # ====== Views
82
+ inject_into_file("app/views/users/_form.html.erb",
83
+ ' <div class="form-group">
84
+ <%= f.label :password, t(\'app.models.user.bs_attributes.password\', :default => \'password\').capitalize, :class => "control-label" %><br />
85
+ <%= f.password_field :password, :class => "form-control" %>
86
+ </div>
87
+ <div class="form-group">
88
+ <%= f.label :password_confirmation, t(\'app.models.user.bs_attributes.password_confirmation\', :default => \'password_confirmation\').capitalize, :class => "control-label" %><br />
89
+ <%= f.password_field :password_confirmation, :class => "form-control" %>
90
+ </div>', before: '<!-- Beautiful_scaffold - AddField - Do not remove -->')
91
+
92
+ # Install all views for login/logout
93
+ directory "app/views/login_logout", "app/views"
94
+
95
+ # Domain in action_mailer
96
+ for current_env in ['production', 'development', 'test']
97
+ inject_into_file("config/environments/#{current_env}.rb", " config.action_mailer.default_url_options = { :host => 'localhost:3000' }", :after => "Rails.application.configure do\n" )
98
+ end
99
+
100
+ # In model
101
+ #remove_file("app/models/user.rb") # remove generated by sorcery
102
+ #copy_file("app/models/user.rb") # copy BS version ;)
103
+
104
+ # Limited access
105
+ inject_into_file("app/controllers/beautiful_controller.rb",
106
+ "\n before_action :require_login, except: [:dashboard]\n",
107
+ :after => 'layout "beautiful_layout"' + "\n")
108
+
109
+ inject_into_file("config/initializers/sorcery.rb",
110
+ "\nuser.user_activation_mailer = UserMailer\n",
111
+ :after => "# user.user_activation_mailer =\n")
112
+
113
+ # Routes (session)
114
+ inject_into_file("config/routes.rb",
115
+ '
116
+ resources :user_sessions, only: [:create]
117
+ get "login" => "user_sessions#new", :as => :login
118
+ post "logout" => "user_sessions#destroy", :as => :logout' + "\n\n\n",
119
+ :after => "Rails.application.routes.draw do\n")
120
+
121
+ # Activate
122
+ inject_into_file("config/routes.rb", " do
123
+ member do
124
+ get :activate
125
+ end
126
+ end", after: 'resources :users, concerns: :bs_routes')
127
+
128
+ copy_file("#{view_path}partials/_login_logout_register.html.erb", "#{view_path}layouts/_login_logout_register.html.erb")
129
+
130
+ # Sign in sign out
131
+ inject_into_file("#{view_path}layouts/beautiful_layout.html.erb",
132
+ "\n<%= render :partial => 'layouts/login_logout_register' %>\n",
133
+ :after => "<!-- Beautiful_scaffold - Signin - Do not remove -->")
134
+
135
+ say "Beautiful-Scaffold enable 'user_activation' sorcery module for you, so when you sign up, find in logs the activation link. You can't sign in yourself until you activate the account"
136
+ end
137
+ end