Class: Decidim::EnhancedTextwork::CollaborativeDraftSearch
- Inherits:
-
ResourceSearch
- Object
- ResourceSearch
- Decidim::EnhancedTextwork::CollaborativeDraftSearch
- Defined in:
- app/services/decidim/enhanced_textwork/collaborative_draft_search.rb
Overview
A service to encapsualte all the logic when searching and filtering collaborative drafts in a participatory process.
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ CollaborativeDraftSearch
constructor
Public: Initializes the service.
-
#search_related_to ⇒ Object
Filters drafts by the name of the classes they are linked to.
-
#search_search_text ⇒ Object
Handle the search_text filter.
-
#search_state ⇒ Object
Handle the state filter.
Constructor Details
#initialize(options = {}) ⇒ CollaborativeDraftSearch
Public: Initializes the service. component - A Decidim::Component to get the drafts from. page - The page number to paginate the results. per_page - The number of drafts to return per page.
14 15 16 |
# File 'app/services/decidim/enhanced_textwork/collaborative_draft_search.rb', line 14 def initialize( = {}) super(CollaborativeDraft.all, ) end |
Instance Method Details
#search_related_to ⇒ Object
Filters drafts by the name of the classes they are linked to. By default, returns all drafts. When a `related_to` param is given, then it camelcases item to find the real class name and checks the links for the Collaborative Draft.
The `related_to` param is expected to be in this form:
"decidim/meetings/meeting"
This can be achieved by performing `klass.name.underscore`.
Returns only those drafts that are linked to the given class name.
46 47 48 49 50 51 52 53 54 55 56 |
# File 'app/services/decidim/enhanced_textwork/collaborative_draft_search.rb', line 46 def from = query .joins(:resource_links_from) .where(decidim_resource_links: { to_type: .camelcase }) to = query .joins(:resource_links_to) .where(decidim_resource_links: { from_type: .camelcase }) query.where(id: from).or(query.where(id: to)) end |
#search_search_text ⇒ Object
Handle the search_text filter
We can't use the search from `ResourceFilter` since these fields aren't translated.
22 23 24 25 26 |
# File 'app/services/decidim/enhanced_textwork/collaborative_draft_search.rb', line 22 def search_search_text query .where("title::text ILIKE ?", "%#{search_text}%") .or(query.where("body ILIKE ?", "%#{search_text}%")) end |
#search_state ⇒ Object
Handle the state filter
29 30 31 32 33 |
# File 'app/services/decidim/enhanced_textwork/collaborative_draft_search.rb', line 29 def search_state return query if state.member?("all") apply_scopes(%w(open withdrawn published), state) end |