-
Notifications
You must be signed in to change notification settings - Fork 0
Allow dynamic sorting #31
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require "jsonapi/query_builder/base_sort" | ||
|
|
||
| module Jsonapi | ||
| module QueryBuilder | ||
| class DynamicSort < BaseSort | ||
| attr_reader :dynamic_attribute | ||
|
|
||
| # @param [ActiveRecord::Relation] collection | ||
| # @param [Symbol] dynamic_attribute, which attribute to dynamically sort by | ||
| # @param [Symbol] direction of the ordering, one of :asc or :desc | ||
| def initialize(collection, dynamic_attribute, direction = :asc) | ||
| super(collection, direction) | ||
| @dynamic_attribute = dynamic_attribute | ||
| end | ||
|
|
||
| # @return [ActiveRecord::Relation] Collection with order applied | ||
| def results | ||
| raise NotImplementedError, "#{self.class} should implement #results" | ||
| end | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module Jsonapi | ||
| module QueryBuilder | ||
| module Mixins | ||
| module Sort | ||
| class Dynamic | ||
| attr_reader :attribute_prefix, :sort | ||
|
|
||
| def initialize(attribute_prefix, sort) | ||
| @attribute_prefix = attribute_prefix.to_s | ||
| @sort = sort | ||
| end | ||
|
|
||
| def matches?(sort_attribute) | ||
| sort_attribute.to_s.start_with?(attribute_prefix) | ||
| end | ||
|
|
||
| def results(collection, sort_param) | ||
| dynamic_attribute = sort_param.attribute.delete_prefix(attribute_prefix) | ||
| if sort.respond_to?(:call) | ||
| sort.call(collection, dynamic_attribute, sort_param.direction) | ||
| else | ||
| sort.new(collection, dynamic_attribute, sort_param.direction).results | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module Jsonapi | ||
| module QueryBuilder | ||
| module Mixins | ||
| module Sort | ||
| class Static | ||
| attr_reader :attribute, :sort | ||
|
|
||
| def initialize(attribute, sort) | ||
| @attribute = attribute | ||
| @sort = sort || ->(collection, direction) { collection.order(attribute => direction) } | ||
| end | ||
|
|
||
| def results(collection, sort_param) | ||
| if sort.respond_to?(:call) | ||
| sort.call(collection, sort_param.direction) | ||
| else | ||
| sort.new(collection, sort_param.direction).results | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| RSpec.describe Jsonapi::QueryBuilder::DynamicSort do | ||
| let(:sort_class) { Class.new(described_class) } | ||
|
|
||
| before do | ||
| stub_const "FakeSort", sort_class | ||
| end | ||
|
|
||
| it "defaults to ascending sort direction" do | ||
| expect(FakeSort.new(instance_double("collection"), "attribute")).to have_attributes(direction: :asc) | ||
| end | ||
|
|
||
| context "with required interface methods" do | ||
| it "raises an error for results method" do | ||
| expect { FakeSort.new(instance_double("collection"), "attribute", :desc).results }.to raise_error( | ||
| NotImplementedError, "FakeSort should implement #results" | ||
| ) | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| RSpec.describe Jsonapi::QueryBuilder::Mixins::Sort::Dynamic do | ||
| subject(:dynamic_sort) { described_class.new(:"data.", sort) } | ||
|
|
||
| let(:collection) { instance_double("collection") } | ||
| let(:sort) { ->(collection, attribute, direction) { collection.order(attribute => direction) } } | ||
| let(:sort_param) { Jsonapi::QueryBuilder::Mixins::Sort::Param.new("-data.description") } | ||
|
|
||
| before do | ||
| allow(collection).to receive(:order).and_return(collection) | ||
| end | ||
|
|
||
| describe "#matches?" do | ||
| context "when sort attribute starts with configured prefix" do | ||
| it "returns true" do | ||
| expect(dynamic_sort.matches?(:"data.description")).to be true | ||
| end | ||
| end | ||
|
|
||
| context "when sort attribute does not match" do | ||
| it "returns false" do | ||
| expect(dynamic_sort.matches?(:description)).to be false | ||
| end | ||
| end | ||
| end | ||
|
|
||
| describe "#results" do | ||
| context "when sort is a Proc" do | ||
| it "calls the provided proc" do | ||
| dynamic_sort.results(collection, sort_param) | ||
|
|
||
| expect(collection).to have_received(:order).with("description" => :desc) | ||
| end | ||
| end | ||
|
|
||
| context "when sort is a class" do | ||
| let(:sort_class_instance) { instance_double("SortClass", results: collection) } | ||
| let(:sort) { SortClass } | ||
|
|
||
| before do | ||
| class_double("SortClass", new: sort_class_instance).as_stubbed_const | ||
| end | ||
|
|
||
| it "uses the provided sort class", :aggregate_failures do | ||
| dynamic_sort.results(collection, sort_param) | ||
|
|
||
| expect(SortClass).to have_received(:new).with(collection, "description", :desc) | ||
| expect(sort_class_instance).to have_received(:results) | ||
| end | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| RSpec.describe Jsonapi::QueryBuilder::Mixins::Sort::Static do | ||
| subject(:static_sort) { described_class.new(:description, sort) } | ||
|
|
||
| let(:collection) { instance_double("collection") } | ||
|
|
||
| let(:sort_param) { Jsonapi::QueryBuilder::Mixins::Sort::Param.new("-description") } | ||
|
|
||
| before do | ||
| allow(collection).to receive(:order).and_return(collection) | ||
| end | ||
|
|
||
| describe "#results" do | ||
| context "when sort is not given" do | ||
| let(:sort) { nil } | ||
|
|
||
| it "defaults to ordering collection by attribute name" do | ||
| static_sort.results(collection, sort_param) | ||
|
|
||
| expect(collection).to have_received(:order).with(description: :desc) | ||
| end | ||
| end | ||
|
|
||
| context "when sort is a Proc" do | ||
| let(:sort) { ->(collection, direction) { collection.order(foobar: direction) } } | ||
|
|
||
| it "calls the provided proc" do | ||
| static_sort.results(collection, sort_param) | ||
|
|
||
| expect(collection).to have_received(:order).with(foobar: :desc) | ||
| end | ||
| end | ||
|
|
||
| context "when sort is a class" do | ||
| let(:sort_class_instance) { instance_double("SortClass", results: collection) } | ||
| let(:sort) { SortClass } | ||
|
|
||
| before do | ||
| class_double("SortClass", new: sort_class_instance).as_stubbed_const | ||
| end | ||
|
|
||
| it "uses the provided sort class", :aggregate_failures do | ||
| static_sort.results(collection, sort_param) | ||
|
|
||
| expect(SortClass).to have_received(:new).with(collection, :desc) | ||
| expect(sort_class_instance).to have_received(:results) | ||
| end | ||
| end | ||
| end | ||
| end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.