scaffold_plus 1.3.0 → 1.4.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: a08fcefd45e286fd8445802b84da15d435af04e8
4
- data.tar.gz: 07646c0fcff9389f982d2b2b5c3972e43715bdae
3
+ metadata.gz: c5b490cd1736e29f658be2225acfb7acf13d583d
4
+ data.tar.gz: 65a025ea73ecce6f5a7ec16b745ed5a903d8e694
5
5
  SHA512:
6
- metadata.gz: 2bc399c4a5be223a17ec05c73d750831c2648ed236539aefa70a10c5d878c794eeb9cd2be86e54e184979f878b19ad8ef443121b71a93406007fbb850bda7fb6
7
- data.tar.gz: 82b65abe4d9d91a9c5890949608fd822833129a57bd586d784461f95e751c525c82281f4c89ae86115f7c3ae4939b2d9a90f02daef0ce20a279bc18fde14294d
6
+ metadata.gz: 1d45b528fc3807500c43485658ed1510bc97daef86d980fdf6d7af2d8604306d80867ccbd3dc80338afa70a2680b034e25f341e4f50be949a5e5fc1345fe0f0b
7
+ data.tar.gz: 73f83b76bacaf97b7248a5056735d928b8633ebae1649171ebc60cfe81a8a5924b7afef2e58d75a909288c2bd8a19f06d7e7d385c30db83f033fa26eb55d5e46
data/README.md CHANGED
@@ -44,6 +44,12 @@ This helper creates a join table and updates the two parent resources.
44
44
  It can handle additional attributes in the join table incl. whitelisting
45
45
  and accepts_nested_attributes_for in one of the parents.
46
46
 
47
+ ### Add many-to-many association with has_and_belongs_to_many
48
+ rails generate scaffold_plus:habtm
49
+
50
+ This helper scaffolds a has_and_belongs_to_many relationship with migration
51
+ and updates to the models.
52
+
47
53
  ## Testing
48
54
 
49
55
  Since I have no experience with test driven development (yet), this is
@@ -0,0 +1,77 @@
1
+ require 'rails/generators/active_record'
2
+
3
+ module ScaffoldPlus
4
+ module Generators
5
+ class HabtmGenerator < ActiveRecord::Generators::Base
6
+ desc "Add many-to-many association with has_and_belongs_to_many"
7
+ argument :name, type: :string,
8
+ desc: "The first object that has_and_belongs_to_many OTHERs"
9
+ argument :other, type: :string,
10
+ desc: "The second object that has_and_belongs_to_many NAMEs"
11
+ class_option :migration, type: :boolean, default: true,
12
+ desc: 'Create a migration for the join table'
13
+ class_option :permit, type: :boolean, default: true,
14
+ desc: 'Allow mass assignment for added attributes in NAME'
15
+ class_option :before, type: :array,
16
+ desc: 'Add a line before generated text in models'
17
+ class_option :after, type: :array,
18
+ desc: 'Add a line after generated text in models'
19
+ source_root File.expand_path('../templates', __FILE__)
20
+
21
+ def add_migration
22
+ return unless options.migration?
23
+ migration_template "habtm_migration.rb", "db/migrate/#{habtm_migration_name}.rb"
24
+ end
25
+
26
+ def add_to_models
27
+ inject_into_class "app/models/#{name}.rb", class_name do
28
+ text = before_array.include?(name) ? "\n" : ""
29
+ text << " has_and_belongs_to_many :#{other.pluralize}\n"
30
+ text << "\n" if after_array.include?(name)
31
+ text
32
+ end
33
+ inject_into_class "app/models/#{other}.rb", other.camelize do
34
+ text = before_array.include?(other) ? "\n" : ""
35
+ text << " has_and_belongs_to_many :#{table_name}\n"
36
+ text << "\n" if after_array.include?(other)
37
+ text
38
+ end
39
+ end
40
+
41
+ def add_to_permit
42
+ return unless options.permit?
43
+ text = "{ :#{other}_ids => [] }"
44
+ file = "app/controllers/#{table_name}_controller.rb"
45
+ gsub_file file, /(permit\(.*)\)/, "\\1, #{text})"
46
+ # Special case: no previous permit
47
+ gsub_file file, /^(\s*params)\[:#{name}\]$/, "\\1.require(:#{name}).permit(#{text})"
48
+ end
49
+
50
+ protected
51
+
52
+ def before_array
53
+ options['before'] || []
54
+ end
55
+
56
+ def after_array
57
+ options['after'] || []
58
+ end
59
+
60
+ def sorted_sg
61
+ [name, other].sort
62
+ end
63
+
64
+ def sorted_pl
65
+ [table_name, other.pluralize].sort
66
+ end
67
+
68
+ def habtm_migration_name
69
+ "create_#{sorted_pl.first}_and_#{sorted_pl.second}"
70
+ end
71
+
72
+ def habtm_table_name
73
+ "#{sorted_pl.first}_#{sorted_pl.second}"
74
+ end
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,10 @@
1
+ class <%= habtm_migration_name.camelize %> < ActiveRecord::Migration
2
+ def change
3
+ create_table :<%= habtm_table_name %>, id: false do |t|
4
+ t.belongs_to :<%= sorted_sg[0] %>
5
+ t.belongs_to :<%= sorted_sg[1] %>
6
+ end
7
+ add_index :<%= habtm_table_name %>, :<%= sorted_sg[0] %>_id
8
+ add_index :<%= habtm_table_name %>, :<%= sorted_sg[1] %>_id
9
+ end
10
+ end
@@ -1,3 +1,3 @@
1
1
  module ScaffoldPlus
2
- VERSION = "1.3.0"
2
+ VERSION = "1.4.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.3.0
4
+ version: 1.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Volker Wiegand
@@ -83,6 +83,8 @@ files:
83
83
  - lib/generators/scaffold_plus/ancestry/templates/ancestry_migration.rb
84
84
  - lib/generators/scaffold_plus/collection/collection_generator.rb
85
85
  - lib/generators/scaffold_plus/collection/templates/view.html.erb
86
+ - lib/generators/scaffold_plus/habtm/habtm_generator.rb
87
+ - lib/generators/scaffold_plus/habtm/templates/habtm_migration.rb
86
88
  - lib/generators/scaffold_plus/has_many/has_many_generator.rb
87
89
  - lib/generators/scaffold_plus/has_many/templates/child_migration.rb
88
90
  - lib/generators/scaffold_plus/has_many/templates/counter_migration.rb