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

Unexpected behaviour of @subtype.cast ?? #452

Open
chrhsmt opened this issue Jun 25, 2024 · 1 comment
Open

Unexpected behaviour of @subtype.cast ?? #452

chrhsmt opened this issue Jun 25, 2024 · 1 comment

Comments

@chrhsmt
Copy link

chrhsmt commented Jun 25, 2024

I have found some behavior changes as a result of updating from 2.7.0 to 2.8.0.
When assigning to a field using enum key, the value is no longer set, is this the expected behavior?
It just seemed like a bug to me....

2.7.0

class Sample < ApplicationRecord
  extend Enumerize
  enumerize :code, in: {
    initial: 1,
    start: 2,
    finish: 3,
  }
end

o = Sample.new
o["code"] = "start"
o
#<Sample:0x0000000129696db8> {
              :id => nil,
            :code => "start",
      :created_at => nil,
      :updated_at => nil
}

2.8.0

class Sample < ApplicationRecord
  extend Enumerize
  enumerize :code, in: {
    initial: 1,
    start: 2,
    finish: 3,
  }
end

o = Sample.new
o["code"] = "start"
o
#<Sample:0x000000012244f570> {
              :id => nil,
            :code => nil, # 👈
      :created_at => nil,
      :updated_at => nil
}

The following commit seems to have affected this.
018c5a4

@attr.find_value(@subtype.cast(value))

@chrhsmt
Copy link
Author

chrhsmt commented Jul 2, 2024

This is code to reproduce.

# frozen_string_literal: true

require "bundler/inline"

gemfile(true) do
  source "https://rubygems.org"

  gem "rails"
  # If you want to test against edge Rails replace the previous line with this:
  # gem "rails", github: "rails/rails", branch: "main"

  gem "sqlite3", "~> 1.4"
  gem "enumerize", "2.8.0"
end

require "active_record"
require "minitest/autorun"
require "logger"

# This connection will do for database-independent bug reports.
ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
ActiveRecord::Base.logger = Logger.new(STDOUT)

ActiveRecord::Schema.define do
  create_table :samples, force: true do |t|
    t.integer :code
  end
end

class Sample < ActiveRecord::Base
  extend Enumerize
  enumerize :code, in: {
    initial: 1,
    start: 2,
    finish: 3,
  }
end

class BugTest < Minitest::Test
  def test_enum_assignment
    sample = Sample.new
    sample["code"] = "start"
    assert_equal "start", sample.code
  end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant