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

#446 - Added support for attributes to be arrays #448

Closed

Conversation

cfeckardt
Copy link

@cfeckardt cfeckardt commented Apr 30, 2018

This PR should solve #446

I have tried to keep conventions similar to what you have done in the past.

The code works by checking if the action is an update when reading the new attributes.

I have not yet tested this extensively in my own application so please test it first. The tests are passing with green (however there are race conditions for me on the test suite on master too)

@cfeckardt cfeckardt force-pushed the feature/array_support branch 2 times, most recently from 68a2332 to 2e6176c Compare May 2, 2018 13:52
@@ -40,6 +40,7 @@
t.column :created_at, :datetime
t.column :updated_at, :datetime
t.column :favourite_device, :string
t.column :favourite_colours, :json
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

json is only supported on postgres, tests are failing for mysql/sqlite because of this.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I ran bundle exec rspec. I see now that only runs tests on 1 db.

Will address this by only running the new tests if the db is postgres.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tbrisker The changes you requested have been implemented.

@cfeckardt cfeckardt force-pushed the feature/array_support branch from 2e6176c to c2376a2 Compare May 4, 2018 10:04
@cfeckardt cfeckardt force-pushed the feature/array_support branch from c2376a2 to a4e9c7c Compare May 4, 2018 10:37
@cfeckardt
Copy link
Author

@tbrisker changed as per your requests, with a green build

Copy link
Collaborator

@tbrisker tbrisker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A couple of minor comments inline. could you also update the Changelog file with this fix please?

attrs
end
end

# Returns a hash of the changed attributes with the old values
def old_attributes
(audited_changes || {}).inject({}.with_indifferent_access) do |attrs, (attr, values)|
attrs[attr] = Array(values).first

attrs[attr] = self.action == 'update' ? Array(values).first : values
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not crucial, but now we don't need to wrap the values as an array since when the action is an update the changes are saved as an array.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure

@@ -40,6 +40,10 @@
t.column :created_at, :datetime
t.column :updated_at, :datetime
t.column :favourite_device, :string

if ENV['DB'] == 'POSTGRES'
t.column :favourite_colours, :json
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a way to test this on other DBs as well? perhaps using serialization to save the array as a string column?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I considered this.

However, if the field is not :json, then active record does not automatically cast, and audited_changes would contains strings.

We'd get audited_changes: ["['blue', 'red']", "['blue']"]
instead of [[blue, red],[blue]]

Therefore, it's just another string, and already covered in other tests.

If you disagree I'd be happy to come up with something a bit more explicit

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tbrisker thoughts?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure, this is better in any case. I think we could use serialize somehow to test other dbs, but i won't block on that.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't looked into it too deeply, but at least in Rails 5 this can be tested using audit.revision. That will raise a SerializationTypeMismatch when trying to get the revision for a create audit that contains an array serialized using Rails' serialize (because it gets a string rather than an array).

@tbrisker
Copy link
Collaborator

ping @cfeckardt - care to fix the remaining comment and add a line to the changelog?

@cfeckardt
Copy link
Author

Sorry about the AWOL ill get this sorted

@davidpatters0n
Copy link

@cfeckardt - Would love to see this finalised. Any chance on getting this over the finishing line? c @tbrisker

@gingerlime
Copy link

sorry for the "me too", but I'm just trying audited out and bumped into this limitation. When I pull a revision out, it shows nil for jsonb fields that had an empty array in there. Looks like this has been open for a while. Is there anything I can help with? (I literally just started using audited a couple of hours ago, so not sure how much I can contribute, but happy to try)

alexanderweiss added a commit to sping/audited that referenced this pull request Dec 13, 2019
Notalifeform pushed a commit to sping/audited that referenced this pull request Mar 24, 2020
jranshuysen pushed a commit to sping/audited that referenced this pull request Jan 12, 2021
@yuki24 yuki24 mentioned this pull request Jun 10, 2021
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.

6 participants