scaffold_plus 1.9.1 → 1.9.2
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 +4 -4
- data/lib/generators/scaffold_plus/has_many/has_many_generator.rb +2 -2
- data/lib/generators/scaffold_plus/many_to_many/many_to_many_generator.rb +33 -30
- data/lib/generators/scaffold_plus/many_to_many/templates/many_to_many_migration.rb +11 -4
- data/lib/scaffold_plus/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3de785ac42f73968a486b8d7157760326b85bfb5
|
4
|
+
data.tar.gz: 01ef894b1cec18b39c4b2bc89c706e5f3a6dedce
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 69155f0155898a034bf851f35d446a2821a82c5efe3c97ed3d7c1e6e612469abe2f9c11ec7c8b65cdb87f3804ce42c00ee4805e87ee26f4cd4db196f904b749a
|
7
|
+
data.tar.gz: b8c550856d29d22da4ce436ed9bbe286a731a7c49a110796e6599e6a3b199fdfc82a5e10e8efdfd17fbb2a30556ee7bcec3501922a65f7700eada8101c9fb8fe
|
@@ -10,8 +10,8 @@ module ScaffoldPlus
|
|
10
10
|
desc: "The first object that has_many TWO objects through NAME"
|
11
11
|
argument :two, type: :string,
|
12
12
|
desc: "The second object that has_many ONE objects through NAME"
|
13
|
-
class_option :
|
14
|
-
desc: '
|
13
|
+
class_option :add_attr, type: :array, banner: "FIELD[:TYPE][:INDEX] ...",
|
14
|
+
desc: 'Setup additional attributes for join table'
|
15
15
|
class_option :dependent, type: :string, banner: 'ACTION',
|
16
16
|
desc: 'Can be destroy, delete, or restrict'
|
17
17
|
class_option :nested, type: :array, banner: 'attribute [...]',
|
@@ -27,84 +27,87 @@ module ScaffoldPlus
|
|
27
27
|
class_option :index, type: :boolean, default: true,
|
28
28
|
desc: 'Add an index to the migration'
|
29
29
|
source_root File.expand_path('../templates', __FILE__)
|
30
|
-
|
30
|
+
|
31
31
|
def add_migration
|
32
32
|
return unless options.migration?
|
33
33
|
migration_template 'many_to_many_migration.rb', "db/migrate/#{migration_name}.rb"
|
34
34
|
end
|
35
|
-
|
35
|
+
|
36
36
|
def add_counter
|
37
37
|
return unless options.counter?
|
38
38
|
migration_template 'counter_migration.rb', "db/migrate/#{counter_migration}.rb"
|
39
39
|
end
|
40
|
-
|
40
|
+
|
41
41
|
def add_to_models
|
42
42
|
[[one, two], [two, one]].each do |pair|
|
43
43
|
current, partner = pair
|
44
44
|
inject_into_class "app/models/#{current}.rb", current.camelize do
|
45
45
|
text = before_array.include?(current) ? "\n" : ""
|
46
46
|
text << " has_many :#{table_name}"
|
47
|
-
text << ", dependent: :#{dependent}" if options
|
47
|
+
text << ", dependent: :#{dependent}" if options.dependent.present?
|
48
48
|
text << "\n"
|
49
49
|
text << " has_many :#{partner.pluralize}, through: :#{table_name}\n"
|
50
50
|
if current == one
|
51
|
-
text << " accepts_nested_attributes_for :#{table_name}\n" if options
|
51
|
+
text << " accepts_nested_attributes_for :#{table_name}\n" if options.nested.present?
|
52
52
|
end
|
53
53
|
text << "\n" if after_array.include?(current)
|
54
54
|
text
|
55
55
|
end
|
56
56
|
end
|
57
|
-
|
57
|
+
|
58
58
|
template 'many_to_many_model.rb', "app/models/#{name}.rb"
|
59
59
|
end
|
60
|
-
|
60
|
+
|
61
61
|
def add_to_permit
|
62
|
-
return unless options
|
63
|
-
list = options
|
62
|
+
return unless options.nested.present?
|
63
|
+
list = options.nested.map{|n| ":#{n}"}.join(', ')
|
64
64
|
text = "#{table_name}_attributes: [ #{list} ]"
|
65
65
|
file = "app/controllers/#{one.pluralize}_controller.rb"
|
66
66
|
gsub_file file, /(permit\(.*)\)/, "\\1, #{text})"
|
67
67
|
# Special case: no previous permit
|
68
68
|
gsub_file file, /^(\s*params)\[:#{name}\]$/, "\\1.require(:#{name}).permit(#{text})"
|
69
69
|
end
|
70
|
-
|
70
|
+
|
71
71
|
protected
|
72
|
-
|
72
|
+
|
73
|
+
def added_fields
|
74
|
+
list = options.add_attr || []
|
75
|
+
array = []
|
76
|
+
list.each do |entry|
|
77
|
+
name, type, index = entry.split(':')
|
78
|
+
type, index = ["string", type] if %w(index uniq).include? type
|
79
|
+
array << [name, type || "string", index]
|
80
|
+
end
|
81
|
+
array
|
82
|
+
end
|
83
|
+
|
73
84
|
def before_array
|
74
|
-
options
|
85
|
+
options.before || []
|
75
86
|
end
|
76
|
-
|
87
|
+
|
77
88
|
def after_array
|
78
|
-
options
|
89
|
+
options.after || []
|
79
90
|
end
|
80
|
-
|
91
|
+
|
81
92
|
def dependent
|
82
|
-
if options
|
93
|
+
if options.dependent.present? and options.dependent == "restrict"
|
83
94
|
"restrict_with_exception"
|
84
95
|
else
|
85
|
-
options
|
96
|
+
options.dependent
|
86
97
|
end
|
87
98
|
end
|
88
|
-
|
99
|
+
|
89
100
|
def migration_name
|
90
101
|
"create_#{table_name}"
|
91
102
|
end
|
92
|
-
|
93
|
-
def create_index?
|
94
|
-
options.index?
|
95
|
-
end
|
96
|
-
|
103
|
+
|
97
104
|
def counter_migration
|
98
105
|
"add_#{table_name}_count_to_#{one}"
|
99
106
|
end
|
100
|
-
|
107
|
+
|
101
108
|
def counter_cache
|
102
109
|
options.counter? ? ", counter_cache: true" : ""
|
103
110
|
end
|
104
|
-
|
105
|
-
def attributes
|
106
|
-
options[:attributes] || []
|
107
|
-
end
|
108
111
|
end
|
109
112
|
end
|
110
113
|
end
|
@@ -3,15 +3,22 @@ class <%= migration_name.camelize %> < ActiveRecord::Migration
|
|
3
3
|
create_table :<%= table_name %> do |t|
|
4
4
|
t.belongs_to :<%= one %>
|
5
5
|
t.belongs_to :<%= two %>
|
6
|
-
<%-
|
7
|
-
t.<%=
|
8
|
-
<%- end -%>
|
6
|
+
<%- added_fields.each do |field| -%>
|
7
|
+
t.<%= field[1] %> :<%= field[0] %>
|
8
|
+
<%- end -%>
|
9
9
|
|
10
10
|
t.timestamps
|
11
11
|
end
|
12
|
-
<%- if
|
12
|
+
<%- if options.index? -%>
|
13
13
|
add_index :<%= table_name %>, :<%= one %>_id
|
14
14
|
add_index :<%= table_name %>, :<%= two %>_id
|
15
|
+
<%- added_fields.each do |field| -%>
|
16
|
+
<%- if field[2] == "uniq" -%>
|
17
|
+
add_index :<%= table_name %>, :<%= field[0] %>, unique: true
|
18
|
+
<%- elsif field[2] == "index" -%>
|
19
|
+
add_index :<%= table_name %>, :<%= field[0] %>
|
20
|
+
<%- end -%>
|
21
|
+
<%- end -%>
|
15
22
|
<%- end -%>
|
16
23
|
end
|
17
24
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: scaffold_plus
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.9.
|
4
|
+
version: 1.9.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Volker Wiegand
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-11-
|
11
|
+
date: 2014-11-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|