Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion test/enumerations_test.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# frozen_string_literal: true

require_relative 'helpers/test_helper'
require 'enumerations/errors'

class EnumerationsTest < Minitest::Test
class EnumerationsTest < Minitest::Test # rubocop:disable Metrics/ClassLength
def test_reflect_on_all_enumerations
enumerations = Post.reflect_on_all_enumerations

Expand Down Expand Up @@ -157,4 +159,12 @@ def test_on_nil_value_assignment
user = User.new(role: nil)
assert_nil user.role
end

def test_type_cast_when_saving_value_to_database
client = ApiClient.new(api_client_permission: ApiClientPermission.people_first_name)
client.save

# overriding to_s doesn't have an effect on the value saved to the database
assert_equal 'people_first_name', client[:api_client_permission]
Comment thread
gogi1805 marked this conversation as resolved.
end
end
6 changes: 6 additions & 0 deletions test/helpers/database_helper.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'logger'

ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
Expand All @@ -21,4 +23,8 @@
create_table :overridable_models, force: true do |t|
t.integer :overridable_status_id
end

create_table :api_clients, force: true do |t|
t.string :api_client_permission
end
end
13 changes: 13 additions & 0 deletions test/helpers/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,19 @@ def my_custom_name
end
end

class ApiClientPermission < Enumerations::Base
value :people_first_name, id: 'people.first_name'
value :people_last_name, id: 'people.last_name'

def to_s
id
end
end

class ApiClient < ActiveRecord::Base
enumeration :api_client_permission
end

class Post < ActiveRecord::Base
enumeration :status
enumeration :different_status, foreign_key: :some_other_status, class_name: 'Status'
Expand Down