scaffold_plus 1.9.1 → 1.9.2

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: 87cdca95970c67bcb7fffbb7624108b5d889e622
4
- data.tar.gz: 8eb87613b8b7cdb4ebbf73b9573359a069325e86
3
+ metadata.gz: 3de785ac42f73968a486b8d7157760326b85bfb5
4
+ data.tar.gz: 01ef894b1cec18b39c4b2bc89c706e5f3a6dedce
5
5
  SHA512:
6
- metadata.gz: 9965bd780a6f65704c4e6cd24a08c20a47e3179516e3b42b3e7b896c2a21114bcdf33ea239da4ec63fc349387365d0cb73ef8860a456e9930140da9c69689688
7
- data.tar.gz: 14316209cd999b6d5be0f8f43f10d812cb50f90cd333dae3099fcc03be6fdd2c73738e4eed11993d14e8d72d2760f71336e0ac74626d23188fb6c7a031a18d44
6
+ metadata.gz: 69155f0155898a034bf851f35d446a2821a82c5efe3c97ed3d7c1e6e612469abe2f9c11ec7c8b65cdb87f3804ce42c00ee4805e87ee26f4cd4db196f904b749a
7
+ data.tar.gz: b8c550856d29d22da4ce436ed9bbe286a731a7c49a110796e6599e6a3b199fdfc82a5e10e8efdfd17fbb2a30556ee7bcec3501922a65f7700eada8101c9fb8fe
@@ -120,11 +120,11 @@ module ScaffoldPlus
120
120
  protected
121
121
 
122
122
  def before_array
123
- options['before'] || []
123
+ options.before || []
124
124
  end
125
125
 
126
126
  def after_array
127
- options['after'] || []
127
+ options.after || []
128
128
  end
129
129
 
130
130
  def dependent
@@ -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 :attributes, type: :array, banner: 'name:type [...]',
14
- desc: 'Additional attributes for the join table'
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[:dependent].present?
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[:nested].present?
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[:nested].present?
63
- list = options[:nested].map{|n| ":#{n}"}.join(', ')
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['before'] || []
85
+ options.before || []
75
86
  end
76
-
87
+
77
88
  def after_array
78
- options['after'] || []
89
+ options.after || []
79
90
  end
80
-
91
+
81
92
  def dependent
82
- if options[:dependent].present? && options[:dependent] == "restrict"
93
+ if options.dependent.present? and options.dependent == "restrict"
83
94
  "restrict_with_exception"
84
95
  else
85
- options[:dependent]
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
- <%- attributes.each do |attribute| -%>
7
- t.<%= attribute.split(':').second %> :<%= attribute.split(':').first %>
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 create_index? -%>
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
@@ -1,3 +1,3 @@
1
1
  module ScaffoldPlus
2
- VERSION = "1.9.1"
2
+ VERSION = "1.9.2"
3
3
  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.1
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-05 00:00:00.000000000 Z
11
+ date: 2014-11-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord