Files
nairobi/app/controllers/debates_controller.rb
Javi Martín f7e2d724dd Replace ahoy events with real data
We were tracking some events with Ahoy, but in an inconsistent way. For
example, we were tracking when a debate was created, but (probably
accidentally) we were only tracking proposals when they were created
from the management section. For budget investments and their supports,
we weren't using Ahoy events but checking their database tables instead.
And we were only using ahoy events for the charts; for the other stats,
we were using the real data.

While we could actually fix these issues and start tracking events
correctly, existing production data would remain broken because we
didn't track a certain event when it happened. And, besides, why should
we bother, for instance, to track when a debate is created, when we can
instead access that information in the debates table?

There are probably some features related to tracking an event and their
visits, but we weren't using them, and we were storing more user data
than we needed to.

So we're removing the track events, allowing us to simplify the code and
make it more consistent. We aren't removing the `ahoy_events` table in
case existing Consul Democracy installations use it, but we'll remove it
after releasing version 2.2.0 and adding a warning in the release notes.

This change fixes the proposal created chart, since we were only
tracking proposals created in the management section, and opens the
possibility to add more charts in the future using data we didn't track
with Ahoy.

Also note the "Level 2 user Graph" test wasn't testing the graph, so
we're changing it in order to test it. We're also moving it next to the
other graphs test and, since we were tracking the event when we were
confirming the phone, we're renaming to "Level 3 users".

Finally, note that, since we were tracking events when something was
created, we're including the `with_hidden` scope. This is also
consistent with the other stats shown in the admin section as well as
the public stats.
2024-05-09 14:28:32 +02:00

84 lines
2.1 KiB
Ruby

class DebatesController < ApplicationController
include FeatureFlags
include CommentableActions
include FlagActions
include Translatable
before_action :authenticate_user!, except: [:index, :show]
before_action :set_view, only: :index
before_action :debates_recommendations, only: :index, if: :current_user
feature_flag :debates
invisible_captcha only: [:create, :update], honeypot: :subtitle
has_orders ->(c) { Debate.debates_orders(c.current_user) }, only: :index
has_orders %w[most_voted newest oldest], only: :show
load_and_authorize_resource
helper_method :resource_model, :resource_name
respond_to :html, :js
def create
@debate = Debate.new(debate_params)
@debate.author = current_user
if @debate.save
redirect_to debate_path(@debate), notice: t("flash.actions.create.debate")
else
render :new
end
end
def index_customization
@featured_debates = @debates.featured
end
def show
super
redirect_to debate_path(@debate), status: :moved_permanently if request.path != debate_path(@debate)
end
def unmark_featured
@debate.update!(featured_at: nil)
redirect_to debates_path
end
def mark_featured
@debate.update!(featured_at: Time.current)
redirect_to debates_path
end
def disable_recommendations
if current_user.update(recommended_debates: false)
redirect_to debates_path, notice: t("debates.index.recommendations.actions.success")
else
redirect_to debates_path, error: t("debates.index.recommendations.actions.error")
end
end
private
def debate_params
params.require(:debate).permit(allowed_params)
end
def allowed_params
[:tag_list, :terms_of_service, :related_sdg_list, translation_params(Debate)]
end
def resource_model
Debate
end
def set_view
@view = (params[:view] == "minimal") ? "minimal" : "default"
end
def debates_recommendations
if Setting["feature.user.recommendations_on_debates"] && current_user.recommended_debates
@recommended_debates = Debate.recommendations(current_user).sort_by_random.limit(3)
end
end
end