-
Notifications
You must be signed in to change notification settings - Fork 67
Added write_attribute method for attribute setter methods #120
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
*Allows similar use of 'write_attribute' in ActiveRecord for methods that override an attribute setter *Same as ActiveRecord, write_attribute does not save the model instance
|
Would it be beneficial if the write_attribute fired a notification when the change occurred? |
|
In the case of I noticed in the ...
def some_attribute=(value)
# Optionally do something to 'value'
write_attribute(:some_attribute, value)
end
...This all being said, if there was an |
|
Calling Consider: columns foo: :integer
...
my_model_obj.foo = '1'
my_model_obj.save
# Validations and casting take place, foo is an integer, 1
my_model_obj.foo.write_attr ['1', '2', '3']
my_model_obj.save
# What is the intended behavior here?The logic behind I guess I'm not sure why you want to modify a given attribute directly and that's what is causing me to ask these questions. |
|
It was really my intention to provide a cleaner abstraction for a custom setter, otherwise why even dispatch attribute assignment to a defined method here? I didn't see a mention in the documentation of how best to implement a custom setter method. How else would you set the attribute value aside from doing In some cases, defining a method setter can provide more detailed validations or data manipulation. Not the best example, but consider: class UserProfile
...
def username=(value)
name = value.empty? ? 'Guest' : value.downcase
write_attribute(:username, name)
end
...
endIn the case of the above, calling Defining a setter method also allows bypassing the type casting if a different behavior is needed, as is the case for |
|
Ok, got it. I guess the issue of custom setters has never arisen before. |
|
@sxross if this isn't going to be merged, is the method within the pull request the recommended way of modifying an attribute during bulk assignment prior to being saved? For example: @data[attr_name] = value
@dirty = trueI know this is dealing with the internals of |
Added write_attribute method for attribute setter methods
|
This merge ignores #121, but does enable the manual setting of a |
|
This does ignore #121, and one could in theory set a :date column via over-riding the setter: def created_at=(date_string)
....
ns_date = ... # Parse the string and return an NSDate object
write_attribute(:created_at, ns_date)
endThat being said, I created #121 because it seemed that they were two separate features. I'll continue the discussion within the #121 conversation. |
*Allows similar use of 'write_attribute' in ActiveRecord for methods that override an attribute setter
*Same as ActiveRecord, write_attribute does not save the model instance