Class: BootswatchRails::Generators::InstallGenerator

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

Instance Method Summary collapse

Instance Method Details

#copy_directoriesObject



122
123
124
125
126
# File 'lib/generators/bootswatch_rails/install/install_generator.rb', line 122

def copy_directories
  directory "app", force: true
  directory "config"
  directory "lib", force: true
end

#setup_assets_precompileObject



111
112
113
114
115
116
117
118
119
120
# File 'lib/generators/bootswatch_rails/install/install_generator.rb', line 111

def setup_assets_precompile
  return if options.cdn == "none"
  initializer "bootswatch_assets.rb" do
    assets  = "jquery.js"
    assets += " jquery-ui.js" if options.ui?
    assets += " jquery.dataTables.js dataTables.responsive.js" if options.dt?
    assets += " bootstrap.js"
    "Rails.application.config.assets.precompile += %w( #{assets} )"
  end
end

#setup_headObject



128
129
130
# File 'lib/generators/bootswatch_rails/install/install_generator.rb', line 128

def setup_head
  template "head.html.erb", "app/views/layouts/_head.html.erb"
end

#setup_layoutObject



132
133
134
135
136
137
138
139
# File 'lib/generators/bootswatch_rails/install/install_generator.rb', line 132

def setup_layout
  file = "app/views/layouts/application.html.erb"
  remove_file file
  template "#{options.layout}.html.erb", file
  if options.auth != "none"
    template "theme.html.erb", "app/views/layouts/_theme.html.erb"
  end
end

#update_application_controllerObject



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
70
71
72
73
74
# File 'lib/generators/bootswatch_rails/install/install_generator.rb', line 22

def update_application_controller
  file = "app/controllers/application_controller.rb"
  if options.auth == "none"
    lines = [
      ""
    ]
  elsif options.auth == "sorcery"
    lines = [
      "",
      "  before_action :require_login"
    ]
  else
    lines = [
      "",
      "  before_action :authenticate_#{options.auth}!"
    ]
  end
  lines += [
    "",
    "  private",
    "",
    "  def default_theme",
    "    BootswatchRails::THEMES[BootswatchRails::DEFAULT].to_s",
    "  end",
    "  helper_method :default_theme"
  ]
  if options.auth != "none"
    lines += [
      "",
      "  def current_theme",
      "    @current_theme = current_#{auth_resource}.theme if #{auth_check}",
      "    @current_theme ||= default_theme",
      "  end",
      "  helper_method :current_theme"
    ]
  end
  if options.auth == "sorcery"
    lines += [
      "",
      "  def not_authenticated",
      "    redirect_to login_path, alert: t('sorcery.required')",
      "  end"
    ]
  elsif options.auth != "none"
    lines += [
      "",
      "  def after_sign_in_path_for(resource)",
      "    session['#{auth_resource}_return_to'] || root_path",
      "  end"
    ]
  end
  inject_into_file file, lines.join("\n"), after: /protect_from_forgery.*$/
end

#update_application_cssObject



99
100
101
102
103
104
105
106
107
108
109
# File 'lib/generators/bootswatch_rails/install/install_generator.rb', line 99

def update_application_css
  if options.cdn == "none"
    file = "app/assets/stylesheets/application.css"
    if options.dt?
      inject_into_file file, before: /^.*require_self$/ do
        " *= require jquery.dataTables\n" +
        " *= require responsive.dataTables\n"
      end
    end
  end
end

#update_application_jsObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/generators/bootswatch_rails/install/install_generator.rb', line 76

def update_application_js
  file = "app/assets/javascripts/application.js"
  unless options.turbolinks?
    gsub_file file, /\/\/= require turbolinks\n/, ''
  end
  if options.cdn == "none"
    inject_into_file file, after: /require jquery_ujs$/ do
      "\n//= require bootstrap"
    end
    if options.ui?
      inject_into_file file, after: /require jquery$/ do
        "\n//= require jquery-ui"
      end
    end
    if options.dt?
      inject_into_file file, before: /^\/\/= require_tree.*$/ do
        "//= require jquery.dataTables\n" +
        "//= require dataTables.responsive\n"
      end
    end
  end
end