Skip to content
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

Add support for :default option #6

Closed
skydan opened this issue May 30, 2019 · 6 comments

Comments

@skydan
Copy link

commented May 30, 2019

Does it make sense to add support for :default option on top of set of typecasting options?

@palkan palkan added the enhancement label May 30, 2019

@palkan

This comment has been minimized.

Copy link
Owner

commented May 30, 2019

I think, it makes sense.

Though it's not clear how exactly we want it to work. For example:

class Product < ActiveRecord::Base
   store_attribute :settings, :color, :string, default: "red"
end

How this should work?

  • accessor returns the default value (seems reasonable)
Product.new.color #=> "red"
  • should store attribute contain the default?
Product.new.settings #=> ? should this be `{ "color" => "red"}` or `{}`?
  • should we persist the default value on save?
Product.new.save!
#=> INSERT INTO products (settings) values ('{"color":"red"}'); ???
@skydan

This comment has been minimized.

Copy link
Author

commented May 31, 2019

How this should work?

  • accessor returns the default value (seems reasonable)
Product.new.color #=> "red"
  • should store attribute contain the default?
Product.new.settings #=> ? should this be `{ "color" => "red"}` or `{}`?
  • should we persist the default value on save?
Product.new.save!
#=> INSERT INTO products (settings) values ('{"color":"red"}'); ???

I think that behavior should be the same as when we pass this attribute explicitly:

Product.new.settings #=>`{ "color" => "red"}`
Product.new(color: "red").settings #=>`{ "color" => "red"}`

Product.new.save!
#=> INSERT INTO products (settings) values ('{"color":"red"}'); 
Product.new(color: "red").save!
#=> INSERT INTO products (settings) values ('{"color":"red"}'); 
@palkan

This comment has been minimized.

Copy link
Owner

commented Jun 3, 2019

I think, we need a parity with Attributes API here:

# create_table :posts do |t|
#   t.string :data
# end
class Post < ActiveRecord::Base
  attribute :data, :datetime, default: -> { Time.current }
end

How does this behave? Not sure, need to check.

@theycallmeswift

This comment has been minimized.

Copy link

commented Jul 12, 2019

+1 that this would be valuable and should mirror the attributes API.

@SumLare

This comment has been minimized.

Copy link
Contributor

commented Jul 15, 2019

I'm gonna take a look at it this week if nobody minds.

dreikanter added a commit to dreikanter/store_attribute that referenced this issue Jul 18, 2019

dreikanter added a commit to dreikanter/store_attribute that referenced this issue Jul 20, 2019

dreikanter added a commit to dreikanter/store_attribute that referenced this issue Jul 23, 2019

palkan added a commit that referenced this issue Jul 24, 2019

palkan added a commit that referenced this issue Jul 24, 2019

@palkan

This comment has been minimized.

Copy link
Owner

commented Jul 24, 2019

Closed by #7 and released in 0.6.0

@palkan palkan closed this Jul 24, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants
You can’t perform that action at this time.