rails-formation 0.0.0.2 → 0.0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/rails-formation/formatters/controller.rb +42 -0
- data/lib/rails-formation/formatters/factories/value.rb +1 -1
- data/lib/rails-formation/formatters/factory.rb +1 -1
- data/lib/rails-formation/formatters/migration.rb +11 -0
- data/lib/rails-formation/formatters/migrations/options.rb +19 -0
- data/lib/rails-formation/formatters/model.rb +7 -0
- data/lib/rails-formation/formatters/models/association.rb +37 -0
- data/lib/rails-formation/formatters/route.rb +28 -0
- data/lib/rails-formation/formatters/routes/resource.rb +73 -0
- data/lib/rails-formation/templates/controller.rb.tt +20 -0
- data/lib/rails-formation/templates/factory.rb.tt +3 -0
- data/lib/rails-formation/templates/migration.rb.tt +4 -4
- data/lib/rails-formation/templates/model.rb.tt +5 -0
- data/lib/rails-formation/templates/routes.rb.tt +24 -0
- data/lib/rails-formation/version.rb +1 -1
- data/lib/rails_formation.rb +23 -3
- metadata +10 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9469ea562d25b27c66b6880818b7e4988d081657f6ca3239648e8b81ceaaa056
|
4
|
+
data.tar.gz: 5f00c9fa7aaa1ba25292d1d02ac6bad396eaaa38c7ee4a36587678a0317b79ae
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cbd92ad8f1dc4afff5af17619048cf0e9a66788d89378f222d9e81cd096eb2893adf32dd1e558b32a80fa3972439f13039d05333e6f68135404dc6d0c4839bc8
|
7
|
+
data.tar.gz: 3226617d7cc7545841a28dc84c2d7a74e6ded6133aea534a213220841d976ab0e1561ee789a74e6d242957a3c1f9ec5c0d47fc449bb304fcc25a573160ae8030
|
@@ -0,0 +1,42 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RailsFormation
|
4
|
+
module Formatters
|
5
|
+
class Controller < Thor::Group
|
6
|
+
include Thor::Actions
|
7
|
+
|
8
|
+
argument :controller_configuration
|
9
|
+
argument :controller_path
|
10
|
+
|
11
|
+
def create_controller
|
12
|
+
template('../templates/controller.rb.tt', controller_path)
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.source_root
|
16
|
+
__dir__
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def controller_class
|
22
|
+
controller_configuration.fetch('class_name')
|
23
|
+
end
|
24
|
+
|
25
|
+
def public_actions
|
26
|
+
controller_configuration.fetch('actions').select(&method(:public_action?)).map { |action| action['action'] }
|
27
|
+
end
|
28
|
+
|
29
|
+
def private_actions
|
30
|
+
controller_configuration.fetch('actions').select(&method(:private_action?)).map { |action| action['action'] }
|
31
|
+
end
|
32
|
+
|
33
|
+
def public_action?(action)
|
34
|
+
action['access'] == 'public'
|
35
|
+
end
|
36
|
+
|
37
|
+
def private_action?(action)
|
38
|
+
action['access'] == 'private'
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -49,7 +49,7 @@ module RailsFormation
|
|
49
49
|
'email' => 'FFaker::Internet.email',
|
50
50
|
'username' => 'FFaker::Internet.user_name',
|
51
51
|
'domain' => 'FFaker::Internet.domain_name',
|
52
|
-
'slug' => "
|
52
|
+
'slug' => "FFaker::Internet.slug(nil, '-')",
|
53
53
|
'job_title' => 'FFaker::Job.title',
|
54
54
|
'locale' => 'FFaker::Locale.code',
|
55
55
|
'language' => 'FFaker::Locale.language',
|
@@ -2,6 +2,7 @@
|
|
2
2
|
|
3
3
|
require_relative 'migrations/column'
|
4
4
|
require_relative 'migrations/index'
|
5
|
+
require_relative 'migrations/options'
|
5
6
|
|
6
7
|
module RailsFormation
|
7
8
|
module Formatters
|
@@ -25,10 +26,20 @@ module RailsFormation
|
|
25
26
|
migration_configuration.fetch('table')
|
26
27
|
end
|
27
28
|
|
29
|
+
def migration_name
|
30
|
+
migration_configuration.fetch('migration_name')
|
31
|
+
end
|
32
|
+
|
28
33
|
def timestamps?
|
29
34
|
migration_configuration.fetch('timestamps', false)
|
30
35
|
end
|
31
36
|
|
37
|
+
def table_options
|
38
|
+
RailsFormation::Formatters::Migrations::Options.new(
|
39
|
+
migration_configuration.fetch('options', [])
|
40
|
+
).to_s
|
41
|
+
end
|
42
|
+
|
32
43
|
def columns
|
33
44
|
migration_configuration.fetch('columns', []).map do |config|
|
34
45
|
RailsFormation::Formatters::Migrations::Column.new(config).to_s
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RailsFormation
|
4
|
+
module Formatters
|
5
|
+
module Migrations
|
6
|
+
class Options
|
7
|
+
def initialize(options)
|
8
|
+
@options = options
|
9
|
+
end
|
10
|
+
|
11
|
+
def to_s
|
12
|
+
return if @options.size.zero?
|
13
|
+
|
14
|
+
@options.map { |option| option.values.join(': ') }.join(', ')
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -1,6 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require_relative 'models/validation'
|
4
|
+
require_relative 'models/association'
|
4
5
|
|
5
6
|
module RailsFormation
|
6
7
|
module Formatters
|
@@ -24,6 +25,12 @@ module RailsFormation
|
|
24
25
|
model_configuration.fetch('name')
|
25
26
|
end
|
26
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
|
+
|
27
34
|
def validations
|
28
35
|
models_with_validations = model_configuration.fetch('columns', []).select do |config|
|
29
36
|
config.fetch('validations', []).size.positive?
|
@@ -0,0 +1,37 @@
|
|
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
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'routes/resource'
|
4
|
+
|
5
|
+
module RailsFormation
|
6
|
+
module Formatters
|
7
|
+
class Route < Thor::Group
|
8
|
+
include Thor::Actions
|
9
|
+
|
10
|
+
argument :routes_configuration
|
11
|
+
argument :routes_path
|
12
|
+
|
13
|
+
def insert_routes
|
14
|
+
template('../templates/routes.rb.tt', routes_path)
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.source_root
|
18
|
+
__dir__
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
def resources
|
24
|
+
routes_configuration.map { |config| ::RailsFormation::Formatters::Routes::Resource.new(config) }
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,73 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RailsFormation
|
4
|
+
module Formatters
|
5
|
+
module Routes
|
6
|
+
class Resource
|
7
|
+
def initialize(config)
|
8
|
+
@config = config
|
9
|
+
end
|
10
|
+
|
11
|
+
def start_line
|
12
|
+
if actions_line.nil?
|
13
|
+
"resources :#{resource_name}"
|
14
|
+
else
|
15
|
+
"resources :#{resource_name}, #{actions_line}"
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def additional_routes
|
20
|
+
mappings = {}
|
21
|
+
|
22
|
+
mappings['member'] = member_routes.map(&method(:build_route)) if member_routes.size.positive?
|
23
|
+
|
24
|
+
mappings['collection'] = collection_routes.map(&method(:build_route)) if collection_routes.size.positive?
|
25
|
+
|
26
|
+
mappings
|
27
|
+
end
|
28
|
+
|
29
|
+
def create_block?
|
30
|
+
member_routes.size.positive? || collection_routes.size.positive?
|
31
|
+
end
|
32
|
+
|
33
|
+
private
|
34
|
+
|
35
|
+
def build_route(route)
|
36
|
+
"#{route['request']} :#{route['action']}"
|
37
|
+
end
|
38
|
+
|
39
|
+
def actions_line
|
40
|
+
return if actions.size == default_actions.size
|
41
|
+
|
42
|
+
if actions.size > 3
|
43
|
+
except_actions = (default_actions - actions).map { |action| ":#{action}" }
|
44
|
+
"except: [#{except_actions.join(', ')}]"
|
45
|
+
else
|
46
|
+
only_actions = actions.map { |action| ":#{action}" }
|
47
|
+
"only: [#{only_actions.join(', ')}]"
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def resource_name
|
52
|
+
@config.fetch('resource')
|
53
|
+
end
|
54
|
+
|
55
|
+
def actions
|
56
|
+
@config.fetch('restful_actions')
|
57
|
+
end
|
58
|
+
|
59
|
+
def default_actions
|
60
|
+
%w[index show new create edit update destroy]
|
61
|
+
end
|
62
|
+
|
63
|
+
def member_routes
|
64
|
+
@config.fetch('additional_routes').fetch('member', [])
|
65
|
+
end
|
66
|
+
|
67
|
+
def collection_routes
|
68
|
+
@config.fetch('additional_routes').fetch('collection', [])
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
class <%= controller_class %> < ApplicationController
|
2
|
+
<% if public_actions.any? -%>
|
3
|
+
<% public_actions.each do |action| -%>
|
4
|
+
def <%= action %>
|
5
|
+
|
6
|
+
end
|
7
|
+
|
8
|
+
<% end -%>
|
9
|
+
<% end -%>
|
10
|
+
<% if private_actions.any? -%>
|
11
|
+
private
|
12
|
+
|
13
|
+
<% private_actions.each do |action| -%>
|
14
|
+
def <%= action %>
|
15
|
+
|
16
|
+
end
|
17
|
+
|
18
|
+
<% end -%>
|
19
|
+
<% end -%>
|
20
|
+
end
|
@@ -1,11 +1,11 @@
|
|
1
|
-
class Create<%=
|
1
|
+
class Create<%= migration_name %> < ActiveRecord::Migration[7.0]
|
2
2
|
def change
|
3
|
-
create_table :<%= table_name %> do |t|
|
3
|
+
<% if table_options.nil? %>create_table :<%= table_name %> do |t|<% else %>create_table :<%= table_name %>, <%= table_options %> do |t|<% end %>
|
4
4
|
<% columns.each do |column| -%>
|
5
5
|
<%= column %>
|
6
6
|
<% end -%>
|
7
|
-
|
8
|
-
|
7
|
+
<%- if timestamps? -%>
|
8
|
+
t.timestamps
|
9
9
|
<% end -%>
|
10
10
|
end
|
11
11
|
|
@@ -0,0 +1,24 @@
|
|
1
|
+
Rails.application.routes.draw do
|
2
|
+
<% resources.each do |resource| -%>
|
3
|
+
<% if resource.create_block? -%>
|
4
|
+
<%= resource.start_line -%> do
|
5
|
+
<% if resource.additional_routes.key?('member') -%>
|
6
|
+
member do
|
7
|
+
<% resource.additional_routes['member'].each do |route| -%>
|
8
|
+
<%= route %>
|
9
|
+
<% end -%>
|
10
|
+
end
|
11
|
+
<% end -%>
|
12
|
+
<% if resource.additional_routes.key?('collection') -%>
|
13
|
+
collection do
|
14
|
+
<% resource.additional_routes['collection'].each do |route| -%>
|
15
|
+
<%= route %>
|
16
|
+
<% end -%>
|
17
|
+
end
|
18
|
+
<% end -%>
|
19
|
+
end
|
20
|
+
<% else -%>
|
21
|
+
<%= resource.start_line %>
|
22
|
+
<% end -%>
|
23
|
+
<% end -%>
|
24
|
+
end
|
data/lib/rails_formation.rb
CHANGED
@@ -6,6 +6,8 @@ require 'rails-formation/formatters/rubygem'
|
|
6
6
|
require 'rails-formation/formatters/model'
|
7
7
|
require 'rails-formation/formatters/factory'
|
8
8
|
require 'rails-formation/formatters/seed'
|
9
|
+
require 'rails-formation/formatters/route'
|
10
|
+
require 'rails-formation/formatters/controller'
|
9
11
|
require 'rails-formation/cli/file_adapter'
|
10
12
|
require 'rails-formation/cli/api_adapter'
|
11
13
|
require 'rails-formation/cli/processor'
|
@@ -23,6 +25,8 @@ module RailsFormation
|
|
23
25
|
create_factories(template.fetch('factories', []))
|
24
26
|
create_and_build_models(template.fetch('models', []))
|
25
27
|
insert_seeds(template.fetch('seeds', []))
|
28
|
+
create_routes(template.fetch('routes', []))
|
29
|
+
create_controllers(template.fetch('controllers', []))
|
26
30
|
end
|
27
31
|
|
28
32
|
private
|
@@ -34,8 +38,9 @@ module RailsFormation
|
|
34
38
|
end
|
35
39
|
|
36
40
|
def generate_project(template)
|
37
|
-
system
|
38
|
-
|
41
|
+
system template['installation_command']
|
42
|
+
|
43
|
+
Dir.chdir(template['app_name'])
|
39
44
|
end
|
40
45
|
|
41
46
|
def bundle_and_install_gems(rubygems)
|
@@ -77,9 +82,17 @@ module RailsFormation
|
|
77
82
|
end
|
78
83
|
end
|
79
84
|
|
85
|
+
def create_controllers(controllers)
|
86
|
+
controllers.each do |config|
|
87
|
+
controller_path = File.join(Dir.pwd, 'app', 'controllers', config['file_name'])
|
88
|
+
|
89
|
+
::RailsFormation::Formatters::Controller.new([config, controller_path]).invoke_all
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
80
93
|
def create_and_build_models(models)
|
81
94
|
models.each do |config|
|
82
|
-
model_name = "#{config.fetch('
|
95
|
+
model_name = "#{config.fetch('file_name').downcase}.rb"
|
83
96
|
model_path = File.join(Dir.pwd, 'app', 'models', model_name)
|
84
97
|
|
85
98
|
::RailsFormation::Formatters::Model.new([config, model_path]).invoke_all
|
@@ -95,5 +108,12 @@ module RailsFormation
|
|
95
108
|
|
96
109
|
system('./bin/rails db:seed')
|
97
110
|
end
|
111
|
+
|
112
|
+
def create_routes(routes)
|
113
|
+
return if routes.size.zero?
|
114
|
+
|
115
|
+
routes_path = File.join(Dir.pwd, 'config', 'routes.rb')
|
116
|
+
::RailsFormation::Formatters::Route.new([routes, routes_path]).invoke_all
|
117
|
+
end
|
98
118
|
end
|
99
119
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rails-formation
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.0.
|
4
|
+
version: 0.0.0.6
|
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-
|
11
|
+
date: 2022-05-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: ffaker
|
@@ -121,6 +121,7 @@ files:
|
|
121
121
|
- lib/rails-formation/cli/api_adapter.rb
|
122
122
|
- lib/rails-formation/cli/file_adapter.rb
|
123
123
|
- lib/rails-formation/cli/processor.rb
|
124
|
+
- lib/rails-formation/formatters/controller.rb
|
124
125
|
- lib/rails-formation/formatters/factories/column.rb
|
125
126
|
- lib/rails-formation/formatters/factories/default_value.rb
|
126
127
|
- lib/rails-formation/formatters/factories/value.rb
|
@@ -128,7 +129,9 @@ files:
|
|
128
129
|
- lib/rails-formation/formatters/migration.rb
|
129
130
|
- lib/rails-formation/formatters/migrations/column.rb
|
130
131
|
- lib/rails-formation/formatters/migrations/index.rb
|
132
|
+
- lib/rails-formation/formatters/migrations/options.rb
|
131
133
|
- lib/rails-formation/formatters/model.rb
|
134
|
+
- lib/rails-formation/formatters/models/association.rb
|
132
135
|
- lib/rails-formation/formatters/models/validation.rb
|
133
136
|
- lib/rails-formation/formatters/models/validations/base.rb
|
134
137
|
- lib/rails-formation/formatters/models/validations/comparsion.rb
|
@@ -140,13 +143,17 @@ files:
|
|
140
143
|
- lib/rails-formation/formatters/models/validations/numericality.rb
|
141
144
|
- lib/rails-formation/formatters/models/validations/presence.rb
|
142
145
|
- lib/rails-formation/formatters/models/validations/uniqueness.rb
|
146
|
+
- lib/rails-formation/formatters/route.rb
|
147
|
+
- lib/rails-formation/formatters/routes/resource.rb
|
143
148
|
- lib/rails-formation/formatters/rubygem.rb
|
144
149
|
- lib/rails-formation/formatters/rubygems/gem.rb
|
145
150
|
- lib/rails-formation/formatters/seed.rb
|
146
151
|
- lib/rails-formation/formatters/seeds/row.rb
|
152
|
+
- lib/rails-formation/templates/controller.rb.tt
|
147
153
|
- lib/rails-formation/templates/factory.rb.tt
|
148
154
|
- lib/rails-formation/templates/migration.rb.tt
|
149
155
|
- lib/rails-formation/templates/model.rb.tt
|
156
|
+
- lib/rails-formation/templates/routes.rb.tt
|
150
157
|
- lib/rails-formation/templates/seeds.rb.tt
|
151
158
|
- lib/rails-formation/version.rb
|
152
159
|
- lib/rails_formation.rb
|
@@ -175,7 +182,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
175
182
|
- !ruby/object:Gem::Version
|
176
183
|
version: '0'
|
177
184
|
requirements: []
|
178
|
-
rubygems_version: 3.
|
185
|
+
rubygems_version: 3.2.32
|
179
186
|
signing_key:
|
180
187
|
specification_version: 4
|
181
188
|
summary: Templates for Rails bootstraping
|