Class: Typstify::Generators::TemplateGenerator

Inherits:
Rails::Generators::NamedBase
  • Object
show all
Defined in:
lib/generators/typstify/template/template_generator.rb

Overview

rails g typstify:template invoice

Copies a starter template and its sample data into app/views, so the preview task works immediately and you have something real to edit.

Constant Summary collapse

AVAILABLE =
%w[invoice receipt report certificate].freeze

Instance Method Summary collapse

Instance Method Details

#copy_sample_dataObject



34
35
36
37
# File 'lib/generators/typstify/template/template_generator.rb', line 34

def copy_sample_data
  copy_file "#{name}/sample_data.json",
            "app/views/#{File.dirname(destination)}/sample_data.json"
end

#copy_templateObject



30
31
32
# File 'lib/generators/typstify/template/template_generator.rb', line 30

def copy_template
  copy_file "#{name}/#{name}.typ", "app/views/#{destination}.typ"
end

#ensure_brandingObject



39
40
41
42
43
# File 'lib/generators/typstify/template/template_generator.rb', line 39

def ensure_branding
  return if File.exist?(File.join(destination_root, "app/views/shared/branding.typ"))

  copy_file "shared/branding.typ", "app/views/shared/branding.typ"
end

#reportObject



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/generators/typstify/template/template_generator.rb', line 45

def report
  say ""
  say "Preview it:  rake typstify:preview[#{destination}]", :green
  say ""
  say "Render it from a controller:"
  say ""
  say "    render pdf: #{destination.inspect},"
  say "           data: { ... },"
  say "           filename: #{"#{name}.pdf".inspect}"
  say ""
end

#validate_nameObject

Raises:

  • (::Rails::Generators::Error)


23
24
25
26
27
28
# File 'lib/generators/typstify/template/template_generator.rb', line 23

def validate_name
  return if AVAILABLE.include?(name)

  raise ::Rails::Generators::Error,
        "Unknown template #{name.inspect}. Available: #{AVAILABLE.join(", ")}"
end