Skip to content

Commit

Permalink
minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
katafira committed Sep 1, 2016
1 parent 3f23a3e commit cc3b530
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
20 changes: 12 additions & 8 deletions app/controllers/admin_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
8 changes: 8 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit cc3b530

Please sign in to comment.