Class: Decidim::EnhancedTextwork::ParagraphSearch
- Inherits:
-
ResourceSearch
- Object
- ResourceSearch
- Decidim::EnhancedTextwork::ParagraphSearch
- Defined in:
- app/services/decidim/enhanced_textwork/paragraph_search.rb
Overview
A service to encapsualte all the logic when searching and filtering paragraphs in a participatory process.
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ ParagraphSearch
constructor
Public: Initializes the service.
-
#search_activity ⇒ Object
Handle the activity filter.
-
#search_related_to ⇒ Object
Filters Paragraphs by the name of the classes they are linked to.
-
#search_state ⇒ Object
Handle the state filter.
- #search_state_withdraw ⇒ Object
-
#search_type ⇒ Object
Handle the amendment type filter.
Constructor Details
#initialize(options = {}) ⇒ ParagraphSearch
Public: Initializes the service. component - A Decidim::Component to get the paragraphs from. page - The page number to paginate the results. per_page - The number of paragraphs to return per page.
14 15 16 17 18 |
# File 'app/services/decidim/enhanced_textwork/paragraph_search.rb', line 14 def initialize( = {}) [:scope] = .fetch(:scope, Paragraph) [:scope] = [:state_withdraw] == "withdrawn" ? [:scope].withdrawn : [:scope].except_withdrawn super([:scope], ) end |
Instance Method Details
#search_activity ⇒ Object
Handle the activity filter
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'app/services/decidim/enhanced_textwork/paragraph_search.rb', line 21 def search_activity case activity when "voted" query .includes(:votes) .where(decidim_enhanced_textwork_paragraph_votes: { decidim_author_id: user }) when "my_paragraphs" query .where.not(coauthorships_count: 0) .joins(:coauthorships) .where(decidim_coauthorships: { decidim_author_type: "Decidim::UserBaseEntity" }) .where(decidim_coauthorships: { decidim_author_id: user }) else # Assume 'all' query end end |
#search_related_to ⇒ Object
Filters Paragraphs by the name of the classes they are linked to. By default, returns all Paragraphs. When a `related_to` param is given, then it camelcases item to find the real class name and checks the links for the Paragraphs.
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 paragraphs that are linked to the given class name.
72 73 74 75 76 77 78 79 80 81 82 |
# File 'app/services/decidim/enhanced_textwork/paragraph_search.rb', line 72 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_state ⇒ Object
Handle the state filter
45 46 47 |
# File 'app/services/decidim/enhanced_textwork/paragraph_search.rb', line 45 def search_state apply_scopes(%w(accepted rejected evaluating state_not_published), state) end |
#search_state_withdraw ⇒ Object
38 39 40 41 42 |
# File 'app/services/decidim/enhanced_textwork/paragraph_search.rb', line 38 def search_state_withdraw return query if state_withdraw == "withdrawn" query.except_withdrawn end |
#search_type ⇒ Object
Handle the amendment type filter
50 51 52 53 54 55 56 57 58 59 |
# File 'app/services/decidim/enhanced_textwork/paragraph_search.rb', line 50 def search_type case type when "paragraphs" query.only_amendables when "amendments" query.only_visible_emendations_for(user, component) else # Assume 'all' query.amendables_and_visible_emendations_for(user, component) end end |