scaffold_plus 1.2.1 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 50d302dc3f95b88359a6225eecd90c2b42e2b1a1
4
- data.tar.gz: 84209f42800021b06ef49179f759976c885b477a
3
+ metadata.gz: a08fcefd45e286fd8445802b84da15d435af04e8
4
+ data.tar.gz: 07646c0fcff9389f982d2b2b5c3972e43715bdae
5
5
  SHA512:
6
- metadata.gz: ef3389982dd0ffc31a98b44dc697986e26b7c4c1360f4b402065e9fc6f0e54a7b7c546922f0921ca55ac0550fd2dc66cbb8d6650d8104d2d221fbe2528532fc8
7
- data.tar.gz: 0f244a6fc9a876c2573b7a4421a41e7052c2101044469e3da5b4ed266a78573ff20dc9b4eccb644c3ffeb83cd53bc2d6a53efc3a71b077145b9e6ffb72ceecc7
6
+ metadata.gz: 2bc399c4a5be223a17ec05c73d750831c2648ed236539aefa70a10c5d878c794eeb9cd2be86e54e184979f878b19ad8ef443121b71a93406007fbb850bda7fb6
7
+ data.tar.gz: 82b65abe4d9d91a9c5890949608fd822833129a57bd586d784461f95e751c525c82281f4c89ae86115f7c3ae4939b2d9a90f02daef0ce20a279bc18fde14294d
data/README.md CHANGED
@@ -34,6 +34,16 @@ to a newly created resource route.
34
34
  ### Add ancestry to create a tree structure (or hierarchy)
35
35
  rails generate scaffold_plus:ancestry
36
36
 
37
+ This helper adds has_ancestry to the model and updates the mass assignment
38
+ whitelist in the controller. It can also add a migration.
39
+
40
+ ### Add many-to-many association with intermediate join table
41
+ rails generate scaffold_plus:many_to_many
42
+
43
+ This helper creates a join table and updates the two parent resources.
44
+ It can handle additional attributes in the join table incl. whitelisting
45
+ and accepts_nested_attributes_for in one of the parents.
46
+
37
47
  ## Testing
38
48
 
39
49
  Since I have no experience with test driven development (yet), this is
@@ -41,9 +41,9 @@ module ScaffoldPlus
41
41
  text = before_array.include?(name) ? "\n" : ""
42
42
  text << " has_many :#{children}"
43
43
  text << ", inverse_of: :#{name}" if options.inverse?
44
- text << ", dependent: :#{dependent}" if options[:dependent]
44
+ text << ", dependent: :#{dependent}" if options[:dependent].present?
45
45
  text << "\n"
46
- text << " accepts_nested_attributes_for :#{children}\n" if options[:nested]
46
+ text << " accepts_nested_attributes_for :#{children}\n" if options[:nested].present?
47
47
  text << "\n" if after_array.include?(name)
48
48
  text
49
49
  end
@@ -61,7 +61,7 @@ module ScaffoldPlus
61
61
  end
62
62
 
63
63
  def add_to_permit
64
- return unless options[:nested]
64
+ return unless options[:nested].present?
65
65
  list = options[:nested].map{|n| ":#{n}"}.join(', ')
66
66
  text = "#{children}_attributes: [ #{list} ]"
67
67
  file = "app/controllers/#{table_name}_controller.rb"
@@ -81,7 +81,7 @@ module ScaffoldPlus
81
81
  end
82
82
 
83
83
  def dependent
84
- if options[:dependent] && options[:dependent] == "restrict"
84
+ if options[:dependent].present? && options[:dependent] == "restrict"
85
85
  "restrict_with_exception"
86
86
  else
87
87
  options[:dependent]
@@ -0,0 +1,110 @@
1
+ require 'rails/generators/active_record'
2
+
3
+ module ScaffoldPlus
4
+ module Generators
5
+ class ManyToManyGenerator < ActiveRecord::Generators::Base
6
+ desc "Add many-to-many association with intermediate join table"
7
+ argument :name, type: :string,
8
+ desc: "The name of the intermediate table"
9
+ argument :one, type: :string,
10
+ desc: "The first object that has_many TWO objects through NAME"
11
+ argument :two, type: :string,
12
+ desc: "The second object that has_many ONE objects through NAME"
13
+ class_option :attributes, type: :array, banner: 'name:type [...]',
14
+ desc: 'Additional attributes for the join table'
15
+ class_option :dependent, type: :string, banner: 'ACTION',
16
+ desc: 'Can be destroy, delete, or restrict'
17
+ class_option :nested, type: :array, banner: 'attribute [...]',
18
+ desc: 'Add accepts_nested_attributes_for to ONE incl. whitelisting'
19
+ class_option :counter, type: :boolean, default: false,
20
+ desc: 'Add counter_cache to ONE (and only to ONE)'
21
+ class_option :before, type: :array,
22
+ desc: 'Add a line before generated text in models'
23
+ class_option :after, type: :array,
24
+ desc: 'Add a line after generated text in models'
25
+ class_option :migration, type: :boolean, default: true,
26
+ desc: 'Create a migration for the join table'
27
+ class_option :index, type: :boolean, default: true,
28
+ desc: 'Add an index to the migration'
29
+ source_root File.expand_path('../templates', __FILE__)
30
+
31
+ def add_migration
32
+ return unless options.migration?
33
+ migration_template 'many_to_many_migration.rb', "db/migrate/#{migration_name}.rb"
34
+ end
35
+
36
+ def add_counter
37
+ return unless options.counter?
38
+ migration_template 'counter_migration.rb', "db/migrate/#{counter_migration}.rb"
39
+ end
40
+
41
+ def add_to_models
42
+ [[one, two], [two, one]].each do |pair|
43
+ current, partner = pair
44
+ inject_into_class "app/models/#{current}.rb", current.camelize do
45
+ text = before_array.include?(current) ? "\n" : ""
46
+ text << " has_many :#{table_name}"
47
+ text << ", dependent: :#{dependent}" if options[:dependent].present?
48
+ text << "\n"
49
+ text << " has_many :#{partner.pluralize}, through: :#{table_name}\n"
50
+ if current == one
51
+ text << " accepts_nested_attributes_for :#{table_name}\n" if options[:nested].present?
52
+ end
53
+ text << "\n" if after_array.include?(current)
54
+ text
55
+ end
56
+ end
57
+
58
+ template 'many_to_many_model.rb', "app/models/#{name}.rb"
59
+ end
60
+
61
+ def add_to_permit
62
+ return unless options[:nested].present?
63
+ list = options[:nested].map{|n| ":#{n}"}.join(', ')
64
+ text = "#{table_name}_attributes: [ #{list} ]"
65
+ file = "app/controllers/#{one.pluralize}_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].present? && options[:dependent] == "restrict"
83
+ "restrict_with_exception"
84
+ else
85
+ options[:dependent]
86
+ end
87
+ end
88
+
89
+ def migration_name
90
+ "create_#{table_name}"
91
+ end
92
+
93
+ def create_index?
94
+ options.index?
95
+ end
96
+
97
+ def counter_migration
98
+ "add_#{table_name}_count_to_#{one}"
99
+ end
100
+
101
+ def counter_cache
102
+ options.counter? ? ", counter_cache: true" : ""
103
+ end
104
+
105
+ def attributes
106
+ options[:attributes] || []
107
+ end
108
+ end
109
+ end
110
+ end
@@ -0,0 +1,5 @@
1
+ class <%= counter_migration.camelize %> < ActiveRecord::Migration
2
+ def change
3
+ add_column :<%= one.pluralize %>, :<%= table_name %>_count, :integer
4
+ end
5
+ end
@@ -0,0 +1,17 @@
1
+ class <%= migration_name.camelize %> < ActiveRecord::Migration
2
+ def change
3
+ create_table :<%= table_name %> do |t|
4
+ t.belongs_to :<%= one %>
5
+ t.belongs_to :<%= two %>
6
+ <%- attributes.each do |attribute| -%>
7
+ t.<%= attribute.split(':').second %> :<%= attribute.split(':').first %>
8
+ <%- end -%>
9
+
10
+ t.timestamps
11
+ end
12
+ <%- if create_index? -%>
13
+ add_index :<%= table_name %>, :<%= one %>_id
14
+ add_index :<%= table_name %>, :<%= two %>_id
15
+ <%- end -%>
16
+ end
17
+ end
@@ -0,0 +1,4 @@
1
+ class <%= class_name %> < ActiveRecord::Base
2
+ belongs_to :<%= one %><%= counter_cache %>
3
+ belongs_to :<%= two %>
4
+ end
@@ -1,3 +1,3 @@
1
1
  module ScaffoldPlus
2
- VERSION = "1.2.1"
2
+ VERSION = "1.3.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.2.1
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Volker Wiegand
@@ -86,6 +86,10 @@ files:
86
86
  - lib/generators/scaffold_plus/has_many/has_many_generator.rb
87
87
  - lib/generators/scaffold_plus/has_many/templates/child_migration.rb
88
88
  - lib/generators/scaffold_plus/has_many/templates/counter_migration.rb
89
+ - lib/generators/scaffold_plus/many_to_many/many_to_many_generator.rb
90
+ - lib/generators/scaffold_plus/many_to_many/templates/counter_migration.rb
91
+ - lib/generators/scaffold_plus/many_to_many/templates/many_to_many_migration.rb
92
+ - lib/generators/scaffold_plus/many_to_many/templates/many_to_many_model.rb
89
93
  - lib/scaffold_plus.rb
90
94
  - lib/scaffold_plus/version.rb
91
95
  - scaffold_plus.gemspec