Class: Belt::CLI::ViewsCommand
- Inherits:
-
Object
- Object
- Belt::CLI::ViewsCommand
- Defined in:
- lib/belt/cli/views_command.rb
Constant Summary collapse
- TEMPLATE_DIR =
File.('../../templates/views', __dir__)
Class Method Summary collapse
Instance Method Summary collapse
- #generate ⇒ Object
-
#initialize(name, fields) ⇒ ViewsCommand
constructor
A new instance of ViewsCommand.
Constructor Details
#initialize(name, fields) ⇒ ViewsCommand
Returns a new instance of ViewsCommand.
54 55 56 57 58 59 60 |
# File 'lib/belt/cli/views_command.rb', line 54 def initialize(name, fields) @name = name.downcase.gsub(/[^a-z0-9_]/, '_') @fields = fields @singular_name = Belt::Inflector.singularize(@name) @resource_name = Belt::Inflector.pluralize(@singular_name) @class_name = Belt::Inflector.classify(@singular_name) end |
Class Method Details
.read_schema_fields(name) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/belt/cli/views_command.rb', line 34 def self.read_schema_fields(name) schema_file = 'infrastructure/schema.tf.rb' return [] unless File.exist?(schema_file) content = File.read(schema_file) singular = Belt::Inflector.singularize(name) # Extract fields from model block if content =~ /model :#{singular} do\n(.*?)\n\s*end/m ::Regexp.last_match(1).scan(/field :(\w+), type: :(\w+)/).except('created_at', 'updated_at') .map do |n, t| { name: n, type: t } end else [] end end |
.run(args) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/belt/cli/views_command.rb', line 12 def self.run(args) name = args.shift if name.nil? || name.empty? puts 'Usage: belt generate views <resource> [field:type ...]' puts "\nGenerates React pages for all REST actions (index, show, new, edit)." puts "\nExamples:" puts ' belt generate views post title:string content:text status:string' puts ' belt generate views comment body:text author:string' exit 1 end fields = args.map do |arg| n, t = arg.split(':', 2) { name: n, type: t || 'string' } end # If no fields provided, try to read from schema.tf.rb fields = read_schema_fields(name) if fields.empty? new(name, fields).generate end |
Instance Method Details
#generate ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/belt/cli/views_command.rb', line 62 def generate unless Dir.exist?('frontend/src') puts 'ā No frontend/ directory found. Run `belt generate frontend react` first.' exit 1 end pages_dir = "frontend/src/pages/#{@resource_name}" @plural_class_name = Belt::Inflector.camelize(@resource_name) FileUtils.mkdir_p(pages_dir) write_template('Index.jsx.erb', "#{pages_dir}/#{@plural_class_name}Index.jsx") write_template('Show.jsx.erb', "#{pages_dir}/#{@class_name}Show.jsx") write_template('New.jsx.erb', "#{pages_dir}/#{@class_name}New.jsx") write_template('Edit.jsx.erb', "#{pages_dir}/#{@class_name}Edit.jsx") write_template('Form.jsx.erb', "#{pages_dir}/#{@class_name}Form.jsx") inject_routes puts "\nā Views for '#{@singular_name}' generated!" puts "\nFiles created:" puts " #{pages_dir}/#{@plural_class_name}Index.jsx" puts " #{pages_dir}/#{@class_name}Show.jsx" puts " #{pages_dir}/#{@class_name}New.jsx" puts " #{pages_dir}/#{@class_name}Edit.jsx" puts " #{pages_dir}/#{@class_name}Form.jsx" puts ' frontend/src/App.jsx (updated)' end |