This project is archived and is in readonly mode.
[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
-
Jamie Hill July 22nd, 2009 @ 10:50 AM
- Assigned user set to Yehuda Katz (wycats)
-
Jamie Hill July 22nd, 2009 @ 12:33 PM
- Title changed from Allow classes using validation to specify how attributes should be read to [PATCH] Allow classes using validation to specify how attributes should be read
-
darkliquid July 27th, 2009 @ 12:04 PM
+1
This certainly makes reuse of the Validation system in isolation from the rest of the rails framework a little easier since you don't have the restriction of having to define a public reader for every attribute you want to validate.
-
Michael Koziarski August 3rd, 2009 @ 06:03 AM
- Assigned user changed from Yehuda Katz (wycats) to josh
- Milestone cleared.
Josh is working in this area now
-
josh August 3rd, 2009 @ 08:15 PM
- State changed from new to open
-
Jamie Hill August 5th, 2009 @ 05:15 PM
Thanks for getting back to me on this.
Agreed on the name, new patch attached.
-
Repository August 5th, 2009 @ 05:54 PM
- State changed from open to resolved
(from [cfd421daa2b04216e27d666361eb4053020e027d]) Allow validations to use values from custom readers [#2936 state:resolved]
Signed-off-by: Joshua Peek josh@joshpeek.com
http://github.com/rails/rails/commit/cfd421daa2b04216e27d666361eb40... -
Jeremy Kemper October 15th, 2010 @ 11:01 PM
- Milestone set to 3.0.2
- Importance changed from to
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>
People watching this ticket
Attachments
Tags
Referenced by
- 2936 [PATCH] Allow classes using validation to specify how attributes should be read (from [cfd421daa2b04216e27d666361eb4053020e027d]) Allow v...
- 2935 Allow classes using validation to specify how attributes should be read Duplicated of #2936 (which was already committed).
- 3058 [PATCH] Sexy Validations Following on from my previous patch https://rails.lightho...