lib/decidim/surveys/feature.rb in decidim-surveys-0.4.2 vs lib/decidim/surveys/feature.rb in decidim-surveys-0.4.3

- old
+ new

@@ -1,8 +1,8 @@ # frozen_string_literal: true -require_dependency "decidim/features/namer" +require "decidim/features/namer" Decidim.register_feature(:surveys) do |feature| feature.engine = Decidim::Surveys::Engine feature.admin_engine = Decidim::Surveys::AdminEngine feature.icon = "decidim/surveys/icon.svg" @@ -16,9 +16,24 @@ feature.on(:before_destroy) do |instance| if Decidim::Surveys::Survey.where(feature: instance).any? raise "Can't destroy this feature when there are surveys" end + end + + feature.register_stat :surveys_count do |features, start_at, end_at| + surveys = Decidim::Surveys::Survey.where(feature: features) + surveys = surveys.where("created_at >= ?", start_at) if start_at.present? + surveys = surveys.where("created_at <= ?", end_at) if end_at.present? + surveys.count + end + + feature.register_stat :answers_count, priority: Decidim::StatsRegistry::HIGH_PRIORITY do |features, start_at, end_at| + surveys = Decidim::Surveys::Survey.where(feature: features) + answers = Decidim::Surveys::SurveyAnswer.where(survey: surveys) + answers = answers.where("created_at >= ?", start_at) if start_at.present? + answers = answers.where("created_at <= ?", end_at) if end_at.present? + answers.group(:decidim_user_id).count.size end # These actions permissions can be configured in the admin panel feature.actions = %w(answer)