Created
November 25, 2012 02:22
-
-
Save bcardarella/4142149 to your computer and use it in GitHub Desktop.
Proper password confirmation
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
| en: | |
| errors: | |
| messages: | |
| confirmation: "doesn't match %{attribute}" |
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
| class User < ActiveRecord::Base | |
| class ConfirmationValidator < ActiveModel::EachValidator # :nodoc: | |
| def initialize(options) | |
| options[:attributes] = options[:attributes].map { |attribute| "#{attribute}_confirmation" } | |
| super | |
| end | |
| def validate_each(record, attribute, value) | |
| attribute_to_confirm = attribute.to_s.sub('_confirmation', '') | |
| confirmed = record.send(attribute_to_confirm) | |
| if value != confirmed | |
| human_attribute_name = record.class.human_attribute_name(attribute_to_confirm) | |
| record.errors.add(attribute, :confirmation, options.merge(:attribute => human_attribute_name)) | |
| end | |
| end | |
| def client_side_hash(model, attribute, force = nil) | |
| attribute_to_confirm = attribute.to_s.split(/_confirmation/)[0] | |
| human_attribute_name = model.class.human_attribute_name(attribute_to_confirm) | |
| build_client_side_hash(model, attribute, self.options.dup.merge(:attribute => human_attribute_name)) | |
| end | |
| def setup(klass) | |
| klass.send(:attr_accessor, *attributes.map do |attribute| | |
| attribute unless klass.method_defined?(attribute) | |
| end.compact) | |
| end | |
| end | |
| validates :password, :confirmation => true | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Did you find a solution for it??