diff --git a/app/controllers/admin_controller.rb b/app/controllers/admin_controller.rb index c47d705..70cffa4 100644 --- a/app/controllers/admin_controller.rb +++ b/app/controllers/admin_controller.rb @@ -9,14 +9,10 @@ def dashboard @users = User.all @offsprings = Offspring.all @rooms = Room.all - end - - def offsprings - @offsprings = Offspring.all - end - - def rooms - @rooms = Room.all + @users_count = User.where(admin: false).count + @offspring_count = Offspring.all.count + @zero_offspring_count = User.count - User.joins(:offsprings).uniq.count + @users_with_at_least_2_offspring = user_2_offspr_count end private @@ -26,4 +22,12 @@ def admin_user? redirect_to home_path end end + + def user_2_offspr_count + count = 0 + User.all.each do |u| + count += 1 if u.more_than_2_offspring? + end + count + end end diff --git a/app/models/user.rb b/app/models/user.rb index d474e29..edf4d13 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -17,4 +17,12 @@ class User < ActiveRecord::Base validates :phone, format: { with: VALID_TELEPHONE_REGEX} # The offspring need to have a parent, they will be destroyed if the parent is has_many :offsprings, dependent: :destroy + + def no_offspring? + offsprings.empty? || offsprings.count.zero? + end + + def more_than_2_offspring? + !offsprings.empty? && offsprings.count >= 2 + end end