From c945a064b7e59d1d5eb19ef8bf7e8d111fd3562a Mon Sep 17 00:00:00 2001 From: Valera Petrusenko Date: Tue, 8 May 2018 19:26:21 +0300 Subject: [PATCH] Move all logic from profile show to accounts show (close #449) --- app/controllers/admin/accounts_controller.rb | 8 +++ app/controllers/admin/labels_controller.rb | 6 +-- app/controllers/admin/profiles_controller.rb | 36 ++++++++----- app/mailers/account_mailer.rb | 25 ++++++++++ app/mailers/application_mailer.rb | 8 ++- app/mailers/concerns/skip_emails.rb | 15 ++++++ app/mailers/profile_review_mailer.rb | 4 +- app/views/admin/accounts/index.html.erb | 2 +- .../{profiles => accounts}/show.html.erb | 50 +++++++++++++++---- app/views/admin/profiles/_form.html.erb | 47 +++++++++++++++++ app/views/admin/profiles/edit.html.erb | 5 ++ app/views/admin/profiles/index.html.erb | 44 ---------------- app/views/admin/shared/_navigation.html.erb | 6 --- config/initializers/devise.rb | 2 +- config/routes.rb | 4 +- 15 files changed, 179 insertions(+), 83 deletions(-) create mode 100644 app/mailers/account_mailer.rb create mode 100644 app/mailers/concerns/skip_emails.rb rename app/views/admin/{profiles => accounts}/show.html.erb (66%) create mode 100644 app/views/admin/profiles/_form.html.erb create mode 100644 app/views/admin/profiles/edit.html.erb delete mode 100644 app/views/admin/profiles/index.html.erb diff --git a/app/controllers/admin/accounts_controller.rb b/app/controllers/admin/accounts_controller.rb index a8810a6c6..bf3d9c719 100644 --- a/app/controllers/admin/accounts_controller.rb +++ b/app/controllers/admin/accounts_controller.rb @@ -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] diff --git a/app/controllers/admin/labels_controller.rb b/app/controllers/admin/labels_controller.rb index d64c2fce5..ab6111096 100644 --- a/app/controllers/admin/labels_controller.rb +++ b/app/controllers/admin/labels_controller.rb @@ -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 @@ -24,7 +24,7 @@ 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 @@ -32,7 +32,7 @@ def update 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 diff --git a/app/controllers/admin/profiles_controller.rb b/app/controllers/admin/profiles_controller.rb index 1760668c4..89e12108b 100644 --- a/app/controllers/admin/profiles_controller.rb +++ b/app/controllers/admin/profiles_controller.rb @@ -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 diff --git a/app/mailers/account_mailer.rb b/app/mailers/account_mailer.rb new file mode 100644 index 000000000..08a5fcbd6 --- /dev/null +++ b/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 diff --git a/app/mailers/application_mailer.rb b/app/mailers/application_mailer.rb index d84cb6e71..59f5d8432 100644 --- a/app/mailers/application_mailer.rb +++ b/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 diff --git a/app/mailers/concerns/skip_emails.rb b/app/mailers/concerns/skip_emails.rb new file mode 100644 index 000000000..11894df6c --- /dev/null +++ b/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 diff --git a/app/mailers/profile_review_mailer.rb b/app/mailers/profile_review_mailer.rb index 4c8c9e507..1d59bb74f 100644 --- a/app/mailers/profile_review_mailer.rb +++ b/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') diff --git a/app/views/admin/accounts/index.html.erb b/app/views/admin/accounts/index.html.erb index ea73036ef..dede1f014 100644 --- a/app/views/admin/accounts/index.html.erb +++ b/app/views/admin/accounts/index.html.erb @@ -20,7 +20,7 @@ <% @accounts.each do |account| %> > <%= account.id %> - <%= account.email %> + <%= link_to account.email, admin_account_path(account.id) %> <%= account.role %> <%= account.level %> <%= label_tags(account) %> diff --git a/app/views/admin/profiles/show.html.erb b/app/views/admin/accounts/show.html.erb similarity index 66% rename from app/views/admin/profiles/show.html.erb rename to app/views/admin/accounts/show.html.erb index 160b0936e..2b1363615 100644 --- a/app/views/admin/profiles/show.html.erb +++ b/app/views/admin/accounts/show.html.erb @@ -1,10 +1,17 @@
-

Profile

+

Profile


+

Phones

+ + + + + + + + + + + <% @phones.each do |phone| %> + + + + + + + <% end %> + +
IDCountryNumberVlidated at
<%= phone.id %><%= phone.country %><%= phone.number %><%= phone.validated_at %>
+

Documents

@@ -77,7 +109,7 @@

- <%= 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' %>

Labels

@@ -96,8 +128,8 @@ - - + + <% end %> diff --git a/app/views/admin/profiles/_form.html.erb b/app/views/admin/profiles/_form.html.erb new file mode 100644 index 000000000..e7bb9cbc7 --- /dev/null +++ b/app/views/admin/profiles/_form.html.erb @@ -0,0 +1,47 @@ +<%= form_with(model: [:admin, @profile], local: false) do |form| %> + <% if profile.errors.any? %> +
+

<%= pluralize(profile.errors.count, "error") %> prohibited this website from being saved:

+ +
    + <% profile.errors.full_messages.each do |message| %> +
  • <%= message %>
  • + <% end %> +
+
+ <% end %> + + <%= form.label :first_name, class: 'control-label'%> + <%= form.text_field :first_name, class: 'form-control col-sm-10'%> +
+ + <%= form.label :last_name, class: 'control-label'%> + <%= form.text_field :last_name, class: 'form-control col-sm-10'%> +
+ + <%= form.label :dob, class: 'control-label'%> + <%= form.text_field :dob, class: 'form-control col-sm-10'%> +
+ + <%= form.label :address, class: 'control-label'%> + <%= form.text_field :address, class: 'form-control col-sm-10'%> +
+ + <%= form.label :postcode, class: 'control-label'%> + <%= form.text_field :postcode, class: 'form-control col-sm-10'%> +
+ + <%= form.label :city, class: 'control-label'%> + <%= form.text_field :city, class: 'form-control col-sm-10'%> +
+ + <%= form.label :country, class: 'control-label'%> + <%= form.text_field :country, class: 'form-control col-sm-10'%> +
+ + <%= form.submit 'Submit', class: 'btn btn-primary' %> + +
+
+ +<% end %> diff --git a/app/views/admin/profiles/edit.html.erb b/app/views/admin/profiles/edit.html.erb new file mode 100644 index 000000000..12b30111c --- /dev/null +++ b/app/views/admin/profiles/edit.html.erb @@ -0,0 +1,5 @@ +
+

Editing profile

+ <%= render 'form', profile: @profile %> +
+
diff --git a/app/views/admin/profiles/index.html.erb b/app/views/admin/profiles/index.html.erb deleted file mode 100644 index c81de36da..000000000 --- a/app/views/admin/profiles/index.html.erb +++ /dev/null @@ -1,44 +0,0 @@ -
- -

Profiles

- -
  • - Filter by state: - -
  • - -
    <%= label.key %> <%= label.value %> <%= label.scope %><%= link_to 'Edit', edit_admin_account_label_path(id: label.id, account_id: @profile.account_id), class: 'btn btn-primary btn-sm' %><%= 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?'} %><%= link_to 'Edit', edit_admin_account_label_path(id: label.id, account_id: @account.id), class: 'btn btn-primary btn-sm' %><%= 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?'} %>
    - - - - - - - - - - - - <% @profiles.each do |profile| %> - - - - - - - - - <% end %> - -
    IDStateFirst nameLast nameCountry
    <%= profile.id %><%= profile.state %><%= profile.first_name %><%= profile.last_name %><%= profile.country %><%= link_to 'Show', admin_profile_path(profile), class: 'btn btn-info btn-sm' %>
    - <%= paginate @profiles %> -
    diff --git a/app/views/admin/shared/_navigation.html.erb b/app/views/admin/shared/_navigation.html.erb index a43db2e4f..0ccb2db8b 100644 --- a/app/views/admin/shared/_navigation.html.erb +++ b/app/views/admin/shared/_navigation.html.erb @@ -9,12 +9,6 @@ <% end %> - <% if can? :read, Profile %> - - <% end %> - <% if current_account.role.admin? %>