Skip to content

Instantly share code, notes, and snippets.

@Sylvance
Created January 31, 2020 18:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Sylvance/5c7d5db8ce8609fa16403e74255dee39 to your computer and use it in GitHub Desktop.
Save Sylvance/5c7d5db8ce8609fa16403e74255dee39 to your computer and use it in GitHub Desktop.
Template to setup a rails app with authentication and users seed data

Setup

To set this up you will need to download this gist. Change into the directory this gist is in then;

  • rails new your_app_name --database=postgresql -m ./template.rb
project_name = ask("What is the project name?")
gem 'bcrypt', '~> 3.1.7'
gem 'will_paginate', '~> 3.1.0'
gem 'faker'
gem_group :development, :test do
gem 'rspec-rails'
gem 'pry'
end
generate(:scaffold, "user name:string email:string password:digest")
route "root to: 'users#index'"
route "get 'signup', to: 'users#new', as: 'signup'"
route "get 'login', to: 'sessions#new', as: 'login'"
route "get 'logout', to: 'sessions#destroy', as: 'logout'"
rails_command("db:create")
rails_command("db:migrate")
after_bundle do
run "echo '#{project_name}' >> README.md"
git :init
end
file 'app/controllers/sessions_controller.rb', <<-CODE
class SessionsController < ApplicationController
skip_before_action :authenticate_request
def new
end
def create
user = User.find_by_email(params[:email])
if user && user.authenticate(params[:password])
session[:user_id] = user.id
redirect_to root_url, notice: "Logged in!"
else
flash.now[:alert] = "Email or password is invalid"
redirect_to login_path, notice: "Email or password is invalid"
end
end
def destroy
session[:user_id] = nil
redirect_to root_url, notice: "Logged out!"
end
end
CODE
file 'app/controllers/application_controller.rb', <<-CODE
class ApplicationController < ActionController::Base
include ApplicationHelper
helper_method :current_user,
:logged_in?
before_action :authenticate_request
def current_user
if session[:user_id]
@current_user ||= User.find(session[:user_id])
else
@current_user = nil
end
end
def logged_in?
!current_user.nil?
end
private
def authenticate_request
if !logged_in?
redirect_to login_path, notice: "Log in first to get access!"
end
end
end
CODE
file 'app/helpers/application_helper.rb', <<-CODE
module ApplicationHelper
def is_current_path?(test_path)
request.path == test_path
end
def current_class?(test_path)
return ' border-b-2 border-green-500 hover:border-green-700' if is_current_path?(test_path)
''
end
end
CODE
inject_into_file 'app/controllers/users_controller.rb', :after => "before_action :set_user, only: [:show, :edit, :update, :destroy]" do
"\nskip_before_action :authenticate_request, only: [:new, :create]"
end
inject_into_file 'config/routes.rb', :before => "# For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html" do
"\nresources :sessions, only: [:new, :create, :destroy]\n"
end
file 'app/views/sessions/new.html.erb', <<-'CODE'
<div class="">
<%= form_tag(sessions_path, {class: ""}) do |form| %>
<h1 class="">Login</h1>
<p class="notice text-blue-500"><%= notice %></p>
<div class="">
<%= label_tag :email, 'Email', class: "" %>
<%= text_field_tag :email, nil, class: "", placeholder: 'Email' %>
</div>
<div class="">
<%= label_tag :password, 'Password', class: "" %>
<%= password_field_tag :password, nil, class: "", placeholder: 'Password' %>
</div>
<div class="actions">
<%= submit_tag "Login", class: "" %>
</div>
<% end %>
</div>
CODE
file 'app/views/sessions/create.html.erb', <<-'CODE'
<h1>Sessions#create</h1>
<p>Find me in app/views/sessions/create.html.erb</p>
CODE
file 'app/views/sessions/destroy.html.erb', <<-'CODE'
<h1>Sessions#destroy</h1>
<p>Find me in app/views/sessions/destroy.html.erb</p>
CODE
file 'db/seeds.rb', <<-'CODE'
require 'faker'
begin
require 'io/console/size'
rescue LoadError
# for JRuby
require 'io/console'
end
@last_width = 0
def progress(subject, done_so_far, total)
progress_bar = sprintf("%3d%% [%2d/%2d] ",
100 * done_so_far / total,
done_so_far,
total)
size = IO.respond_to?(:console_size) ? IO.console_size : IO.console.winsize
terminal_width = size[1].to_i.nonzero? || 80
max_size = terminal_width - progress_bar.size
line = "#{progress_bar}#{subject}"
if $stdout.tty?
$stdout.print("\r" + (" " * @last_width) + ("\b" * @last_width) + "\r") if @last_width && @last_width > 0
@last_width = line.size
$stdout.print("#{line}\r")
else
$stdout.puts(line)
end
$stdout.flush
end
user_number = 0
total_number_of_users = 81
emails = ["@gmail.com", "@yahoo.com", "@onelive.com", "@me.com", "@tech.com", "@company.com"]
total_number_of_users.times do
name = Faker::Name.first_name
other_name = Faker::Name.last_name
email = name.downcase + "." + other_name.downcase + emails.sample
user = User.new(
email: email,
password: "password",
password_confirmation: "password",
name: name
)
user.save
user_number = user_number + 1
progress('Users seed data', user_number, total_number_of_users)
end
user = User.new(
email: "demo.user@mail.com",
password: "password",
password_confirmation: "password",
name: "demouser"
)
user.save
CODE
rails_command("db:seed")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment