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

Maps enums to database values before storing in object_changes #731

Merged
merged 1 commit into from
Mar 13, 2016

Conversation

cbarton
Copy link
Contributor

@cbarton cbarton commented Mar 11, 2016

Keep consistency between versions with regard to changes and
object_changes and how enum columns store their values.

Before, changes would map the changed attributes enum columns to the database
values (integer values). This allows reifying that version to maintain the
integrity of the enum. It did not do so for object_changes and thus, 0
for non-json columns, and the enum value for json columns would be stored instead.
For the non-json columns, it mapped any non-integer enum value to 0 because
during serialization that column is an integer. Now this is fixed,
so that object_changes stores the enum mapped value.

Here is an example:

class PostWithStatus < ActiveRecord::Base
  has_paper_trail
  enum status: { draft: 0, published: 1, archived: 2 }
end

post = PostWithStatus.new(status: :draft)
post.published!
version = post.versions.last

# Before
version.changeset #> { 'status' => ['draft', 'draft'] } (stored as [0, 0])

# After
version.changeset #> { 'status' => ['draft', 'published'] } (stored as [0, 1])

@cbarton cbarton force-pushed the object_changes_w_enum branch 4 times, most recently from 349eeab to 0926651 Compare March 12, 2016 05:38
Keep consistency between versions with regard to `changes` and
`object_changes` and how enum columns store their values.

Before, `changes` would map the changed attributes enum columns to the database
values (integer values). This allows reifying that version to maintain the
integrity of the enum. It did not do so for `object_changes` and thus, `0`
for non-json columns, and the enum value for json columns would be stored instead.
For the non-json columns, it mapped any non-integer enum value to `0` because
during serialization that column is an `integer`.  Now this is fixed,
so that `object_changes` stores the enum mapped value.

Here is an example:

```ruby
class PostWithStatus < ActiveRecord::Base
  has_paper_trail
  enum status: { draft: 0, published: 1, archived: 2 }
end

post = PostWithStatus.new(status: :draft)
post.published!
version = post.versions.last

 # Before
version.changeset #> { 'status' => ['draft', 'draft'] } (stored as [0, 0])

 # After
version.changeset #> { 'status' => ['draft', 'published'] } (stored as [0, 1])
```
@cbarton cbarton force-pushed the object_changes_w_enum branch from 0926651 to e1f94d4 Compare March 12, 2016 06:12
jaredbeck added a commit that referenced this pull request Mar 13, 2016
Maps enums to database values before storing in `object_changes`
@jaredbeck jaredbeck merged commit 48949c4 into paper-trail-gem:master Mar 13, 2016
@jaredbeck
Copy link
Member

Looks good, Chris, thanks!

@jaredbeck jaredbeck added this to the 5.0.0 milestone Mar 13, 2016
jaredbeck added a commit that referenced this pull request Mar 13, 2016
@cbarton cbarton deleted the object_changes_w_enum branch May 7, 2016 18:57
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

Successfully merging this pull request may close these issues.

2 participants