Skip to content

Instantly share code, notes, and snippets.

@rummelonp
Created October 2, 2013 10:15
Show Gist options
  • Save rummelonp/6791611 to your computer and use it in GitHub Desktop.
Save rummelonp/6791611 to your computer and use it in GitHub Desktop.
特定の条件の時に変更不可能なフィールドのためのバリデーション
# -*- 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