From c4be93831c675c71ed93a9f769f6275196a88fac Mon Sep 17 00:00:00 2001 From: Alex Kotov Date: Thu, 31 Jan 2019 05:33:14 +0500 Subject: [PATCH] Some refactoring --- app/models/person.rb | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/app/models/person.rb b/app/models/person.rb index f2fc3e0..b53e6ef 100644 --- a/app/models/person.rb +++ b/app/models/person.rb @@ -33,25 +33,14 @@ class Person < ApplicationRecord private def possible_member - return if member_since.nil? - - return errors.add :member_since if supporter_since.nil? - return errors.add :member_since if member_since < supporter_since - - nil + errors.add :member_since unless member_since_not_before_supporter_since? end def possible_excluded - return if excluded_since.nil? + return if excluded_since_not_before_member_since? || + excluded_since_not_before_supporter_since? - return errors.add :excluded_since if supporter_since.nil? && - member_since.nil? - return errors.add :excluded_since if supporter_since && - excluded_since < supporter_since - return errors.add :excluded_since if member_since && - excluded_since < member_since - - nil + errors.add :excluded_since end def supporter_since_not_in_future @@ -66,6 +55,18 @@ private errors.add :excluded_since unless excluded_since_not_in_future? end + def member_since_not_before_supporter_since? + member_since.nil? || supporter_since && member_since >= supporter_since + end + + def excluded_since_not_before_supporter_since? + excluded_since.nil? || supporter_since && excluded_since >= supporter_since + end + + def excluded_since_not_before_member_since? + excluded_since.nil? || member_since && excluded_since >= member_since + end + def supporter_since_not_in_future? supporter_since.nil? || supporter_since <= Time.zone.today end