rails-formation 0.0.0.3 → 0.0.0.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (33) hide show
  1. checksums.yaml +4 -4
  2. data/lib/rails-formation/version.rb +1 -1
  3. data/lib/rails_formation.rb +17 -71
  4. data/rails-formation.gemspec +0 -3
  5. metadata +2 -58
  6. data/lib/rails-formation/formatters/factories/column.rb +0 -63
  7. data/lib/rails-formation/formatters/factories/default_value.rb +0 -27
  8. data/lib/rails-formation/formatters/factories/value.rb +0 -78
  9. data/lib/rails-formation/formatters/factory.rb +0 -38
  10. data/lib/rails-formation/formatters/migration.rb +0 -45
  11. data/lib/rails-formation/formatters/migrations/column.rb +0 -33
  12. data/lib/rails-formation/formatters/migrations/index.rb +0 -30
  13. data/lib/rails-formation/formatters/model.rb +0 -49
  14. data/lib/rails-formation/formatters/models/association.rb +0 -37
  15. data/lib/rails-formation/formatters/models/validation.rb +0 -41
  16. data/lib/rails-formation/formatters/models/validations/base.rb +0 -62
  17. data/lib/rails-formation/formatters/models/validations/comparsion.rb +0 -31
  18. data/lib/rails-formation/formatters/models/validations/confirmation.rb +0 -19
  19. data/lib/rails-formation/formatters/models/validations/exclusion.rb +0 -25
  20. data/lib/rails-formation/formatters/models/validations/format.rb +0 -31
  21. data/lib/rails-formation/formatters/models/validations/inclusion.rb +0 -25
  22. data/lib/rails-formation/formatters/models/validations/length.rb +0 -58
  23. data/lib/rails-formation/formatters/models/validations/numericality.rb +0 -59
  24. data/lib/rails-formation/formatters/models/validations/presence.rb +0 -19
  25. data/lib/rails-formation/formatters/models/validations/uniqueness.rb +0 -58
  26. data/lib/rails-formation/formatters/rubygem.rb +0 -24
  27. data/lib/rails-formation/formatters/rubygems/gem.rb +0 -56
  28. data/lib/rails-formation/formatters/seed.rb +0 -30
  29. data/lib/rails-formation/formatters/seeds/row.rb +0 -19
  30. data/lib/rails-formation/templates/factory.rb.tt +0 -7
  31. data/lib/rails-formation/templates/migration.rb.tt +0 -16
  32. data/lib/rails-formation/templates/model.rb.tt +0 -12
  33. data/lib/rails-formation/templates/seeds.rb.tt +0 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 50040a27201ad285113ca45ba39a904f4cc29e6876d6054a12253bc2294e209b
4
- data.tar.gz: c10d8b45c7478f6592c5437346ce70682dec3463911f5f9db322822b0b812d53
3
+ metadata.gz: 8e81589480aa07b6cb398acdf1a67041c2e6c64fd064f34ddfaadab0adb0d38e
4
+ data.tar.gz: 5a959729cecd94d4582daaec57da1c4ec64d796fc69e7bff4129b698e86b87fc
5
5
  SHA512:
6
- metadata.gz: 7daf43d31ef3128f8e4a154d7fb76c1eee8158146b63908bfbe01346057a961dbbd23af918df1c87db4874b3f04546dbf471fa565800db32405bed4679b64362
7
- data.tar.gz: 77415da7829c9badc09107668c5085d270a775d3c16dddfb1b362334119783d8f146b3eb3649187a31ade1b3271db3d9d4f93a91e0741569e26f05b19b7634c6
6
+ metadata.gz: 44f2a5437bcb97dd274f96b76c343e387a7b1c72600a4da96f217c1cf6f51db7d99e10d05954e2c2f15703f3fd1e405fd0330c242e3384ba7f1630493dcc0f6b
7
+ data.tar.gz: 07700bfbc719f185cd5c49c82c2b9a8adf9e3b4c8d934814c622c90d1f641de92e835c5a7ad043cf8339a5ca48e5e20b88fdc7e758a17432d1299d12040e2259
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RailsFormation
4
- VERSION = '0.0.0.3'
4
+ VERSION = '0.0.0.7'
5
5
  RAILS_VERSION = '7.0.2.3'
6
6
  end
@@ -1,11 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'thor'
4
- require 'rails-formation/formatters/migration'
5
- require 'rails-formation/formatters/rubygem'
6
- require 'rails-formation/formatters/model'
7
- require 'rails-formation/formatters/factory'
8
- require 'rails-formation/formatters/seed'
3
+ require 'fileutils'
9
4
  require 'rails-formation/cli/file_adapter'
10
5
  require 'rails-formation/cli/api_adapter'
11
6
  require 'rails-formation/cli/processor'
@@ -18,11 +13,11 @@ module RailsFormation
18
13
  def apply(template)
19
14
  verify_rails_installation
20
15
  generate_project(template)
21
- bundle_and_install_gems(template.fetch('rubygems', []))
22
- create_and_run_migrations(template.fetch('migrations', []))
23
- create_factories(template.fetch('factories', []))
24
- create_and_build_models(template.fetch('models', []))
25
- insert_seeds(template.fetch('seeds', []))
16
+ generate_files(template.fetch('files', []))
17
+
18
+ template.fetch('commands', []).each do |project_command|
19
+ system(project_command)
20
+ end
26
21
  end
27
22
 
28
23
  private
@@ -34,71 +29,22 @@ module RailsFormation
34
29
  end
35
30
 
36
31
  def generate_project(template)
37
- if template['architecture'] == 'api'
38
- system "rails _#{RailsFormation::RAILS_VERSION}_ new #{template['app_name']} --api -d=postgresql"
39
- else
40
- system "rails _#{RailsFormation::RAILS_VERSION}_ new #{template['app_name']} -d=postgresql"
41
- end
32
+ system template['installation_command']
42
33
 
43
34
  Dir.chdir(template['app_name'])
44
35
  end
45
36
 
46
- def bundle_and_install_gems(rubygems)
47
- return if rubygems.size.zero?
48
-
49
- gemfile_path = File.join(Dir.pwd, 'Gemfile')
50
-
51
- rubygems.each do |config|
52
- ::RailsFormation::Formatters::Rubygem.new([config, gemfile_path]).invoke_all
37
+ def generate_files(files)
38
+ files.each do |section|
39
+ section_name = section.keys.first
40
+ puts "\e[0;39;44m#{section_name}\e[0m"
41
+ section[section_name].each do |config|
42
+ puts " -> \e[1;32;49mCreate\e[0m #{config['file_path']}"
43
+ file_path = File.join(Dir.pwd, config['file_path'])
44
+ FileUtils.mkdir_p(File.dirname(file_path))
45
+ File.write(file_path, config['code'])
46
+ end
53
47
  end
54
-
55
- system 'bundle install'
56
-
57
- rubygems.each do |config|
58
- config.fetch('install_commands', []).map { |command| system(command) }
59
- end
60
- end
61
-
62
- def create_and_run_migrations(migrations)
63
- return if migrations.size.zero?
64
-
65
- migrations.each do |config|
66
- table_name = config.fetch('table')
67
- migration_name = "#{Time.now.strftime('%Y%m%d%H%M%S')}_create_#{table_name}.rb"
68
- migration_path = File.join(Dir.pwd, 'db', 'migrate', migration_name)
69
- ::RailsFormation::Formatters::Migration.new([config, migration_path]).invoke_all
70
- sleep(1)
71
- end
72
-
73
- system('./bin/rails db:create')
74
- system('./bin/rails db:migrate')
75
- end
76
-
77
- def create_factories(factories)
78
- factories.each do |config|
79
- factory_name = "#{config.fetch('name')}.rb"
80
- factory_path = File.join(Dir.pwd, 'spec', 'factories', factory_name)
81
- ::RailsFormation::Formatters::Factory.new([config, factory_path]).invoke_all
82
- end
83
- end
84
-
85
- def create_and_build_models(models)
86
- models.each do |config|
87
- model_name = "#{config.fetch('name').downcase}.rb"
88
- model_path = File.join(Dir.pwd, 'app', 'models', model_name)
89
-
90
- ::RailsFormation::Formatters::Model.new([config, model_path]).invoke_all
91
- end
92
- end
93
-
94
- def insert_seeds(seeds)
95
- return if seeds.size.zero?
96
-
97
- seeds_path = File.join(Dir.pwd, 'db', 'seeds.rb')
98
-
99
- ::RailsFormation::Formatters::Seed.new([seeds, seeds_path]).invoke_all
100
-
101
- system('./bin/rails db:seed')
102
48
  end
103
49
  end
104
50
  end
@@ -28,9 +28,6 @@ Gem::Specification.new do |spec|
28
28
 
29
29
  spec.required_ruby_version = '>= 2.7.0'
30
30
 
31
- spec.add_runtime_dependency 'ffaker', '~> 2.20.0'
32
- spec.add_runtime_dependency 'thor', '~> 1.2.1'
33
-
34
31
  spec.add_development_dependency 'bundler'
35
32
  spec.add_development_dependency 'rake'
36
33
  spec.add_development_dependency 'rspec'
metadata CHANGED
@@ -1,43 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails-formation
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.0.3
4
+ version: 0.0.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Paweł Dąbrowski
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-04-18 00:00:00.000000000 Z
11
+ date: 2022-05-22 00:00:00.000000000 Z
12
12
  dependencies:
13
- - !ruby/object:Gem::Dependency
14
- name: ffaker
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - "~>"
18
- - !ruby/object:Gem::Version
19
- version: 2.20.0
20
- type: :runtime
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - "~>"
25
- - !ruby/object:Gem::Version
26
- version: 2.20.0
27
- - !ruby/object:Gem::Dependency
28
- name: thor
29
- requirement: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - "~>"
32
- - !ruby/object:Gem::Version
33
- version: 1.2.1
34
- type: :runtime
35
- prerelease: false
36
- version_requirements: !ruby/object:Gem::Requirement
37
- requirements:
38
- - - "~>"
39
- - !ruby/object:Gem::Version
40
- version: 1.2.1
41
13
  - !ruby/object:Gem::Dependency
42
14
  name: bundler
43
15
  requirement: !ruby/object:Gem::Requirement
@@ -121,34 +93,6 @@ files:
121
93
  - lib/rails-formation/cli/api_adapter.rb
122
94
  - lib/rails-formation/cli/file_adapter.rb
123
95
  - lib/rails-formation/cli/processor.rb
124
- - lib/rails-formation/formatters/factories/column.rb
125
- - lib/rails-formation/formatters/factories/default_value.rb
126
- - lib/rails-formation/formatters/factories/value.rb
127
- - lib/rails-formation/formatters/factory.rb
128
- - lib/rails-formation/formatters/migration.rb
129
- - lib/rails-formation/formatters/migrations/column.rb
130
- - lib/rails-formation/formatters/migrations/index.rb
131
- - lib/rails-formation/formatters/model.rb
132
- - lib/rails-formation/formatters/models/association.rb
133
- - lib/rails-formation/formatters/models/validation.rb
134
- - lib/rails-formation/formatters/models/validations/base.rb
135
- - lib/rails-formation/formatters/models/validations/comparsion.rb
136
- - lib/rails-formation/formatters/models/validations/confirmation.rb
137
- - lib/rails-formation/formatters/models/validations/exclusion.rb
138
- - lib/rails-formation/formatters/models/validations/format.rb
139
- - lib/rails-formation/formatters/models/validations/inclusion.rb
140
- - lib/rails-formation/formatters/models/validations/length.rb
141
- - lib/rails-formation/formatters/models/validations/numericality.rb
142
- - lib/rails-formation/formatters/models/validations/presence.rb
143
- - lib/rails-formation/formatters/models/validations/uniqueness.rb
144
- - lib/rails-formation/formatters/rubygem.rb
145
- - lib/rails-formation/formatters/rubygems/gem.rb
146
- - lib/rails-formation/formatters/seed.rb
147
- - lib/rails-formation/formatters/seeds/row.rb
148
- - lib/rails-formation/templates/factory.rb.tt
149
- - lib/rails-formation/templates/migration.rb.tt
150
- - lib/rails-formation/templates/model.rb.tt
151
- - lib/rails-formation/templates/seeds.rb.tt
152
96
  - lib/rails-formation/version.rb
153
97
  - lib/rails_formation.rb
154
98
  - rails-formation.gemspec
@@ -1,63 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative 'default_value'
4
- require_relative 'value'
5
-
6
- module RailsFormation
7
- module Formatters
8
- module Factories
9
- class Column
10
- def initialize(config)
11
- @config = config
12
- end
13
-
14
- def to_s
15
- if @config.key?('has_confirmation')
16
- base_part = "#{name} { #{value} }"
17
- "#{base_part}\n #{name}_confirmation { #{name} }"
18
- else
19
- "#{name} { #{value} }"
20
- end
21
- end
22
-
23
- private
24
-
25
- def name
26
- @config.fetch('name')
27
- end
28
-
29
- def sql_type
30
- @config.fetch('sql_type')
31
- end
32
-
33
- def specialized
34
- @config.fetch('specialized')
35
- end
36
-
37
- def use_sql_type?
38
- specialized == 'default'
39
- end
40
-
41
- def default_value
42
- ::RailsFormation::Formatters::Factories::DefaultValue
43
- end
44
-
45
- def value
46
- if @config.key?('in')
47
- "#{@config.fetch('in')}.sample"
48
- else
49
- raw_value
50
- end
51
- end
52
-
53
- def raw_value
54
- if use_sql_type?
55
- ::RailsFormation::Formatters::Factories::DefaultValue.detect(sql_type)
56
- else
57
- ::RailsFormation::Formatters::Factories::Value.detect(specialized)
58
- end
59
- end
60
- end
61
- end
62
- end
63
- end
@@ -1,27 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module RailsFormation
4
- module Formatters
5
- module Factories
6
- class DefaultValue
7
- MAPPINGS = {
8
- 'string' => 'FFaker::Lorem.sentence',
9
- 'text' => 'FFaker::Lorem.sentence(50)',
10
- 'boolean' => 'FFaker::Boolean.maybe',
11
- 'time' => 'Time.zone.now',
12
- 'date' => 'FFaker::Time.date',
13
- 'float' => 'FFaker::Number.decimal',
14
- 'decimal' => 'FFaker::Number.decimal',
15
- 'integer' => 'FFaker::Number.number',
16
- 'binary' => '',
17
- 'timestamp' => 'Time.now.getutc',
18
- 'datetime' => 'DateTime.now'
19
- }.freeze
20
-
21
- def self.detect(sql_type)
22
- MAPPINGS[sql_type]
23
- end
24
- end
25
- end
26
- end
27
- end
@@ -1,78 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module RailsFormation
4
- module Formatters
5
- module Factories
6
- class Value
7
- MAPPINGS = {
8
- 'first_name' => 'FFaker::Name.first_name',
9
- 'last_name' => 'FFaker::Name.last_name',
10
- 'zip_code' => 'FFaker::AddressUS.zip_code',
11
- 'state' => 'FFaker::AddressUS.state',
12
- 'city' => 'FFaker::Address.city',
13
- 'street_name' => 'FFaker::Address.street_name',
14
- 'building_number' => 'FFaker::Address.building_number',
15
- 'country_code' => 'FFaker::Address.country_code',
16
- 'country' => 'FFaker::Address.country',
17
- 'time_zone' => 'FFaker::Address.time_zone',
18
- 'plane_name' => 'FFaker::Airline.name',
19
- 'flight_number' => 'FFaker::Airline.flight_number',
20
- 'animal' => 'FFaker::AnimalUS.common_name',
21
- 'iban' => 'FFaker::Bank.iban',
22
- 'card_number' => 'FFaker::Bank.card_number',
23
- 'card_type' => 'FFaker::Bank.card_number',
24
- 'card_expiry_date' => 'FFaker::Bank.card_expiry_date',
25
- 'book_title' => 'FFaker::Book.title',
26
- 'book_genre' => 'FFaker::Book.genre',
27
- 'book_author' => 'FFaker::Book.author',
28
- 'book_isbn' => 'FFaker::Book.isbn',
29
- 'boolean' => 'FFaker::Boolean.maybe',
30
- 'color' => 'FFaker::Color.name',
31
- 'color_hex_code' => 'FFaker::Color.hex_code',
32
- 'company_name' => 'FFaker::Company.name',
33
- 'position' => 'FFaker::Company.position',
34
- 'currency_code' => 'FFaker::Currency.code',
35
- 'currency_name' => 'FFaker::Currency.name',
36
- 'currency_symbol' => 'FFaker::Currency.symbol',
37
- 'education_degree' => 'FFaker::Education.degree',
38
- 'school_name' => 'FFaker::Education.school_name',
39
- 'ingredient' => 'FFaker::Food.ingredient',
40
- 'vegetable' => 'FFaker::Food.vegetable',
41
- 'fruit' => 'FFaker::Food.fruit',
42
- 'meat' => 'FFaker::Food.meat',
43
- 'game_title' => 'FFaker::Game.title',
44
- 'game_category' => 'FFaker::Game.category',
45
- 'gender_binary' => 'FFaker::Gender.binary',
46
- 'gender_random' => 'FFaker::Gender.random',
47
- 'latitude' => 'FFaker::Geolocation.lat',
48
- 'longitude' => 'FFaker::Geolocation.lng',
49
- 'email' => 'FFaker::Internet.email',
50
- 'username' => 'FFaker::Internet.user_name',
51
- 'domain' => 'FFaker::Internet.domain_name',
52
- 'slug' => "Faker::Internet.slug(nil, '-')",
53
- 'job_title' => 'FFaker::Job.title',
54
- 'locale' => 'FFaker::Locale.code',
55
- 'language' => 'FFaker::Locale.language',
56
- 'movie' => 'FFaker::Movie.title',
57
- 'name' => 'FFaker::Name.name',
58
- 'number' => 'FFaker::Number.number',
59
- 'decimal' => 'FFaker::Number.decimal',
60
- 'phone_number' => 'FFaker::PhoneNumber.phone_number',
61
- 'brand_name' => 'FFaker::Product.brand',
62
- 'product_name' => 'FFaker::Product.product_name',
63
- 'tech_skill' => 'FFaker::Skill.tech_skill',
64
- 'specialty' => 'FFaker::Skill.specialty',
65
- 'sport_name' => 'FFaker::SportUS.name',
66
- 'ssn' => 'FFaker::SSN.ssn',
67
- 'month' => 'FFaker::Time.month',
68
- 'datetime' => 'FFaker::Time.datetime',
69
- 'date' => 'FFaker::Time.date'
70
- }.freeze
71
-
72
- def self.detect(specialized)
73
- MAPPINGS[specialized]
74
- end
75
- end
76
- end
77
- end
78
- end
@@ -1,38 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative 'factories/column'
4
-
5
- module RailsFormation
6
- module Formatters
7
- class Factory < Thor::Group
8
- include Thor::Actions
9
-
10
- argument :factory_configuration
11
- argument :factory_path
12
-
13
- def create_factory
14
- template('../templates/factory.rb.tt', factory_path)
15
- end
16
-
17
- def self.source_root
18
- __dir__
19
- end
20
-
21
- private
22
-
23
- def factory_name
24
- factory_configuration.fetch('name')
25
- end
26
-
27
- def associations
28
- []
29
- end
30
-
31
- def columns
32
- factory_configuration.fetch('fields', []).map do |config|
33
- RailsFormation::Formatters::Factories::Column.new(config).to_s
34
- end
35
- end
36
- end
37
- end
38
- end
@@ -1,45 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative 'migrations/column'
4
- require_relative 'migrations/index'
5
-
6
- module RailsFormation
7
- module Formatters
8
- class Migration < Thor::Group
9
- include Thor::Actions
10
-
11
- argument :migration_configuration
12
- argument :migration_path
13
-
14
- def create_migration
15
- template('../templates/migration.rb.tt', migration_path)
16
- end
17
-
18
- def self.source_root
19
- __dir__
20
- end
21
-
22
- private
23
-
24
- def table_name
25
- migration_configuration.fetch('table')
26
- end
27
-
28
- def timestamps?
29
- migration_configuration.fetch('timestamps', false)
30
- end
31
-
32
- def columns
33
- migration_configuration.fetch('columns', []).map do |config|
34
- RailsFormation::Formatters::Migrations::Column.new(config).to_s
35
- end
36
- end
37
-
38
- def indices
39
- migration_configuration.fetch('indices', []).map do |config|
40
- RailsFormation::Formatters::Migrations::Index.new(table_name, config).to_s
41
- end
42
- end
43
- end
44
- end
45
- end
@@ -1,33 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module RailsFormation
4
- module Formatters
5
- module Migrations
6
- class Column
7
- def initialize(config)
8
- @config = config
9
- end
10
-
11
- def to_s
12
- base_definition = [
13
- 't.',
14
- @config.fetch('type'),
15
- ' :',
16
- @config.fetch('name')
17
- ].join
18
-
19
- return base_definition if @config['options'].nil? || @config['options'].size.zero?
20
-
21
- options = @config['options'].map do |option|
22
- value = option['value']
23
- parsed_value = value.is_a?(String) ? "'#{value}'" : value
24
-
25
- "#{option['name']}: #{parsed_value}"
26
- end.join(', ')
27
-
28
- [base_definition, ', ', options].join
29
- end
30
- end
31
- end
32
- end
33
- end
@@ -1,30 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module RailsFormation
4
- module Formatters
5
- module Migrations
6
- class Index
7
- def initialize(table_name, config)
8
- @table_name = table_name
9
- @config = config
10
- end
11
-
12
- def to_s
13
- column_slug = if @config.fetch('column_names').size == 1
14
- ":#{@config.fetch('column_names').first}"
15
- else
16
- "[#{@config.fetch('column_names').map { |col| ":#{col}" }.join(', ')}]"
17
- end
18
-
19
- base_definition = "add_index :#{@table_name}, #{column_slug}"
20
-
21
- return base_definition if @config.fetch('options', []).size.zero?
22
-
23
- options = @config['options'].map { |option| option.values.join(': ') }.join(', ')
24
-
25
- "#{base_definition}, #{options}"
26
- end
27
- end
28
- end
29
- end
30
- end
@@ -1,49 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative 'models/validation'
4
- require_relative 'models/association'
5
-
6
- module RailsFormation
7
- module Formatters
8
- class Model < Thor::Group
9
- include Thor::Actions
10
-
11
- argument :model_configuration
12
- argument :model_path
13
-
14
- def create_model
15
- template('../templates/model.rb.tt', model_path)
16
- end
17
-
18
- def self.source_root
19
- __dir__
20
- end
21
-
22
- private
23
-
24
- def class_name
25
- model_configuration.fetch('name')
26
- end
27
-
28
- def associations
29
- model_configuration.fetch('associations', []).map do |config|
30
- RailsFormation::Formatters::Models::Association.new(config).to_s
31
- end
32
- end
33
-
34
- def validations
35
- models_with_validations = model_configuration.fetch('columns', []).select do |config|
36
- config.fetch('validations', []).size.positive?
37
- end
38
-
39
- models_with_validations.map do |config|
40
- config.fetch('validations', []).map do |val_config|
41
- ::RailsFormation::Formatters::Models::Validation.new(
42
- val_config.merge('column' => config.fetch('name'))
43
- ).to_s
44
- end
45
- end.flatten
46
- end
47
- end
48
- end
49
- end
@@ -1,37 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module RailsFormation
4
- module Formatters
5
- module Models
6
- class Association
7
- def initialize(config)
8
- @config = config
9
- end
10
-
11
- def to_s
12
- base_def = "#{relation} :#{destination}"
13
-
14
- return base_def if options.size.zero?
15
-
16
- options_def = options.map { |option| option.values.join(': ') }.join(', ')
17
-
18
- "#{base_def}, #{options_def}"
19
- end
20
-
21
- private
22
-
23
- def relation
24
- @config.fetch('type')
25
- end
26
-
27
- def destination
28
- @config.fetch('destination')
29
- end
30
-
31
- def options
32
- @config.fetch('options', [])
33
- end
34
- end
35
- end
36
- end
37
- end
@@ -1,41 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative 'validations/base'
4
- require_relative 'validations/presence'
5
- require_relative 'validations/uniqueness'
6
- require_relative 'validations/length'
7
- require_relative 'validations/inclusion'
8
- require_relative 'validations/exclusion'
9
- require_relative 'validations/confirmation'
10
- require_relative 'validations/format'
11
- require_relative 'validations/numericality'
12
- require_relative 'validations/comparsion'
13
-
14
- module RailsFormation
15
- module Formatters
16
- module Models
17
- class Validation
18
- MAPPINGS = {
19
- 'presence' => ::RailsFormation::Formatters::Models::Validations::Presence,
20
- 'uniqueness' => ::RailsFormation::Formatters::Models::Validations::Uniqueness,
21
- 'length' => ::RailsFormation::Formatters::Models::Validations::Length,
22
- 'inclusion' => ::RailsFormation::Formatters::Models::Validations::Inclusion,
23
- 'exclusion' => ::RailsFormation::Formatters::Models::Validations::Exclusion,
24
- 'confirmation' => ::RailsFormation::Formatters::Models::Validations::Confirmation,
25
- 'format' => ::RailsFormation::Formatters::Models::Validations::Format,
26
- 'numericality' => ::RailsFormation::Formatters::Models::Validations::Numericality,
27
- 'comparsion' => ::RailsFormation::Formatters::Models::Validations::Comparsion
28
- }.freeze
29
-
30
- def initialize(config)
31
- @config = config
32
- end
33
-
34
- def to_s
35
- klass = MAPPINGS[@config.fetch('name')]
36
- klass.new(@config).to_s
37
- end
38
- end
39
- end
40
- end
41
- end
@@ -1,62 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module RailsFormation
4
- module Formatters
5
- module Models
6
- module Validations
7
- class Base
8
- def initialize(config)
9
- @config = config
10
- end
11
-
12
- private
13
-
14
- def allow_blank_option
15
- return unless @config.fetch('allow_blank')
16
-
17
- 'allow_blank: true'
18
- end
19
-
20
- def allow_nil_option
21
- return unless @config.fetch('allow_nil')
22
-
23
- 'allow_nil: true'
24
- end
25
-
26
- def message_option
27
- return if @config.fetch('message').nil? || @config.fetch('message').empty?
28
-
29
- "message: '#{@config.fetch('message')}'"
30
- end
31
-
32
- def on_option
33
- return if @config.fetch('on_create') && @config.fetch('on_update')
34
-
35
- if @config.fetch('on_create')
36
- 'on: :create'
37
- elsif @config.fetch('on_update')
38
- 'on: :update'
39
- end
40
- end
41
-
42
- def column_name
43
- @config.fetch('column')
44
- end
45
-
46
- def base_options
47
- [
48
- allow_blank_option,
49
- allow_nil_option,
50
- message_option,
51
- on_option
52
- ].compact
53
- end
54
-
55
- def validation_options
56
- @config.fetch('options', {})
57
- end
58
- end
59
- end
60
- end
61
- end
62
- end
@@ -1,31 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module RailsFormation
4
- module Formatters
5
- module Models
6
- module Validations
7
- class Comparsion < RailsFormation::Formatters::Models::Validations::Base
8
- def to_s
9
- "validates :#{column_name}, comparsion: { #{extra_options.join(', ')} }"
10
- end
11
-
12
- private
13
-
14
- def extra_options
15
- base_options | extra_validation_options
16
- end
17
-
18
- def extra_validation_options
19
- value = if validation_options['comparsion_value_type'] == 'column_value'
20
- ":#{validation_options['comparsion_value']}"
21
- else
22
- validation_options['comparsion_value']
23
- end
24
-
25
- ["#{validation_options['comparsion_operator']}: #{value}"]
26
- end
27
- end
28
- end
29
- end
30
- end
31
- end
@@ -1,19 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module RailsFormation
4
- module Formatters
5
- module Models
6
- module Validations
7
- class Confirmation < RailsFormation::Formatters::Models::Validations::Base
8
- def to_s
9
- if base_options.any?
10
- "validates :#{column_name}, confirmation: { #{base_options.join(', ')} }"
11
- else
12
- "validates :#{column_name}, confirmation: true"
13
- end
14
- end
15
- end
16
- end
17
- end
18
- end
19
- end
@@ -1,25 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module RailsFormation
4
- module Formatters
5
- module Models
6
- module Validations
7
- class Exclusion < RailsFormation::Formatters::Models::Validations::Base
8
- def to_s
9
- "validates :#{column_name}, exclusion: { #{extra_options.join(', ')} }"
10
- end
11
-
12
- private
13
-
14
- def extra_options
15
- base_options | extra_validation_options
16
- end
17
-
18
- def extra_validation_options
19
- ["in: #{validation_options['in']}"]
20
- end
21
- end
22
- end
23
- end
24
- end
25
- end
@@ -1,31 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module RailsFormation
4
- module Formatters
5
- module Models
6
- module Validations
7
- class Format < RailsFormation::Formatters::Models::Validations::Base
8
- def to_s
9
- "validates :#{column_name}, format: { #{extra_options.join(', ')} }"
10
- end
11
-
12
- private
13
-
14
- def extra_options
15
- base_options | extra_validation_options
16
- end
17
-
18
- def extra_validation_options
19
- options = []
20
-
21
- options << "with: #{validation_options['format_value']}" if validation_options['format_matcher'] == 'with'
22
-
23
- options << "without: #{validation_options['format_value']}" if validation_options['format_matcher'] == 'without'
24
-
25
- options
26
- end
27
- end
28
- end
29
- end
30
- end
31
- end
@@ -1,25 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module RailsFormation
4
- module Formatters
5
- module Models
6
- module Validations
7
- class Inclusion < RailsFormation::Formatters::Models::Validations::Base
8
- def to_s
9
- "validates :#{column_name}, inclusion: { #{extra_options.join(', ')} }"
10
- end
11
-
12
- private
13
-
14
- def extra_options
15
- base_options | extra_validation_options
16
- end
17
-
18
- def extra_validation_options
19
- ["in: #{validation_options['in']}"]
20
- end
21
- end
22
- end
23
- end
24
- end
25
- end
@@ -1,58 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module RailsFormation
4
- module Formatters
5
- module Models
6
- module Validations
7
- class Length < RailsFormation::Formatters::Models::Validations::Base
8
- def to_s
9
- "validates :#{column_name}, length: { #{extra_options.join(', ')} }"
10
- end
11
-
12
- private
13
-
14
- def extra_options
15
- base_options | extra_validation_options
16
- end
17
-
18
- def extra_validation_options
19
- options = []
20
-
21
- options << "is: #{validation_options['length_value']}" if exact_length?
22
-
23
- options << "minimum: #{validation_options['length_min_value']}" if min_length?
24
-
25
- options << "maximum: #{validation_options['length_max_value']}" if max_length?
26
-
27
- options << "in: #{validation_options['length_min_value']}..#{validation_options['length_max_value']}" if range_length?
28
-
29
- options
30
- end
31
-
32
- def exact_length?
33
- validation_options['length_operator_type'] == 'value' &&
34
- !validation_options['length_value'].nil?
35
- end
36
-
37
- def min_length?
38
- validation_options['length_operator_type'] == 'range' &&
39
- validation_options['length_max_value'].to_i.zero? &&
40
- validation_options['length_min_value'].to_i.positive?
41
- end
42
-
43
- def max_length?
44
- validation_options['length_operator_type'] == 'range' &&
45
- validation_options['length_min_value'].to_i.zero? &&
46
- validation_options['length_max_value'].to_i.positive?
47
- end
48
-
49
- def range_length?
50
- validation_options['length_operator_type'] == 'range' &&
51
- validation_options['length_min_value'].to_i.positive? &&
52
- validation_options['length_max_value'].to_i.positive?
53
- end
54
- end
55
- end
56
- end
57
- end
58
- end
@@ -1,59 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module RailsFormation
4
- module Formatters
5
- module Models
6
- module Validations
7
- class Numericality < RailsFormation::Formatters::Models::Validations::Base
8
- def to_s
9
- "validates :#{column_name}, numericality: { #{extra_options.join(', ')} }"
10
- end
11
-
12
- private
13
-
14
- def extra_options
15
- base_options | extra_validation_options
16
- end
17
-
18
- def extra_validation_options
19
- options = []
20
-
21
- if validation_options['numericality_operator'] == 'greater_than'
22
- options << "greater_than: #{validation_options['numericality_value']}"
23
- end
24
-
25
- options << 'only_integer: true' if validation_options['numericality_operator'] == 'only_integer'
26
-
27
- if validation_options['numericality_operator'] == 'greater_than_or_equal_to'
28
- options << "greater_than_or_equal_to: #{validation_options['numericality_value']}"
29
- end
30
-
31
- if validation_options['numericality_operator'] == 'equal_to'
32
- options << "equal_to: #{validation_options['numericality_value']}"
33
- end
34
-
35
- if validation_options['numericality_operator'] == 'less_than'
36
- options << "less_than: #{validation_options['numericality_value']}"
37
- end
38
-
39
- if validation_options['numericality_operator'] == 'less_than_or_equal_to'
40
- options << "less_than_or_equal_to: #{validation_options['numericality_value']}"
41
- end
42
-
43
- if validation_options['numericality_operator'] == 'other_than'
44
- options << "other_than: #{validation_options['numericality_value']}"
45
- end
46
-
47
- options << "in: #{validation_options['numericality_value']}" if validation_options['numericality_operator'] == 'in'
48
-
49
- options << 'odd: true' if validation_options['numericality_operator'] == 'odd'
50
-
51
- options << 'even: true' if validation_options['numericality_operator'] == 'even'
52
-
53
- options
54
- end
55
- end
56
- end
57
- end
58
- end
59
- end
@@ -1,19 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module RailsFormation
4
- module Formatters
5
- module Models
6
- module Validations
7
- class Presence < RailsFormation::Formatters::Models::Validations::Base
8
- def to_s
9
- if base_options.any?
10
- "validates :#{column_name}, presence: { #{base_options.join(', ')} }"
11
- else
12
- "validates :#{column_name}, presence: true"
13
- end
14
- end
15
- end
16
- end
17
- end
18
- end
19
- end
@@ -1,58 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module RailsFormation
4
- module Formatters
5
- module Models
6
- module Validations
7
- class Uniqueness < RailsFormation::Formatters::Models::Validations::Base
8
- def to_s
9
- if extra_options.any?
10
- "validates :#{column_name}, uniqueness: { #{extra_options.join(', ')} }"
11
- else
12
- "validates :#{column_name}, uniqueness: true"
13
- end
14
- end
15
-
16
- private
17
-
18
- def extra_options
19
- base_options | extra_validation_options
20
- end
21
-
22
- def extra_validation_options
23
- options = []
24
-
25
- options << 'case_sensitive: false' unless case_sensitive?
26
-
27
- options << scope unless blank_scope?
28
-
29
- options
30
- end
31
-
32
- def case_sensitive?
33
- validation_options['uniqueness_case_sensitive'].to_i == 1
34
- end
35
-
36
- def blank_scope?
37
- validation_options['uniqueness_scope'].nil? ||
38
- validation_options['uniqueness_scope'].reject(&:empty?).size.zero?
39
- end
40
-
41
- def scope
42
- return [] if validation_options['uniqueness_scope'].nil?
43
-
44
- sanitized_scope = validation_options['uniqueness_scope'].reject(&:empty?)
45
-
46
- return [] if sanitized_scope.size.zero?
47
-
48
- if sanitized_scope.size == 1
49
- "scope: :#{sanitized_scope.first}"
50
- else
51
- "scope: [#{sanitized_scope.map { |s| ":#{s}" }.join(', ')}]"
52
- end
53
- end
54
- end
55
- end
56
- end
57
- end
58
- end
@@ -1,24 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative 'rubygems/gem'
4
-
5
- module RailsFormation
6
- module Formatters
7
- class Rubygem < Thor::Group
8
- include Thor::Actions
9
-
10
- argument :gem
11
- argument :gemfile_path
12
-
13
- def apply_to_gemfile
14
- gem_definition = ::RailsFormation::Formatters::Rubygems::Gem.new(gem, gemfile_path)
15
-
16
- insert_into_file gemfile_path, gem_definition.to_s, gem_definition.options
17
- end
18
-
19
- def self.source_root
20
- __dir__
21
- end
22
- end
23
- end
24
- end
@@ -1,56 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module RailsFormation
4
- module Formatters
5
- module Rubygems
6
- class Gem
7
- def initialize(config, gemfile_path)
8
- @config = config
9
- @gemfile_path = gemfile_path
10
- end
11
-
12
- def to_s
13
- if create_group?
14
- "#{groups_def} #{base_definition}end"
15
- elsif insert_after?
16
- " #{base_definition}"
17
- else
18
- base_definition
19
- end
20
- end
21
-
22
- def insert_after?
23
- !groups_def.nil?
24
- end
25
-
26
- def options
27
- if !create_group? && insert_after?
28
- { after: groups_def }
29
- else
30
- {}
31
- end
32
- end
33
-
34
- private
35
-
36
- def base_definition
37
- "gem '#{@config['name']}', '#{@config['version']}'\n"
38
- end
39
-
40
- def create_group?
41
- return false if groups_def.nil?
42
-
43
- !File.read(@gemfile_path).include?(groups_def.gsub("\n", ''))
44
- end
45
-
46
- def groups_def
47
- return if @config.fetch('groups', []).size.zero?
48
-
49
- groups_def = @config['groups'].map { |name| ":#{name}" }.join(', ')
50
-
51
- "group #{groups_def} do\n"
52
- end
53
- end
54
- end
55
- end
56
- end
@@ -1,30 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative 'seeds/row'
4
-
5
- module RailsFormation
6
- module Formatters
7
- class Seed < Thor::Group
8
- include Thor::Actions
9
-
10
- argument :seeds_configuration
11
- argument :seeds_path
12
-
13
- def update_seeds
14
- template('../templates/seeds.rb.tt', seeds_path)
15
- end
16
-
17
- def self.source_root
18
- __dir__
19
- end
20
-
21
- private
22
-
23
- def seeds
24
- seeds_configuration.map do |config|
25
- RailsFormation::Formatters::Seeds::Row.new(config).to_s
26
- end
27
- end
28
- end
29
- end
30
- end
@@ -1,19 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module RailsFormation
4
- module Formatters
5
- module Seeds
6
- class Row
7
- def initialize(config)
8
- @config = config
9
- end
10
-
11
- def to_s
12
- factory = "FactoryBot.create(:#{@config.fetch('factory_name')})"
13
-
14
- "#{@config.fetch('count')}.times { #{factory} }"
15
- end
16
- end
17
- end
18
- end
19
- end
@@ -1,7 +0,0 @@
1
- FactoryBot.define do
2
- factory :<%= factory_name %> do
3
- <% columns.each do |column| -%>
4
- <%= column %>
5
- <% end -%>
6
- end
7
- end
@@ -1,16 +0,0 @@
1
- class Create<%= table_name.capitalize %> < ActiveRecord::Migration[7.0]
2
- def change
3
- create_table :<%= table_name %> do |t|
4
- <% columns.each do |column| -%>
5
- <%= column %>
6
- <% end -%>
7
- <% if timestamps? -%>
8
- t.timestamps
9
- <% end -%>
10
- end
11
-
12
- <% indices.each do |index| -%>
13
- <%= index %>
14
- <% end -%>
15
- end
16
- end
@@ -1,12 +0,0 @@
1
- class <%= class_name %> < ApplicationRecord
2
- <% if validations.any? -%>
3
- <% validations.each do |validation| -%>
4
- <%= validation %>
5
- <% end -%>
6
- <% end -%>
7
- <% if associations.any? -%>
8
- <% associations.each do |association| -%>
9
- <%= association %>
10
- <% end -%>
11
- <% end -%>
12
- end
@@ -1,3 +0,0 @@
1
- <%- seeds.each do |seed| -%>
2
- <%= seed %>
3
- <% end -%>