From 853bcf1ee9ae0bb3e1e541f30e3dffde21185f25 Mon Sep 17 00:00:00 2001 From: msdundar Date: Wed, 1 Aug 2018 03:11:01 +0300 Subject: [PATCH] Move common behavior to concern --- app/controllers/account/addresses_controller.rb | 8 ++++---- app/controllers/account/identities_controller.rb | 8 ++++---- app/controllers/concerns/last_update_from_mernis.rb | 10 ++++++++++ 3 files changed, 18 insertions(+), 8 deletions(-) create mode 100644 app/controllers/concerns/last_update_from_mernis.rb diff --git a/app/controllers/account/addresses_controller.rb b/app/controllers/account/addresses_controller.rb index 86dbaada5..c09bd4411 100644 --- a/app/controllers/account/addresses_controller.rb +++ b/app/controllers/account/addresses_controller.rb @@ -2,6 +2,8 @@ module Account class AddressesController < ApplicationController + include LastUpdateFromMernis + before_action :set_address, only: %i[edit update destroy] before_action :check_formality, only: %i[edit update destroy] before_action :set_elapsed_time, only: %i[save_from_mernis] @@ -46,10 +48,8 @@ def check_formality def set_elapsed_time formal_address = current_user.addresses.formal - return unless formal_address.present? - - elapsed_time = (Time.zone.now - formal_address.first.updated_at) / 1.day - redirect_with('wait') if elapsed_time.present? && elapsed_time < 7 + return if formal_address.blank? + elapsed_time(formal_address) end def redirect_with(message) diff --git a/app/controllers/account/identities_controller.rb b/app/controllers/account/identities_controller.rb index a56a86552..8c5f65e4e 100644 --- a/app/controllers/account/identities_controller.rb +++ b/app/controllers/account/identities_controller.rb @@ -2,6 +2,8 @@ module Account class IdentitiesController < ApplicationController + include LastUpdateFromMernis + before_action :set_identity, only: %i[edit update destroy] before_action :check_formality, only: %i[edit update destroy] before_action :set_elapsed_time, only: %i[save_from_mernis] @@ -46,10 +48,8 @@ def check_formality def set_elapsed_time formal_identity = current_user.identities.formal - return unless formal_identity.present? - - elapsed_time = (Time.zone.now - formal_identity.first.updated_at) / 1.day - redirect_with('wait') if elapsed_time.present? && elapsed_time < 7 + return if formal_identity.blank? + elapsed_time(formal_identity) end def redirect_with(message) diff --git a/app/controllers/concerns/last_update_from_mernis.rb b/app/controllers/concerns/last_update_from_mernis.rb new file mode 100644 index 000000000..0a00b2944 --- /dev/null +++ b/app/controllers/concerns/last_update_from_mernis.rb @@ -0,0 +1,10 @@ +# frozen_string_literal: true + +module LastUpdateFromMernis + extend ActiveSupport::Concern + + def elapsed_time(resource) + elapsed_time = (Time.zone.now - resource.first.updated_at) / 1.day + redirect_with('wait') if elapsed_time.present? && elapsed_time < 7 + end +end