Skip to content

Commit

Permalink
Fix Rails 7.2/8.0 enum deprecations
Browse files Browse the repository at this point in the history
> DEPRECATION WARNING: Defining enums with keyword arguments is deprecated and will be removed
in Rails 8.0. Positional arguments should be used instead:
>
> enum :status, {:queued=>0, :started=>1, :finished=>2}
  • Loading branch information
Earlopain committed Aug 13, 2024
1 parent 0a73f50 commit d12b7af
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions spec/acceptance/enum_traits_spec.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
describe "enum traits" do
def define_model_with_enum(class_name, field, values)
define_model(class_name, status: :integer) do
if ActiveRecord::VERSION::STRING >= "7.2"
enum field, values
else
enum field => values
end
end
end

context "when automatically_define_enum_traits is true" do
it "builds traits automatically for model enum field" do
define_model("Task", status: :integer) do
enum status: {queued: 0, started: 1, finished: 2}
end
define_model_with_enum("Task", :status, {queued: 0, started: 1, finished: 2})

FactoryBot.define do
factory :task
Expand All @@ -19,9 +27,7 @@
end

it "prefers user defined traits over automatically built traits" do
define_model("Task", status: :integer) do
enum status: {queued: 0, started: 1, finished: 2}
end
define_model_with_enum("Task", :status, {queued: 0, started: 1, finished: 2})

FactoryBot.define do
factory :task do
Expand Down Expand Up @@ -118,9 +124,7 @@ def each(&block)
context "when automatically_define_enum_traits is false" do
it "raises an error for undefined traits" do
with_temporary_assignment(FactoryBot, :automatically_define_enum_traits, false) do
define_model("Task", status: :integer) do
enum status: {queued: 0, started: 1, finished: 2}
end
define_model_with_enum("Task", :status, {queued: 0, started: 1, finished: 2})

FactoryBot.define do
factory :task
Expand All @@ -138,9 +142,7 @@ def each(&block)

it "builds traits for each enumerated value when traits_for_enum are specified" do
with_temporary_assignment(FactoryBot, :automatically_define_enum_traits, false) do
define_model("Task", status: :integer) do
enum status: {queued: 0, started: 1, finished: 2}
end
define_model_with_enum("Task", :status, {queued: 0, started: 1, finished: 2})

FactoryBot.define do
factory :task do
Expand Down

0 comments on commit d12b7af

Please sign in to comment.