Skip to content

Commit

Permalink
Merge pull request #7 from betacraft/address_rails_8_enum_syntax_depr…
Browse files Browse the repository at this point in the history
…ecation

Address rails 8 enum syntax deprecation
  • Loading branch information
harunkumars authored Aug 21, 2024
2 parents 5ef7213 + 26c8e45 commit fb7d848
Show file tree
Hide file tree
Showing 7 changed files with 206 additions and 21 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ jobs:
strategy:
matrix:
include:
- ruby: '3.3'
rails: '7.2'
- ruby: '3.2'
rails: '7.1'
- ruby: '3.2'
Expand Down
2 changes: 1 addition & 1 deletion .ruby-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.1.1
3.3.4
72 changes: 72 additions & 0 deletions gemfiles/activerecord_7.1.gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
PATH
remote: ..
specs:
rich_enums (0.1.5)
activerecord (>= 6.1, < 8.0)

GEM
remote: https://rubygems.org/
specs:
activemodel (7.1.3.2)
activesupport (= 7.1.3.2)
activerecord (7.1.3.2)
activemodel (= 7.1.3.2)
activesupport (= 7.1.3.2)
timeout (>= 0.4.0)
activesupport (7.1.3.2)
base64
bigdecimal
concurrent-ruby (~> 1.0, >= 1.0.2)
connection_pool (>= 2.2.5)
drb
i18n (>= 1.6, < 2)
minitest (>= 5.1)
mutex_m
tzinfo (~> 2.0)
base64 (0.2.0)
bigdecimal (3.1.7)
concurrent-ruby (1.2.3)
connection_pool (2.4.1)
diff-lcs (1.5.1)
drb (2.2.1)
i18n (1.14.4)
concurrent-ruby (~> 1.0)
minitest (5.22.3)
mutex_m (0.2.0)
rake (12.3.3)
rspec (3.13.0)
rspec-core (~> 3.13.0)
rspec-expectations (~> 3.13.0)
rspec-mocks (~> 3.13.0)
rspec-core (3.13.0)
rspec-support (~> 3.13.0)
rspec-expectations (3.13.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.13.0)
rspec-mocks (3.13.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.13.0)
rspec-support (3.13.1)
sqlite3 (1.7.3-arm64-darwin)
sqlite3 (1.7.3-x86_64-darwin)
temping (4.1.1)
activerecord (>= 6.0, < 7.2)
activesupport (>= 6.0, < 7.2)
timeout (0.4.1)
tzinfo (2.0.6)
concurrent-ruby (~> 1.0)

PLATFORMS
arm64-darwin-23
x86_64-darwin-21

DEPENDENCIES
activerecord (~> 7.1)
rake (~> 12.0)
rich_enums!
rspec (~> 3.0)
sqlite3 (~> 1.4)
temping (~> 4.1)

BUNDLED WITH
2.5.16
8 changes: 8 additions & 0 deletions gemfiles/activerecord_7.2.gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
source "https://rubygems.org"

gem "activerecord", "~> 7.2"
gem "rake", "~> 12.0"
gem "rspec", "~> 3.0"

# Specify your gem's dependencies in rich_enums.gemspec
gemspec path: "../"
74 changes: 74 additions & 0 deletions gemfiles/activerecord_7.2.gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
PATH
remote: ..
specs:
rich_enums (0.1.5)
activerecord (>= 6.1, < 8.0)

GEM
remote: https://rubygems.org/
specs:
activemodel (7.2.0)
activesupport (= 7.2.0)
activerecord (7.2.0)
activemodel (= 7.2.0)
activesupport (= 7.2.0)
timeout (>= 0.4.0)
activesupport (7.2.0)
base64
bigdecimal
concurrent-ruby (~> 1.0, >= 1.3.1)
connection_pool (>= 2.2.5)
drb
i18n (>= 1.6, < 2)
logger (>= 1.4.2)
minitest (>= 5.1)
securerandom (>= 0.3)
tzinfo (~> 2.0, >= 2.0.5)
base64 (0.2.0)
bigdecimal (3.1.8)
concurrent-ruby (1.3.4)
connection_pool (2.4.1)
diff-lcs (1.5.1)
drb (2.2.1)
i18n (1.14.5)
concurrent-ruby (~> 1.0)
logger (1.6.0)
minitest (5.25.1)
rake (12.3.3)
rspec (3.13.0)
rspec-core (~> 3.13.0)
rspec-expectations (~> 3.13.0)
rspec-mocks (~> 3.13.0)
rspec-core (3.13.0)
rspec-support (~> 3.13.0)
rspec-expectations (3.13.2)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.13.0)
rspec-mocks (3.13.1)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.13.0)
rspec-support (3.13.1)
securerandom (0.3.1)
sqlite3 (1.7.3-arm64-darwin)
sqlite3 (1.7.3-x86_64-darwin)
temping (4.2.0)
activerecord (>= 6.0, < 7.3)
activesupport (>= 6.0, < 7.3)
timeout (0.4.1)
tzinfo (2.0.6)
concurrent-ruby (~> 1.0)

PLATFORMS
arm64-darwin-23
x86_64-darwin-21

DEPENDENCIES
activerecord (~> 7.2)
rake (~> 12.0)
rich_enums!
rspec (~> 3.0)
sqlite3 (~> 1.4)
temping (~> 4.1)

BUNDLED WITH
2.5.16
6 changes: 5 additions & 1 deletion lib/rich_enums.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,11 @@ def rich_enum(column_symbol_value_string_options)
end

# 1. Define the Enum
enum "#{column}": split_hash[:for_enum], **options
if Gem::Version.new(ActiveRecord::VERSION::STRING) >= Gem::Version.new('7.2')
enum column, split_hash[:for_enum], **options
else
enum "#{column}": split_hash[:for_enum], **options
end

# 2. Define our custom class methods
# - the data to be returned by our custom methods is available as a class instance variable
Expand Down
63 changes: 44 additions & 19 deletions spec/rich_enums_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,29 +44,54 @@
end
end

it "invokes the enum method with the correct arguments" do
allow(course_class).to receive(:enum).and_call_original
if Gem::Version.new(ActiveRecord::VERSION::STRING) >= Gem::Version.new('7.2')
it "invokes the enum method with the correct arguments" do
allow(course_class).to receive(:enum).and_call_original

course_class.rich_enum status: { active: 0, inactive: 1 }, alt: :name
# it passes only the necessary arguments to the enum method, stripping out the alt: option
expect(course_class).to have_received(:enum).with(status: { active: 0, inactive: 1 })
end
course_class.rich_enum status: { active: 0, inactive: 1 }, alt: :name
expect(course_class).to have_received(:enum).with(:status, { active: 0, inactive: 1 })
expect(course_class.defined_enums).to include("status")
end

it "invokes the enum method with the correct arguments" do
allow(course_class).to receive(:enum).and_call_original
it "invokes the enum method with the correct arguments" do
allow(course_class).to receive(:enum).and_call_original

course_class.rich_enum status: { active: [0, 'LIVE'], inactive: [1, 'NOT_LIVE'] }, alt: :name
# it passes only the necessary arguments to the enum method,
# stripping out the alternate names LIVE and NOT_LIVE and the alt: option
expect(course_class).to have_received(:enum).with(status: { active: 0, inactive: 1 })
end
course_class.rich_enum status: { active: [0, 'LIVE'], inactive: [1, 'NOT_LIVE'] }, alt: :name
expect(course_class).to have_received(:enum).with(:status, { active: 0, inactive: 1 })
expect(course_class.defined_enums).to include("status")
end

it "invokes the enum method with the correct arguments" do
allow(course_class).to receive(:enum).and_call_original
course_class.rich_enum status: { active: [0, 'LIVE'], inactive: [1, 'NOT_LIVE'] }, prefix: true, alt: 'state'

expect(course_class).to have_received(:enum).with(:status, { active: 0, inactive: 1 }, prefix: true)
expect(course_class.defined_enums).to include("status")
end
else
it "invokes the enum method with the correct arguments" do
allow(course_class).to receive(:enum).and_call_original

it "invokes the enum method with the correct arguments" do
allow(course_class).to receive(:enum).and_call_original
course_class.rich_enum status: { active: [0, 'LIVE'], inactive: [1, 'NOT_LIVE'] }, _prefix: true, alt: 'state'
# it passes only the necessary arguments to the enum method,
# stripping out the alternate names LIVE and NOT_LIVE and the alt: option
expect(course_class).to have_received(:enum).with(status: { active: 0, inactive: 1 }, _prefix: true)
course_class.rich_enum status: { active: 0, inactive: 1 }, alt: :name
expect(course_class).to have_received(:enum).with(status: { active: 0, inactive: 1 })
expect(course_class.defined_enums).to include("status")
end

it "invokes the enum method with the correct arguments" do
allow(course_class).to receive(:enum).and_call_original

course_class.rich_enum status: { active: [0, 'LIVE'], inactive: [1, 'NOT_LIVE'] }, alt: :name
expect(course_class).to have_received(:enum).with(status: { active: 0, inactive: 1 })
expect(course_class.defined_enums).to include("status")
end

it "invokes the enum method with the correct arguments" do
allow(course_class).to receive(:enum).and_call_original
course_class.rich_enum status: { active: [0, 'LIVE'], inactive: [1, 'NOT_LIVE'] }, _prefix: true, alt: 'state'

expect(course_class).to have_received(:enum).with(status: { active: 0, inactive: 1 }, _prefix: true)
expect(course_class.defined_enums).to include("status")
end
end
end

Expand Down

0 comments on commit fb7d848

Please sign in to comment.