Created
October 18, 2018 18:38
-
-
Save pesta/306757c425abb77aed390f660437330e to your computer and use it in GitHub Desktop.
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 ImmutableValidator < ActiveModel::EachValidator | |
def validate_each(record, attribute, _value) | |
return unless record.persisted? | |
association_key = if record.attribute_names.include?(attribute.to_s) | |
attribute | |
else | |
record.reflections[attribute].foreign_key | |
end | |
record.errors[attribute] << 'can\'t be changed' if record.send("#{association_key}_changed?") | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment