sxross / MotionModel

Simple Model and Validation Mixins for RubyMotion
MIT License
192 stars 67 forks source link

Defaults over-ride a supplied value -- intended? #124

Closed cognitiveflux closed 9 years ago

cognitiveflux commented 9 years ago

I'm not sure if this is the intended behavior, but if a default value is provided to a model and that column has an attribute assigned using the create method, the supplied value is not applied; instead, the default value is applied.

I was thinking that the default value is applied if and only if there is no value for that column on creation, not instead of any values upon creation.

I was going to trying using a Proc, but looking at the call that applies the value here, it looks like the value is not passed to the Proc either.

For example:

class Task
  include MotionModel::Model
  include MotionModel::ArrayModelAdapter

  columns :enabled => {:type => :boolean, :default => false}
end

Task.create({"enabled" => true})
Task.first.enabled
# => false

Perhaps this method should be like the following?

def initialize_data_columns(column, value) #nodoc
  self.attributes = value ? {column => value} : evaluate_default_value(column, value)
end

Or since I'm not sure how that would impact the Proc default value, changing this line to:

{column => value || default}

Easy to create a pull request, just not sure what the intended behavior is when this was originally designed.

sxross commented 9 years ago

It's supposed to work as you say: The default or Proc are only applied if no value is present. Would you be able to create a branch with a breaking spec so I can clone it and take a look? Or would you rather just fix it. The behavior is (was) just as Rails. Defaults only override missing values.

cognitiveflux commented 9 years ago

Unless you need it done right away, I'll create the spec and provide a fix.

sxross commented 9 years ago

That would be perfect. Thanks!

cognitiveflux commented 9 years ago

Just submitted #125, seems to solve it.

sxross commented 9 years ago

125 seems to fix this.

cognitiveflux commented 9 years ago

how often do you typically publish new gems?

sxross commented 9 years ago

I’m overdue.

--  Steve Ross

On November 6, 2014 at 9:01:27 AM, cognitiveflux (notifications@github.com) wrote:

how often do you typically publish new gems?

— Reply to this email directly or view it on GitHub.