Fixes #voodoorai2000 comments
Fixes some comments from #voodoorai2000 for the PR to consul
This commit is contained in:
44
app/controllers/dashboard/achievements_controller.rb
Normal file
44
app/controllers/dashboard/achievements_controller.rb
Normal file
@@ -0,0 +1,44 @@
|
||||
class Dashboard::AchievementsController < Dashboard::BaseController
|
||||
include Dashboard::ExpectsDateRange
|
||||
|
||||
def index
|
||||
authorize! :dashboard, proposal
|
||||
render json: processed_groups
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def processed_groups
|
||||
grouped_results = groups
|
||||
grouped_results.each do |key, results|
|
||||
grouped_results[key] = {
|
||||
executed_at: results.last.executed_at,
|
||||
title: results.last.action.title
|
||||
}
|
||||
end
|
||||
|
||||
grouped_results
|
||||
end
|
||||
|
||||
def groups
|
||||
if params[:group_by] == 'week'
|
||||
return executed_proposed_actions.group_by { |v| "#{v.executed_at.to_date.cweek}/#{v.executed_at.to_date.year}"}
|
||||
end
|
||||
|
||||
if params[:group_by] == 'month'
|
||||
return executed_proposed_actions.group_by { |v| "#{v.executed_at.to_date.year}-#{v.executed_at.to_date.month}"}
|
||||
end
|
||||
|
||||
executed_proposed_actions.group_by { |a| a.executed_at.to_date }
|
||||
end
|
||||
|
||||
def executed_proposed_actions
|
||||
@executed_proposed_actions ||= Dashboard::ExecutedAction
|
||||
.joins(:action)
|
||||
.includes(:action)
|
||||
.where(proposal: proposal)
|
||||
.where(executed_at: start_date.beginning_of_day..end_date.end_of_day)
|
||||
.where(dashboard_actions: { action_type: 0 })
|
||||
.order(executed_at: :asc)
|
||||
end
|
||||
end
|
||||
@@ -1,6 +1,8 @@
|
||||
class Dashboard::BaseController < ApplicationController
|
||||
before_action :authenticate_user!
|
||||
|
||||
include Dashboard::HasProposal
|
||||
|
||||
helper_method :proposal, :proposed_actions, :resource, :resources, :next_goal, :next_goal_supports, :next_goal_progress, :community_members_count
|
||||
|
||||
respond_to :html
|
||||
@@ -8,10 +10,6 @@ class Dashboard::BaseController < ApplicationController
|
||||
|
||||
private
|
||||
|
||||
def proposal
|
||||
@proposal ||= Proposal.includes(:community).find(params[:proposal_id])
|
||||
end
|
||||
|
||||
def proposed_actions
|
||||
@proposed_actions ||= Dashboard::Action.proposed_actions.active_for(proposal).order(order: :asc)
|
||||
end
|
||||
|
||||
58
app/controllers/dashboard/successful_supports_controller.rb
Normal file
58
app/controllers/dashboard/successful_supports_controller.rb
Normal file
@@ -0,0 +1,58 @@
|
||||
class Dashboard::SuccessfulSupportsController < Dashboard::BaseController
|
||||
include Dashboard::ExpectsDateRange
|
||||
|
||||
def index
|
||||
authorize! :dashboard, proposal
|
||||
render json: accumulated_grouped_supports
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def accumulated_grouped_supports
|
||||
grouped_votes = grouped_supports
|
||||
grouped_votes.each do |group, votes|
|
||||
grouped_votes[group] = votes.inject(0) { |sum, vote| sum + vote.vote_weight }
|
||||
end
|
||||
|
||||
accumulated = 0
|
||||
grouped_votes.each do |k, v|
|
||||
accumulated += v
|
||||
grouped_votes[k] = accumulated
|
||||
end
|
||||
|
||||
grouped_votes
|
||||
end
|
||||
|
||||
def grouped_supports
|
||||
if params[:group_by] == 'week'
|
||||
return supports.group_by { |v| "#{v.voted_at.to_date.cweek}/#{v.voted_at.to_date.year}" }
|
||||
end
|
||||
|
||||
if params[:group_by] == 'month'
|
||||
return supports.group_by { |v| "#{v.voted_at.to_date.year}-#{v.voted_at.to_date.month}" }
|
||||
end
|
||||
|
||||
supports.group_by { |v| v.voted_at.to_date }
|
||||
end
|
||||
|
||||
def supports
|
||||
return [] if successful_proposal.nil?
|
||||
|
||||
Vote
|
||||
.select("created_at + interval '#{days_diff} day' voted_at, *")
|
||||
.where(votable: successful_proposal)
|
||||
.where("created_at + interval '#{days_diff} day' between ? and ?", start_date.beginning_of_day, end_date.end_of_day)
|
||||
.order(created_at: :asc)
|
||||
end
|
||||
|
||||
def successful_proposal
|
||||
@successful_proposal ||= Proposal.find_by(id: Setting['proposals.successful_proposal_id'])
|
||||
end
|
||||
|
||||
def days_diff
|
||||
return 0 if successful_proposal.nil?
|
||||
return 0 if proposal.published_at.nil?
|
||||
|
||||
(proposal.published_at.to_date - successful_proposal.published_at.to_date).to_i
|
||||
end
|
||||
end
|
||||
43
app/controllers/dashboard/supports_controller.rb
Normal file
43
app/controllers/dashboard/supports_controller.rb
Normal file
@@ -0,0 +1,43 @@
|
||||
class Dashboard::SupportsController < Dashboard::BaseController
|
||||
include Dashboard::ExpectsDateRange
|
||||
|
||||
def index
|
||||
authorize! :dashboard, proposal
|
||||
render json: accumulated_supports
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def accumulated_supports
|
||||
grouped_votes = grouped_supports
|
||||
grouped_votes.each do |group, votes|
|
||||
grouped_votes[group] = votes.inject(0) { |sum, vote| sum + vote.vote_weight }
|
||||
end
|
||||
|
||||
accumulated = 0
|
||||
grouped_votes.each do |k, v|
|
||||
accumulated += v
|
||||
grouped_votes[k] = accumulated
|
||||
end
|
||||
|
||||
grouped_votes
|
||||
end
|
||||
|
||||
def grouped_supports
|
||||
if params[:group_by] == 'week'
|
||||
return supports.group_by { |v| "#{v.created_at.to_date.cweek}/#{v.created_at.to_date.year}" }
|
||||
end
|
||||
|
||||
if params[:group_by] == 'month'
|
||||
return supports.group_by { |v| "#{v.created_at.to_date.year}-#{v.created_at.to_date.month}" }
|
||||
end
|
||||
|
||||
supports.group_by { |v| v.created_at.to_date }
|
||||
end
|
||||
|
||||
def supports
|
||||
@supports ||= Vote
|
||||
.where(votable: proposal, created_at: start_date.beginning_of_day..end_date.end_of_day)
|
||||
.order(created_at: :asc)
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user