-
-
Notifications
You must be signed in to change notification settings - Fork 62
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
Refactor how Rails handles filter categories passed to and from Vue #546
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
05a7450
refactor filter categories passed from Rails to Vue
d5afa82
WIP: refactoring contribution filters
maebeale 2d6a09b
I think this is everything for the refactor
a14d4a3
rename FilterOptionsBlueprint to FilterOptionBlueprint
52126ec
move an HTML comment into a Ruby comment
86ab4a1
refactor names in Rails around filter groups
a9044d8
simplify rails-to-vue flow of link URLs for each contribution
2715452
remove extra binding.pry
63d505d
rename filterGroups to filterGroupings
965d4b2
remove unused browse filter code
5062a99
shorten lenghty BrowseFilter constant
a77c2a1
note filtering refactor in the changelog
a1201b0
Merge branch 'main' into filter-by-date
solebared dcfa193
explain why ContributionTypeFilter violates Liskov substitution
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 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,19 @@ | ||
# frozen_string_literal: true | ||
|
||
# FilterOptionsBlueprint emits a structure that the browse Vue page can turn | ||
# into checkboxes or other form UI elements to filter a list of contributions | ||
# | ||
# We assume that there's a top level type of filter (e.g ContactMethodFilter) | ||
# that then references a series of these FilterOptionBlueprint objects for | ||
# each element (e.g. each available contact method) | ||
class FilterOptionBlueprint < Blueprinter::Base | ||
# The identifier here needs to be in a format that the BrowseFilter can then | ||
# interpret and use to filter results | ||
identifier :id do |filter_option, _options| | ||
"#{filter_option.class}[#{filter_option.id}]" | ||
end | ||
|
||
# This is currently used as a display name so people can understand | ||
# which each option represents | ||
field :name | ||
end |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,25 @@ | ||
# This class has information about how we sort and filter groupings | ||
# and is capable of accepting a model scope and adding additional | ||
# `where` clauses to it in order to further filter the model scope | ||
|
||
# BaseFilter is more like an abstract parent class, but it also serves | ||
# as a NullObject version of the filter grouping, if you want one | ||
class BaseFilter | ||
# This method should be overridden to return a hash with the following keys: | ||
# * :name => a short string that the user will see that describes what type of filters these are | ||
# * :filters => the output a call to FilterOptionBlueprint.render_as_hash that represent each filter option to check or uncheck | ||
def self.filter_grouping | ||
{} | ||
end | ||
|
||
attr_reader :parameters | ||
|
||
def initialize(parameters) | ||
@parameters = parameters | ||
end | ||
|
||
# By default, return the model scope unfiltered | ||
def filter(scope) | ||
scope | ||
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,17 @@ | ||
class CategoryFilter < BaseFilter | ||
def self.filter_grouping | ||
{ | ||
name: "Categories", | ||
# Currently only filtering by top-level categories | ||
filter_options: FilterOptionBlueprint.render_as_hash(Category.roots) | ||
} | ||
end | ||
|
||
def filter(scope) | ||
return super unless parameters | ||
scope.tagged_with( | ||
Category.roots.where(id: parameters.keys).pluck('name'), | ||
any: true | ||
) | ||
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,13 @@ | ||
class ContactMethodFilter < BaseFilter | ||
def self.filter_grouping | ||
{ | ||
name: 'Contact Methods', | ||
filter_options: FilterOptionBlueprint.render_as_hash(ContactMethod.enabled.distinct(:name)) | ||
} | ||
end | ||
|
||
def filter(scope) | ||
return super unless parameters | ||
scope.joins(:person).where(people: {preferred_contact_method: parameters.keys}) | ||
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,24 @@ | ||
class ContributionTypeFilter < BaseFilter | ||
def self.filter_grouping | ||
{name: 'Contribution Types', filter_options: [ | ||
{id: 'ContributionType[Ask]', name: 'Ask'}, | ||
{id: 'ContributionType[Offer]', name: 'Offer'} | ||
]} | ||
end | ||
ALL_ALLOWED_TYPES = ['Ask', 'Offer'].freeze | ||
|
||
def filter(scope) | ||
raise NotImplementedError.new( | ||
# So far the best solution I've found for filtering scopes by contribution types would require | ||
# using SQL UNIONs, which have no good support in Rails | ||
"Can't filter an existing scope by contribution type. Use the `ContributionTypeFilter#scopes` method generate scopes for other filters" | ||
) | ||
end | ||
|
||
def scopes | ||
classes = parameters.blank? ? ALL_ALLOWED_TYPES : parameters.keys | ||
classes.intersection(ALL_ALLOWED_TYPES).map do |type| | ||
type.constantize.matchable | ||
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,13 @@ | ||
class ServiceAreaFilter < BaseFilter | ||
def self.filter_grouping | ||
{ | ||
name: 'Service Areas', | ||
filter_options: FilterOptionBlueprint.render_as_hash(ServiceArea.i18n) | ||
} | ||
end | ||
|
||
def filter(scope) | ||
return super unless parameters | ||
scope.where(service_area_id: parameters.keys) | ||
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
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.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this right — that
matchable
is what we want here?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes. we'll need to add that scope to
CommunityResource
to return objects that are still published