templaty 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (67) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +6 -0
  3. data/LICENSE +21 -0
  4. data/README.md +67 -0
  5. data/lib/generators/templaty/crud_generator.rb +179 -0
  6. data/lib/generators/templaty/migrations/add_column_generator.rb +102 -0
  7. data/lib/generators/templaty/templates/crud/app/assets/stylesheets/namespace/views/table/form.scss.erb +21 -0
  8. data/lib/generators/templaty/templates/crud/app/assets/stylesheets/namespace/views/table/index.scss.erb +3 -0
  9. data/lib/generators/templaty/templates/crud/app/assets/templates/namespace/table.gridy.hbs.erb +33 -0
  10. data/lib/generators/templaty/templates/crud/app/controllers/system/table_controller.rb.erb +71 -0
  11. data/lib/generators/templaty/templates/crud/app/javascripts/form.js.erb +24 -0
  12. data/lib/generators/templaty/templates/crud/app/javascripts/index.js.erb +48 -0
  13. data/lib/generators/templaty/templates/crud/app/models/model.rb.erb +41 -0
  14. data/lib/generators/templaty/templates/crud/app/views/namespace/table/_form.html.erb.erb +32 -0
  15. data/lib/generators/templaty/templates/crud/app/views/namespace/table/_submenu.html.erb.erb +5 -0
  16. data/lib/generators/templaty/templates/crud/app/views/namespace/table/_title.html.erb.erb +1 -0
  17. data/lib/generators/templaty/templates/crud/app/views/namespace/table/edit.html.erb.erb +14 -0
  18. data/lib/generators/templaty/templates/crud/app/views/namespace/table/gridy.json.jbuilder.erb +23 -0
  19. data/lib/generators/templaty/templates/crud/app/views/namespace/table/index.html.erb.erb +8 -0
  20. data/lib/generators/templaty/templates/crud/app/views/namespace/table/new.html.erb.erb +5 -0
  21. data/lib/generators/templaty/templates/crud/app/views/shared/icons/_model.html.erb.erb +1 -0
  22. data/lib/generators/templaty/templates/crud/config/locales/en/model.yml.erb +14 -0
  23. data/lib/generators/templaty/templates/crud/config/locales/en/namespace/table/create.yml.erb +5 -0
  24. data/lib/generators/templaty/templates/crud/config/locales/en/namespace/table/destroy.yml.erb +6 -0
  25. data/lib/generators/templaty/templates/crud/config/locales/en/namespace/table/edit.yml.erb +5 -0
  26. data/lib/generators/templaty/templates/crud/config/locales/en/namespace/table/index.yml.erb +5 -0
  27. data/lib/generators/templaty/templates/crud/config/locales/en/namespace/table/show.yml.erb +5 -0
  28. data/lib/generators/templaty/templates/crud/config/locales/en/namespace/table/update.yml.erb +6 -0
  29. data/lib/generators/templaty/templates/crud/config/locales/pt-BR/model.yml.erb +15 -0
  30. data/lib/generators/templaty/templates/crud/config/locales/pt-BR/namespace/table/create.yml.erb +5 -0
  31. data/lib/generators/templaty/templates/crud/config/locales/pt-BR/namespace/table/destroy.yml.erb +6 -0
  32. data/lib/generators/templaty/templates/crud/config/locales/pt-BR/namespace/table/edit.yml.erb +5 -0
  33. data/lib/generators/templaty/templates/crud/config/locales/pt-BR/namespace/table/index.yml.erb +5 -0
  34. data/lib/generators/templaty/templates/crud/config/locales/pt-BR/namespace/table/show.yml.erb +5 -0
  35. data/lib/generators/templaty/templates/crud/config/locales/pt-BR/namespace/table/update.yml.erb +7 -0
  36. data/lib/generators/templaty/templates/crud/config/locales/ru/model.yml.erb +14 -0
  37. data/lib/generators/templaty/templates/crud/config/locales/ru/namespace/table/create.yml.erb +5 -0
  38. data/lib/generators/templaty/templates/crud/config/locales/ru/namespace/table/destroy.yml.erb +6 -0
  39. data/lib/generators/templaty/templates/crud/config/locales/ru/namespace/table/edit.yml.erb +5 -0
  40. data/lib/generators/templaty/templates/crud/config/locales/ru/namespace/table/index.yml.erb +5 -0
  41. data/lib/generators/templaty/templates/crud/config/locales/ru/namespace/table/show.yml.erb +5 -0
  42. data/lib/generators/templaty/templates/crud/config/locales/ru/namespace/table/update.yml.erb +6 -0
  43. data/lib/generators/templaty/templates/crud/db/seeds/pd/model.rb.erb +8 -0
  44. data/lib/generators/templaty/templates/crud/spec/controllers/namespace/model/create_spec.rb.erb +60 -0
  45. data/lib/generators/templaty/templates/crud/spec/controllers/namespace/model/destroy_spec.rb.erb +120 -0
  46. data/lib/generators/templaty/templates/crud/spec/controllers/namespace/model/edit_spec.rb.erb +43 -0
  47. data/lib/generators/templaty/templates/crud/spec/controllers/namespace/model/gridy/json_spec.rb.erb +52 -0
  48. data/lib/generators/templaty/templates/crud/spec/controllers/namespace/model/gridy/unlogged_spec.rb.erb +12 -0
  49. data/lib/generators/templaty/templates/crud/spec/controllers/namespace/model/index_spec.rb.erb +24 -0
  50. data/lib/generators/templaty/templates/crud/spec/controllers/namespace/model/new_spec.rb.erb +24 -0
  51. data/lib/generators/templaty/templates/crud/spec/controllers/namespace/model/update_spec.rb.erb +82 -0
  52. data/lib/generators/templaty/templates/crud/spec/features/namespace/model/create_spec.rb.erb +35 -0
  53. data/lib/generators/templaty/templates/crud/spec/features/namespace/model/destroy_spec.rb.erb +27 -0
  54. data/lib/generators/templaty/templates/crud/spec/features/namespace/model/gridy/initial_spec.rb.erb +53 -0
  55. data/lib/generators/templaty/templates/crud/spec/features/namespace/model/gridy/search_spec.rb.erb +46 -0
  56. data/lib/generators/templaty/templates/crud/spec/features/namespace/model/gridy/sort_spec.rb.erb +47 -0
  57. data/lib/generators/templaty/templates/crud/spec/features/namespace/model/update_spec.rb.erb +34 -0
  58. data/lib/generators/templaty/templates/crud/spec/models/model/list_spec.rb.erb +16 -0
  59. data/lib/generators/templaty/templates/db/migrate/add_column.rb.erb +7 -0
  60. data/lib/generators/templaty/templates/db/migrate/backfill.rb.erb +13 -0
  61. data/lib/generators/templaty/templates/db/migrate/not_null.rb.erb +7 -0
  62. data/lib/generators/templaty/templates/db/migrate/set_default.rb.erb +12 -0
  63. data/lib/generators/templaty/templates/db/migrate/validate_not_null.rb.erb +12 -0
  64. data/lib/templaty/helper.rb +131 -0
  65. data/lib/templaty/version.rb +5 -0
  66. data/lib/templaty.rb +5 -0
  67. metadata +208 -0
@@ -0,0 +1,82 @@
1
+ <%- data = Templaty::Helper.data_for(options) -%>
2
+ <%- data_as_hash_string_formatted = Templaty::Helper.data_as_hash_string(data, value_attribute: :formatted) -%>
3
+ <%- data_as_hash_string_raw = Templaty::Helper.data_as_hash_string(data, value_attribute: :raw) -%>
4
+ <%- fields = Templaty::Helper.fields(options) -%>
5
+ <%- fields_presence = Templaty::Helper.fields_presence(options) -%>
6
+ <%- model_name = options[:table].singularize -%>
7
+ # frozen_string_literal: true
8
+
9
+ require 'support/factory_bot'
10
+ require 'support/shared/logged'
11
+
12
+ RSpec.describe <%= options[:namespace].classify %>::<%= options[:table].classify.pluralize %>Controller, '#update' do
13
+ context 'when unlogged' do
14
+ it 'redirects' do
15
+ patch :update, params: { id: 0 }
16
+
17
+ expect(response.status).to be 302
18
+ end
19
+ end
20
+
21
+ context 'when logged' do
22
+ include_context 'when_logged', permissions: [['<%= options[:namespace] %>/<%= options[:table] %>', :update]], with_plan: true
23
+
24
+ let!(:<%= model_name %>) do
25
+ create(:<%= model_name %>, <%= data_as_hash_string_raw %>, unit: current_unit)
26
+ end
27
+
28
+ let!(:parameters) do
29
+ { <%= data_as_hash_string_formatted %> }
30
+ end
31
+
32
+ context 'when fail' do
33
+ before { parameters[:<%= fields_presence.first %>] = nil }
34
+
35
+ it 'assigns record' do
36
+ patch :update, params: { id: <%= model_name %>, <%= model_name %>: parameters }
37
+
38
+ expect(assigns(:<%= model_name %>)).to eq(<%= model_name %>)
39
+ end
40
+
41
+ it 'does not update the record the fields value' do
42
+ expect { patch :update, params: { id: <%= model_name %>, <%= model_name %>: parameters } }.not_to change(<%= model_name %>, :<%= fields_presence.first %>)
43
+ end
44
+
45
+ it 're-renders page' do
46
+ patch :update, params: { id: <%= model_name %>, <%= model_name %>: parameters }
47
+
48
+ expect(response).to render_template(:edit)
49
+ end
50
+ end
51
+
52
+ context 'when success' do
53
+ it 'assigns record' do
54
+ patch :update, params: { id: <%= model_name %>, <%= model_name %>: parameters }
55
+
56
+ expect(assigns(:<%= model_name %>)).to eq(<%= model_name %>)
57
+ end
58
+
59
+ it 'updates the record' do
60
+ patch :update, params: { id: <%= model_name %>, <%= model_name %>: parameters }
61
+
62
+ <%= model_name %>.reload
63
+
64
+ <%- fields.sort.each do |field| -%>
65
+ expect(<%= model_name %>.<%= field %>).to <%= Templaty::Helper.rspec_matcher(data.dig(field, :raw)) %>
66
+ <%- end -%>
67
+ end
68
+
69
+ it 'redirects' do
70
+ patch :update, params: { id: <%= model_name %>, <%= model_name %>: parameters }
71
+
72
+ expect(response).to redirect_to(<%= options[:namespace] %>_<%= options[:table] %>_url)
73
+ end
74
+
75
+ it 'flashes' do
76
+ patch :update, params: { id: <%= model_name %>, <%= model_name %>: parameters }
77
+
78
+ expect(flash[:info]).to eq('<%= options[:name_one].capitalize %> <%= Templaty::Helper.i18n_updated(options) %> com sucesso.')
79
+ end
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,35 @@
1
+ <%- fields = Templaty::Helper.fields(options) -%>
2
+ <%- data = Templaty::Helper.data_for(options) -%>
3
+ <%- model_name = options[:table].singularize -%>
4
+ # frozen_string_literal: true
5
+
6
+ require 'support/capybara_box'
7
+ require 'support/factory_bot'
8
+
9
+ RSpec.describe <%= options[:table].classify%>, '#create', :js do
10
+ let!(:profile) { create_profile(with_plan: true) }
11
+
12
+ it 'works' do
13
+ login profile.user,
14
+ permissions: [
15
+ ['<%= options[:namespace] %>/<%= options[:table] %>', :create],
16
+ ['<%= options[:namespace] %>/<%= options[:table] %>', :edit],
17
+ ['<%= options[:namespace] %>/<%= options[:table] %>', :index],
18
+ ],
19
+ redirect_to: new_<%= options[:namespace] %>_<%= model_name %>_path
20
+
21
+ <%- fields.sort.each do |field| -%>
22
+ fill_in '<%= model_name %>_<%= field %>', with: '<%= data[field][:formatted] %>'
23
+ <%- end -%>
24
+
25
+ click_button('Salvar')
26
+
27
+ expect(page).to have_text('<%= options[:name_one].capitalize %> <%= Templaty::Helper.i18n_created(options) %> com sucesso.')
28
+
29
+ visit edit_<%= options[:namespace] %>_<%= model_name %>_path(described_class.last)
30
+
31
+ <%- fields.sort.each do |field| -%>
32
+ expect(page).to have_field('<%= model_name %>_<%= field %>', with: '<%= data[field][:formatted] %>')
33
+ <%- end -%>
34
+ end
35
+ end
@@ -0,0 +1,27 @@
1
+ <%- controller_path = "#{options[:namespace]}/#{options[:table]}" %>
2
+ # frozen_string_literal: true
3
+
4
+ require 'support/capybara_box'
5
+ require 'support/factory_bot'
6
+
7
+ RSpec.describe <%= options[:table].classify%>, '#destroy', :js do
8
+ let!(:profile) { create_profile(with_plan: true) }
9
+ let!(:<%= options[:table].singularize %>) { create(:<%= options[:table].singularize %>, unit: profile.unit) }
10
+
11
+ it 'works' do
12
+ login profile.user,
13
+ permissions: [
14
+ ['<%= controller_path %>', :destroy],
15
+ ['<%= controller_path %>', :gridy],
16
+ ['<%= controller_path %>', :index],
17
+ ['<%= controller_path %>', :update],
18
+ ],
19
+ redirect_to: edit_<%= options[:namespace] %>_<%= options[:table].singularize %>_path(<%= options[:table].singularize %>)
20
+
21
+ page.accept_confirm { click_button('Apagar <%= Templaty::Helper.i18n_this(options) %> <%= options[:name_one]%>') }
22
+
23
+ expect(page).to have_text('<%= options[:name_one].capitalize %> <%= Templaty::Helper.i18n_removed(options) %> com sucesso.')
24
+
25
+ expect(described_class.count).to be(0)
26
+ end
27
+ end
@@ -0,0 +1,53 @@
1
+ <%- fields = Templaty::Helper.fields(options) -%>
2
+ <%- datas = fields.sort.reduce({}) { |acc, field| acc.tap { |data| data[field] = Templaty::Helper.data_for(options) } } -%>
3
+ <%- model_name = options[:table].singularize -%>
4
+ # frozen_string_literal: true
5
+
6
+ require 'support/capybara_box'
7
+ require 'support/factory_bot'
8
+
9
+ RSpec.describe <%= options[:table].classify%>, '#index', :js do
10
+ def line(record)
11
+ [
12
+ <%- fields.each do |field| -%>
13
+ <%- if field.start_with?('percentage') -%>
14
+ Helper.percentage(record.<%= field %>, type: :cents),
15
+ <%- elsif field.end_with?('_cents') -%>
16
+ Helper.money(record.<%= field %>, type: :cents),
17
+ <%- else -%>
18
+ record.<%= field %>,
19
+ <%- end -%>
20
+ <%- end -%>
21
+ ].join("\n")
22
+ end
23
+
24
+ def links
25
+ [
26
+ { href: ->(record) { edit_<%= options[:namespace] %>_<%= options[:table].singularize %>_path(record) }, text: 'Editar' },
27
+ <%- if options[:show_route] -%>
28
+ { href: ->(record) { <%= options[:table].singularize %>_path(record) }, text: 'Visualizar' },
29
+ <%- end -%>
30
+ ]
31
+ end
32
+
33
+ let!(:profile) { create_profile(with_plan: true) }
34
+ <%- fields.sort.each.with_index do |field, index| -%>
35
+ let!(:record_<%= index + 1 %>) do
36
+ create(:<%= model_name %>, <%= Templaty::Helper.data_as_hash_string(datas[field], value_attribute: :raw) %>, unit: profile.unit)
37
+ end
38
+ <%- end -%>
39
+
40
+ it 'works' do
41
+ login profile.user, permissions: [['<%= options[:namespace] %>/<%= options[:table] %>', :index]], redirect_to: <%= options[:namespace] %>_<%= options[:table] %>_path
42
+
43
+ expect(page).to have_link('<%= Templaty::Helper.i18n_new(options).capitalize %>', href: new_<%= options[:namespace] %>_<%= options[:table].singularize %>_path)
44
+
45
+ gridy_head %w[<%= Templaty::Helper.fields_i18n(options).join(' ') %> Menu]
46
+
47
+ gridy_rows links, [
48
+ <%- fields.size.times do |index| -%>
49
+ record_<%= index + 1 %>,
50
+ <%- end -%>
51
+ ]
52
+ end
53
+ end
@@ -0,0 +1,46 @@
1
+ <%- fields = Templaty::Helper.fields(options) -%>
2
+ <%- datas = fields.sort.reduce({}) { |acc, field| acc.tap { |data| data[field] = Templaty::Helper.data_for(options) } } -%>
3
+ <%- model_name = options[:table].singularize -%>
4
+ # frozen_string_literal: true
5
+
6
+ require 'support/capybara_box'
7
+ require 'support/factory_bot'
8
+
9
+ RSpec.describe <%= options[:table].classify%>, '#index', :js do
10
+ def line(record)
11
+ [
12
+ <%- fields.each do |field| -%>
13
+ <%- if field.start_with?('percentage') -%>
14
+ Helper.percentage(record.<%= field %>, type: :cents),
15
+ <%- elsif field.end_with?('_cents') -%>
16
+ Helper.money(record.<%= field %>, type: :cents),
17
+ <%- else -%>
18
+ record.<%= field %>,
19
+ <%- end -%>
20
+ <%- end -%>
21
+ ].join("\n")
22
+ end
23
+
24
+ def links; end
25
+
26
+ let!(:profile) { create_profile(with_plan: true) }
27
+ <%- fields.sort.each.with_index do |field, index| -%>
28
+ let!(:record_<%= index + 1 %>) do
29
+ create(:<%= model_name %>, <%= Templaty::Helper.data_as_hash_string(datas[field], value_attribute: :raw) %>, unit: profile.unit)
30
+ end
31
+ <%- end -%>
32
+
33
+ it 'works' do
34
+ login profile.user, permissions: [['<%= options[:namespace] %>/<%= options[:table] %>', :index]], redirect_to: <%= options[:namespace] %>_<%= options[:table] %>_path
35
+
36
+ <%- fields.each.with_index do |field, index| -%>
37
+ gridy_search with: record_<%= index + 1 %>.<%= field %>
38
+
39
+ gridy_rows links, [record_<%= index + 1 %>]
40
+
41
+ <%- end -%>
42
+ gridy_search with: :missing
43
+
44
+ gridy_no_result
45
+ end
46
+ end
@@ -0,0 +1,47 @@
1
+ <%- fields = Templaty::Helper.fields(options) -%>
2
+ <%- datas = fields.sort.reduce({}) { |acc, field| acc.tap { |data| data[field] = Templaty::Helper.data_for(options) } } -%>
3
+ <%- model_name = options[:table].singularize -%>
4
+ # frozen_string_literal: true
5
+
6
+ require 'support/capybara_box'
7
+ require 'support/factory_bot'
8
+
9
+ RSpec.describe <%= options[:table].classify%>, '#index', :js do
10
+ def line(record)
11
+ [
12
+ <%- fields.each do |field| -%>
13
+ <%- if field.start_with?('percentage') -%>
14
+ Helper.percentage(record.<%= field %>, type: :cents),
15
+ <%- elsif field.end_with?('_cents') -%>
16
+ Helper.money(record.<%= field %>, type: :cents),
17
+ <%- else -%>
18
+ record.<%= field %>,
19
+ <%- end -%>
20
+ <%- end -%>
21
+ ].join("\n")
22
+ end
23
+
24
+ def links; end
25
+
26
+ let!(:profile) { create_profile(with_plan: true) }
27
+ <%- fields.sort.each.with_index do |field, index| -%>
28
+ let!(:record_<%= index + 1 %>) do
29
+ create(:<%= model_name %>, <%= Templaty::Helper.data_as_hash_string(datas[field], value_attribute: :raw) %>, unit: profile.unit)
30
+ end
31
+ <%- end -%>
32
+
33
+ it 'works' do
34
+ login profile.user, permissions: [['<%= options[:namespace] %>/<%= options[:table] %>', :index]], redirect_to: <%= options[:namespace] %>_<%= options[:table] %>_path
35
+
36
+ <%- fields.each.with_index do |field, index| -%>
37
+ gridy_sort by: '<%= field %>'
38
+
39
+ gridy_rows links, [
40
+ <%- fields.size.times do |number| -%>
41
+ record_<%= number + 1 %>,
42
+ <%- end -%>
43
+ ]
44
+
45
+ <%- end -%>
46
+ end
47
+ end
@@ -0,0 +1,34 @@
1
+ <%- data = Templaty::Helper.data_for(options) -%>
2
+ <%- data_as_hash_string_raw = Templaty::Helper.data_as_hash_string(data, value_attribute: :raw) -%>
3
+ <%- fields = Templaty::Helper.fields(options) -%>
4
+ <%- controller_path = "#{options[:namespace]}/#{options[:table]}" -%>
5
+ <%- model_name = options[:table].singularize -%>
6
+ # frozen_string_literal: true
7
+
8
+ require 'support/capybara_box'
9
+ require 'support/factory_bot'
10
+
11
+ RSpec.describe <%= options[:table].classify%>, '#update', :js do
12
+ let!(:profile) { create_profile(with_plan: true) }
13
+ let!(:<%= model_name %>) { create(:<%= model_name %>, <%= data_as_hash_string_raw %>, unit: current_unit) }
14
+
15
+ it 'works' do
16
+ login profile.user,
17
+ permissions: [['<%= controller_path %>', :update], ['<%= controller_path %>', :index]],
18
+ redirect_to: edit_<%= options[:namespace] %>_<%= model_name %>_path(<%= model_name %>)
19
+
20
+ <%- fields.each do |field| -%>
21
+ fill_in '<%= model_name %>_<%= field %>', with: '<%= data[field][:formatted] %>'
22
+ <%- end -%>
23
+
24
+ click_button('Salvar')
25
+
26
+ expect(page).to have_text('<%= options[:name_one].capitalize %> <%= Templaty::Helper.i18n_updated(options) %> com sucesso.')
27
+
28
+ visit edit_<%= options[:namespace] %>_<%= model_name %>_path(<%= model_name %>.reload)
29
+
30
+ <%- fields.each do |field| -%>
31
+ expect(page).to have_field('<%= model_name %>_<%= field %>', with: '<%= data[field][:formatted] %>')
32
+ <%- end -%>
33
+ end
34
+ end
@@ -0,0 +1,16 @@
1
+ <%- fields = Templaty::Helper.fields(options) -%>
2
+ <%- datas = fields.sort.reduce({}) { |acc, field| acc.tap { |data| data[field] = Templaty::Helper.data_for(options) } } -%>
3
+ <%- model_name = options[:table].singularize -%>
4
+ # frozen_string_literal: true
5
+
6
+ require 'support/factory_bot'
7
+
8
+ RSpec.describe <%= options[:table].classify%>, '.list' do
9
+ <%- fields.sort.each.with_index do |field, index| -%>
10
+ let!(:<%= model_name %>_<%= index + 1 %>) { create(:<%= model_name %>, <%= Templaty::Helper.data_as_hash_string(datas[field], value_attribute: :raw) %>) }
11
+ <%- end -%>
12
+
13
+ <%- fields.sort.each.with_index do |field, index| -%>
14
+ it { expect(described_class.list(<%= Templaty::Helper.data_wrap(datas[field][field][:raw]) %>).ids).to eq([<%= model_name %>_<%= index + 1 %>.id]) }
15
+ <%- end -%>
16
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ class AddColumn<%= options[:column].classify %>To<%= options[:table].classify.pluralize %> < ActiveRecord::Migration[<%= options[:rails_version] %>]
4
+ def change
5
+ add_column <%= options[:table] %>, :<%= options[:column] %>, :<%= options[:type] %>
6
+ end
7
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Backfill<%= options[:column].classify %>To<%= options[:table].classify.pluralize %> < ActiveRecord::Migration[<%= options[:rails_version] %>]
4
+ disable_ddl_transaction!
5
+
6
+ def up
7
+ <%= options[:table].classify %>.unscoped.in_batches do |criteria|
8
+ criteria.update_all(<%= options[:column] %>: <%= options[:default] %>) # rubocop:disable Rails/SkipsModelValidations
9
+
10
+ sleep(0.01) # throttle
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ class NotNull<%= options[:column].classify %>To<%= options[:table].classify.pluralize %> < ActiveRecord::Migration[<%= options[:rails_version] %>]
4
+ def change
5
+ add_check_constraint :<%= options[:table] %>, '<%= options[:column] %> IS NOT NULL', name: '<%= options[:table] %>_<%= options[:column] %>_null', validate: false
6
+ end
7
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ # In Postgres 11+, MySQL 8.0.12+, and MariaDB 10.3.2+, a new column with default no longer requires a table rewrite and is safe.
4
+ class SetColumn<%= options[:column].classify %>WithDefault < ActiveRecord::Migration[<%= options[:rails_version] %>]
5
+ def change
6
+ <%- if options[:type] == 'string' -%>
7
+ change_column_default :<%= options[:table] %>, :<%= options[:column] %>, "<%= options[:default] %>"
8
+ <%- else -%>
9
+ change_column_default :<%= options[:table] %>, :<%= options[:column] %>, <%= options[:default] %>
10
+ <%- end -%>
11
+ end
12
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ class ValidateNotNull<%= options[:column].classify %>To<%= options[:table].classify.pluralize %> < ActiveRecord::Migration[<%= options[:rails_version] %>]
4
+ def change
5
+ validate_check_constraint :<%= options[:table] %>, name: '<%= options[:table] %>_<%= options[:column] %>_null'
6
+
7
+ # in Postgres 12+, you can then safely set NOT NULL on the column
8
+ change_column_null :<%= options[:table] %>, :<%= options[:column] %>, false
9
+
10
+ remove_check_constraint :<%= options[:table] %>, name: '<%= options[:table] %>_<%= options[:column] %>_null'
11
+ end
12
+ end
@@ -0,0 +1,131 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'faker'
4
+
5
+ module Templaty
6
+ module Helper
7
+ module_function
8
+
9
+ def belongs_to(options)
10
+ options[:belongs_to].split(',')
11
+ end
12
+
13
+ def data(field)
14
+ amount = Faker::Number.number(digits: 5)
15
+ description = Faker::Lorem.paragraphs.join("\r\n\n")
16
+ name = Faker::Artist.name
17
+ percentage = Faker::Number.number(digits: 4)
18
+
19
+ {
20
+ 'amount_cents' => { raw: amount, formatted: ::Helper.money(amount, type: :cents) },
21
+ 'description' => { raw: description, formatted: description },
22
+ 'name' => { raw: name, formatted: name },
23
+ 'percentage_cents' => { raw: percentage, formatted: ::Helper.percentage(percentage, type: :cents) },
24
+ }[field] || { field => { raw: 'any', formatted: 'any' } }
25
+ end
26
+
27
+ def data_as_hash_string(data, value_attribute: :formatted)
28
+ data.map { |key, value| "#{key}: #{data_wrap(value[value_attribute])}" }.join(', ')
29
+ end
30
+
31
+ def data_for(options)
32
+ fields(options)
33
+ .sort
34
+ .reduce({}) { |acc, field| acc.tap { |data| data[field] = Templaty::Helper.data(field) } }
35
+ .tap do |_field, item|
36
+ if options[:avatar]
37
+ item << { 'avatar' => { raw: "fixture_image('image.jpg')", formatted: "fixture_image('image.jpg')" } }
38
+ end
39
+
40
+ if options[:cover]
41
+ item << { 'cover' => { raw: "fixture_image('image.png')", formatted: "fixture_image('image.png')" } }
42
+ end
43
+
44
+ if options[:photos]
45
+ item << {
46
+ 'photos' => {
47
+ raw: "[fixture_image('image-1.jpg'), fixture_image('image-2.jpg')]",
48
+ formatted: "[fixture_image('image-1.jpg'), fixture_image('image-2.jpg')]",
49
+ },
50
+ }
51
+ end
52
+ end
53
+ end
54
+
55
+ def data_i18n(options)
56
+ fields(options).zip(fields_i18n(options)).to_h
57
+ end
58
+
59
+ def data_wrap(value)
60
+ value.is_a?(String) ? "'#{value}'" : value
61
+ end
62
+
63
+ def fields(options)
64
+ options[:fields].split(',')
65
+ end
66
+
67
+ def fields_i18n(options)
68
+ options[:fields_i18n].split(',')
69
+ end
70
+
71
+ def fields_presence(options)
72
+ options[:fields_presence].split(',')
73
+ end
74
+
75
+ def fields_grid(options)
76
+ options[:fields_grid].split(',').map { |item| [item.split(':')].flatten.map(&:to_i) }
77
+ end
78
+
79
+ def fields_grid_style(options)
80
+ fields_grid(options).flatten.map do |number|
81
+ case number.to_i
82
+ when 50
83
+ {
84
+ 'margin-left' => '16px',
85
+ 'width' => 'calc(50% - 24px)',
86
+ }
87
+ when 100
88
+ {
89
+ 'margin-left' => '16px',
90
+ 'width' => 'calc(100% - 32px)',
91
+ }
92
+ end
93
+ end
94
+ end
95
+
96
+ def i18n_created(options)
97
+ options[:gender] == 'male' ? 'criado' : 'criada'
98
+ end
99
+
100
+ def i18n_new(options)
101
+ options[:gender] == 'male' ? 'novo' : 'nova'
102
+ end
103
+
104
+ def i18n_removed(options)
105
+ options[:gender] == 'male' ? 'removido' : 'removida'
106
+ end
107
+
108
+ def i18n_this(options)
109
+ options[:gender] == 'male' ? 'este' : 'esta'
110
+ end
111
+
112
+ def i18n_updated(options)
113
+ options[:gender] == 'male' ? 'atualizado' : 'atualizada'
114
+ end
115
+
116
+ def rspec_matcher(value)
117
+ return "be(#{value.inspect})" if value.nil? || value.is_a?(Numeric)
118
+ return "eq('')" if value == ''
119
+
120
+ "eq('#{value}')"
121
+ end
122
+
123
+ def validates_numericality(options)
124
+ options[:validates_numericality]&.split(',')&.reduce({}) do |acc, item|
125
+ field, min, max = item.split(':')
126
+
127
+ acc.tap { |data| data[field] = { min: min, max: max } }
128
+ end
129
+ end
130
+ end
131
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Templaty
4
+ VERSION = '0.1.0'
5
+ end
data/lib/templaty.rb ADDED
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Templaty
4
+ require 'templaty/helper'
5
+ end