Skip to content

Commit

Permalink
Move all logic from profile show to accounts show (close openware#449)
Browse files Browse the repository at this point in the history
  • Loading branch information
vpetrusenko committed May 8, 2018
1 parent 3e66f25 commit c945a06
Show file tree
Hide file tree
Showing 15 changed files with 179 additions and 83 deletions.
8 changes: 8 additions & 0 deletions app/controllers/admin/accounts_controller.rb
Expand Up @@ -6,6 +6,14 @@ def index
@accounts = Account.page(params[:page])
end

def show
@account = Account.find(params[:id])
@profile = @account.profile
@documents = @account.documents
@labels = @account.labels
@phones = @account.phones
end

def edit
@account = Account.find(params[:id])
@roles = %w[admin compliance member]
Expand Down
6 changes: 3 additions & 3 deletions app/controllers/admin/labels_controller.rb
Expand Up @@ -13,7 +13,7 @@ def create
@label = @account.labels.new(label_params)

if @label.save
redirect_to admin_profile_path(@account.profile), notice: 'Label was successfully created.'
redirect_to admin_account_path(@account), notice: 'Label was successfully created.'
else
render :new
end
Expand All @@ -24,15 +24,15 @@ def edit

def update
if @label.update(label_params)
redirect_to admin_profile_path(@account.profile), notice: 'Label was successfully updated.'
redirect_to admin_account_path(@account), notice: 'Label was successfully updated.'
else
render :edit
end
end

def destroy
@label.destroy!
redirect_to admin_profile_path(@account.profile), notice: 'Label was successfully destroyed.'
redirect_to admin_account_path(@account), notice: 'Label was successfully destroyed.'
end

private
Expand Down
36 changes: 23 additions & 13 deletions app/controllers/admin/profiles_controller.rb
Expand Up @@ -2,27 +2,37 @@

module Admin
class ProfilesController < ModuleController
def index
params[:filter] = params[:filter] || 'pending'
@profiles = Profile.all
@profiles = @profiles.where(state: params[:filter]) if params[:filter].present?
@profiles = @profiles.page(params[:page])
end
before_action :find_profile

def show
@profile = Profile.find(params[:id])
@documents = @profile.account.documents
@labels = @profile.account.labels
def edit
end

def update
if @profile.update(profile_params)
redirect_to admin_account_path(@profile.account), notice: 'Profile was successfully updated.'
else
render :edit
end
end

def change_state
@profile = Profile.find(params[:id])
if @profile.update(state: params[:state])
redirect_to admin_profile_path(@profile), notice: 'Profile was successfully updated.'
redirect_to admin_account_path(@profile.account), notice: 'Profile was successfully updated.'
else
redirect_to admin_profiles_path
redirect_to admin_account_path
end
end

private

def find_profile
@profile = Profile.find(params[:id])
end

def profile_params
params.require(:profile)
.permit(:first_name, :last_name,
:dob, :address, :postcode, :city, :country)
end
end
end
25 changes: 25 additions & 0 deletions app/mailers/account_mailer.rb
@@ -0,0 +1,25 @@
# frozen_string_literal: true

class AccountMailer < Devise::Mailer
include SkipEmails

def confirmation_instructions(record, token, opts = {})
send_email_if_enabled { super }
end

def reset_password_instructions(record, token, opts = {})
send_email_if_enabled { super }
end

def unlock_instructions(record, token, opts = {})
send_email_if_enabled { super }
end

def email_changed(record, opts = {})
send_email_if_enabled { super }
end

def password_change(record, opts = {})
send_email_if_enabled { super }
end
end
8 changes: 7 additions & 1 deletion app/mailers/application_mailer.rb
@@ -1,6 +1,12 @@
# frozen_string_literal: true

class ApplicationMailer < ActionMailer::Base
default from: 'from@example.com'
include SkipEmails

default from: ENV.fetch('SENDER_EMAIL', 'noreply@barong.io')
layout 'mailer'

def mail(options)
send_email_if_enabled { super }
end
end
15 changes: 15 additions & 0 deletions app/mailers/concerns/skip_emails.rb
@@ -0,0 +1,15 @@
# frozen_string_literal: true

module SkipEmails
extend ActiveSupport::Concern

def send_email_if_enabled
raise 'Block is required' unless block_given?

if ENV['SKIP_EMAILS']
return Rails.logger.info 'Emails are skip. You need to use Event API to handle emails manually'
end

yield
end
end
4 changes: 1 addition & 3 deletions app/mailers/profile_review_mailer.rb
@@ -1,8 +1,6 @@
# frozen_string_literal: true

class ProfileReviewMailer < ActionMailer::Base
default from: ENV.fetch('SENDER_EMAIL', 'noreply@barong.io')

class ProfileReviewMailer < ApplicationMailer
def approved(account)
@profile = account.profile
@app_name = ENV.fetch('APP_NAME', 'Barong')
Expand Down
2 changes: 1 addition & 1 deletion app/views/admin/accounts/index.html.erb
Expand Up @@ -20,7 +20,7 @@
<% @accounts.each do |account| %>
<tr class=<%= 'table-danger' if account.unlock_token? %>>
<td><%= account.id %></td>
<td><%= account.email %></td>
<td><%= link_to account.email, admin_account_path(account.id) %></td>
<td><span class=" badge <%= badge_by_role(account.role) %>"> <%= account.role %> </span></td>
<td><%= account.level %></td>
<td><%= label_tags(account) %></td>
Expand Down
@@ -1,10 +1,17 @@
<div class="container">
<h1>Profile</h1>
<h4 class="text-center"> Profile </h4>
<ul class="list-group">
<li class="list-group-item">
<b> E-mail: </b>
<%= @profile.account.email%>
</li>
<b> E-mail: </b>
<%= @account.email%>
</li >

<li class="list-group-item">
<b>Phones:</b>
<% @phones.each do |phone| %>
<%= phone.number%>
<% end %>
</li >

<li class="list-group-item">
<b> First Name: </b>
Expand Down Expand Up @@ -36,21 +43,46 @@
<%= @profile.country%>
</li>

<li class="list-group-item">
<li class="list-group-item">
<b> State: </b>
<div class="dropdown show">
<a class="btn btn-secondary dropdown-toggle" href="#" role="button" id="dropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<%= @profile.state.upcase_first %>
</a>
<div class="dropdown-menu">
<% Profile::STATES.each do |state|%>
<%= link_to(state.upcase_first, change_state_admin_profile_path(@profile, state: state), method: :put, class: 'dropdown-item') unless @profile.state == state%>
<%= link_to(state.upcase_first, change_state_admin_profile_path(@account.profile, state: state), method: :put, class: 'dropdown-item') unless @account.profile.state == state%>
<% end %>
</div>
</div>
</li>
<li class="list-group-item">
<%= link_to 'Edit', edit_admin_profile_path(@account.profile), class: 'btn btn-primary btn-sm' %>
</li>
</ul>
<br>
<h4 class="text-center"> Phones </h4>
<table class="table table-bordered table-striped table-hover table-sm text-center">
<thead>
<tr>
<th>ID</th>
<th>Country</th>
<th>Number</th>
<th>Vlidated at</th>
</tr>
</thead>
<tbody>
<% @phones.each do |phone| %>
<tr>
<td><%= phone.id %></td>
<td><%= phone.country %></td>
<td><%= phone.number %></td>
<td><%= phone.validated_at %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<h4 class="text-center"> Documents </h4>
<table class="table table-bordered table-striped table-hover table-sm text-center">
<thead>
Expand All @@ -77,7 +109,7 @@
</tbody>
</table>
<br>
<%= link_to 'New Label', new_admin_account_label_path(@profile.account_id), class: 'btn btn-success' %>
<%= link_to 'New Label', new_admin_account_label_path(@account.id), class: 'btn btn-success' %>
<br>
<h4 class="text-center"> Labels </h4>
<table class="table table-bordered table-striped table-hover table-sm text-center">
Expand All @@ -96,8 +128,8 @@
<td><%= label.key %></td>
<td><%= label.value %></td>
<td><%= label.scope %></td>
<td><%= link_to 'Edit', edit_admin_account_label_path(id: label.id, account_id: @profile.account_id), class: 'btn btn-primary btn-sm' %></td>
<td><%= link_to 'Delete', admin_account_label_path(id: label.id, account_id: @profile.account_id), class: 'btn btn-danger btn-sm', method: :delete, data: {confirm: 'Are you sure?'} %></td>
<td><%= link_to 'Edit', edit_admin_account_label_path(id: label.id, account_id: @account.id), class: 'btn btn-primary btn-sm' %></td>
<td><%= link_to 'Delete', admin_account_label_path(id: label.id, account_id: @account.id), class: 'btn btn-danger btn-sm', method: :delete, data: {confirm: 'Are you sure?'} %></td>
</tr>
<% end %>
</tbody>
Expand Down
47 changes: 47 additions & 0 deletions app/views/admin/profiles/_form.html.erb
@@ -0,0 +1,47 @@
<%= form_with(model: [:admin, @profile], local: false) do |form| %>
<% if profile.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(profile.errors.count, "error") %> prohibited this website from being saved:</h2>

<ul>
<% profile.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<%= form.label :first_name, class: 'control-label'%>
<%= form.text_field :first_name, class: 'form-control col-sm-10'%>
<br>

<%= form.label :last_name, class: 'control-label'%>
<%= form.text_field :last_name, class: 'form-control col-sm-10'%>
<br>

<%= form.label :dob, class: 'control-label'%>
<%= form.text_field :dob, class: 'form-control col-sm-10'%>
<br>

<%= form.label :address, class: 'control-label'%>
<%= form.text_field :address, class: 'form-control col-sm-10'%>
<br>

<%= form.label :postcode, class: 'control-label'%>
<%= form.text_field :postcode, class: 'form-control col-sm-10'%>
<br>

<%= form.label :city, class: 'control-label'%>
<%= form.text_field :city, class: 'form-control col-sm-10'%>
<br>

<%= form.label :country, class: 'control-label'%>
<%= form.text_field :country, class: 'form-control col-sm-10'%>
<br>

<%= form.submit 'Submit', class: 'btn btn-primary' %>

<br>
<br>

<% end %>
5 changes: 5 additions & 0 deletions app/views/admin/profiles/edit.html.erb
@@ -0,0 +1,5 @@
<div class="container">
<h1>Editing profile</h1>
<%= render 'form', profile: @profile %>
<br>
</div>
44 changes: 0 additions & 44 deletions app/views/admin/profiles/index.html.erb

This file was deleted.

6 changes: 0 additions & 6 deletions app/views/admin/shared/_navigation.html.erb
Expand Up @@ -9,12 +9,6 @@
</li>
<% end %>
<% if can? :read, Profile %>
<li class="nav-item <%= 'active' if params[:controller].match?(/profiles/) %>">
<%= link_to 'Profiles', admin_profiles_path, class: 'nav-link' %>
</li>
<% end %>
<% if current_account.role.admin? %>
<li class="nav-item <%= 'active' if params[:controller].match?(/application/) %>" >
<%= link_to 'Applications', oauth_applications_path, class: 'nav-link' %>
Expand Down
2 changes: 1 addition & 1 deletion config/initializers/devise.rb
Expand Up @@ -19,7 +19,7 @@
config.mailer_sender = "#{sender_name} <#{sender_email}>"

# Configure the class responsible to send e-mails.
# config.mailer = 'Devise::Mailer'
config.mailer = 'AccountMailer'

# Configure the parent class responsible to send e-mails.
# config.parent_mailer = 'ActionMailer::Base'
Expand Down
4 changes: 2 additions & 2 deletions config/routes.rb
Expand Up @@ -24,11 +24,11 @@

namespace :admin do
get '/', to: 'accounts#index', as: :accounts
resources :accounts, except: %i[new create show] do
resources :accounts, except: %i[new create] do
resources :labels, except: %i[index show]
end
resources :websites
resources :profiles, only: %i[index show] do
resources :profiles, except: %i[index new show] do
put :change_state, on: :member
end
end
Expand Down

0 comments on commit c945a06

Please sign in to comment.