Class: Kowl::ViewsAndHelpersGenerator

Inherits:
Generators::Base
  • Object
show all
Defined in:
lib/kowl/generators/views_and_helpers_generator.rb

Instance Method Summary collapse

Methods inherited from Generators::Base

default_source_root, source_paths

Methods included from Docker

#alpine_docker_dependencies, #app_js_volumes, #app_volumes, #db_volumes, #debian_database_dependencies, #debian_docker_dependencies, #docker_app_command, #docker_compose_database_string, #docker_databases, #docker_depends_on, #docker_port_watcher, #docker_redis_service, #docker_sidekiq_service, #docker_variables, #docker_volumes, #docker_webpacker_service, #dockerfile_database_args, #dockerfile_migration_snip, #js_volumes, #mysql_volumes, #postgresql_volumes, #redis_volumes

Methods included from Actions

#add_extension_routes, #add_package, #append_to_file, #database_route, #dev_config, #dup_file, #file_exists?, #mailer_gems, #mailer_route, #mk_dir, #move_file, #pry_gems, #rails_cmd, #remove_dir, #remove_file, #remove_gem, #replace_string_in_file, #robocop_test_engine, #sidekiq_route, #template_linter_gems

Instance Method Details

Generate an application footer view based on framework/template_engine



55
56
57
# File 'lib/kowl/generators/views_and_helpers_generator.rb', line 55

def copy_footer
  template("shared/footer/#{options[:framework]}.html.#{options[:template_engine]}.tt", "app/views/shared/_footer.html.#{options[:template_engine]}") if %w[bootstrap semantic].include? options[:framework]
end

#copy_navigationObject

Generate an application navigation view based on framework/template_engine



50
51
52
# File 'lib/kowl/generators/views_and_helpers_generator.rb', line 50

def copy_navigation
  template("shared/navigation/#{options[:framework]}.html.#{options[:template_engine]}.tt", "app/views/shared/_navigation.html.#{options[:template_engine]}") if %w[bootstrap semantic].include? options[:framework]
end

#generate_templatesObject

Generate templates with framework and templae_engine with required application options



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/kowl/generators/views_and_helpers_generator.rb', line 19

def generate_templates
  # Generate views using bootstrap and semanic view generators
  gen_tags = []
  gen_tags.push("--template-engine=#{options[:template_engine]}")
  gen_tags.push('--simpleform') if options[:simpleform]
  gen_tags.push('--pagination') unless options[:skip_pagination]
  gen_tags.push('--devise') unless options[:noauth]
  gen_tags.push('--skip_javascript') if options[:skip_javascript]
  gen_tags.push('--skip_turbolinks') if options[:skip_turbolinks]
  gen_tags.push('--metatags')
  gen_tags.push('--layout')
  generate("#{options[:framework]}:install #{gen_tags.join(' ')}") if %w[bootstrap semantic].include? options[:framework]
end

Inject view template partial into application layout



60
61
62
63
64
65
66
67
# File 'lib/kowl/generators/views_and_helpers_generator.rb', line 60

def inject_footer_into_layout
  footer_str = "#{template_prefix}= render 'shared/footer'#{template_postfix}\n"
  if options[:template_engine] == 'erb'
    insert_into_file 'app/views/layouts/application.html.erb', optimize_indentation(footer_str, 4), before: "  </body>\n"
  else
    append_to_file "app/views/layouts/application.html.#{options[:template_engine]}", optimize_indentation(footer_str, 4)
  end
end

#inject_header_into_layoutObject

Inject navigation header into application layout file based on template_engine/framework



39
40
41
42
43
44
45
46
47
# File 'lib/kowl/generators/views_and_helpers_generator.rb', line 39

def inject_header_into_layout
  # prefix, includes, postfix
  # Pre and Postfix is determined if the template engine is ERB, Slim, or HAML
  nav_str = "#{template_prefix}= render 'shared/navigation'#{template_postfix}\n"
  match = { 'erb' => /\s\<body\>\n/,
            'haml' => /\s%body\n/,
            'slim' => /\sbody\n/ }
  insert_into_file "app/views/layouts/application.html.#{options[:template_engine]}", optimize_indentation(nav_str, 4), after: match[options[:template_engine]]
end

#remove_application_layoutObject

If template engine isn’t set for erb remove application.erb



34
35
36
# File 'lib/kowl/generators/views_and_helpers_generator.rb', line 34

def remove_application_layout
  remove_file 'app/views/layouts/application.html.erb' if %w[slim haml].include? options[:template_engine]
end

#update_application_helpersObject

Skip a flash type if the key is a cookie/session timeone, since this can occure so regularly



70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/kowl/generators/views_and_helpers_generator.rb', line 70

def update_application_helpers
  flash_str = <<~STR
    \n# remove any blank devise timeout errors
    flash.delete(:timedout)
  STR

  # User to prevent blank devise timeout error from flashing up
  insert_into_file 'app/helpers/application_helper.rb', "\n#{optimize_indentation(flash_str.strip, 4)}", after: "return '' unless flash.any?\n" if options[:framework] == 'bootstrap'

  # Make applicaction_helper immutable
  insert_into_file 'app/helpers/application_helper.rb', "# frozen_string_literal: true\n\n", before: "module ApplicationHelper\n"
end