Class: TailwindViews::Generators::InstallGenerator

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

Overview

Generate scaffolds and views for tailwind

Instance Method Summary collapse

Methods included from TailwindViewsGenerator::Helpers

#app_name

Instance Method Details

#copy_formObject



47
48
49
50
51
52
53
54
55
# File 'lib/generators/tailwind_views/install_generator.rb', line 47

def copy_form
  if options[:simpleform]
    copy_file("scaffolds/simple_form/_form.html.#{options[:template_engine]}",
              "lib/templates/#{options[:template_engine]}/scaffold/_form.html.#{options[:template_engine]}", force: true)
  else
    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
end

#copy_layout_viewsObject



57
58
59
60
61
62
# File 'lib/generators/tailwind_views/install_generator.rb', line 57

def copy_layout_views
  return unless options[:layout]

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

#copy_pagination_viewObject



71
72
73
74
75
76
# File 'lib/generators/tailwind_views/install_generator.rb', line 71

def copy_pagination_view
  return unless options[:pagination]

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

#copy_scaffold_viewsObject



39
40
41
42
43
44
45
# File 'lib/generators/tailwind_views/install_generator.rb', line 39

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
end

#copy_shared_viewsObject



64
65
66
67
68
69
# File 'lib/generators/tailwind_views/install_generator.rb', line 64

def copy_shared_views
  %w[footer navigation].each do |template_file|
    template("shared/#{template_file}/_#{template_file}.html.#{options[:template_engine]}.tt",
             "app/views/shared/_#{template_file}.html.#{options[:template_engine]}", force: true)
  end
end

#inject_application_helpersObject



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/generators/tailwind_views/install_generator.rb', line 78

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

    FLASH_TYPE_HASH = { success: 'green', error: 'yellow', alert: 'red', notice: 'indigo' }.freeze

    def alert_color(flash_type)
      (FLASH_TYPE_HASH[flash_type.to_sym] || flash_type.to_s)
    end

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

      # remove any blank devise timeout errors
      flash.delete(:timedout)
      flash.each do |msg_type, message|
        # You don't need to create an empty alert message
        next if message.blank? || message.to_s.length.zero?

        concat(tag.div(class: "bg-\#{alert_color(msg_type)}-50 p-5 lg:w-full rounded border-l-4 border-\#{alert_color(msg_type)}-400 mb-5", role: 'alert') do
          concat(tag.p(message, class: "text-sm text-\#{alert_color(msg_type)}-500"))
        end)
      end
      nil
    end

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

      content_tag(:div, class: 'text-sm bg-red-50 rounded p-5 mb-5') do
        concat(content_tag(:div, class: 'text-red-600 font-medium') do
          concat "\#{pluralize(object.errors.count, 'error')} prohibited this \#{object.class.name.downcase} from being saved:"
        end)
        concat(content_tag(:ul, class: 'px-0 text-red-500 italic font-sm') do
          object.errors.full_messages.each do |msg|
            concat content_tag(:li, msg, class: 'list-disc px-0 py-1 mx-4')
          end
        end)
      end
    end
  HELPER

  inject_into_file('app/helpers/application_helper.rb',
                   optimize_indentation(helper_str, 2),
                   after: "module ApplicationHelper\n",
                   force: true)
end

#invoke_devise_generatorObject



128
129
130
131
# File 'lib/generators/tailwind_views/install_generator.rb', line 128

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