scaffold_plus 1.6.0 → 1.7.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: 8611eb567ad99bbb859e145a779ce56fa28e17d4
4
- data.tar.gz: ea3c09627c8a3343739170d9d2106eeb5dd57506
3
+ metadata.gz: caa0a9cb1b62f5b977a467ce59d8b762148cfeae
4
+ data.tar.gz: 08d78efa2e08a76bcf69e206c89e7a385f2860dd
5
5
  SHA512:
6
- metadata.gz: 71c6ea4131e975934d3f24530ce6033ab5b3ef99fa0893017cbe1a392581903067858a6193d32d2d5ba9e0389958195e604885555d84c658c8f61ea3291df800
7
- data.tar.gz: 6412c9b5cdf06fae2ff3fddabc9d1d2e9b6200876e200d8f8b886fcc0cab8acfc4d5ec78400a501333549c74cb44f41dc113c70eed55eae0f92737e7c5b57ca3
6
+ metadata.gz: b2b75fdf0d344f40a4922b7a99fa3586a2d700b5b1a37a3ccbe6828a79733ad22da659c6b65e17f7301387c870aa143178c2041c61d76aea578f9d2291e2edc9
7
+ data.tar.gz: e7e05931840aa773c96acd386f98d12f93f38ccc6582e0ceabab00adf2f0cfba76a31f33c86f3957a603bb0eb0172025ae13a2a6cfe833aef1026ae7c7ff9d3a
data/README.md CHANGED
@@ -25,6 +25,13 @@ This helper adds parent#has_many and child#belongs_to to the models
25
25
  and updates the mass assignment whitelist in the controller.
26
26
  It can also add a migration for the parent_id and a counter.
27
27
 
28
+ ### Add regular one-to-one association (has_one / belongs_to)
29
+ rails generate scaffold_plus:has_one
30
+
31
+ This helper adds parent#has_one and child#belongs_to to the models
32
+ and updates the mass assignment whitelist in the controller.
33
+ It can also add a migration for the parent_id.
34
+
28
35
  ### Add a collection to a resource route
29
36
  rails generate scaffold_plus:collection
30
37
 
@@ -0,0 +1,94 @@
1
+ require 'rails/generators/active_record'
2
+
3
+ module ScaffoldPlus
4
+ module Generators
5
+ class HasOneGenerator < ActiveRecord::Generators::Base
6
+ desc "Add regular one-to-one association (has_one / belongs_to)"
7
+ argument :name, type: :string,
8
+ desc: "The parent resource that has_one CHILD object"
9
+ argument :child, type: :string,
10
+ desc: "The child resource that belongs_to the NAME object"
11
+ class_option :dependent, type: :string, banner: 'ACTION',
12
+ desc: 'Can be destroy, delete, or restrict'
13
+ class_option :nested, type: :array, banner: 'attribute [...]',
14
+ desc: 'Add accepts_nested_attributes_for (incl. whitelisting)'
15
+ class_option :foreign_key, type: :string,
16
+ desc: 'Set the name of the foreign key directly'
17
+ class_option :inverse, type: :boolean, default: false,
18
+ desc: 'Add inverse_of to both models'
19
+ class_option :migration, type: :boolean, default: false,
20
+ desc: 'Create a migration for added attributes'
21
+ class_option :index, type: :boolean, default: true,
22
+ desc: 'Add an index to the child migration'
23
+ class_option :before, type: :array,
24
+ desc: 'Add a line before generated text in models'
25
+ class_option :after, type: :array,
26
+ desc: 'Add a line after generated text in models'
27
+ source_root File.expand_path('../templates', __FILE__)
28
+
29
+ def add_migration
30
+ return unless options.migration?
31
+ migration_template 'child_migration.rb', "db/migrate/#{migration_name}.rb"
32
+ end
33
+
34
+ def add_to_models
35
+ inject_into_class "app/models/#{name}.rb", class_name do
36
+ text = before_array.include?(name) ? "\n" : ""
37
+ text << " has_one :#{child}"
38
+ text << ", inverse_of: :#{name}" if options.inverse?
39
+ text << ", dependent: :#{dependent}" if options.dependent
40
+ text << "\n"
41
+ if options.nested
42
+ text << " accepts_nested_attributes_for :#{child}\n"
43
+ end
44
+ text << "\n" if after_array.include?(name)
45
+ text
46
+ end
47
+
48
+ inject_into_class "app/models/#{child}.rb", child.camelize do
49
+ text = before_array.include?(child) ? "\n" : ""
50
+ text << " belongs_to :#{name}"
51
+ if options.foreign_key
52
+ text << ", foreign_key: \"#{options.foreign_key}\""
53
+ end
54
+ text << ", inverse_of: :#{child}" if options.inverse?
55
+ text << "\n"
56
+ text << "\n" if after_array.include?(child)
57
+ text
58
+ end
59
+ end
60
+
61
+ def add_to_permit
62
+ return unless options.nested
63
+ list = options.nested.map{|n| ":#{n}"}.join(', ')
64
+ text = "#{child}_attributes: [ #{list} ]"
65
+ file = "app/controllers/#{table_name}_controller.rb"
66
+ gsub_file file, /(permit\(.*)\)/, "\\1, #{text})"
67
+ # Special case: no previous permit
68
+ gsub_file file, /^(\s*params)\[:#{name}\]$/, "\\1.require(:#{name}).permit(#{text})"
69
+ end
70
+
71
+ protected
72
+
73
+ def before_array
74
+ options.before || []
75
+ end
76
+
77
+ def after_array
78
+ options.after || []
79
+ end
80
+
81
+ def dependent
82
+ if options.dependent && options.dependent == "restrict"
83
+ "restrict_with_exception"
84
+ else
85
+ options.dependent
86
+ end
87
+ end
88
+
89
+ def migration_name
90
+ "add_#{name}_id_to_#{child}"
91
+ end
92
+ end
93
+ end
94
+ end
@@ -0,0 +1,8 @@
1
+ class <%= migration_name.camelize %> < ActiveRecord::Migration
2
+ def change
3
+ add_column :<%= child.pluralize %>, :<%= name %>_id, :integer
4
+ <%- if options.index? -%>
5
+ add_index :<%= child.pluralize %>, :<%= name %>_id
6
+ <%- end -%>
7
+ end
8
+ end
@@ -1,3 +1,3 @@
1
1
  module ScaffoldPlus
2
- VERSION = "1.6.0"
2
+ VERSION = "1.7.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.6.0
4
+ version: 1.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Volker Wiegand
@@ -95,6 +95,8 @@ files:
95
95
  - lib/generators/scaffold_plus/has_many/has_many_generator.rb
96
96
  - lib/generators/scaffold_plus/has_many/templates/child_migration.rb
97
97
  - lib/generators/scaffold_plus/has_many/templates/counter_migration.rb
98
+ - lib/generators/scaffold_plus/has_one/has_one_generator.rb
99
+ - lib/generators/scaffold_plus/has_one/templates/child_migration.rb
98
100
  - lib/generators/scaffold_plus/many_to_many/many_to_many_generator.rb
99
101
  - lib/generators/scaffold_plus/many_to_many/templates/counter_migration.rb
100
102
  - lib/generators/scaffold_plus/many_to_many/templates/many_to_many_migration.rb