Class: Layout::Generators::InstallGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Defined in:
lib/generators/layout/install/install_generator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#app_nameObject (readonly)

Returns the value of attribute app_name.



11
12
13
# File 'lib/generators/layout/install/install_generator.rb', line 11

def app_name
  @app_name
end

Instance Method Details

#add_forms_stylesheetObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/generators/layout/install/install_generator.rb', line 71

def add_forms_stylesheet
  return unless (File.exists?('config/initializers/devise.rb') or File.exists?('config/initializers/omniauth.rb'))
  dir = File.expand_path("../templates", __FILE__)
  case framework_name
    when 'none'
      # TODO
    when 'simple'
      # TODO
    when 'bootstrap2'
      # TODO
    when 'bootstrap3'
      append_file 'app/assets/stylesheets/1st_load_framework.css.scss', File.read("#{dir}/bootstrap3-forms.css.scss")
    when 'bootstrap4'
      append_file 'app/assets/stylesheets/1st_load_framework.css.scss', File.read("#{dir}/bootstrap4-forms.css.scss")
    when 'foundation4'
      # TODO
    when 'foundation5'
      append_file 'app/assets/stylesheets/1st_load_framework.css.scss', File.read("#{dir}/foundation5-forms.css.scss")
  end
end

Add navigation links



128
129
130
131
132
133
134
135
136
137
138
# File 'lib/generators/layout/install/install_generator.rb', line 128

def add_navigation_links
  return if framework_name == 'bootstrap4'
  app = ::Rails.application
  ext = app.config.generators.options[:rails][:template_engine] || :erb
  unless framework_name == 'none'
    # ABOUT
    append_file 'app/views/layouts/_navigation_links.html.erb', "<li><%= link_to 'About', page_path('about') %></li>\n" if File.exists?("app/views/pages/about.html.#{ext}")
    # CONTACT
    append_file 'app/views/layouts/_navigation_links.html.erb', "<li><%= link_to 'Contact', new_contact_path %></li>\n" if File.exists?("app/views/contacts/new.html.#{ext}")
  end
end

Add navigation links



114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/generators/layout/install/install_generator.rb', line 114

def add_navigation_links_for_bootstrap4
  return unless framework_name == 'bootstrap4'
  app = ::Rails.application
  ext = app.config.generators.options[:rails][:template_engine] || :erb
  unless framework_name == 'none'
    # ABOUT
    append_file 'app/views/layouts/_navigation_links.html.erb', "<li class='nav-item'><%= link_to 'About', page_path('about'), class: 'nav-link' %></li>\n" if File.exists?("app/views/pages/about.html.#{ext}")
    # CONTACT
    append_file 'app/views/layouts/_navigation_links.html.erb', "<li class='nav-item'><%= link_to 'Contact', new_contact_path, class: 'nav-link' %></li>
\n" if File.exists?("app/views/contacts/new.html.#{ext}")
  end
end

#generate_layoutObject

Create an application layout file with partials for messages and navigation



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/generators/layout/install/install_generator.rb', line 93

def generate_layout
  app = ::Rails.application
  @app_name = app.class.to_s.split("::").first
  ext = app.config.generators.options[:rails][:template_engine] || :erb
  remove_file 'app/views/layouts/application.html.erb'
  template "#{framework_name}-application.html.#{ext}", "app/views/layouts/application.html.#{ext}"
  if Rails::VERSION::MAJOR.to_s == "3"
    gsub_file "app/views/layouts/application.html.#{ext}", /, "data-turbolinks-track" => true/, ''
  end
  if framework_name == 'none'
    remove_file "app/views/layouts/_messages.html.#{ext}"
    remove_file "app/views/layouts/_navigation.html.#{ext}"
    remove_file "app/views/layouts/_navigation_links.html.#{ext}"
  else
    copy_file "#{framework_name}-messages.html.#{ext}", "app/views/layouts/_messages.html.#{ext}"
    copy_file "#{framework_name}-navigation.html.#{ext}", "app/views/layouts/_navigation.html.#{ext}"
    copy_file "navigation_links.html.erb", "app/views/layouts/_navigation_links.html.erb"
  end
end

#install_frameworkObject

Install the desired framework



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/generators/layout/install/install_generator.rb', line 14

def install_framework
  remove_file 'app/assets/stylesheets/application.css'
  copy_file 'application.css.scss', 'app/assets/stylesheets/application.css.scss'
  case framework_name
    when 'none'
      copy_file 'application.js', 'app/assets/javascripts/application.js'
      remove_file 'app/assets/stylesheets/simple.css'
      remove_file 'app/assets/stylesheets/bootstrap_and_overrides.css.scss'
      remove_file 'app/assets/stylesheets/foundation_and_overrides.css.scss'
      remove_file 'app/assets/stylesheets/1st_load_framework.css.scss'
    when 'simple'
      copy_file 'simple.css', 'app/assets/stylesheets/simple.css'
      copy_file 'application.js', 'app/assets/javascripts/application.js'
      remove_file 'app/assets/stylesheets/bootstrap_and_overrides.css.scss'
      remove_file 'app/assets/stylesheets/foundation_and_overrides.css.scss'
      remove_file 'app/assets/stylesheets/1st_load_framework.css.scss'
    when 'bootstrap2'
      copy_file 'bootstrap2_and_overrides.css.scss', 'app/assets/stylesheets/1st_load_framework.css.scss'
      copy_file 'bootstrap-application.js', 'app/assets/javascripts/application.js'
      remove_file 'app/assets/stylesheets/simple.css'
      remove_file 'app/assets/stylesheets/foundation_and_overrides.css.scss'
    when 'bootstrap3'
      copy_file 'bootstrap3_and_overrides.css.scss', 'app/assets/stylesheets/1st_load_framework.css.scss'
      copy_file 'bootstrap-application.js', 'app/assets/javascripts/application.js'
      remove_file 'app/assets/stylesheets/simple.css'
      remove_file 'app/assets/stylesheets/foundation_and_overrides.css.scss'
    when 'bootstrap4'
      copy_file 'bootstrap4_and_overrides.css.scss', 'app/assets/stylesheets/1st_load_framework.css.scss'
      copy_file 'bootstrap4-application.js', 'app/assets/javascripts/application.js'
      remove_file 'app/assets/stylesheets/simple.css'
      remove_file 'app/assets/stylesheets/foundation_and_overrides.css.scss'
    when 'foundation4'
      copy_file 'foundation4_and_overrides.css.scss', 'app/assets/stylesheets/1st_load_framework.css.scss'
      copy_file 'foundation4-application.js', 'app/assets/javascripts/application.js'
      remove_file 'app/assets/stylesheets/simple.css'
      remove_file 'app/assets/stylesheets/bootstrap_and_overrides.css.scss'
    when 'foundation5'
      copy_file 'foundation5_and_overrides.css.scss', 'app/assets/stylesheets/1st_load_framework.css.scss'
      copy_file 'foundation5-application.js', 'app/assets/javascripts/application.js'
      remove_file 'app/assets/stylesheets/simple.css'
      remove_file 'app/assets/stylesheets/bootstrap_and_overrides.css.scss'
      insert_into_file('config/application.rb', "\n\n    # For Foundation 5\n    config.assets.precompile += %w( vendor/modernizr )\n", :after => /^ *# config.i18n.default_locale = :de/, :force => false)
  end
  # Rails 3 doesn't have turbolinks
  # Rails 5.1 uses rails-ujs but earlier versions need jquery_ujs
  if Rails::VERSION::MAJOR == 3
    gsub_file 'app/assets/javascripts/application.js', /\/\/= require turbolinks\n/, ''
    gsub_file 'app/assets/javascripts/application.js', /\/\/= require rails-ujs/, '//= require jquery_ujs'
  end
  if Rails::VERSION::MAJOR == 4
    gsub_file 'app/assets/javascripts/application.js', /\/\/= require rails-ujs/, '//= require jquery_ujs'
  end
  if Rails::VERSION::MAJOR == 5 && Rails::VERSION::MINOR == 0
    gsub_file 'app/assets/javascripts/application.js', /\/\/= require rails-ujs/, '//= require jquery_ujs'
  end
end