Last active
December 20, 2015 07:29
-
-
Save chriscooning/6093869 to your computer and use it in GitHub Desktop.
check_in.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Factories::CheckIn | |
def initialize(options = {}) | |
@options = options || {} | |
end | |
def create(attributes) | |
build(nil, attributes).tap do |check_in| | |
validate_user_schedule_existance(check_in) | |
validate_check_in_date(check_in) if @options[:fastpass] | |
check_out_existed_check_ins(check_in) if @options[:fastpass] | |
check_in.save if check_in.errors.empty? | |
check_in | |
end | |
end | |
def update(id, attributes) | |
resource = id.present? ? CheckIn.find(id) : CheckIn.find_by_user_schedule_id(attributes["user_schedule_id"]) | |
build(resource, attributes).tap do |check_in| | |
validate_user_schedule_existance(check_in) | |
check_in.save if check_in.errors.empty? | |
check_in | |
end | |
end | |
def build(resource = nil, attributes = {}) | |
resource ||= CheckIn.new | |
attributes = attributes.with_indifferent_access | |
attrs = filter_attributes(attributes) | |
resource.assign_attributes(attrs) | |
ensure_in_out_date(resource) | |
resource.valid? | |
validate_dates(resource) | |
resource | |
end | |
private | |
def check_out_existed_check_ins(entity) | |
return unless entity.user_schedule | |
past_check_ins = entity.user_schedule.check_ins.where(status: '1') | |
past_check_ins.find_each { |check_in| check_in.update_attributes(status: "2") | |
end | |
def ensure_in_out_date(entity) | |
if entity.created_at_changed? | |
entity.check_out_time = entity.check_out_time.change( | |
year: entity.created_at.year, month: entity.created_at.month, day: entity.created_at.day | |
) | |
elsif entity.check_out_time_changed? | |
entity.created_at = entity.created_at.change( | |
year: entity.check_out_time.year, month: entity.check_out_time.month, day: entity.check_out_time.day | |
) | |
end | |
end | |
def validate_user_schedule_existance(entity) | |
user_schedule = UserSchedule.where(id: entity.user_schedule_id).first | |
if user_schedule | |
entity.user_id ||= user_schedule.user_id | |
else | |
entity.errors.add(:user_schedule_id, :not_exist) | |
end | |
end | |
def validate_check_in_date(entity) | |
if entity.user_schedule && day = entity.user_schedule.try(:department_block).try(:day) | |
entity.errors.add(:created_at, :not_assigned) if day.to_date != Time.zone.now.to_date | |
end | |
end | |
def validate_dates(entity) | |
if entity.check_out_time && entity.created_at_changed? && entity.created_at > entity.check_out_time | |
entity.errors.add(:created_at, :less_than_check_out) | |
end | |
end | |
def filter_attributes(attrs) | |
attrs.keep_if { |k,v| @options[:accessible_attributes].include?(k.to_s) } | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment