rails-formation 0.0.0.4 → 0.0.0.6

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: 8efb69cf115eb4c7ca797d5992be87fa2a87eb43846b939454f9c59b42142e1c
4
- data.tar.gz: 237ccc291969455ce85cc68e3a73b2989ccd504550b945aeb0e82930bd6773dd
3
+ metadata.gz: 9469ea562d25b27c66b6880818b7e4988d081657f6ca3239648e8b81ceaaa056
4
+ data.tar.gz: 5f00c9fa7aaa1ba25292d1d02ac6bad396eaaa38c7ee4a36587678a0317b79ae
5
5
  SHA512:
6
- metadata.gz: 561247aecde1b2afc1f5aad8a26fb34da6130c97435d0f5a0b4748b86ab6f26e1952bd09af8c279ba1f0f8ea76e773dde4412beac5d5d5174300dd1cabdf9796
7
- data.tar.gz: '038e3871279c32d1365f7cb8beaf66bcc7f0433ba04d7fb989b80597fc84148d31d443496fc73301954a7fd7e2d3c216f90872270790f38a12ee17fed5d63cc1'
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
@@ -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
@@ -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
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RailsFormation
4
- VERSION = '0.0.0.4'
4
+ VERSION = '0.0.0.6'
5
5
  RAILS_VERSION = '7.0.2.3'
6
6
  end
@@ -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,11 +38,7 @@ module RailsFormation
34
38
  end
35
39
 
36
40
  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
41
+ system template['installation_command']
42
42
 
43
43
  Dir.chdir(template['app_name'])
44
44
  end
@@ -82,6 +82,14 @@ module RailsFormation
82
82
  end
83
83
  end
84
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
+
85
93
  def create_and_build_models(models)
86
94
  models.each do |config|
87
95
  model_name = "#{config.fetch('file_name').downcase}.rb"
@@ -100,5 +108,12 @@ module RailsFormation
100
108
 
101
109
  system('./bin/rails db:seed')
102
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
103
118
  end
104
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
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-04-23 00:00:00.000000000 Z
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
@@ -142,13 +143,17 @@ files:
142
143
  - lib/rails-formation/formatters/models/validations/numericality.rb
143
144
  - lib/rails-formation/formatters/models/validations/presence.rb
144
145
  - lib/rails-formation/formatters/models/validations/uniqueness.rb
146
+ - lib/rails-formation/formatters/route.rb
147
+ - lib/rails-formation/formatters/routes/resource.rb
145
148
  - lib/rails-formation/formatters/rubygem.rb
146
149
  - lib/rails-formation/formatters/rubygems/gem.rb
147
150
  - lib/rails-formation/formatters/seed.rb
148
151
  - lib/rails-formation/formatters/seeds/row.rb
152
+ - lib/rails-formation/templates/controller.rb.tt
149
153
  - lib/rails-formation/templates/factory.rb.tt
150
154
  - lib/rails-formation/templates/migration.rb.tt
151
155
  - lib/rails-formation/templates/model.rb.tt
156
+ - lib/rails-formation/templates/routes.rb.tt
152
157
  - lib/rails-formation/templates/seeds.rb.tt
153
158
  - lib/rails-formation/version.rb
154
159
  - lib/rails_formation.rb