Class: Binda::MaintenanceGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Defined in:
lib/generators/binda/maintenance/maintenance_generator.rb

Instance Method Summary collapse

Instance Method Details

#add_controllerObject



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/generators/binda/maintenance/maintenance_generator.rb', line 4

def add_controller
	puts "Maintenance page setup"
	puts ""
	puts "1) Adding controller"
	if File.exist?(Rails.root.join('app', 'controllers', 'maintenance_controller.rb' ))
		puts "\e[31m-------------------------------------------------------------------------------\e[0m"
		puts "\e[31mWARNING! Binda::MaintenanceController is already registered here:\e[0m"
		puts "\e[31m    app/controllers/maintenance_controller.rb\e[0m"
		puts
		puts "\e[31mUnless you know what you are doing, delete that file and run:\e[0m"
		puts "\e[31m    rails generate binda:maintenance\e[0m"
		puts "\e[31m-------------------------------------------------------------------------------\e[0m"
		exit
	else
		generate "controller", "maintenance index --no-assets --no-helper"
		inject_into_file Rails.root.join('app', 'controllers', 'maintenance_controller.rb'), after: "def index" do
			"\n render template: 'layouts/maintenance'"
		end
	end
end

#add_helperObject



40
41
42
43
44
45
46
47
# File 'lib/generators/binda/maintenance/maintenance_generator.rb', line 40

def add_helper
	ac_path = Rails.root.join('app', 'controllers', 'application_controller.rb' )
	unless File.readlines(ac_path).grep(/::Binda::MaintenanceHelpers/).size > 0
		inject_into_file ac_path, after: "ActionController::Base" do 
			"\n  include ::Binda::MaintenanceHelpers"
		end
	end
end

#add_routeObject



33
34
35
36
37
38
# File 'lib/generators/binda/maintenance/maintenance_generator.rb', line 33

def add_route
	return if Rails.application.routes.named_routes.any?{ |key, _| key.to_s === "maintenance" }
	# replace 'maintenance/index' root with 'maintenance'
	route "get 'maintenance', to: 'maintenance#index', as: 'maintenance'"
	gsub_file Rails.root.join('config', 'routes.rb'), "get 'maintenance/index'", ""
end

#add_templatesObject



25
26
27
28
29
30
31
# File 'lib/generators/binda/maintenance/maintenance_generator.rb', line 25

def add_templates
	puts "2) Adding templates"
	template 'app/assets/javascripts/maintenance.js'
	template 'app/assets/stylesheets/maintenance.scss'
	template 'app/views/layouts/maintenance.html.erb'
	template 'config/initializers/maintenance.rb'
end

#endObject



49
50
51
52
53
# File 'lib/generators/binda/maintenance/maintenance_generator.rb', line 49

def end
	puts ""
	puts "Maintenance page setup completed! Yay!"
	puts "==============================================================================="
end