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 10, 2018
1 parent 3e66f25 commit d9b4a39
Show file tree
Hide file tree
Showing 17 changed files with 276 additions and 267 deletions.
18 changes: 15 additions & 3 deletions app/controllers/admin/accounts_controller.rb
Expand Up @@ -2,29 +2,41 @@

module Admin
class AccountsController < ModuleController
before_action :find_account, except: %i[index]

def index
@accounts = Account.page(params[:page])
end

def show
@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]
end

def update
Account.find(params[:id]).update!(account_params)
@account.update!(account_params)
redirect_to admin_accounts_url
end

def destroy
Account.find(params[:id]).destroy!
@account.destroy!
respond_to do |format|
format.html { redirect_to admin_accounts_url, notice: 'Successfully destroyed.' }
end
end

private

def find_account
@account = Account.find(params[:id])
end

def account_params
params.require(:account).permit(:role)
end
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
131 changes: 131 additions & 0 deletions app/views/admin/accounts/show.html.erb
@@ -0,0 +1,131 @@
<div class="container">
<h4 class="text-center"> Profile </h4>
<ul class="list-group">
<li class="list-group-item">
<b> E-mail: </b>
<%= @account.email%>
</li >
<% if @profile %>
<li class="list-group-item">
<b> First Name: </b>
<%= @profile.first_name%>
</li>

<li class="list-group-item">
<b> Last Name: </b>
<%= @profile.last_name%>
</li>

<li class="list-group-item">
<b> Address: </b>
<%= @profile.address%>
</li>

<li class="list-group-item">
<b> Postcode: </b>
<%= @profile.postcode%>
</li>

<li class="list-group-item">
<b> City: </b>
<%= @profile.city%>
</li>

<li class="list-group-item">
<b> Country: </b>
<%= @profile.country%>
</li>

<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(@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>
<% end %>
</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>
<tr>
<th>ID</th>
<th>Doc Photo</th>
<th>Doc Name</th>
<th>Doc Type</th>
<th>Doc Number</th>
<th>Doc Expire</th>
</tr>
</thead>
<tbody>
<% @documents.each do |document| %>
<tr>
<td><%= document.id %></td>
<td><%= document.upload.present? ? link_to(image_tag(document.upload.url, width: '100', alt: 'Doc Preview'), asset_path(document.upload), target: '_blank') : ''%></td>
<td><%= document.upload&.file&.filename%></td>
<td><%= document.doc_type %></td>
<td><%= document.doc_number %></td>
<td><%= document.doc_expire %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= 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">
<thead>
<tr>
<th>ID</th>
<th>Key</th>
<th>Value</th>
<th>Scope</th>
</tr>
</thead>
<tbody>
<% @labels.each do |label| %>
<tr>
<td><%= label.id %></td>
<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: @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>
</table>
</div>
47 changes: 47 additions & 0 deletions app/views/admin/profiles/_form.html.erb
@@ -0,0 +1,47 @@
<%= form_with(model: [:admin, @account, @profile]) do |form| %>
<% if profile.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(profile.errors.count, "error") %> prohibited this profile 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>

0 comments on commit d9b4a39

Please sign in to comment.