Skip to content

Commit

Permalink
Fixed devise modules issue
Browse files Browse the repository at this point in the history
  • Loading branch information
spencerldixon committed Jul 17, 2024
1 parent 45882a0 commit d2cfa6d
Showing 1 changed file with 60 additions and 2 deletions.
62 changes: 60 additions & 2 deletions lib/boilerplate/modules/devise.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
require "active_support/inflector"

module Devise
MODULES = %w(
lockable
trackable
confirmable
)

def install_devise(silent: false, **)
prompt = TTY::Prompt.new(quiet: true)

Expand Down Expand Up @@ -32,10 +38,13 @@ def install_devise(silent: false, **)
eos
end

# Enable confirmable and lockable
if silent || prompt.yes?("Enable :lockable and :confirmable modules for Devise?")
# Install additional devise modules
selected_modules = silent ? MODULES : prompt.multi_select("Install additional modules for Devise", MODULES)
selected_modules.each do |devise_module|
self.send("install_devise_#{devise_module}", resource)
end

# Install views
if silent || prompt.yes?("What do you want to copy over boilerplate's prestyled devise views and partials?")
# TODO Copy devise views
else
Expand All @@ -45,4 +54,53 @@ def install_devise(silent: false, **)
end
end
end

def install_devise_lockable(resource)
text = <<-eos
t.integer :failed_attempts, default: 0, null: false
t.string :unlock_token
t.datetime :locked_at
add_index :users, :unlock_token, unique: true
eos

self.send("install_devise_module", resource, "lockable", text)
end

def install_devise_trackable(resource)
text = <<-eos
Trackable
t.integer :sign_in_count, default: 0, null: false
t.datetime :current_sign_in_at
t.datetime :last_sign_in_at
t.string :current_sign_in_ip
t.string :last_sign_in_ip
eos

self.send("install_devise_module", resource, "trackable", text)
end

def install_devise_confirmable(resource)
text = <<-eos
Confirmable
t.string :confirmation_token
t.datetime :confirmed_at
t.datetime :confirmation_sent_at
t.string :unconfirmed_email # Only if using reconfirmable
add_index :users, :confirmation_token, unique: true
eos

self.send("install_devise_module", resource, "confirmable", text)
end

def install_devise_module(resource, module_name, migration_text)
# Add to the modules list in the model file
inject_into_file "app/models/#{resource.downcase}.rb", after: /devise :.+\n.+/, force: true do
", :#{module_name}"
end

# Uncomment lines from devise migration
migration_text.lines.each do |line|
uncomment_lines Dir.glob("db/migrate/*_devise_create_#{resource.pluralize.downcase}.rb").first, line.strip
end
end
end

0 comments on commit d2cfa6d

Please sign in to comment.