Class: Belt::CLI::ViewsCommand

Inherits:
Object
  • Object
show all
Defined in:
lib/belt/cli/views_command.rb

Constant Summary collapse

TEMPLATE_DIR =
File.expand_path('../../templates/views', __dir__)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, fields, force: false, quiet: false, frontend: nil) ⇒ ViewsCommand

Returns a new instance of ViewsCommand.



83
84
85
86
87
88
89
90
91
92
93
# File 'lib/belt/cli/views_command.rb', line 83

def initialize(name, fields, force: false, quiet: false, frontend: nil)
  @name = name.downcase.gsub(/[^a-z0-9_]/, '_')
  @fields = fields
  @force = force
  @quiet = quiet
  @overwrite_all = false
  @singular_name = Belt::Inflector.singularize(@name)
  @resource_name = Belt::Inflector.pluralize(@singular_name)
  @class_name = Belt::Inflector.classify(@singular_name)
  @frontend = frontend || FrontendRegistry.new.resolve!
end

Class Method Details

.read_schema_fields(name) ⇒ Object



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
77
78
79
80
81
# File 'lib/belt/cli/views_command.rb', line 43

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)
    timestamp_fields = %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 timestamp_fields.include?(n)

      { name: n, type: t }
    end
  else
    []
  end
end

.run(args) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/belt/cli/views_command.rb', line 13

def self.run(args)
  force = args.delete('--force') || args.delete('-f')
  frontend_name = FrontendRegistry.extract_flag!(args, '--frontend')

  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 '  --frontend NAME     Target frontend when several exist'
    puts "\nExamples:"
    puts '  belt generate views post title:string content:text status:string'
    puts '  belt generate views comment body:text author:string'
    puts '  belt generate views bag --frontend ops'
    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?

  frontend = FrontendRegistry.new.resolve!(frontend_name)
  new(name, fields, force: force, frontend: frontend).generate
end

Instance Method Details

#generateObject



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 95

def generate
  unless Dir.exist?(@frontend.src_dir)
    puts "āœ— No #{@frontend.src_dir}/ directory found. Run `belt generate frontend react` first."
    exit 1
  end

  pages_dir = "#{@frontend.src_dir}/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!" unless @quiet
end