Class: VueComponentBuilder::Generators::NewGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Defined in:
lib/vue_component_builder.rb

Instance Method Summary collapse

Instance Method Details

#executeObject



9
10
11
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
37
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
# File 'lib/vue_component_builder.rb', line 9

def execute
  p 'Building Vue component...'

  if ARGV.empty?
    raise StandardError, <<-ERROR.strip_heredoc

    Be sure to have informed the params needed:
      e.g
          model=Fruit
          component=MyFruitComponent
          theme=default [default or element-plus]
          exclude=id,created_at,updated_at [optional]

      rails g vue_component_builder:new model=Fruit  component=MyFruitComponent theme=default exclude=id,created_at,updated_at
    ERROR
  end

  input = {}
  ARGV.to_a.each do |arg|
    command = arg.split('=')
    input[command[0].to_sym] = command[1]
  end

  input[:exclude] = input[:exclude].try(:split, ',')

  if !input[:theme].present? || !input[:component].present? || !input[:model].present?
    raise StandardError, <<-ERROR.strip_heredoc

    Be sure to have informed the params needed:
      e.g
        model=Fruit
        component=MyFruitComponent
        theme=element-plus
        exclude=id,created_at,updated_at [optional]

      rails g vue_component_builder:new model=Fruit  component=MyFruitComponent theme=element-plus exclude=id,created_at,updated_at
    ERROR
  end

  @options = {
    component: input[:component],
    theme: input[:theme],
    model: {
      name: input[:model].downcase.to_s,
      attributes: [],
      exclude: input[:exclude],
      class: eval(input[:model].capitalize.to_s)
    },
    controller: {
      name: "#{input[:model].to_s.pluralize}Controller",
      methods: []
    },
  }

  @options[:controller].merge!(methods: eval("Api::V1::#{@options[:controller][:name]}").instance_methods(false).map { |m| m.to_s })
  attributes = @options[:model][:class].columns_hash.map do |k, v|
    label = I18n.t("activerecord.attributes.#{@options[:model][:name]}.#{k}", default: k.upcase)
    unless @options[:model][:exclude].nil?
      { name: k, type: v.type.to_s, label: label } if !@options[:model][:exclude].include? k
    else
      { name: k, type: v.type.to_s, label: label }
    end
  end
  @options[:model].merge!(attributes: attributes.compact)
  self.build
end