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, force: false) ⇒ ViewsCommand
constructor
A new instance of ViewsCommand.
Constructor Details
#initialize(name, fields, force: false) ⇒ ViewsCommand
Returns a new instance of ViewsCommand.
78 79 80 81 82 83 84 85 86 |
# File 'lib/belt/cli/views_command.rb', line 78 def initialize(name, fields, force: false) @name = name.downcase.gsub(/[^a-z0-9_]/, '_') @fields = fields @force = force @overwrite_all = false @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
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/belt/cli/views_command.rb', line 38 def self.read_schema_fields(name) schema_file = [ 'config/contracts.rb', 'config/contracts.tf.rb', 'config/schema.tf.rb', 'infrastructure/schema.tf.rb' ].find { |f| File.exist?(f) } return [] unless 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 block_content = ::Regexp.last_match(1) = %w[created_at updated_at] # Support both formats: # field :name, type: :string (legacy) # string :name (current schema DSL) dsl_types = %w[string text integer number boolean float date datetime] dsl_pattern = /(?:#{dsl_types.join('|')}) :(\w+)/ fields = block_content.scan(/field :(\w+), type: :(\w+)/) fields += block_content.scan(dsl_pattern).map do |match| field_name = match[0] # Extract type from the DSL method name on that line type_match = block_content.match(/(\w+) :#{Regexp.escape(field_name)}/) [field_name, type_match ? type_match[1] : 'string'] end fields.filter_map do |n, t| next if .include?(n) { 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 33 34 35 36 |
# File 'lib/belt/cli/views_command.rb', line 12 def self.run(args) force = args.delete('--force') || args.delete('-f') name = args.shift if name.nil? || name.empty? puts 'Usage: belt generate views <resource> [field:type ...] [options]' puts "\nGenerates React pages for all REST actions (index, show, new, edit)." puts "\nOptions:" puts ' --force, -f Overwrite existing files without prompting' 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 contracts.rb fields = read_schema_fields(name) if fields.empty? new(name, fields, force: force).generate end |
Instance Method Details
#generate ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/belt/cli/views_command.rb', line 88 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 |