-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add support for a default scope and auto-detecting based on whether or not the model is orderable, and has default scope method defined * Detect whether the order param was applied, and if not, apply the default scope * Expose the ability to set the default scope for projects in settings using the same values as ProjectCollections * Clean up some of the project collection sorting logic not to be so dependent on raw strings and instead actual programmatic calls * Use sort title for project collections and universally when sorting projects by title * Blacklist some params that should not be included in filters ever for any reason Resolves RET-1727
- Loading branch information
1 parent
6fbab30
commit 89c01a5
Showing
20 changed files
with
352 additions
and
44 deletions.
There are no files selected for viewing
This file contains 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 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 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,55 @@ | ||
# frozen_string_literal: true | ||
|
||
module LazyOrdering | ||
extend ActiveSupport::Concern | ||
|
||
SimpleSortDirection = Dry::Types["coercible.string"].default("asc").enum("asc", "desc").fallback("asc") | ||
|
||
module ClassMethods | ||
# @param [Symbol] column | ||
# @return [ActiveRecord::Relation] | ||
def lazily_order(column, raw_direction = :asc) | ||
base = lazy_order_scope_for column | ||
|
||
expr = lazy_order_expr_for(column, raw_direction: raw_direction) | ||
|
||
base.order(expr) | ||
end | ||
|
||
def lazy_order_column_for(column) | ||
method_name = :"#{column}_order_column" | ||
|
||
if respond_to?(method_name) | ||
public_send(method_name) | ||
else | ||
column | ||
end.then { |expr| arel_attrify(expr) } | ||
end | ||
|
||
def lazy_order_expr_for(column, raw_direction: "asc") | ||
direction = ::Filtering::Types::SortDirection[raw_direction] | ||
|
||
method_name = :"#{column}_order_expression" | ||
|
||
if respond_to?(method_name) | ||
public_send(method_name, direction: direction) | ||
else | ||
attr = lazy_order_column_for column | ||
|
||
attr.public_send(direction) | ||
end | ||
end | ||
|
||
# @param [Symbol] column | ||
# @return [ActiveRecord::Relation] | ||
def lazy_order_scope_for(column) | ||
scope_name = :"prepare_order_for_#{column}" | ||
|
||
if respond_to?(scope_name) | ||
all.public_send(scope_name) | ||
else | ||
all | ||
end | ||
end | ||
end | ||
end |
This file contains 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,15 @@ | ||
# frozen_string_literal: true | ||
|
||
# Shared logic for | ||
module ProjectOrdering | ||
extend ActiveSupport::Concern | ||
|
||
ALLOWED_SORT_KEYS = %w(created_at updated_at publication_date title).freeze | ||
ALLOWED_SORT_DIRECTIONS = %w(asc desc).freeze | ||
|
||
ALLOWED_SORT_MAPPING = Filtering::SortMapping.from(*ALLOWED_SORT_KEYS) | ||
|
||
ALLOWED_SORT_VALUES = ALLOWED_SORT_MAPPING.keys.freeze | ||
|
||
DEFAULT_COLLECTION_SORT_VALUE = "created_at_desc" | ||
end |
This file contains 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 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 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 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 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 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 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 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
Oops, something went wrong.