scaffold_plus 1.8.0 → 1.9.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5b36d5e911b460ba3b91084f076a0c9905aab19d
4
- data.tar.gz: fdcefa09351b7cfac5a43e460ea62f38c5626bb8
3
+ metadata.gz: 784dd7153e90d8309d4fff348da8592ea4a95e7d
4
+ data.tar.gz: c177a2e5941f52aecf4e141089f93da22b212de0
5
5
  SHA512:
6
- metadata.gz: 3a7a18ef9e70632da0d2bdf1c3698ee20a0579f867c5e7a59d752a8021638e2a42e4622bba1848237be31a754f1671f9950bb590ffa09f5b27fe538b260c6e13
7
- data.tar.gz: 5f14a2d39d0e5c38d0d94adf7c34bb6f6c58f596ea3d5fe308a18fcd2b9d0b1fd5fe5dae91ebb91541f982bd858812b15ebbcbfa581f4988a07933922cf16f7c
6
+ metadata.gz: fc2de205ec80b1a11dfc489ed615f6cb40b3a62c0b54831749b8ffe5314fcbba5984c12617489614789b308a2ee759383e9a71562651cde39ff03a8ac1ad854d
7
+ data.tar.gz: 6a74fa0d56e659817da2fbb5b5fbf882bb51ba5ac9b0e76368ac23730ddef82cdbd20f4e50b5bde30b4e0251afa759bf7b486fc04d9c7c03297a7ef6d00dd05c
data/README.md CHANGED
@@ -27,6 +27,13 @@ Or install it yourself as:
27
27
 
28
28
  This helper creates migrations for some 'ALTER TABLE' statements.
29
29
 
30
+ ### Add support for an enum field (requires Rails 4.1 and the enum_help Gem)
31
+ rails generate scaffold_plus:enum
32
+
33
+ This helper adds parent#has_many and child#belongs_to to the models
34
+ and updates the mass assignment whitelist in the controller.
35
+ It can also add a migration for the parent_id and a counter.
36
+
30
37
  ### Add regular one-to-many association (has_many / belongs_to)
31
38
  rails generate scaffold_plus:has_many
32
39
 
@@ -0,0 +1,56 @@
1
+ require 'rails/generators/active_record'
2
+
3
+ module ScaffoldPlus
4
+ module Generators
5
+ class EnumGenerator < ActiveRecord::Generators::Base
6
+ desc "Add an enum field to a resource (requires enum_help gem)"
7
+ argument :name, type: :string,
8
+ desc: "The object that will have the enum field"
9
+ argument :column, type: :string,
10
+ desc: "The column to be used as enum field"
11
+ argument :values, type: :array, banner: "VALUE [...]",
12
+ desc: "Values (counting from 0, first is default)"
13
+ class_option :migration, type: :boolean, default: false,
14
+ desc: 'Create a migration for added attributes'
15
+ class_option :index, type: :boolean, default: true,
16
+ desc: 'Add an index to the migration'
17
+ class_option :permit, type: :boolean, default: false,
18
+ desc: 'Allow mass assignment for added attributes'
19
+ class_option :before, type: :boolean, default: false,
20
+ desc: 'Add a line before generated text in model'
21
+ class_option :after, type: :boolean, default: false,
22
+ desc: 'Add a line after generated text in model'
23
+ source_root File.expand_path('../templates', __FILE__)
24
+
25
+ def add_migration
26
+ return unless options.migration?
27
+ migration_template "enum_migration.rb", "db/migrate/#{migration_name}.rb"
28
+ end
29
+
30
+ def update_model
31
+ inject_into_class "app/models/#{name}.rb", class_name do
32
+ list = values.map{|v| ":#{v}"}.join(', ')
33
+ text = options.before? ? "\n" : ""
34
+ text << " enum #{column}: [ #{list} ]\n"
35
+ text << "\n" if options.after?
36
+ text
37
+ end
38
+ end
39
+
40
+ def update_controller
41
+ return unless options.permit?
42
+ text = ":#{column}"
43
+ file = "app/controllers/#{table_name}_controller.rb"
44
+ gsub_file file, /(permit\(.*)\)/, "\\1, #{text})"
45
+ # Special case: no previous permit
46
+ gsub_file file, /^(\s*params)\[:#{name}\]$/, "\\1.require(:#{name}).permit(#{text})"
47
+ end
48
+
49
+ protected
50
+
51
+ def migration_name
52
+ "add_ancestry_to_#{table_name}"
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,8 @@
1
+ class <%= migration_name.camelize %> < ActiveRecord::Migration
2
+ def change
3
+ add_column :<%= table_name %>, :<%= column %>, :integer, default: 0
4
+ <%- if options.index? -%>
5
+ add_index :<%= table_name %>, :<%= column %>
6
+ <%- end -%>
7
+ end
8
+ end
@@ -10,6 +10,8 @@ module ScaffoldPlus
10
10
  desc: "The child resources that belongs_to the NAME object"
11
11
  class_option :dependent, type: :string, banner: 'ACTION',
12
12
  desc: 'Can be destroy, delete, or restrict'
13
+ class_option :permit, type: :boolean, default: false,
14
+ desc: 'Allow mass assignment for added attributes'
13
15
  class_option :nested, type: :array, banner: 'attribute [...]',
14
16
  desc: 'Add accepts_nested_attributes_for (incl. whitelisting)'
15
17
  class_option :route, type: :boolean, default: false,
@@ -79,7 +81,7 @@ module ScaffoldPlus
79
81
  end
80
82
  end
81
83
 
82
- def add_to_permit
84
+ def update_parent_controller
83
85
  return if options.nested.blank?
84
86
  list = options.nested.map{|n| ":#{n}"}.join(', ')
85
87
  text = "#{children}_attributes: [ #{list} ]"
@@ -89,7 +91,16 @@ module ScaffoldPlus
89
91
  gsub_file file, /^(\s*params)\[:#{name}\]$/, "\\1.require(:#{name}).permit(#{text})"
90
92
  end
91
93
 
92
- def update_child_controller_and_view
94
+ def update_child_controller
95
+ return unless options.permit?
96
+ text = ":#{name}_id"
97
+ file = "app/controllers/#{children}_controller.rb"
98
+ gsub_file file, /(permit\(.*)\)/, "\\1, #{text})"
99
+ # Special case: no previous permit
100
+ gsub_file file, /^(\s*params)\[:#{name}\]$/, "\\1.require(:#{name}).permit(#{text})"
101
+ end
102
+
103
+ def update_nested_resource
93
104
  return unless options.route?
94
105
  child = children.singularize
95
106
  file = "app/controllers/#{children}_controller.rb"
@@ -10,8 +10,12 @@ module ScaffoldPlus
10
10
  desc: "The child resource that belongs_to the NAME object"
11
11
  class_option :dependent, type: :string, banner: 'ACTION',
12
12
  desc: 'Can be destroy, delete, or restrict'
13
+ class_option :permit, type: :boolean, default: false,
14
+ desc: 'Allow mass assignment for added attributes'
13
15
  class_option :nested, type: :array, banner: 'attribute [...]',
14
16
  desc: 'Add accepts_nested_attributes_for (incl. whitelisting)'
17
+ class_option :route, type: :boolean, default: false,
18
+ desc: 'Add a nested route and update CHILD controller'
15
19
  class_option :foreign_key, type: :string,
16
20
  desc: 'Set the name of the foreign key directly'
17
21
  class_option :inverse, type: :boolean, default: false,
@@ -31,6 +35,16 @@ module ScaffoldPlus
31
35
  migration_template 'child_migration.rb', "db/migrate/#{migration_name}.rb"
32
36
  end
33
37
 
38
+ def add_to_route
39
+ return unless options.route?
40
+ gsub_file "config/routes.rb", /^ resources :#{table_name} do$/ do |match|
41
+ match << "\n resources :#{child.pluralize}"
42
+ end
43
+ gsub_file "config/routes.rb", /^ resources :#{table_name}$/ do |match|
44
+ match << " do\n resources :#{child.pluralize}\n end"
45
+ end
46
+ end
47
+
34
48
  def add_to_models
35
49
  inject_into_class "app/models/#{name}.rb", class_name do
36
50
  text = before_array.include?(name) ? "\n" : ""
@@ -58,16 +72,42 @@ module ScaffoldPlus
58
72
  end
59
73
  end
60
74
 
61
- def add_to_permit
62
- return unless options.nested
75
+ def update_parent_controller
76
+ return if options.nested.blank?
63
77
  list = options.nested.map{|n| ":#{n}"}.join(', ')
64
- text = ":#{child}, #{child}_attributes: [ #{list} ]"
78
+ text = "#{child}_attributes: [ #{list} ]"
65
79
  file = "app/controllers/#{table_name}_controller.rb"
66
80
  gsub_file file, /(permit\(.*)\)/, "\\1, #{text})"
67
81
  # Special case: no previous permit
68
82
  gsub_file file, /^(\s*params)\[:#{name}\]$/, "\\1.require(:#{name}).permit(#{text})"
69
83
  end
70
84
 
85
+ def update_child_controller
86
+ return unless options.permit?
87
+ text = ":#{name}_id"
88
+ file = "app/controllers/#{child.pluralize}_controller.rb"
89
+ gsub_file file, /(permit\(.*)\)/, "\\1, #{text})"
90
+ # Special case: no previous permit
91
+ gsub_file file, /^(\s*params)\[:#{name}\]$/, "\\1.require(:#{name}).permit(#{text})"
92
+ end
93
+
94
+ def update_nested_resource
95
+ return unless options.route?
96
+ children = child.pluralize
97
+ file = "app/controllers/#{children}_controller.rb"
98
+ gsub_file file, /GET .#{children}.new$/ do |match|
99
+ match = "GET /#{table_name}/:id/#{children}/new"
100
+ end
101
+ gsub_file file, /^ @#{child} = #{child.camelize}.new$/ do |match|
102
+ match = " @#{name} = #{class_name}.find(params[:#{name}_id])\n" +
103
+ " @#{child} = @#{name}.#{children}.build"
104
+ end
105
+ file = "app/views/#{children}/_form.html.erb"
106
+ gsub_file file, /form_for\(@#{child}/ do |match|
107
+ match = "form_for([@#{name}, @#{child}]"
108
+ end
109
+ end
110
+
71
111
  protected
72
112
 
73
113
  def before_array
@@ -1,3 +1,3 @@
1
1
  module ScaffoldPlus
2
- VERSION = "1.8.0"
2
+ VERSION = "1.9.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: scaffold_plus
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.8.0
4
+ version: 1.9.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Volker Wiegand
@@ -84,6 +84,8 @@ files:
84
84
  - lib/generators/scaffold_plus/autofocus/autofocus_generator.rb
85
85
  - lib/generators/scaffold_plus/collection/collection_generator.rb
86
86
  - lib/generators/scaffold_plus/collection/templates/view.html.erb
87
+ - lib/generators/scaffold_plus/enum/enum_generator.rb
88
+ - lib/generators/scaffold_plus/enum/templates/enum_migration.rb
87
89
  - lib/generators/scaffold_plus/force_ssl/force_ssl_generator.rb
88
90
  - lib/generators/scaffold_plus/friendly_id/friendly_id_generator.rb
89
91
  - lib/generators/scaffold_plus/friendly_id/templates/friendly_id_migration.rb