3
4
5
6
7
8
9
10
11
12
13
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
70
71
72
73
74
75
76
77
|
# File 'lib/myrails/modules/devise.rb', line 3
def self.included(thor)
thor.class_eval do
desc 'add_gem', 'Add devise to Gemfile and run bundler'
def add_gem
insert_into_file 'Gemfile', after: "gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]\n" do " gem 'devise'\n"
end
run 'bundle update'
end
desc 'add_rspec_config', 'Add RSpec support file for feature and controller tests'
def add_rspec_config
copy_file 'spec/support/configs/devise.rb', 'spec/support/configs/devise.rb'
end
desc 'configure_devise', 'Genreate devise with a given model name'
def configure_devise
@devise_model = ask "What would you like to call the devise model? Default: ", :yellow, default: 'user'
run 'rails generate devise:install'
run 'rake db:migrate'
run "rails generate devise #{@devise_model}"
run 'rails generate devise:views'
gsub_file 'app/controllers/application_controller.rb', "protect_from_forgery with: :exception", "protect_from_forgery"
inject_into_file 'app/controllers/application_controller.rb', after: "protect_from_forgery\n" do " # Devise authentication method\n before_action :authenticate_\#{@devise_model}!\n CODE\n end\n add_additional_fields\n end\n\n desc 'configure_ui_controller', 'Add code to not prompt for a login if there is a ui_controller'\n def configure_ui_controller\n if File.exist?('app/controllers/ui_controller.rb')\n inject_into_file 'app/controllers/ui_controller.rb', after: \"class UiController < ApplicationController\\n\" do <<-CODE\n skip_before_action :authenticate_\#{@devise_model}!\n"
end
end
end
desc 'add_additional_fields', 'Ask if you want to include additional devise fields like first_name & last_name'
def add_additional_fields
if yes? "Will you be needing registration params override? Explicitly answer 'yes' / 'no' if you will be adding attributes to your #{@devise_model} model", :yellow
inject_into_file 'app/controllers/application_controller.rb', after: "before_action :authenticate_#{@devise_model}!\n" do "# Before action include additional registration params\n# (see #configure_permitted_parameters)\nbefore_action :configure_permitted_parameters, if: :devise_controller?\n CODE\n end\n\n inject_into_file 'app/controllers/application_controller.rb', after: \"private\\n\" do <<-CODE\n# Register additional registration params\ndef configure_permitted_parameters\n devise_parameter_sanitizer.permit(:sign_up, keys: [:attribute, :attribute])\nend\n CODE\n end\n end\n end\n \n desc 'setup_devise', 'Run devise setup actions in order'\n def setup_devise\n add_gem\n add_rspec_config\n configure_devise\n configure_ui_controller\n end\n\n end\nend\n"
|