Created
October 2, 2013 10:15
-
-
Save rummelonp/6791611 to your computer and use it in GitHub Desktop.
特定の条件の時に変更不可能なフィールドのためのバリデーション
This file contains hidden or 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
# -*- coding: utf-8 -*- | |
class FreezeValidator < ActiveModel::EachValidator | |
def initialize(options) | |
options[:unless] = Array.wrap(options[:unless]) | |
options[:unless] << :new_record? | |
super | |
end | |
def validate_each(record, attribute, value) | |
if record.send("#{attribute}_changed?") | |
record.errors.add(attribute, options[:message] || 'は変更出来ません。') | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment