Skip to content

Commit

Permalink
improving support assignment model validations
Browse files Browse the repository at this point in the history
  • Loading branch information
yasonk committed May 10, 2015
1 parent 2ee9721 commit 1c0f230
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 deletions.
5 changes: 4 additions & 1 deletion app/models/support_assignment.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
class SupportAssignment < ActiveRecord::Base
belongs_to :user
validates :user_id, uniqueness: {:scope => :date}
validates :user_id, uniqueness: {:scope => :date, message: "already to duty on this date"}
validate do | assignment |
assignment.errors.add(:user_id, ' is unavailable on the specified date') unless assignment.user.available?(assignment.date)
end
end
8 changes: 5 additions & 3 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ def future_assignment
support_assignments.select{ |assignment| assignment.date.future? }.first
end

def self.available_users date
User.where(unavailable_date: nil) | User.where.not(unavailable_date: date)
end

private

def reconcile_schedule_conflicts date
Expand All @@ -35,7 +39,5 @@ def replace_assignment assignment
future_assignment.destroy
end

def available_users date
User.where(unavailable_date: nil) | User.where.not(unavailable_date: date)
end

end
2 changes: 1 addition & 1 deletion app/views/support_assignments/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
</div>
<div class="field">
<%= f.label :user_id %><br>
<%= f.collection_select(:user_id, Schedule.new.available_users(@support_assignment.date), :id, :name) %>
<%= f.collection_select(:user_id, User.available_users(@support_assignment.date), :id, :name) %>
</div>
<div class="actions">
<%= f.submit %>
Expand Down

0 comments on commit 1c0f230

Please sign in to comment.