Skip to content

Fix a couple of bugs in klass_for_keypath#56

Open
barendt wants to merge 1 commit intofractaledmind:masterfrom
barendt:feature/fix-filter-type
Open

Fix a couple of bugs in klass_for_keypath#56
barendt wants to merge 1 commit intofractaledmind:masterfrom
barendt:feature/fix-filter-type

Conversation

@barendt
Copy link
Contributor

@barendt barendt commented Aug 8, 2019

This fixes a couple of bugs I hit in the new type-hinting code:

  1. If filter_set_types is defined by the controller, but the hash it returns doesn't include the keypath, make sure the hash lookup doesn't throw an exception.

  2. When inspecting the ActiveRecord columns_hash, make sure an array keypath is converted to a string.

1. If `filter_set_types` is defined by the controller, but the hash
it returns doesn't include the keypath, make sure the hash lookup doesn't
throw an exception.

2. When inspecting the ActiveRecord columns_hash, make sure an array
keypath is converted to a string.

if set.is_a?(ActiveRecord::Relation) || set.view.is_a?(ActiveRecord::Relation)
klass_type = set.model.columns_hash.fetch(keypath, nil)&.type
klass_type = set.model.columns_hash.fetch(keypath.join('.'), nil)&.type
Copy link

Choose a reason for hiding this comment

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

NilCheck: ActionSet::FilterInstructions#klass_for_keypath performs a nil-check. More info.

type_declarations = @controller.public_send(:filter_set_types)
types = type_declarations['types'] || type_declarations[:types]
klass = types[keypath.join('.')]
klass = types.fetch(keypath.join('.'), nil)
Copy link

Choose a reason for hiding this comment

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

DuplicateMethodCall: ActionSet::FilterInstructions#klass_for_keypath calls 'keypath.join('.')' 2 times. More info.

@codecov
Copy link

codecov bot commented Aug 8, 2019

Codecov Report

Merging #56 into master will not change coverage.
The diff coverage is 100%.

Impacted file tree graph

@@          Coverage Diff          @@
##           master    #56   +/-   ##
=====================================
  Coverage     100%   100%           
=====================================
  Files          54     54           
  Lines        1109   1109           
=====================================
  Hits         1109   1109
Impacted Files Coverage Δ
lib/action_set/filter_instructions.rb 100% <100%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update f8f6503...91cbd8b. Read the comment docs.

@fractaledmind
Copy link
Owner

@barendt: This fix actually helps to reveal a bug in that method. This bit of code currently doesn't work:

if set.is_a?(ActiveRecord::Relation) || set.view.is_a?(ActiveRecord::Relation)
  klass_type = set.model.columns_hash.fetch(keypath.join('.'), nil)&.type
  return klass_type.class if klass_type
end

This is because klass_type is a symbol like :boolean or :float and not an object. We need to be able to get actual Ruby class constants out of the ActiveRecord columns_hash.

Also, when fixing bugs (which I greatly appreciate), I would quadruple appreciate if you could add just one regression test for the bugs. The test suite is the backbone of this entire project.

@fractaledmind
Copy link
Owner

Note: I am working on fixing the issue I describe above

@fractaledmind
Copy link
Owner

@barendt @riyengar8:

Efficiently typecasting filtering params across all of the various scenarios this gem handles (now that I am thinking clearly and exhaustively about this problem) is a complex problem. I am on the right track, but I won't be able to finish things up tonight. I should be able to get things working over the weekend tho. I will tag you both in the PR that arises so that you can keep tabs on the progress and solution.

Sorry that once again your (much appreciated) work to resolve bugs that you found wasn't quite enough to appease the gluttonous and omnivorous test suite. And I hope it isn't frustrating to have me take over your initial work. However, both of these times, you have presented great ideas or important bugs that have required more substantive work to build in fully robust ways. But you providing such clear starting points has truly been invaluable to me, so I thank you.

Hopefully we can both have what we want (you, a gem that works well and efficiently for your specific apps; me, a gem that works for every logical situation I can imagine) with regard to filtering requests by the end of this weekend.

@barendt
Copy link
Contributor Author

barendt commented Aug 9, 2019

That all makes sense to me.

Sorry about submitting a PR with no test cases. For this PR, where would you like to see tests? The test suite here is pretty DRY/meta/requesty, so I'm not sure where best to add these. My first instinct is to want to create a spec/unit/action_set directory and write some specific tests just to cover some cases for klass_for_keypath. My aim wouldn't be to be exhaustive, but to cover enough that I was satisfied I'd covered these bugs. Does that approach sound OK?

@fractaledmind
Copy link
Owner

Yes. Some directory like /unit or /edge-cases or /regressions would be beneficial for more focused unit tests

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