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.
53 54 55 56 57 58 59 |
# File 'lib/belt/cli/views_command.rb', line 53 def initialize(name, fields) @name = name.downcase.gsub(/[^a-z0-9_]/, '_') @fields = fields @resource_name = @name.end_with?('s') ? @name : "#{@name}s" @singular_name = @name.end_with?('s') ? @name.chomp('s') : @name @class_name = @singular_name.split('_').map(&:capitalize).join end |
Class Method Details
.read_schema_fields(name) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/belt/cli/views_command.rb', line 33 def self.read_schema_fields(name) schema_file = 'infrastructure/schema.tf.rb' return [] unless File.exist?(schema_file) content = File.read(schema_file) singular = name.end_with?('s') ? name.chomp('s') : 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
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/belt/cli/views_command.rb', line 11 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
61 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 |
# File 'lib/belt/cli/views_command.rb', line 61 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}" FileUtils.mkdir_p(pages_dir) write_template('Index.jsx.erb', "#{pages_dir}/#{@class_name}sIndex.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}/#{@class_name}sIndex.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 |