Last active
August 29, 2015 14:04
-
-
Save ozgun/7e75fb47b98beb0da2dd to your computer and use it in GitHub Desktop.
Validator: Bitiş tarihinin başlangıç tarihinden sonra olup olmadığını doğrular
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
# Bitiş tarihinin başlangıç tarihinden sonra olup olmadığını kontrol eden validator. | |
# | |
# Başlangıç tarihinin attribute'u "start_date_field" parametresi ile gönderilir. | |
# | |
# == Example: | |
# | |
# class Subscription | |
# validates :end_date, end_date: { start_date_field: :start_date } | |
# end | |
# | |
# Hata mesajı için ilgili yaml dosyasına aşağıdaki satırlar eklenir: | |
# | |
# tr: | |
# activerecord: | |
# errors: | |
# models: | |
# subscription: | |
# attributes: | |
# end_date: | |
# cant_be_earlier_than_start_date: başlangıç tarihine eşit veya önce olamaz | |
# | |
# Kaynaklar: | |
# | |
# * http://viget.com/extend/seven-useful-activemodel-validators | |
# * http://guides.rubyonrails.org/active_record_validations.html#custom-validators | |
# | |
class EndDateValidator < ActiveModel::EachValidator | |
def validate_each(record, attribute, value) | |
# Başlangıç ve bitiş tarihleri set edilmişse bitiş tarihini kontrol et. | |
# Aksi takdirde bitiş tarihi kontrol edilmeyecek. | |
if ((start_date = record.send(options[:start_date_field])) && value) | |
unless valid_end_date?(start_date, value) | |
default_message = record.errors.generate_message(attribute, :cant_be_earlier_than_start_date) | |
record.errors[attribute] << (options[:message] || default_message) | |
end | |
end | |
end | |
private | |
def valid_end_date?(start_date, end_date) | |
end_date > start_date | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment