Class: Domino::ScaffoldRunner

Inherits:
Object
  • Object
show all
Defined in:
lib/domino/scaffolder.rb

Overview

ScaffoldRunner: handles the actual file generation

Instance Method Summary collapse

Constructor Details

#initialize(model_name, columns, namespace, generate_model) ⇒ ScaffoldRunner

Returns a new instance of ScaffoldRunner.



26
27
28
29
30
31
32
33
34
# File 'lib/domino/scaffolder.rb', line 26

def initialize(model_name, columns, namespace, generate_model)
  @model_name = model_name
  @file_name = model_name.underscore
  @plural_file_name = @file_name.pluralize
  @columns = columns
  @namespace = namespace
  @generate_model = generate_model
  @fields = columns.map(&:name).reject { |c| c == "id" }
end

Instance Method Details

#column_argsObject



50
51
52
# File 'lib/domino/scaffolder.rb', line 50

def column_args
  @columns.map { |col| "#{col.name}:#{col.sql_type}" unless col.name == "id" }.compact
end

#generate_file(type) ⇒ Object

rubocop:disable Metrics/MethodLength



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
# File 'lib/domino/scaffolder.rb', line 54

def generate_file(type) # rubocop:disable Metrics/MethodLength
  require "erb"

  template_path = File.expand_path("../../generators/domino/templates/#{type}.rb.tt", __FILE__)
  raise "Missing template: #{template_path}" unless File.exist?(template_path)

  content = ERB.new(File.read(template_path)).result(binding)
  puts "Generating #{type} for #{@model_name}"

  folder = case type
           when "blueprint" then "app/mappers"
           when "controller" then "app/controllers"
           when "repository" then "app/repositories"
           when "service" then "app/services"
           else "app/#{type}s"
           end

  filename = case type
             when "controller"
               "#{@plural_file_name}_controller.rb"
             else
               "#{@file_name}_#{type}.rb"
             end

  FileUtils.mkdir_p(folder)
  File.write(File.join(folder, filename), content)
end

#generate_model_fileObject



44
45
46
47
48
# File 'lib/domino/scaffolder.rb', line 44

def generate_model_file
  cmd = ["rails", "generate", "model", @model_name] + column_args
  system(*cmd)
  # system("rails generate model #{@model_name} #{column_args.join(" ")}")
end

#runObject



36
37
38
39
40
41
42
# File 'lib/domino/scaffolder.rb', line 36

def run
  generate_model_file if @generate_model
  generate_file("repository")
  generate_file("service")
  generate_file("blueprint")
  generate_file("controller")
end