This project is archived and is in readonly mode.

#2936 ✓resolved
Jamie Hill

[PATCH] Allow classes using validation to specify how attributes should be read

Reported by Jamie Hill | July 22nd, 2009 @ 01:21 AM | in 3.0.2

This patch adds a hook method to validations allowing a class to define how it's attributes should be acquired. This is useful should the class not use the default of having a method for each attribute and instead store values in a hash e.g.

class MyClass
  include ActiveModel::Validations

  def initialize(data = {})
    @data = data
  end
  
  def []=(key, value)
    @data[key] = value
  end

  private
  
  def value_for(key)
    @data[key]
  end
end

By overriding the value_for method you have control over how attribute values are retrieved.

As Rails 3 is all about not having to hack the framework and instead provide hooks I believe this is a great addition an have use-cases right away.

Comments and changes to this ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Referenced by

Pages