Sha256: 1767818641870c1cbd8deded9f058cfa55881e54f1f71d73035b6d056479772b

Contents?: true

Size: 1.19 KB

Versions: 28

Compression:

Stored size: 1.19 KB

Contents

class User < ActiveRecord::Base
  def with_confirm(*fields)
    return if fields.empty?
    fields.map(&:to_sym).each {|field| instance_variable_set "@with_confirm_#{field}", true }
    class_eval do
      attr_accessor *fields.map {|field| "#{field}_confirmation".to_sym }
      attr_reader *fields.map {|field| field.to_sym }.reject {|field| field != :password}
    end

    fields.each do |field|
      confirm = "confirm_#{field}".to_sym
      define_singleton_method confirm do
        if instance_variable_get "@with_#{confirm}"
          case field
          when :password
            return if @password.nil?
            unless authenticate @password_confirmation
              errors.add :password_confirmation, "Passwords don't match"
            end
          else
            actual = self.send field
            return if actual.nil?
            confirmation = instance_variable_get "@#{field}_confirmation"
            unless actual == confirmation
              errors.add "#{field}_confirmation", "#{field.to_s.pluralize.capitalize} don't match"
            end
          end
        end
      end

      class_eval do
        private confirm
        validate confirm
      end
    end
    self
  end
end

Version data entries

28 entries across 28 versions & 1 rubygems

Version Path
eucalypt-0.8.0 lib/eucalypt/security/confirm.rb
eucalypt-0.7.2 lib/eucalypt/security/confirm.rb
eucalypt-0.7.1 lib/eucalypt/security/confirm.rb
eucalypt-0.7.0 lib/eucalypt/security/confirm.rb
eucalypt-0.6.2 lib/eucalypt/security/confirm.rb
eucalypt-0.6.1 lib/eucalypt/security/confirm.rb
eucalypt-0.6.0 lib/eucalypt/security/confirm.rb
eucalypt-0.5.4 lib/eucalypt/security/confirm.rb
eucalypt-0.5.3 lib/eucalypt/security/confirm.rb
eucalypt-0.5.2 lib/eucalypt/security/confirm.rb
eucalypt-0.5.1 lib/eucalypt/security/confirm.rb
eucalypt-0.5.0 lib/eucalypt/security/confirm.rb
eucalypt-0.4.2 lib/eucalypt/security/confirm.rb
eucalypt-0.4.1 lib/eucalypt/security/confirm.rb
eucalypt-0.4.0 lib/eucalypt/security/confirm.rb
eucalypt-0.3.5 lib/eucalypt/security/confirm.rb
eucalypt-0.3.4 lib/eucalypt/security/confirm.rb
eucalypt-0.3.3 lib/eucalypt/security/confirm.rb
eucalypt-0.3.2 lib/eucalypt/security/confirm.rb
eucalypt-0.3.1 lib/eucalypt/security/confirm.rb