Class: DangerzoneGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Includes:
Rails::Generators::Migration
Defined in:
lib/dangerzone/dangerzone_generator.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.next_migration_number(path) ⇒ Object

desc “add the migrations”



9
10
11
12
13
14
15
16
# File 'lib/dangerzone/dangerzone_generator.rb', line 9

def self.next_migration_number(path)
  unless @prev_migration_nr
    @prev_migration_nr = Time.now.utc.strftime('%Y%m%d%H%M%S').to_i
  else
    @prev_migration_nr += 1
  end
  @prev_migration_nr.to_s
end

Instance Method Details

#add_mailer_config_to_development_and_testObject



82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/dangerzone/dangerzone_generator.rb', line 82

def add_mailer_config_to_development_and_test
  comment = '# Via dangerzone: configures actionmailer to use localhost:3000 as its default url'
  config_stuff = "config.action_mailer.default_url_options = { :host => 'localhost:3000' }"
  line = 'config.assets.debug = true'
  gsub_file 'config/environments/development.rb', /.+(#{Regexp.escape(line)})/mi do |match|
    "#{match}\n\n  #{comment}\n  #{config_stuff}\n\n"
  end
  line = 'config.active_support.deprecation = :stderr'
  gsub_file 'config/environments/test.rb', /.+(#{Regexp.escape(line)})/mi do |match|
    "#{match}\n\n  #{comment}\n  #{config_stuff}\n\n"
  end
end

#add_methods_to_application_controllerObject



55
56
57
58
59
60
61
# File 'lib/dangerzone/dangerzone_generator.rb', line 55

def add_methods_to_application_controller
  app_controller_methods = IO.read(get_directory + '/templates/controllers/application_controller.rb')
  line = 'protect_from_forgery'
  gsub_file 'app/controllers/application_controller.rb', /.+(#{Regexp.escape(line)})/mi do |match|
    "#{match}\n\n#{app_controller_methods}\n"
  end
end

#add_nav_partial_and_notice_to_application_html_erbObject



42
43
44
45
46
47
48
# File 'lib/dangerzone/dangerzone_generator.rb', line 42

def add_nav_partial_and_notice_to_application_html_erb
  nav = "<%= render 'layouts/dangerzone_nav' %>\n\n<%= notice %>"
  line = '<body>'
  gsub_file 'app/views/layouts/application.html.erb', /(#{Regexp.escape(line)})/mi do |match|
    "#{match}\n#{nav}\n"
  end
end

#edit_the_routes_fileObject



22
23
24
25
26
27
28
# File 'lib/dangerzone/dangerzone_generator.rb', line 22

def edit_the_routes_file
  routes = IO.read(get_directory + '/templates/routes.rb')
  line = '::Application.routes.draw do'
  gsub_file 'config/routes.rb', /.+(#{Regexp.escape(line)})/mi do |match|
  "#{match}\n\n#{routes}\n"
  end
end

#fill_view_directoriesObject



100
101
102
103
104
105
106
107
108
109
110
# File 'lib/dangerzone/dangerzone_generator.rb', line 100

def fill_view_directories
  file_names = %w(check_your_email.html.erb new.html.erb dangerzone.html.erb)
  copy_files_to_app_dir(file_names, "views/create_accounts")
  file_names = %w(account_confirmation_email.html.erb
                  account_confirmation_email.text.erb
                  reset_password_email.html.erb
                  reset_password_email.text.erb)
  copy_files_to_app_dir(file_names, "views/dangerzone_mailer")
  copy_files_to_app_dir(%w(new.html.erb reset_password_form.html.erb), 'views/reset_passwords')
  copy_files_to_app_dir(%w(new.html.erb), 'views/sessions')
end

#generate_mailerObject



78
79
80
# File 'lib/dangerzone/dangerzone_generator.rb', line 78

def generate_mailer
  copy_files_to_app_dir %w(dangerzone_mailer.rb), 'mailers'
end

#generate_spec_folderObject



63
64
65
66
67
68
# File 'lib/dangerzone/dangerzone_generator.rb', line 63

def generate_spec_folder
  empty_directory 'spec'
  empty_directory 'spec/models'
  empty_directory 'spec/factories'
  empty_directory 'spec/controllers'
end

#generate_the_controllersObject



50
51
52
53
# File 'lib/dangerzone/dangerzone_generator.rb', line 50

def generate_the_controllers
  file_names = %w(create_accounts_controller.rb reset_passwords_controller.rb sessions_controller.rb)
  copy_files_to_app_dir file_names, 'controllers'
end

#generate_the_create_users_migration_fileObject



18
19
20
# File 'lib/dangerzone/dangerzone_generator.rb', line 18

def generate_the_create_users_migration_file
  migration_template 'migration.rb', 'db/migrate/create_users_table_via_dangerzone.rb'
end

#generate_the_nav_partialObject



38
39
40
# File 'lib/dangerzone/dangerzone_generator.rb', line 38

def generate_the_nav_partial
  copy_files_to_app_dir %w(_dangerzone_nav.html.erb), 'views/layouts'
end

#generate_the_specsObject



70
71
72
73
74
75
76
# File 'lib/dangerzone/dangerzone_generator.rb', line 70

def generate_the_specs
  copy_files_to_spec_dir %w(users_factory.rb), 'factories'
  file_names =  %w(create_accounts_controller_spec.rb reset_passwords_controller_spec.rb sessions_controller_spec.rb)
  copy_files_to_spec_dir file_names, 'controllers'
  copy_files_to_spec_dir %w(user_spec.rb), 'models'
  copy_file 'spec/spec_helper.rb', 'spec/spec_helper.rb'
end

#generate_user_model_fileObject



34
35
36
# File 'lib/dangerzone/dangerzone_generator.rb', line 34

def generate_user_model_file
  copy_files_to_app_dir %w(user.rb), 'models'
end

#generate_view_directoriesObject



95
96
97
98
# File 'lib/dangerzone/dangerzone_generator.rb', line 95

def generate_view_directories
  file_names = %w(create_accounts dangerzone_mailer reset_passwords sessions)
  file_names.each { |f| empty_directory "app/views/#{f}" }
end

#get_rid_of_rails_default_index_page_in_indexObject



30
31
32
# File 'lib/dangerzone/dangerzone_generator.rb', line 30

def get_rid_of_rails_default_index_page_in_index
  remove_file 'public/index.html'
end