Skip to content

Commit

Permalink
Autoformat with Rufo
Browse files Browse the repository at this point in the history
  • Loading branch information
tvdeyen committed Oct 26, 2020
1 parent cc6e620 commit 7150865
Show file tree
Hide file tree
Showing 17 changed files with 228 additions and 197 deletions.
6 changes: 4 additions & 2 deletions app/controllers/alchemy/accounts_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# frozen_string_literal: true

module Alchemy
class AccountsController < ::Devise::RegistrationsController
helper 'Alchemy::Pages'
helper "Alchemy::Pages"

def show
authorize! :show, current_alchemy_user
Expand All @@ -11,7 +13,7 @@ def show

def permission_denied(*)
store_location_for(:user, account_path)
flash[:warning] = t(:unauthenticated, scope: 'devise.failure')
flash[:warning] = t(:unauthenticated, scope: "devise.failure")
redirect_to alchemy.login_path
end
end
Expand Down
4 changes: 3 additions & 1 deletion app/controllers/alchemy/confirmations_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# frozen_string_literal: true

module Alchemy
class ConfirmationsController < ::Devise::ConfirmationsController
helper 'Alchemy::Pages'
helper "Alchemy::Pages"

private

Expand Down
4 changes: 3 additions & 1 deletion app/controllers/alchemy/passwords_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# frozen_string_literal: true

module Alchemy
class PasswordsController < ::Devise::PasswordsController
helper 'Alchemy::Pages'
helper "Alchemy::Pages"

private

Expand Down
4 changes: 3 additions & 1 deletion app/controllers/alchemy/sessions_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# frozen_string_literal: true

module Alchemy
class SessionsController < ::Devise::SessionsController
helper 'Alchemy::Pages'
helper "Alchemy::Pages"

private

Expand Down
21 changes: 11 additions & 10 deletions app/mailers/alchemy/notifications.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
# frozen_string_literal: true

module Alchemy
class Notifications < ::Devise::Mailer

default(from: Config.get(:mailer)['mail_from'])
default(from: Config.get(:mailer)["mail_from"])

def member_created(user)
@user = user

mail(
to: user.email,
subject: Alchemy.t("Your user credentials")
subject: Alchemy.t("Your user credentials"),
)
end

Expand All @@ -17,34 +18,34 @@ def alchemy_user_created(user)
@url = admin_url
mail(
to: user.email,
subject: Alchemy.t("Your Alchemy Login")
subject: Alchemy.t("Your Alchemy Login"),
)
end

def member_reset_password_instructions(user, token, opts={})
def member_reset_password_instructions(user, token, _opts = {})
@user = user
@token = token
mail(
to: user.email,
subject: Alchemy.t("Reset password instructions")
subject: Alchemy.t("Reset password instructions"),
)
end

def reset_password_instructions(user, token, opts={})
def reset_password_instructions(user, token, _opts = {})
@user = user
@token = token
mail(
to: user.email,
subject: Alchemy.t("Reset password instructions")
subject: Alchemy.t("Reset password instructions"),
)
end

def confirmation_instructions(user, token, opts={})
def confirmation_instructions(user, token, _opts = {})
@user = user
@token = token
mail(
to: user.email,
subject: Alchemy.t("Account confirmation instructions")
subject: Alchemy.t("Account confirmation instructions"),
)
end
end
Expand Down
34 changes: 18 additions & 16 deletions app/models/alchemy/user.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
require 'devise/orm/active_record'
require 'userstamp'
# frozen_string_literal: true

require "devise/orm/active_record"
require "userstamp"

module Alchemy
class User < ActiveRecord::Base
Expand All @@ -12,7 +14,7 @@ class User < ActiveRecord::Base
:password,
:password_confirmation,
:send_credentials,
:tag_list
:tag_list,
]

devise *Alchemy.devise_modules
Expand All @@ -29,9 +31,9 @@ class User < ActiveRecord::Base
# Unlock all locked pages before destroy.
before_destroy :unlock_pages!

scope :admins, -> { where(arel_table[:alchemy_roles].matches('%admin%')) }
scope :logged_in, -> { where('last_request_at > ?', logged_in_timeout.seconds.ago) }
scope :logged_out, -> { where('last_request_at is NULL or last_request_at <= ?', logged_in_timeout.seconds.ago) }
scope :admins, -> { where(arel_table[:alchemy_roles].matches("%admin%")) }
scope :logged_in, -> { where("last_request_at > ?", logged_in_timeout.seconds.ago) }
scope :logged_out, -> { where("last_request_at is NULL or last_request_at <= ?", logged_in_timeout.seconds.ago) }

ROLES = Config.get(:user_roles)

Expand All @@ -51,10 +53,7 @@ def logged_in_timeout
def search(query)
query = "%#{query.downcase}%"

where arel_table[:login].lower.matches(query)
.or arel_table[:email].lower.matches(query)
.or arel_table[:firstname].lower.matches(query)
.or arel_table[:lastname].lower.matches(query)
where arel_table[:login].lower.matches(query).or arel_table[:email].lower.matches(query).or arel_table[:firstname].lower.matches(query).or arel_table[:lastname].lower.matches(query)
end
end

Expand All @@ -67,12 +66,12 @@ def role
end

def alchemy_roles
read_attribute(:alchemy_roles).split(' ')
read_attribute(:alchemy_roles).split(" ")
end

def alchemy_roles=(roles_string)
if roles_string.is_a? Array
write_attribute(:alchemy_roles, roles_string.join(' '))
write_attribute(:alchemy_roles, roles_string.join(" "))
elsif roles_string.is_a? String
write_attribute(:alchemy_roles, roles_string)
end
Expand All @@ -84,8 +83,9 @@ def add_role(role)

# Returns true if the user ahs admin role
def is_admin?
has_role? 'admin'
has_role? "admin"
end

alias_method :admin?, :is_admin?

# Returns true if the user has the given role.
Expand All @@ -105,6 +105,7 @@ def unlock_pages!
def pages_locked_by_me
Page.locked_by(self).order(:updated_at)
end

alias_method :locked_pages, :pages_locked_by_me

# Returns the firstname and lastname as a string
Expand All @@ -118,11 +119,12 @@ def fullname(options = {})
if lastname.blank? && firstname.blank?
login
else
options = {:flipped => false}.merge(options)
options = { flipped: false }.merge(options)
fullname = options[:flipped] ? "#{lastname}, #{firstname}" : "#{firstname} #{lastname}"
fullname.squeeze(" ").strip
end
end

alias_method :name, :fullname
alias_method :alchemy_display_name, :fullname

Expand Down Expand Up @@ -150,7 +152,7 @@ def store_request_time!
# Delivers a welcome mail depending from user's role.
#
def deliver_welcome_mail
if has_role?('author') || has_role?('editor') || has_role?('admin')
if has_role?("author") || has_role?("editor") || has_role?("admin")
Notifications.alchemy_user_created(self).deliver_later
else
Notifications.member_created(self).deliver_later
Expand All @@ -159,7 +161,7 @@ def deliver_welcome_mail

# Overwritten to send a different email to members
def send_reset_password_instructions_notification(token)
if has_role?('member')
if has_role?("member")
send_devise_notification(:member_reset_password_instructions, token, {})
else
send_devise_notification(:reset_password_instructions, token, {})
Expand Down
45 changes: 23 additions & 22 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
# frozen_string_literal: true

Alchemy::Engine.routes.draw do
if Alchemy::Devise.enable_user_accounts?
devise_for :user,
class_name: 'Alchemy::User',
class_name: "Alchemy::User",
singular: :user,
skip: :all,
controllers: {
registrations: Alchemy::Devise.registrations_enabled? ? 'alchemy/accounts' : nil,
confirmations: Alchemy::Devise.confirmations_enabled? ? 'alchemy/confirmations' : nil,
sessions: 'alchemy/sessions',
passwords: 'alchemy/passwords'
registrations: Alchemy::Devise.registrations_enabled? ? "alchemy/accounts" : nil,
confirmations: Alchemy::Devise.confirmations_enabled? ? "alchemy/confirmations" : nil,
sessions: "alchemy/sessions",
passwords: "alchemy/passwords",
},
path: :account,
router_name: :alchemy

scope :account do
devise_scope :user do
get '/login' => 'sessions#new'
post '/login' => 'sessions#create'
match '/logout' => 'sessions#destroy', via: Devise.sign_out_via
get "/login" => "sessions#new"
post "/login" => "sessions#create"
match "/logout" => "sessions#destroy", via: Devise.sign_out_via

if Alchemy::Devise.confirmations_enabled?
resource :confirmation, only: %i[new create show]
Expand All @@ -33,37 +35,36 @@

namespace :admin, {
path: Alchemy.admin_path,
constraints: Alchemy.admin_constraints
constraints: Alchemy.admin_constraints,
} do

devise_for :user,
class_name: 'Alchemy::User',
class_name: "Alchemy::User",
singular: :user,
skip: :all,
controllers: {
sessions: 'alchemy/admin/user_sessions',
passwords: 'alchemy/admin/passwords'
sessions: "alchemy/admin/user_sessions",
passwords: "alchemy/admin/passwords",
},
router_name: :alchemy

devise_scope :user do
get '/dashboard' => 'dashboard#index',
get "/dashboard" => "dashboard#index",
:as => :user_root
get '/signup' => 'users#signup',
get "/signup" => "users#signup",
:as => :signup
get '/login' => 'user_sessions#new',
get "/login" => "user_sessions#new",
:as => :login
post '/login' => 'user_sessions#create'
match '/logout' => 'user_sessions#destroy',
post "/login" => "user_sessions#create"
match "/logout" => "user_sessions#destroy",
:as => :logout, via: Devise.sign_out_via

get '/passwords' => 'passwords#new',
get "/passwords" => "passwords#new",
:as => :new_password
get '/passwords/:id/edit/:reset_password_token' => 'passwords#edit',
get "/passwords/:id/edit/:reset_password_token" => "passwords#edit",
:as => :edit_password
post '/passwords' => 'passwords#create',
post "/passwords" => "passwords#create",
:as => :reset_password
patch '/passwords' => 'passwords#update',
patch "/passwords" => "passwords#update",
:as => :update_password
end

Expand Down
22 changes: 12 additions & 10 deletions spec/controllers/accounts_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -1,43 +1,45 @@
require 'rails_helper'
# frozen_string_literal: true

require "rails_helper"

describe Alchemy::AccountsController do
routes { Alchemy::Engine.routes }

context 'with user accounts enabled' do
context "with user accounts enabled" do
before do
allow(Alchemy::Devise).to receive(:enable_user_accounts?) { true }
Rails.application.reload_routes!
@request.env["devise.mapping"] = Devise.mappings[:user]
end

describe '#show' do
describe "#show" do
let(:user) { create(:alchemy_member_user) }

context 'with authorized user' do
context "with authorized user" do
before { authorize_user(user) }

render_views

it 'shows account' do
it "shows account" do
get :show
is_expected.to render_template(:show)
end
end

context 'with unauthorized user' do
it 'redirects to login' do
context "with unauthorized user" do
it "redirects to login" do
get :show
is_expected.to redirect_to(login_path)
end

it 'stores current location' do
it "stores current location" do
get :show
expect(session[:user_return_to]).to eq(account_path)
end

it 'shows warning message' do
it "shows warning message" do
get :show
expect(flash[:warning]).to eq I18n.t(:unauthenticated, scope: 'devise.failure')
expect(flash[:warning]).to eq I18n.t(:unauthenticated, scope: "devise.failure")
end
end
end
Expand Down
Loading

0 comments on commit 7150865

Please sign in to comment.