effective_developer 0.6.3 → 0.6.4

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
  SHA256:
3
- metadata.gz: 7e76b8818691ec2238e08a818d209ba7e4d1746734ef3c7c2c2cbe8f4e08aa12
4
- data.tar.gz: a283fca85ffd1c0dc6d73287bd6c239c898e6b6e2aa8500ebc08642960f7ad5f
3
+ metadata.gz: 3d495b7c0e191545d03edac8297a4e6d70b0e73c60e3e750a7746a43a9c45ae0
4
+ data.tar.gz: fda29335818a87f2e76bd21c4f8018aab29833082682ca931339a127d7a9d1d0
5
5
  SHA512:
6
- metadata.gz: 0dc45f6f25c607353f5ea9651cda2054e2d6f2996fac603ebd37df05191d4a2d7981662e7e68681163856a566eeb452c4b0030623d130f2d33507bea36956548
7
- data.tar.gz: 8a4ec7eab0237e8f0ac37a80e1cb7ea3974514b8228a6f8711a9fa00b66e0d2cecb5b22d336c28afd954bb916337b809a1205b8820d3637fb66574829710b73d
6
+ metadata.gz: 4bbed134e5bc6f0b78231501cd866a581a878dd4b5734ca74b480bfc49bc014c25a5e2a23074a62ccdc4ac90d40220ed6a7f5fb42a8c68c44979fd909d427635
7
+ data.tar.gz: b97abb74e30855a39100c9ca66785e89148bfda8e1cc427170b67767007078a5818e194adf34541806d6c4809a3b45bf1bfba97305bff663c4643c21c319d347
@@ -0,0 +1,197 @@
1
+ # Upgrades simple_form_for to effective_form_with
2
+ module Effective
3
+ class FormUpgrader
4
+
5
+ def initialize(folder: 'app/views/')
6
+ @folders = Array(folder)
7
+ end
8
+
9
+ def upgrade!
10
+ @folders.each do |folder|
11
+ Dir.glob(folder + '**/*').each do |path|
12
+ next if File.directory?(path)
13
+ next unless path.include?('.html')
14
+
15
+ writer = Effective::CodeWriter.new(path)
16
+
17
+ name = path.split('/')[0...-1] - ['app', 'views']
18
+ resource = Effective::Resource.new(name)
19
+
20
+ if writer.find { |line| line.include?('simple_form_for') }
21
+ upgrade_simple_form(writer, resource)
22
+ elsif writer.find { |line| line.include?('semantic_form_for') }
23
+ upgrade_formtastic(writer, resource)
24
+ elsif writer.find { |line| line.include?('form_for') }
25
+ upgrade_form_for(writer, resource)
26
+ else
27
+ next # Nothing to do
28
+ end
29
+
30
+ writer.write!
31
+ end
32
+ end
33
+
34
+ puts 'All Done. Have a great day.'
35
+ true
36
+ end
37
+
38
+ private
39
+
40
+ SIMPLE_FORM_FOR_REGEX = /simple_form_for( |\()(\[:[^,]+, ?[^,]+\])?(([^,]+),)?.+do \|(\w+)\|/
41
+ SIMPLE_FORM_INPUT_ATTRIBUTE = /\.input( |\():(\w+)/
42
+ SIMPLE_FORM_INPUT_AS_ONE = /(as: :(\w+))/
43
+ SIMPLE_FORM_INPUT_AS_TWO = /(:as => :(\w+))/
44
+ SIMPLE_FORM_INPUT_COLLECTION_ONE = /(collection: ([^,]+?))(,|$)/
45
+ SIMPLE_FORM_INPUT_COLLECTION_TWO = /(:collection => :([^,]+?))(,|$)/
46
+
47
+ def upgrade_simple_form(writer, resource)
48
+ puts "Upgrading simple form: #{writer.filename}"
49
+
50
+ letter = nil
51
+ model = nil
52
+
53
+ # Replace simple_form_for
54
+ writer.all { |line| line.include?('simple_form_for') }.each do |line|
55
+ content = writer.lines[line]
56
+ matched = content.match(SIMPLE_FORM_FOR_REGEX)
57
+ raise("unable to match simple_form_for from:\n#{content}") unless matched.present?
58
+
59
+ original = matched[0]
60
+ model = matched[2] || matched[4]
61
+ letter = matched[5]
62
+
63
+ raise("unable to determine simple_form_for subject from:\n#{content}") unless original && model && letter
64
+
65
+ content.sub!(original, "effective_form_with(model: #{model}) do |#{letter}|")
66
+ writer.replace(line, content)
67
+ end
68
+
69
+ # Try to figure out klass again if its missing from filename
70
+ if resource.klass.blank? && model.present?
71
+ name = model.sub('[', '').sub(']', '').sub(' ', '').split(',').map do |value|
72
+ value.sub('current_', '').sub('@', '').sub(':', '')
73
+ end
74
+
75
+ resource = Effective::Resource.new(name)
76
+ end
77
+
78
+ if resource.klass.blank? && writer.filename.include?('/devise/')
79
+ resource = Effective::Resource.new('user')
80
+ end
81
+
82
+ if resource.klass.blank?
83
+ puts " => Warning: Unable to determine klass of #{model}"
84
+ end
85
+
86
+ # Replace .input
87
+ writer.all { |line| line.include?('.input :') }.each do |line|
88
+ content = writer.lines[line]
89
+ attribute = content.match(SIMPLE_FORM_INPUT_ATTRIBUTE)
90
+ raise("unable to match simple_form_for input attribute from\n#{content}") unless attribute.present?
91
+
92
+ as = content.match(SIMPLE_FORM_INPUT_AS_ONE) || content.match(SIMPLE_FORM_INPUT_AS_TWO)
93
+ collection = content.match(SIMPLE_FORM_INPUT_COLLECTION_ONE) || content.match(SIMPLE_FORM_INPUT_COLLECTION_TWO)
94
+
95
+ if as.present?
96
+ content.sub!(",#{as[0]}", '')
97
+ content.sub!(", #{as[0]}", '')
98
+ end
99
+
100
+ if collection.present?
101
+ content.sub!(",#{collection[0]}", ',')
102
+ content.sub!(", #{collection[0]}", ',')
103
+ content.sub!(attribute[0], "#{attribute[0]} #{collection[2]},")
104
+ end
105
+
106
+ input_type = find_input_type(resource: resource, attribute: attribute[2], as: (as[2] if as))
107
+
108
+ content.sub!('input', input_type)
109
+ writer.replace(line, content)
110
+ end
111
+
112
+ # Replace simple_fields_for
113
+ writer.all { |line| line.include?(".simple_fields_for") }.each do |line|
114
+ content = writer.lines[line]
115
+
116
+ content.sub!(".simple_fields_for", ".has_many")
117
+ writer.replace(line, content)
118
+ end
119
+
120
+ # Replace f.submit
121
+ writer.all { |line| line.include?("#{letter}.submit") }.each do |line|
122
+ content = writer.lines[line]
123
+
124
+ content.sub!("#{letter}.submit", "#{letter}.save")
125
+ writer.replace(line, content)
126
+ end
127
+
128
+ # Replace f.button :submit
129
+ writer.all { |line| line.include?(".button :submit,") }.each do |line|
130
+ content = writer.lines[line]
131
+
132
+ content.sub!(".button :submit,", ".submit")
133
+ writer.replace(line, content)
134
+ end
135
+
136
+ # Replace .form-actions
137
+ writer.all { |line| line == '.form-actions' }.each do |line|
138
+ content = writer.lines[line]
139
+
140
+ content.sub!('.form-actions', "= #{letter}.submit do")
141
+ writer.replace(line, content)
142
+ end
143
+ end
144
+
145
+ def upgrade_formtastic(writer, resource)
146
+ puts "Detected formtastic: #{writer.filename}"
147
+ end
148
+
149
+ def upgrade_form_for(writer, resource)
150
+ puts "Detected rails form_for: #{writer.filename}"
151
+ end
152
+
153
+ def find_input_type(attribute:, resource:, as: nil)
154
+ input_type = (as || resource.sql_type(attribute)).to_s
155
+
156
+ case input_type
157
+ when 'asset_box_simple_form' then 'file_field'
158
+ when 'belongs_to', 'belongs_to_polymorphic' then 'select'
159
+ when 'boolean' then 'check_box'
160
+ when 'check_boxes' then 'checks'
161
+ when 'date' then 'date_field'
162
+ when 'datetime' then 'datetime_field'
163
+ when 'decimal' then 'float_field'
164
+ when 'effective_ckeditor_text_area' then 'rich_text_area' # I guess
165
+ when 'effective_date_picker' then 'date_field'
166
+ when 'effective_date_time_picker' then 'datetime_field'
167
+ when 'effective_email' then 'email_field'
168
+ when 'effective_price' then 'price_field'
169
+ when 'effective_radio_buttons' then 'radios'
170
+ when 'effective_select' then 'select'
171
+ when 'effective_static_control' then 'static_field'
172
+ when 'effective_tel' then 'phone_field'
173
+ when 'effective_time_picker' then 'time_field'
174
+ when 'effective_url' then 'url_field'
175
+ when 'email' then 'email_field'
176
+ when 'file' then 'file_field'
177
+ when 'hidden' then 'hidden_field'
178
+ when 'integer' then 'number_field'
179
+ when 'number' then 'number_field'
180
+ when 'password' then 'password_field'
181
+ when 'phone' then 'phone_field'
182
+ when 'price' then 'price_field'
183
+ when 'radio_buttons' then 'radios'
184
+ when 'search' then 'search_field'
185
+ when 'select' then 'select'
186
+ when 'static_control' then 'static_field'
187
+ when 'string' then 'text_field'
188
+ when 'tel', 'telephone' then 'phone_field'
189
+ when 'text' then 'text_area'
190
+ when 'url' then 'url_field'
191
+ else
192
+ raise("unknown input type #{input_type} (for attribute :#{attribute})")
193
+ end
194
+ end
195
+
196
+ end
197
+ end
@@ -1,3 +1,3 @@
1
1
  module EffectiveDeveloper
2
- VERSION = '0.6.3'.freeze
2
+ VERSION = '0.6.4'.freeze
3
3
  end
@@ -0,0 +1,7 @@
1
+ # bundle exec rake upgrade_forms
2
+ # bundle exec rake upgrade_forms[app/views/admin/]
3
+ desc 'Upgrades simple_form_for to effective_form_with'
4
+ task :upgrade_forms, [:folder] => :environment do |t, args|
5
+ args.with_defaults(folder: 'app/views/')
6
+ Effective::FormUpgrader.new(folder: args.folder).upgrade!
7
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: effective_developer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.3
4
+ version: 0.6.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Code and Effect
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-05-31 00:00:00.000000000 Z
11
+ date: 2021-06-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -51,6 +51,7 @@ files:
51
51
  - app/models/effective/annotator.rb
52
52
  - app/models/effective/code_writer.rb
53
53
  - app/models/effective/csv_importer.rb
54
+ - app/models/effective/form_upgrader.rb
54
55
  - app/models/effective/live_generator.rb
55
56
  - app/models/effective/profiler.rb
56
57
  - config/effective_developer.rb
@@ -101,6 +102,7 @@ files:
101
102
  - lib/tasks/pg_pull.rake
102
103
  - lib/tasks/rename_class.rake
103
104
  - lib/tasks/reset_pk_sequence.rake
105
+ - lib/tasks/upgrade_forms.rake
104
106
  - lib/tasks/validate.rake
105
107
  homepage: https://github.com/code-and-effect/effective_developer
106
108
  licenses: