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

fix failing serialization with sequel & custom serializer #84

Merged
merged 1 commit into from
Feb 27, 2018

Conversation

azhi
Copy link
Contributor

@azhi azhi commented Feb 26, 2018

refactor checking whether object is a collection: move the check to
helper
use more strict version of a check: responding to each does not always
mean that object is a collection, but most of collections include
Enumerable
add explicit check for ActiveRecord::Relation - though it does not
include Enumerable in version 4.2, we still need to consider it a
collection
improve error message when collection does not fit these rules

Fixes #83

@@ -11,5 +11,9 @@ module Helper
def self.surrealist?(klass)
klass < Surrealist || klass < Surrealist::Serializer
end

def self.collection?(object)
object.kind_of?(Enumerable)

Choose a reason for hiding this comment

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

Prefer Object#is_a? over Object#kind_of?.

@azhi azhi force-pushed the fix-sequel-custom-serializer branch from e4754b4 to f6a7f78 Compare February 26, 2018 18:11
@azhi azhi changed the title fix failing serialization with sequel & custom serializer [WIP] fix failing serialization with sequel & custom serializer Feb 26, 2018
@azhi azhi force-pushed the fix-sequel-custom-serializer branch from f6a7f78 to 62e9e95 Compare February 26, 2018 22:06

private

def self.ar_relation?(object)

Choose a reason for hiding this comment

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

Inconsistent indentation detected.
private (on line 22) does not make singleton methods private. Use private_class_method or private inside a class << self block instead.

object.is_a?(Enumerable) || self.ar_relation?(object)
end

private

Choose a reason for hiding this comment

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

Useless private access modifier.

# 4.2 AR relation object did not include Enumerable (it defined
# all necessary method through ActiveRecord::Delegation module),
# so we need to explicitly check for this
object.is_a?(Enumerable) || self.ar_relation?(object)

Choose a reason for hiding this comment

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

Redundant self detected.

@azhi azhi force-pushed the fix-sequel-custom-serializer branch from 62e9e95 to 696427b Compare February 26, 2018 22:09
@azhi
Copy link
Contributor Author

azhi commented Feb 26, 2018

@nesaulov

We need to make sure this applies to all orms and edit the error message.

Tests on travis succeeded on latest Sequel, ROM, AR, as well as on ROM 3.x and AR 4.2. I didn't check on older sequel or ROM, so it can be broken there :).
Regarding error message: new version of this PR fixes it, let me know if it is good enough.

@azhi azhi changed the title [WIP] fix failing serialization with sequel & custom serializer fix failing serialization with sequel & custom serializer Feb 26, 2018
@nesaulov
Copy link
Owner

@azhi this looks great, only one thing I have to mention: I think we should add a spec for collection serialization through custom serializers (like the one you have added for instance)

refactor checking whether object is a collection: move the check to
helper
use more strict version of a check: responding to `each` does not always
mean that object is a collection, but most of collections include
Enumerable
add explicit check for ActiveRecord::Relation - though it does not
include Enumerable in version 4.2, we still need to consider it a
collection
improve error message when collection does not fit these rules
@azhi azhi force-pushed the fix-sequel-custom-serializer branch from 696427b to a0746a8 Compare February 27, 2018 12:43
@azhi
Copy link
Contributor Author

azhi commented Feb 27, 2018

@nesaulov updated.
Also changed phrasing in readme docs (regrading responding to :each), and my text editor trimmed all trailing whitespaces there, hope that's ok :).

Copy link
Owner

@nesaulov nesaulov left a comment

Choose a reason for hiding this comment

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

Looks perfect, thanks! 🎉

@nesaulov nesaulov merged commit 73584e7 into nesaulov:master Feb 27, 2018
@azhi azhi deleted the fix-sequel-custom-serializer branch February 27, 2018 21:15
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.

3 participants