DmitryTsepelev / store_model

Work with JSON-backed attributes as ActiveRecord-ish models
MIT License
1.04k stars 85 forks source link

Broken defaults in 2.0 #147

Closed colszowka closed 1 year ago

colszowka commented 1 year ago

Hi :wave:

It seems that the changes from #146 broke handling of defaults in version 2.0. I noticed this from our test suite breaking, but here's an isolated test case:

# Gemfile
source "https://rubygems.org"

gem "activerecord"
gem "sqlite3"
gem "store_model", "~> 2.0"
# example.rb
require "bundler"
Bundler.require :default

ActiveRecord::Base.establish_connection adapter: "sqlite3", database: ":memory:"

ActiveRecord::Migration.create_table :users do |t|
  t.string :email, null: false
  t.text :settings, null: false, default: "{}"
end

class User < ActiveRecord::Base
  class Settings
    include StoreModel::Model

    attribute :something, :boolean, default: false
  end

  attribute :settings, Settings.to_type
end

pp User.new.settings

So it looks to me like the adjustments to serialization in #146 made it so that the default defined on the attribute is getting ignored now. Maybe it was also due to some other change, but I only saw this PR being mentioned on the changelog and on the surface it does seem like something that might have caused this issue.

DmitryTsepelev commented 1 year ago

Should be fine now, making new release

colszowka commented 1 year ago

@DmitryTsepelev Thank you for the quick update, I can confirm it works again now :+1: