Skip to content

Commit

Permalink
Store enum value as integer (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
akmalcm authored Nov 7, 2024
1 parent 67308c8 commit 25c25cb
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 1 deletion.
9 changes: 8 additions & 1 deletion lib/active_snapshot/models/snapshot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,15 @@ def metadata=(h)
end

def build_snapshot_item(instance, child_group_name: nil)
attributes = instance.attributes
attributes.each do |k, v|
if instance.class.defined_enums.key?(k)
attributes[k] = instance.class.defined_enums.fetch(k).fetch(v)
end
end

self.snapshot_items.new({
object: instance.attributes,
object: attributes,
item_id: instance.id,
item_type: instance.class.name,
child_group_name: child_group_name,
Expand Down
2 changes: 2 additions & 0 deletions test/dummy_app/app/models/post.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
class Post < ActiveRecord::Base
include ActiveSnapshot

enum status: [:draft, :published]

has_many :comments
has_many :notes

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ def change
create_table :posts do |t|
t.integer :a, :b

t.integer :status, default: 0

t.timestamps
end

Expand Down
10 changes: 10 additions & 0 deletions test/models/snapshot_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,16 @@ def test_build_snapshot_item
@snapshot.build_snapshot_item(Post.first, child_group_name: :foobar)
end

def test_build_snapshot_item_stores_enum_database_value
@snapshot = @snapshot_klass.first

snapshot_item = @snapshot.build_snapshot_item(Post.first)

assert snapshot_item.object['status'] == 0

assert_equal @snapshot.snapshot_items.first.object['status'], snapshot_item.object['status']
end

def test_restore
@snapshot = @snapshot_klass.first

Expand Down

0 comments on commit 25c25cb

Please sign in to comment.