layered_stack 0.0.5 → 0.0.7
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 +4 -4
- data/lib/layered_stack/backend/create.rb +2 -2
- data/lib/layered_stack/frontend/create.rb +8 -8
- data/lib/layered_stack/root/create.rb +80 -0
- data/lib/layered_stack/version.rb +1 -1
- data/lib/layered_stack.rb +11 -4
- data/{assets/backend/template.rb → templates/backend/rails_template.rb} +18 -20
- data/templates/root/Gemfile +10 -0
- data/templates/root/Procfile +2 -0
- data/templates/root/devcontainer/Dockerfile +9 -0
- data/templates/root/devcontainer/devcontainer.json +19 -0
- data/templates/root/devcontainer/docker-compose.yml +12 -0
- data/templates/root/devcontainer/layered-stack-0.0.1.vsix +0 -0
- data/templates/root/layered_stack/cli/LICENSE.md +21 -0
- data/templates/root/layered_stack/cli/bin/layered_stack +3 -0
- data/templates/root/layered_stack/cli/cli/LICENSE.md +21 -0
- data/templates/root/layered_stack/cli/cli/bin/layered_stack +3 -0
- data/templates/root/layered_stack/cli/cli/layered_stack-0.0.7.gem +0 -0
- data/templates/root/layered_stack/cli/cli/layered_stack.gemspec +30 -0
- data/templates/root/layered_stack/cli/cli/lib/layered_stack/backend/create.rb +42 -0
- data/templates/root/layered_stack/cli/cli/lib/layered_stack/backend/start.rb +30 -0
- data/templates/root/layered_stack/cli/cli/lib/layered_stack/frontend/create.rb +137 -0
- data/templates/root/layered_stack/cli/cli/lib/layered_stack/frontend/start.rb +33 -0
- data/templates/root/layered_stack/cli/cli/lib/layered_stack/root/create.rb +80 -0
- data/templates/root/layered_stack/cli/cli/lib/layered_stack/version.rb +5 -0
- data/templates/root/layered_stack/cli/cli/lib/layered_stack.rb +63 -0
- data/templates/root/layered_stack/cli/cli/templates/backend/rails_template.rb +51 -0
- data/templates/root/layered_stack/cli/cli/templates/frontend/images/logo_dark.svg +29 -0
- data/templates/root/layered_stack/cli/cli/templates/frontend/images/logo_light.svg +29 -0
- data/templates/root/layered_stack/cli/cli/templates/frontend/layout.js +21 -0
- data/templates/root/layered_stack/cli/cli/templates/frontend/page.js +35 -0
- data/templates/root/layered_stack/cli/cli/templates/frontend/tailwind.config.js +19 -0
- data/templates/root/layered_stack/cli/cli/templates/root/Gemfile +10 -0
- data/templates/root/layered_stack/cli/cli/templates/root/Procfile +2 -0
- data/templates/root/layered_stack/cli/cli/templates/root/devcontainer/Dockerfile +9 -0
- data/templates/root/layered_stack/cli/cli/templates/root/devcontainer/devcontainer.json +19 -0
- data/templates/root/layered_stack/cli/cli/templates/root/devcontainer/docker-compose.yml +12 -0
- data/templates/root/layered_stack/cli/cli/templates/root/devcontainer/layered-stack-0.0.1.vsix +0 -0
- data/templates/root/layered_stack/cli/cli/templates/root/layered_stack/cli/LICENSE.md +21 -0
- data/templates/root/layered_stack/cli/cli/templates/root/layered_stack/cli/bin/layered_stack +3 -0
- data/templates/root/layered_stack/cli/cli/templates/root/layered_stack/cli/layered_stack-0.0.6.gem +0 -0
- data/templates/root/layered_stack/cli/cli/templates/root/layered_stack/cli/layered_stack-0.0.7.gem +0 -0
- data/templates/root/layered_stack/cli/cli/templates/root/layered_stack/cli/layered_stack.gemspec +30 -0
- data/templates/root/layered_stack/cli/cli/templates/root/layered_stack/cli/lib/layered_stack/backend/create.rb +42 -0
- data/templates/root/layered_stack/cli/cli/templates/root/layered_stack/cli/lib/layered_stack/backend/start.rb +30 -0
- data/templates/root/layered_stack/cli/cli/templates/root/layered_stack/cli/lib/layered_stack/frontend/create.rb +137 -0
- data/templates/root/layered_stack/cli/cli/templates/root/layered_stack/cli/lib/layered_stack/frontend/start.rb +33 -0
- data/templates/root/layered_stack/cli/cli/templates/root/layered_stack/cli/lib/layered_stack/root/create.rb +80 -0
- data/templates/root/layered_stack/cli/cli/templates/root/layered_stack/cli/lib/layered_stack/version.rb +5 -0
- data/templates/root/layered_stack/cli/cli/templates/root/layered_stack/cli/lib/layered_stack.rb +63 -0
- data/templates/root/layered_stack/cli/cli/templates/root/layered_stack/cli/templates/backend/rails_template.rb +51 -0
- data/templates/root/layered_stack/cli/cli/templates/root/layered_stack/cli/templates/frontend/images/logo_dark.svg +29 -0
- data/templates/root/layered_stack/cli/cli/templates/root/layered_stack/cli/templates/frontend/images/logo_light.svg +29 -0
- data/templates/root/layered_stack/cli/cli/templates/root/layered_stack/cli/templates/frontend/layout.js +21 -0
- data/templates/root/layered_stack/cli/cli/templates/root/layered_stack/cli/templates/frontend/page.js +35 -0
- data/templates/root/layered_stack/cli/cli/templates/root/layered_stack/cli/templates/frontend/tailwind.config.js +19 -0
- data/templates/root/layered_stack/cli/cli/templates/root/layered_stack/cli/templates/root/Gemfile +11 -0
- data/templates/root/layered_stack/cli/cli/templates/root/layered_stack/cli/templates/root/Procfile +2 -0
- data/templates/root/layered_stack/cli/cli/templates/root/layered_stack/cli/templates/root/devcontainer/Dockerfile +9 -0
- data/templates/root/layered_stack/cli/cli/templates/root/layered_stack/cli/templates/root/devcontainer/devcontainer.json +19 -0
- data/templates/root/layered_stack/cli/cli/templates/root/layered_stack/cli/templates/root/devcontainer/docker-compose.yml +12 -0
- data/templates/root/layered_stack/cli/cli/templates/root/layered_stack/cli/templates/root/devcontainer/layered-stack-0.0.1.vsix +0 -0
- data/templates/root/layered_stack/cli/cli/templates/root/layered_stack/cli/templates/root/layered_stack/rails/LICENSE.md +21 -0
- data/templates/root/layered_stack/cli/cli/templates/root/layered_stack/cli/templates/root/layered_stack/rails/bin/layered_stack-rails +3 -0
- data/templates/root/layered_stack/cli/cli/templates/root/layered_stack/cli/templates/root/layered_stack/rails/layered_stack-rails-0.0.1.gem +0 -0
- data/templates/root/layered_stack/cli/cli/templates/root/layered_stack/cli/templates/root/layered_stack/rails/layered_stack-rails.gemspec +29 -0
- data/templates/root/layered_stack/cli/cli/templates/root/layered_stack/cli/templates/root/layered_stack/rails/lib/generators/layered_stack_rails/scaffold_generator.rb +89 -0
- data/templates/root/layered_stack/cli/cli/templates/root/layered_stack/cli/templates/root/layered_stack/rails/lib/generators/layered_stack_rails/templates/active_record/model/model.erb +9 -0
- data/templates/root/layered_stack/cli/cli/templates/root/layered_stack/cli/templates/root/layered_stack/rails/lib/layered_stack_rails/example.rb +97 -0
- data/templates/root/layered_stack/cli/cli/templates/root/layered_stack/cli/templates/root/layered_stack/rails/lib/layered_stack_rails/generate.rb +34 -0
- data/templates/root/layered_stack/cli/cli/templates/root/layered_stack/cli/templates/root/layered_stack/rails/lib/layered_stack_rails/generate_new.rb +34 -0
- data/templates/root/layered_stack/cli/cli/templates/root/layered_stack/cli/templates/root/layered_stack/rails/lib/layered_stack_rails/version.rb +5 -0
- data/templates/root/layered_stack/cli/cli/templates/root/layered_stack/cli/templates/root/layered_stack/rails/lib/layered_stack_rails.rb +26 -0
- data/templates/root/layered_stack/cli/cli/templates/root/layered_stack/rails/LICENSE.md +21 -0
- data/templates/root/layered_stack/cli/cli/templates/root/layered_stack/rails/bin/layered_stack-rails +3 -0
- data/templates/root/layered_stack/cli/cli/templates/root/layered_stack/rails/layered_stack-rails-0.0.1.gem +0 -0
- data/templates/root/layered_stack/cli/cli/templates/root/layered_stack/rails/layered_stack-rails.gemspec +29 -0
- data/templates/root/layered_stack/cli/cli/templates/root/layered_stack/rails/lib/generators/layered_stack_rails/scaffold_generator.rb +89 -0
- data/templates/root/layered_stack/cli/cli/templates/root/layered_stack/rails/lib/generators/layered_stack_rails/templates/active_record/model/model.erb +9 -0
- data/templates/root/layered_stack/cli/cli/templates/root/layered_stack/rails/lib/layered_stack_rails/example.rb +97 -0
- data/templates/root/layered_stack/cli/cli/templates/root/layered_stack/rails/lib/layered_stack_rails/generate.rb +34 -0
- data/templates/root/layered_stack/cli/cli/templates/root/layered_stack/rails/lib/layered_stack_rails/generate_new.rb +34 -0
- data/templates/root/layered_stack/cli/cli/templates/root/layered_stack/rails/lib/layered_stack_rails/version.rb +5 -0
- data/templates/root/layered_stack/cli/cli/templates/root/layered_stack/rails/lib/layered_stack_rails.rb +26 -0
- data/templates/root/layered_stack/cli/cli/templates/root/layered_stack/rails/rails/LICENSE.md +21 -0
- data/templates/root/layered_stack/cli/cli/templates/root/layered_stack/rails/rails/bin/layered_stack-rails +3 -0
- data/templates/root/layered_stack/cli/cli/templates/root/layered_stack/rails/rails/layered_stack-rails-0.0.1.gem +0 -0
- data/templates/root/layered_stack/cli/cli/templates/root/layered_stack/rails/rails/layered_stack-rails.gemspec +29 -0
- data/templates/root/layered_stack/cli/cli/templates/root/layered_stack/rails/rails/lib/generators/layered_stack_rails/scaffold_generator.rb +89 -0
- data/templates/root/layered_stack/cli/cli/templates/root/layered_stack/rails/rails/lib/generators/layered_stack_rails/templates/active_record/model/model.erb +9 -0
- data/templates/root/layered_stack/cli/cli/templates/root/layered_stack/rails/rails/lib/layered_stack_rails/example.rb +97 -0
- data/templates/root/layered_stack/cli/cli/templates/root/layered_stack/rails/rails/lib/layered_stack_rails/generate.rb +34 -0
- data/templates/root/layered_stack/cli/cli/templates/root/layered_stack/rails/rails/lib/layered_stack_rails/generate_new.rb +34 -0
- data/templates/root/layered_stack/cli/cli/templates/root/layered_stack/rails/rails/lib/layered_stack_rails/version.rb +5 -0
- data/templates/root/layered_stack/cli/cli/templates/root/layered_stack/rails/rails/lib/layered_stack_rails.rb +26 -0
- data/templates/root/layered_stack/cli/layered_stack-0.0.6.gem +0 -0
- data/templates/root/layered_stack/cli/layered_stack-0.0.7.gem +0 -0
- data/templates/root/layered_stack/cli/layered_stack.gemspec +30 -0
- data/templates/root/layered_stack/cli/lib/layered_stack/backend/create.rb +42 -0
- data/templates/root/layered_stack/cli/lib/layered_stack/backend/start.rb +30 -0
- data/templates/root/layered_stack/cli/lib/layered_stack/frontend/create.rb +137 -0
- data/templates/root/layered_stack/cli/lib/layered_stack/frontend/start.rb +33 -0
- data/templates/root/layered_stack/cli/lib/layered_stack/root/create.rb +80 -0
- data/templates/root/layered_stack/cli/lib/layered_stack/version.rb +5 -0
- data/templates/root/layered_stack/cli/lib/layered_stack.rb +63 -0
- data/templates/root/layered_stack/cli/templates/backend/rails_template.rb +51 -0
- data/templates/root/layered_stack/cli/templates/frontend/images/logo_dark.svg +29 -0
- data/templates/root/layered_stack/cli/templates/frontend/images/logo_light.svg +29 -0
- data/templates/root/layered_stack/cli/templates/frontend/layout.js +21 -0
- data/templates/root/layered_stack/cli/templates/frontend/page.js +35 -0
- data/templates/root/layered_stack/cli/templates/frontend/tailwind.config.js +19 -0
- data/templates/root/layered_stack/cli/templates/root/Gemfile +11 -0
- data/templates/root/layered_stack/cli/templates/root/Procfile +2 -0
- data/templates/root/layered_stack/cli/templates/root/devcontainer/Dockerfile +9 -0
- data/templates/root/layered_stack/cli/templates/root/devcontainer/devcontainer.json +19 -0
- data/templates/root/layered_stack/cli/templates/root/devcontainer/docker-compose.yml +12 -0
- data/templates/root/layered_stack/cli/templates/root/devcontainer/layered-stack-0.0.1.vsix +0 -0
- data/templates/root/layered_stack/cli/templates/root/layered_stack/rails/LICENSE.md +21 -0
- data/templates/root/layered_stack/cli/templates/root/layered_stack/rails/bin/layered_stack-rails +3 -0
- data/templates/root/layered_stack/cli/templates/root/layered_stack/rails/layered_stack-rails-0.0.1.gem +0 -0
- data/templates/root/layered_stack/cli/templates/root/layered_stack/rails/layered_stack-rails.gemspec +29 -0
- data/templates/root/layered_stack/cli/templates/root/layered_stack/rails/lib/generators/layered_stack_rails/scaffold_generator.rb +89 -0
- data/templates/root/layered_stack/cli/templates/root/layered_stack/rails/lib/generators/layered_stack_rails/templates/active_record/model/model.erb +9 -0
- data/templates/root/layered_stack/cli/templates/root/layered_stack/rails/lib/layered_stack_rails/example.rb +97 -0
- data/templates/root/layered_stack/cli/templates/root/layered_stack/rails/lib/layered_stack_rails/generate.rb +34 -0
- data/templates/root/layered_stack/cli/templates/root/layered_stack/rails/lib/layered_stack_rails/generate_new.rb +34 -0
- data/templates/root/layered_stack/cli/templates/root/layered_stack/rails/lib/layered_stack_rails/version.rb +5 -0
- data/templates/root/layered_stack/cli/templates/root/layered_stack/rails/lib/layered_stack_rails.rb +26 -0
- data/templates/root/layered_stack/rails/LICENSE.md +21 -0
- data/templates/root/layered_stack/rails/bin/layered_stack-rails +3 -0
- data/templates/root/layered_stack/rails/layered_stack-rails-0.0.1.gem +0 -0
- data/templates/root/layered_stack/rails/layered_stack-rails.gemspec +29 -0
- data/templates/root/layered_stack/rails/lib/generators/layered_stack_rails/scaffold_generator.rb +89 -0
- data/templates/root/layered_stack/rails/lib/generators/layered_stack_rails/templates/active_record/model/model.erb +9 -0
- data/templates/root/layered_stack/rails/lib/layered_stack_rails/example.rb +97 -0
- data/templates/root/layered_stack/rails/lib/layered_stack_rails/generate.rb +34 -0
- data/templates/root/layered_stack/rails/lib/layered_stack_rails/generate_new.rb +34 -0
- data/templates/root/layered_stack/rails/lib/layered_stack_rails/version.rb +5 -0
- data/templates/root/layered_stack/rails/lib/layered_stack_rails.rb +26 -0
- data/templates/root/layered_stack/rails/rails/LICENSE.md +21 -0
- data/templates/root/layered_stack/rails/rails/bin/layered_stack-rails +3 -0
- data/templates/root/layered_stack/rails/rails/layered_stack-rails-0.0.1.gem +0 -0
- data/templates/root/layered_stack/rails/rails/layered_stack-rails.gemspec +29 -0
- data/templates/root/layered_stack/rails/rails/lib/generators/layered_stack_rails/scaffold_generator.rb +89 -0
- data/templates/root/layered_stack/rails/rails/lib/generators/layered_stack_rails/templates/active_record/model/model.erb +9 -0
- data/templates/root/layered_stack/rails/rails/lib/layered_stack_rails/example.rb +97 -0
- data/templates/root/layered_stack/rails/rails/lib/layered_stack_rails/generate.rb +34 -0
- data/templates/root/layered_stack/rails/rails/lib/layered_stack_rails/generate_new.rb +34 -0
- data/templates/root/layered_stack/rails/rails/lib/layered_stack_rails/version.rb +5 -0
- data/templates/root/layered_stack/rails/rails/lib/layered_stack_rails.rb +26 -0
- metadata +160 -16
- /data/{assets → templates/frontend}/images/logo_dark.svg +0 -0
- /data/{assets → templates/frontend}/images/logo_light.svg +0 -0
- /data/{assets → templates/frontend}/layout.js +0 -0
- /data/{assets → templates/frontend}/page.js +0 -0
- /data/{assets → templates/frontend}/tailwind.config.js +0 -0
Binary file
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require_relative 'lib/layered_stack_rails/version'
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
# General
|
5
|
+
s.name = 'layered_stack-rails'
|
6
|
+
s.version = LayeredStackRails::VERSION
|
7
|
+
s.authors = ['Layered Stack']
|
8
|
+
s.email = ['support@layeredstack.org']
|
9
|
+
|
10
|
+
# About
|
11
|
+
s.summary = 'Layered Stack Rails'
|
12
|
+
s.description = 'Rails tools for Layered Stack'
|
13
|
+
s.homepage = 'https://www.layeredstack.org/'
|
14
|
+
s.license = 'MIT'
|
15
|
+
|
16
|
+
# Metadata
|
17
|
+
s.metadata['homepage_uri'] = s.homepage
|
18
|
+
s.metadata['source_code_uri'] = 'https://github.com/LayeredStack/rails'
|
19
|
+
|
20
|
+
# Ruby
|
21
|
+
s.required_ruby_version = '>=3.3.4'
|
22
|
+
|
23
|
+
# Files and executables
|
24
|
+
s.files = Dir.glob('lib/**/*.rb') + Dir.glob('assets/**/*')
|
25
|
+
s.executables = ['layered_stack-rails']
|
26
|
+
|
27
|
+
# Dependencies
|
28
|
+
s.add_dependency "thor", "~> 1.3.2"
|
29
|
+
end
|
@@ -0,0 +1,89 @@
|
|
1
|
+
require "rails"
|
2
|
+
require "rails/generators"
|
3
|
+
require "yaml"
|
4
|
+
|
5
|
+
class LayeredStackRails::ScaffoldGenerator < Rails::Generators::Base
|
6
|
+
source_root File.expand_path("templates", __dir__)
|
7
|
+
argument :model_name, type: :string
|
8
|
+
|
9
|
+
def execute
|
10
|
+
yaml_content = YAML.load_file(File.join("layered_stack", "resources.yml"))
|
11
|
+
|
12
|
+
model_data = yaml_content["resources"][model_name]
|
13
|
+
if model_data
|
14
|
+
puts "Generating model #{model_name} with attributes: #{model_data["attributes"]}"
|
15
|
+
generate_scaffold(model_name, model_data["attributes"], model_data["associations"])
|
16
|
+
else
|
17
|
+
puts "Model #{model_name} not found in resources.yml"
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def create_model_file
|
22
|
+
template "active_record/model/model.erb", File.join("app/models", "#{model_name}.rb")
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def generate_scaffold(model_name, attributes, associations)
|
28
|
+
@attributes = attributes.map do |name, details|
|
29
|
+
if details['password_digest']
|
30
|
+
"#{name}:digest"
|
31
|
+
elsif details['token']
|
32
|
+
"#{name}:token"
|
33
|
+
else
|
34
|
+
attribute_string = if details['type'] == 'decimal'
|
35
|
+
precision = details['precision']
|
36
|
+
scale = details['scale']
|
37
|
+
"#{name}:decimal{#{precision}.#{scale}}"
|
38
|
+
else
|
39
|
+
type = details['type']
|
40
|
+
limit = details['limit']
|
41
|
+
limit_string = limit ? "{#{limit}}" : ""
|
42
|
+
"#{name}:#{type}#{limit_string}"
|
43
|
+
end
|
44
|
+
|
45
|
+
attribute_string += ":index" if details['index'] == true
|
46
|
+
attribute_string += ":uniq" if details['index'] == 'uniq'
|
47
|
+
|
48
|
+
attribute_string
|
49
|
+
end
|
50
|
+
end.join(" ")
|
51
|
+
|
52
|
+
@class_name = model_name.classify
|
53
|
+
|
54
|
+
@validations = []
|
55
|
+
attributes.each do |name, details|
|
56
|
+
if details['required']
|
57
|
+
@validations << "validates :#{name}, presence: true"
|
58
|
+
end
|
59
|
+
if details['type'] == 'integer'
|
60
|
+
@validations << "validates :#{name}, numericality: { only_integer: true }"
|
61
|
+
end
|
62
|
+
if details['precision'] && details['scale']
|
63
|
+
@validations << "validates :#{name}, numericality: true"
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
@associations = []
|
68
|
+
associations.each do |name, details|
|
69
|
+
case details['type']
|
70
|
+
when 'has_many'
|
71
|
+
if details['through']
|
72
|
+
@associations << "has_many :#{name}, through: :#{details['through']}"
|
73
|
+
else
|
74
|
+
@associations << "has_many :#{name}"
|
75
|
+
end
|
76
|
+
when 'belongs_to'
|
77
|
+
@associations << "belongs_to :#{name}"
|
78
|
+
end
|
79
|
+
end if associations
|
80
|
+
|
81
|
+
# Generate the scaffold command
|
82
|
+
invoke "rails:scaffold", [model_name.camelize, *@attributes.split(" "), "--api"], force: true
|
83
|
+
|
84
|
+
# ToDo: Write a custom generator that avoids this workaround
|
85
|
+
# Delete the model file after scaffolding to avoid duplicate model files
|
86
|
+
model_file_path = File.join("app/models", "#{model_name}.rb")
|
87
|
+
File.delete(model_file_path) if File.exist?(model_file_path)
|
88
|
+
end
|
89
|
+
end
|
@@ -0,0 +1,9 @@
|
|
1
|
+
class <%= @class_name %> < ApplicationRecord
|
2
|
+
<%= "# <associations>" %>
|
3
|
+
<%= @associations.map { |association| association }.join("\n ") %>
|
4
|
+
<%= "# </associations>" %>
|
5
|
+
|
6
|
+
<%= "# <validations>" %>
|
7
|
+
<%= @validations.map { |validation| validation }.join("\n ") %>
|
8
|
+
<%= "# </validations>" %>
|
9
|
+
end
|
@@ -0,0 +1,97 @@
|
|
1
|
+
require 'thor'
|
2
|
+
require 'logger'
|
3
|
+
|
4
|
+
module LayeredStackRails
|
5
|
+
class Example < Thor
|
6
|
+
def self.execute
|
7
|
+
new.execute
|
8
|
+
end
|
9
|
+
|
10
|
+
no_commands do
|
11
|
+
def execute
|
12
|
+
logger.info("> layered_stack-rails/example")
|
13
|
+
|
14
|
+
# Ensure the directory exists
|
15
|
+
directory_path = 'layered_stack'
|
16
|
+
unless File.directory?(directory_path)
|
17
|
+
FileUtils.mkdir_p(directory_path)
|
18
|
+
end
|
19
|
+
|
20
|
+
# Create the example product.yml file
|
21
|
+
example_product = File.join(directory_path, 'resources.yml')
|
22
|
+
unless File.exist?(example_product)
|
23
|
+
File.open(example_product, 'w') do |file|
|
24
|
+
file.write(<<~YAML)
|
25
|
+
resources:
|
26
|
+
product:
|
27
|
+
attributes:
|
28
|
+
name:
|
29
|
+
type: string
|
30
|
+
required: true
|
31
|
+
index: uniq
|
32
|
+
description:
|
33
|
+
type: text
|
34
|
+
price:
|
35
|
+
type: decimal
|
36
|
+
precision: 10
|
37
|
+
scale: 2
|
38
|
+
required: true
|
39
|
+
stock_quantity:
|
40
|
+
type: integer
|
41
|
+
required: true
|
42
|
+
associations:
|
43
|
+
order_items:
|
44
|
+
type: has_many
|
45
|
+
orders:
|
46
|
+
type: has_many
|
47
|
+
through: order_items
|
48
|
+
|
49
|
+
order:
|
50
|
+
attributes:
|
51
|
+
order_number:
|
52
|
+
type: string
|
53
|
+
required: true
|
54
|
+
index: uniq
|
55
|
+
order_date:
|
56
|
+
type: datetime
|
57
|
+
required: true
|
58
|
+
total_amount:
|
59
|
+
type: decimal
|
60
|
+
precision: 10
|
61
|
+
scale: 2
|
62
|
+
required: true
|
63
|
+
associations:
|
64
|
+
order_items:
|
65
|
+
type: has_many
|
66
|
+
products:
|
67
|
+
type: has_many
|
68
|
+
through: order_items
|
69
|
+
|
70
|
+
order_item:
|
71
|
+
attributes:
|
72
|
+
quantity:
|
73
|
+
type: integer
|
74
|
+
required: true
|
75
|
+
price:
|
76
|
+
type: decimal
|
77
|
+
precision: 10
|
78
|
+
scale: 2
|
79
|
+
required: true
|
80
|
+
associations:
|
81
|
+
order:
|
82
|
+
type: belongs_to
|
83
|
+
product:
|
84
|
+
type: belongs_to
|
85
|
+
YAML
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
private
|
91
|
+
|
92
|
+
def logger
|
93
|
+
@logger ||= Logger.new(STDOUT)
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'thor'
|
2
|
+
require 'logger'
|
3
|
+
require 'yaml'
|
4
|
+
require 'rails'
|
5
|
+
|
6
|
+
module LayeredStackRails
|
7
|
+
class Generate < Thor
|
8
|
+
def self.execute
|
9
|
+
new.execute
|
10
|
+
end
|
11
|
+
|
12
|
+
no_commands do
|
13
|
+
def execute
|
14
|
+
logger.info("> layered_stack-rails/generate_new")
|
15
|
+
|
16
|
+
yaml_content = YAML.load_file(File.join("layered_stack", "resources.yml"))
|
17
|
+
resources = yaml_content["resources"]
|
18
|
+
resources.each do |model_name, model_data|
|
19
|
+
if model_data
|
20
|
+
system("rails generate layered_stack_rails:scaffold #{model_name}")
|
21
|
+
else
|
22
|
+
logger.error("Model #{model_name} not found in resources.yml")
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
def logger
|
30
|
+
@logger ||= Logger.new(STDOUT)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'thor'
|
2
|
+
require 'logger'
|
3
|
+
require 'yaml'
|
4
|
+
require 'rails'
|
5
|
+
|
6
|
+
module LayeredStackRails
|
7
|
+
class GenerateNew < Thor
|
8
|
+
def self.execute
|
9
|
+
new.execute
|
10
|
+
end
|
11
|
+
|
12
|
+
no_commands do
|
13
|
+
def execute
|
14
|
+
logger.info("> layered_stack-rails/generate_new")
|
15
|
+
|
16
|
+
yaml_content = YAML.load_file(File.join("layered_stack", "resources.yml"))
|
17
|
+
resources = yaml_content["resources"]
|
18
|
+
resources.each do |model_name, model_data|
|
19
|
+
if model_data
|
20
|
+
system("rails generate layered_stack_rails:scaffold #{model_name}")
|
21
|
+
else
|
22
|
+
logger.error("Model #{model_name} not found in resources.yml")
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
def logger
|
30
|
+
@logger ||= Logger.new(STDOUT)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'thor'
|
2
|
+
require 'fileutils'
|
3
|
+
|
4
|
+
# Require all .rb files in the layered_stack_rails folder recursively
|
5
|
+
Dir[File.join(__dir__, 'layered_stack_rails/**/*.rb')].each { |file| require_relative file }
|
6
|
+
|
7
|
+
module LayeredStackRails
|
8
|
+
class Cli < Thor
|
9
|
+
desc "product", "Creates an example product.yml resource file in the layered_stack/resources directory"
|
10
|
+
def example
|
11
|
+
LayeredStackRails::Example.execute
|
12
|
+
end
|
13
|
+
|
14
|
+
desc "generate", "Generates resources from ./layered_stack/resources.yml"
|
15
|
+
def generate
|
16
|
+
LayeredStackRails::Generate.execute
|
17
|
+
end
|
18
|
+
|
19
|
+
desc "dev", "Run example and generate with 'product' as the name"
|
20
|
+
def dev
|
21
|
+
example
|
22
|
+
generate
|
23
|
+
system('rails db:migrate')
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2024 Layered Stack
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
Binary file
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require_relative 'lib/layered_stack_rails/version'
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
# General
|
5
|
+
s.name = 'layered_stack-rails'
|
6
|
+
s.version = LayeredStackRails::VERSION
|
7
|
+
s.authors = ['Layered Stack']
|
8
|
+
s.email = ['support@layeredstack.org']
|
9
|
+
|
10
|
+
# About
|
11
|
+
s.summary = 'Layered Stack Rails'
|
12
|
+
s.description = 'Rails tools for Layered Stack'
|
13
|
+
s.homepage = 'https://www.layeredstack.org/'
|
14
|
+
s.license = 'MIT'
|
15
|
+
|
16
|
+
# Metadata
|
17
|
+
s.metadata['homepage_uri'] = s.homepage
|
18
|
+
s.metadata['source_code_uri'] = 'https://github.com/LayeredStack/rails'
|
19
|
+
|
20
|
+
# Ruby
|
21
|
+
s.required_ruby_version = '>=3.3.4'
|
22
|
+
|
23
|
+
# Files and executables
|
24
|
+
s.files = Dir.glob('lib/**/*.rb') + Dir.glob('assets/**/*')
|
25
|
+
s.executables = ['layered_stack-rails']
|
26
|
+
|
27
|
+
# Dependencies
|
28
|
+
s.add_dependency "thor", "~> 1.3.2"
|
29
|
+
end
|
@@ -0,0 +1,89 @@
|
|
1
|
+
require "rails"
|
2
|
+
require "rails/generators"
|
3
|
+
require "yaml"
|
4
|
+
|
5
|
+
class LayeredStackRails::ScaffoldGenerator < Rails::Generators::Base
|
6
|
+
source_root File.expand_path("templates", __dir__)
|
7
|
+
argument :model_name, type: :string
|
8
|
+
|
9
|
+
def execute
|
10
|
+
yaml_content = YAML.load_file(File.join("layered_stack", "resources.yml"))
|
11
|
+
|
12
|
+
model_data = yaml_content["resources"][model_name]
|
13
|
+
if model_data
|
14
|
+
puts "Generating model #{model_name} with attributes: #{model_data["attributes"]}"
|
15
|
+
generate_scaffold(model_name, model_data["attributes"], model_data["associations"])
|
16
|
+
else
|
17
|
+
puts "Model #{model_name} not found in resources.yml"
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def create_model_file
|
22
|
+
template "active_record/model/model.erb", File.join("app/models", "#{model_name}.rb")
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def generate_scaffold(model_name, attributes, associations)
|
28
|
+
@attributes = attributes.map do |name, details|
|
29
|
+
if details['password_digest']
|
30
|
+
"#{name}:digest"
|
31
|
+
elsif details['token']
|
32
|
+
"#{name}:token"
|
33
|
+
else
|
34
|
+
attribute_string = if details['type'] == 'decimal'
|
35
|
+
precision = details['precision']
|
36
|
+
scale = details['scale']
|
37
|
+
"#{name}:decimal{#{precision}.#{scale}}"
|
38
|
+
else
|
39
|
+
type = details['type']
|
40
|
+
limit = details['limit']
|
41
|
+
limit_string = limit ? "{#{limit}}" : ""
|
42
|
+
"#{name}:#{type}#{limit_string}"
|
43
|
+
end
|
44
|
+
|
45
|
+
attribute_string += ":index" if details['index'] == true
|
46
|
+
attribute_string += ":uniq" if details['index'] == 'uniq'
|
47
|
+
|
48
|
+
attribute_string
|
49
|
+
end
|
50
|
+
end.join(" ")
|
51
|
+
|
52
|
+
@class_name = model_name.classify
|
53
|
+
|
54
|
+
@validations = []
|
55
|
+
attributes.each do |name, details|
|
56
|
+
if details['required']
|
57
|
+
@validations << "validates :#{name}, presence: true"
|
58
|
+
end
|
59
|
+
if details['type'] == 'integer'
|
60
|
+
@validations << "validates :#{name}, numericality: { only_integer: true }"
|
61
|
+
end
|
62
|
+
if details['precision'] && details['scale']
|
63
|
+
@validations << "validates :#{name}, numericality: true"
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
@associations = []
|
68
|
+
associations.each do |name, details|
|
69
|
+
case details['type']
|
70
|
+
when 'has_many'
|
71
|
+
if details['through']
|
72
|
+
@associations << "has_many :#{name}, through: :#{details['through']}"
|
73
|
+
else
|
74
|
+
@associations << "has_many :#{name}"
|
75
|
+
end
|
76
|
+
when 'belongs_to'
|
77
|
+
@associations << "belongs_to :#{name}"
|
78
|
+
end
|
79
|
+
end if associations
|
80
|
+
|
81
|
+
# Generate the scaffold command
|
82
|
+
invoke "rails:scaffold", [model_name.camelize, *@attributes.split(" "), "--api"], force: true
|
83
|
+
|
84
|
+
# ToDo: Write a custom generator that avoids this workaround
|
85
|
+
# Delete the model file after scaffolding to avoid duplicate model files
|
86
|
+
model_file_path = File.join("app/models", "#{model_name}.rb")
|
87
|
+
File.delete(model_file_path) if File.exist?(model_file_path)
|
88
|
+
end
|
89
|
+
end
|
@@ -0,0 +1,9 @@
|
|
1
|
+
class <%= @class_name %> < ApplicationRecord
|
2
|
+
<%= "# <associations>" %>
|
3
|
+
<%= @associations.map { |association| association }.join("\n ") %>
|
4
|
+
<%= "# </associations>" %>
|
5
|
+
|
6
|
+
<%= "# <validations>" %>
|
7
|
+
<%= @validations.map { |validation| validation }.join("\n ") %>
|
8
|
+
<%= "# </validations>" %>
|
9
|
+
end
|
@@ -0,0 +1,97 @@
|
|
1
|
+
require 'thor'
|
2
|
+
require 'logger'
|
3
|
+
|
4
|
+
module LayeredStackRails
|
5
|
+
class Example < Thor
|
6
|
+
def self.execute
|
7
|
+
new.execute
|
8
|
+
end
|
9
|
+
|
10
|
+
no_commands do
|
11
|
+
def execute
|
12
|
+
logger.info("> layered_stack-rails/example")
|
13
|
+
|
14
|
+
# Ensure the directory exists
|
15
|
+
directory_path = 'layered_stack'
|
16
|
+
unless File.directory?(directory_path)
|
17
|
+
FileUtils.mkdir_p(directory_path)
|
18
|
+
end
|
19
|
+
|
20
|
+
# Create the example product.yml file
|
21
|
+
example_product = File.join(directory_path, 'resources.yml')
|
22
|
+
unless File.exist?(example_product)
|
23
|
+
File.open(example_product, 'w') do |file|
|
24
|
+
file.write(<<~YAML)
|
25
|
+
resources:
|
26
|
+
product:
|
27
|
+
attributes:
|
28
|
+
name:
|
29
|
+
type: string
|
30
|
+
required: true
|
31
|
+
index: uniq
|
32
|
+
description:
|
33
|
+
type: text
|
34
|
+
price:
|
35
|
+
type: decimal
|
36
|
+
precision: 10
|
37
|
+
scale: 2
|
38
|
+
required: true
|
39
|
+
stock_quantity:
|
40
|
+
type: integer
|
41
|
+
required: true
|
42
|
+
associations:
|
43
|
+
order_items:
|
44
|
+
type: has_many
|
45
|
+
orders:
|
46
|
+
type: has_many
|
47
|
+
through: order_items
|
48
|
+
|
49
|
+
order:
|
50
|
+
attributes:
|
51
|
+
order_number:
|
52
|
+
type: string
|
53
|
+
required: true
|
54
|
+
index: uniq
|
55
|
+
order_date:
|
56
|
+
type: datetime
|
57
|
+
required: true
|
58
|
+
total_amount:
|
59
|
+
type: decimal
|
60
|
+
precision: 10
|
61
|
+
scale: 2
|
62
|
+
required: true
|
63
|
+
associations:
|
64
|
+
order_items:
|
65
|
+
type: has_many
|
66
|
+
products:
|
67
|
+
type: has_many
|
68
|
+
through: order_items
|
69
|
+
|
70
|
+
order_item:
|
71
|
+
attributes:
|
72
|
+
quantity:
|
73
|
+
type: integer
|
74
|
+
required: true
|
75
|
+
price:
|
76
|
+
type: decimal
|
77
|
+
precision: 10
|
78
|
+
scale: 2
|
79
|
+
required: true
|
80
|
+
associations:
|
81
|
+
order:
|
82
|
+
type: belongs_to
|
83
|
+
product:
|
84
|
+
type: belongs_to
|
85
|
+
YAML
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
private
|
91
|
+
|
92
|
+
def logger
|
93
|
+
@logger ||= Logger.new(STDOUT)
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'thor'
|
2
|
+
require 'logger'
|
3
|
+
require 'yaml'
|
4
|
+
require 'rails'
|
5
|
+
|
6
|
+
module LayeredStackRails
|
7
|
+
class Generate < Thor
|
8
|
+
def self.execute
|
9
|
+
new.execute
|
10
|
+
end
|
11
|
+
|
12
|
+
no_commands do
|
13
|
+
def execute
|
14
|
+
logger.info("> layered_stack-rails/generate_new")
|
15
|
+
|
16
|
+
yaml_content = YAML.load_file(File.join("layered_stack", "resources.yml"))
|
17
|
+
resources = yaml_content["resources"]
|
18
|
+
resources.each do |model_name, model_data|
|
19
|
+
if model_data
|
20
|
+
system("rails generate layered_stack_rails:scaffold #{model_name}")
|
21
|
+
else
|
22
|
+
logger.error("Model #{model_name} not found in resources.yml")
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
def logger
|
30
|
+
@logger ||= Logger.new(STDOUT)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'thor'
|
2
|
+
require 'logger'
|
3
|
+
require 'yaml'
|
4
|
+
require 'rails'
|
5
|
+
|
6
|
+
module LayeredStackRails
|
7
|
+
class GenerateNew < Thor
|
8
|
+
def self.execute
|
9
|
+
new.execute
|
10
|
+
end
|
11
|
+
|
12
|
+
no_commands do
|
13
|
+
def execute
|
14
|
+
logger.info("> layered_stack-rails/generate_new")
|
15
|
+
|
16
|
+
yaml_content = YAML.load_file(File.join("layered_stack", "resources.yml"))
|
17
|
+
resources = yaml_content["resources"]
|
18
|
+
resources.each do |model_name, model_data|
|
19
|
+
if model_data
|
20
|
+
system("rails generate layered_stack_rails:scaffold #{model_name}")
|
21
|
+
else
|
22
|
+
logger.error("Model #{model_name} not found in resources.yml")
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
def logger
|
30
|
+
@logger ||= Logger.new(STDOUT)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|