Class: Bootstrap::Generators::InstallGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Includes:
BootstrapViewsGenerator::Helpers
Defined in:
lib/generators/bootstrap/install_generator.rb

Instance Method Summary collapse

Methods included from BootstrapViewsGenerator::Helpers

#app_name

Instance Method Details

#copy_scaffold_viewsObject



38
39
40
41
42
43
# File 'lib/generators/bootstrap/install_generator.rb', line 38

def copy_scaffold_views
  %w[edit index show new].each do |file|
    template "scaffolds/#{options[:template_engine]}/#{file}.html.#{options[:template_engine]}", "lib/templates/#{options[:template_engine]}/scaffold/#{file}.html.#{options[:template_engine]}", force: true
  end
  copy_file "scaffolds/#{options[:template_engine]}/_form.html.#{options[:template_engine]}", "lib/templates/#{options[:template_engine]}/scaffold/_form.html.#{options[:template_engine]}", force: true
end

#copy_shared_paginationObject



49
50
51
# File 'lib/generators/bootstrap/install_generator.rb', line 49

def copy_shared_pagination
  copy_file("shared/_pagination.html.#{options[:template_engine]}", "app/views/shared/_pagination.html.#{options[:template_engine]}", force: true) if options[:pagination]
end

#copy_simpleformsObject



53
54
55
# File 'lib/generators/bootstrap/install_generator.rb', line 53

def copy_simpleforms
  copy_file("simple_form/_form.html.#{options[:template_engine]}", "lib/templates/#{options[:template_engine]}/scaffold/_form.html.#{options[:template_engine]}", force: true) if options[:simpleform]
end

#create_layoutObject



45
46
47
# File 'lib/generators/bootstrap/install_generator.rb', line 45

def create_layout
  template("layouts/application.html.#{options[:template_engine]}.tt", "app/views/layouts/application.html.#{options[:template_engine]}", force: true) if options[:layout]
end

#inject_application_helpersObject

Inject Bootstrap helpers into the application_helper file



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/generators/bootstrap/install_generator.rb', line 58

def inject_application_helpers
  pagy_helper = (options[:pagination] ? 'include Pagy::Frontend' : '')
  helper_str = <<~HELPER
  #{pagy_helper}

  # https://gist.github.com/fjahr/b3828b9f4e333e74ba1894687d65e055
  def bootstrap_class_for(flash_type)
    { success: 'alert-success', error: 'alert-danger', alert: 'alert-warning', notice: 'alert-info' }.stringify_keys[flash_type.to_s] || flash_type.to_s
  end

  def flash_messages(_opts = [])
    return '' unless flash.any?

    flash.each do |msg_type, message|
      next unless !message.nil? && message.to_s.length.positive?

      concat(content_tag(:div, message, class: "alert \#{bootstrap_class_for(msg_type)}", role: 'alert') do
        concat content_tag(:button, 'x', class: 'close', data: { dismiss: 'alert' })
        concat message
      end)
    end
    nil
  end

  # for outputting an objects error messages
  def errors_for(object)
    return '' unless object.errors.any?

    content_tag(:div, class: 'card border-danger') do
      concat(content_tag(:div, class: 'card-header bg-danger text-white') do
        concat "\#{pluralize(object.errors.count, 'error')} prohibited this \#{object.class.name.downcase} from being saved:"
      end)
      concat(content_tag(:ul, class: 'mb-0 list-group list-group-flush') do
        object.errors.full_messages.each do |msg|
          concat content_tag(:li, msg, class: 'list-group-item')
        end
      end)
    end
  end
HELPER
  inject_into_file 'app/helpers/application_helper.rb', optimize_indentation(helper_str, 2), after: "module ApplicationHelper\n"
end

#invoke_devise_generatorObject



101
102
103
104
# File 'lib/generators/bootstrap/install_generator.rb', line 101

def invoke_devise_generator
  # Generate bootstrap based devise views if devise is being used
  invoke('bootstrap:devise') if options[:devise]
end