Skip to content

Instantly share code, notes, and snippets.

@oriolgual
Last active July 11, 2018 07:16
Show Gist options
  • Save oriolgual/75c07708dc3a2e0e5260ff465eb5ad62 to your computer and use it in GitHub Desktop.
Save oriolgual/75c07708dc3a2e0e5260ff465eb5ad62 to your computer and use it in GitHub Desktop.
An example implementation of how to get and store metrics for Decidim
module Decidim
class OrganizationComponents < Rectify::Query
def self.for(organization)
new(organization).query
end
def initialize(organization)
@organization = organization
end
def query
Component.where(participatory_space: participatory_spaces).published
end
def participatory_spaces
@participatory_spaces ||= Decidim.participatory_space_manifests.flat_map do |manifest|
manifest.participatory_spaces.call(@organization).public_spaces
end
end
end
end
# Given an organization
organization_proposals = Decidim::Proposals::Proposal.where(component: Decidim::OrganizationComponents.for(organization))
organization_proposals = organization_proposals.published
Decidim::Metric.create!(
metricable: organization,
name: "organization_proposals",
value: organization_proposals.count
)
# Given a participatory space and a category
space_proposals = Decidim::Proposals::Proposal.where(component: Decidim::Component.where(participatory_space: space).published).published
proposals_by_category = space_proposals.joins(:category).where(decidim_categories: {id: category.id})
Decidim::Metric.create!(
metricable: space,
name: "proposals_by_category",
value: proposals_by_category.count,
metadata: {
decidim_category_id: category.id,
category_name: category.name
}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment