class TailwindViews::Generators::InstallGenerator

Generate scaffolds and views for tailwind

Public Instance Methods

copy_form() click to toggle source
# 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_views() click to toggle source
# 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_view() click to toggle source
# 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_views() click to toggle source
# 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_views() click to toggle source
# 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_helpers() click to toggle source
# 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_generator() click to toggle source
# 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