Skip to content
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

Add :comments_count for stats of Accountability, Blogs, Debates, and Sortitions components #637

Merged
merged 1 commit into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions app/queries/decidim/accountability/filtered_results.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# frozen_string_literal: true

module Decidim
module Accountability
# A class used to find results filtered by components and a date range
class FilteredResults < Decidim::Query
# Syntactic sugar to initialize the class and return the queried objects.
#
# components - An array of Decidim::Component
# start_at - A date to filter resources created after it
# end_at - A date to filter resources created before it.
def self.for(components, start_at = nil, end_at = nil)
new(components, start_at, end_at).query
end

# Initializes the class.
#
# components - An array of Decidim::Component
# start_at - A date to filter resources created after it
# end_at - A date to filter resources created before it.
def initialize(components, start_at = nil, end_at = nil)
@components = components
@start_at = start_at
@end_at = end_at
end

# Finds the Results scoped to an array of components and filtered
# by a range of dates.
def query
results = Decidim::Accountability::Result.where(component: @components)
results = results.where(created_at: @start_at..) if @start_at.present?
results = results.where(created_at: ..@end_at) if @end_at.present?
results
end
end
end
end
37 changes: 37 additions & 0 deletions app/queries/decidim/blogs/filtered_posts.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# frozen_string_literal: true

module Decidim
module Blogs
# A class used to find posts filtered by components and a date range
class FilteredPosts < Decidim::Query
# Syntactic sugar to initialize the class and return the queried objects.
#
# components - An array of Decidim::Component
# start_at - A date to filter resources created after it
# end_at - A date to filter resources created before it.
def self.for(components, start_at = nil, end_at = nil)
new(components, start_at, end_at).query
end

# Initializes the class.
#
# components - An array of Decidim::Component
# start_at - A date to filter resources created after it
# end_at - A date to filter resources created before it.
def initialize(components, start_at = nil, end_at = nil)
@components = components
@start_at = start_at
@end_at = end_at
end

# Finds the Posts scoped to an array of components and filtered
# by a range of dates.
def query
posts = Decidim::Blogs::Post.where(component: @components)
posts = posts.where(created_at: @start_at..) if @start_at.present?
posts = posts.where(created_at: ..@end_at) if @end_at.present?
posts
end
end
end
end
37 changes: 37 additions & 0 deletions app/queries/decidim/debates/filtered_debates.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# frozen_string_literal: true

module Decidim
module Debates
# A class used to find debates filtered by components and a date range
class FilteredDebates < Decidim::Query
# Syntactic sugar to initialize the class and return the queried objects.
#
# components - An array of Decidim::Component
# start_at - A date to filter resources created after it
# end_at - A date to filter resources created before it.
def self.for(components, start_at = nil, end_at = nil)
new(components, start_at, end_at).query
end

# Initializes the class.
#
# components - An array of Decidim::Component
# start_at - A date to filter resources created after it
# end_at - A date to filter resources created before it.
def initialize(components, start_at = nil, end_at = nil)
@components = components
@start_at = start_at
@end_at = end_at
end

# Finds the Debates scoped to an array of components and filtered
# by a range of dates.
def query
debates = Decidim::Debates::Debate.where(component: @components)
debates = debates.where(created_at: @start_at..) if @start_at.present?
debates = debates.where(created_at: ..@end_at) if @end_at.present?
debates
end
end
end
end
31 changes: 31 additions & 0 deletions config/initializers/comments_count.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# frozen_string_literal: true

Rails.application.config.to_prepare do
# Add :comment_count to accountability_component's stat
accountability_component = Decidim.find_component_manifest(:accountability)
accountability_component.register_stat :comments_count, tag: :comments do |components, start_at, end_at|
results = Decidim::Accountability::FilteredResults.for(components, start_at, end_at)
results.sum(:comments_count)
end

# Add :comment_count to blogs_component's stat
blogs_component = Decidim.find_component_manifest(:blogs)
blogs_component.register_stat :comments_count, tag: :comments do |components, start_at, end_at|
posts = Decidim::Blogs::FilteredPosts.for(components, start_at, end_at)
posts.sum(:comments_count)
end

# Add :comment_count to debates_component's stat
debates_component = Decidim.find_component_manifest(:debates)
debates_component.register_stat :comments_count, tag: :comments do |components, start_at, end_at|
debates = Decidim::Debates::FilteredDebates.for(components, start_at, end_at)
debates.sum(:comments_count)
end

# Add :comment_count to sortitions_component's stat
sortitions_component = Decidim.find_component_manifest(:sortitions)
sortitions_component.register_stat :comments_count, tag: :comments do |components, start_at, end_at|
sortitions = Decidim::Sortitions::FilteredSortitions.for(components, start_at, end_at)
sortitions.sum(:comments_count)
end
end
Loading