Enhancements and bugfixing
FIxed issue in last commit: supports controller were not correctly filling the holes without data. Fixed duplication in supports and successful supports controller using a concer. Successfull supports controller will fill the holes without data in the same way that supports controller does.
This commit is contained in:
56
app/controllers/concerns/dashboard/group_supports.rb
Normal file
56
app/controllers/concerns/dashboard/group_supports.rb
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
module Dashboard::GroupSupports
|
||||||
|
extend ActiveSupport::Concern
|
||||||
|
|
||||||
|
included do
|
||||||
|
def grouped_supports(attribute)
|
||||||
|
supports.group_by { |v| grouping_key_for(v[attribute].to_date) }
|
||||||
|
end
|
||||||
|
|
||||||
|
def grouping_key_for(date)
|
||||||
|
return "#{date.cweek}/#{date.year}" if params[:group_by] == 'week'
|
||||||
|
return "#{date.year}-#{date.month}" if params[:group_by] == 'month'
|
||||||
|
|
||||||
|
date
|
||||||
|
end
|
||||||
|
|
||||||
|
def accumulate_supports(grouped_votes)
|
||||||
|
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
|
||||||
|
end
|
||||||
|
|
||||||
|
def fill_holes(grouped_votes)
|
||||||
|
(start_date(proposal.published_at.to_date)..end_date).each do |date|
|
||||||
|
missing_key = grouping_key_for(date)
|
||||||
|
next if grouped_votes.key? missing_key
|
||||||
|
|
||||||
|
previous_key = previous_key_for(date)
|
||||||
|
previous_value = if grouped_votes.key? previous_key
|
||||||
|
grouped_votes[previous_key]
|
||||||
|
else
|
||||||
|
0
|
||||||
|
end
|
||||||
|
|
||||||
|
grouped_votes[missing_key] = previous_value
|
||||||
|
end
|
||||||
|
|
||||||
|
grouped_votes
|
||||||
|
end
|
||||||
|
|
||||||
|
def previous_key_for(date)
|
||||||
|
grouping_key_for(date - interval)
|
||||||
|
end
|
||||||
|
|
||||||
|
def interval
|
||||||
|
return 1.week if params[:group_by] == 'week'
|
||||||
|
return 1.month if params[:group_by] == 'month'
|
||||||
|
1.day
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
class Dashboard::SuccessfulSupportsController < Dashboard::BaseController
|
class Dashboard::SuccessfulSupportsController < Dashboard::BaseController
|
||||||
include Dashboard::ExpectsDateRange
|
include Dashboard::ExpectsDateRange
|
||||||
|
include Dashboard::GroupSupports
|
||||||
|
|
||||||
def index
|
def index
|
||||||
authorize! :dashboard, proposal
|
authorize! :dashboard, proposal
|
||||||
@@ -9,30 +10,9 @@ class Dashboard::SuccessfulSupportsController < Dashboard::BaseController
|
|||||||
private
|
private
|
||||||
|
|
||||||
def accumulated_grouped_supports
|
def accumulated_grouped_supports
|
||||||
grouped_votes = grouped_supports
|
grouped_votes = grouped_supports(:voted_at)
|
||||||
grouped_votes.each do |group, votes|
|
accumulate_supports(grouped_votes)
|
||||||
grouped_votes[group] = votes.inject(0) { |sum, vote| sum + vote.vote_weight }
|
fill_holes(grouped_votes)
|
||||||
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
|
end
|
||||||
|
|
||||||
def supports
|
def supports
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
class Dashboard::SupportsController < Dashboard::BaseController
|
class Dashboard::SupportsController < Dashboard::BaseController
|
||||||
include Dashboard::ExpectsDateRange
|
include Dashboard::ExpectsDateRange
|
||||||
|
include Dashboard::GroupSupports
|
||||||
|
|
||||||
def index
|
def index
|
||||||
authorize! :dashboard, proposal
|
authorize! :dashboard, proposal
|
||||||
@@ -9,59 +10,12 @@ class Dashboard::SupportsController < Dashboard::BaseController
|
|||||||
private
|
private
|
||||||
|
|
||||||
def accumulated_supports
|
def accumulated_supports
|
||||||
grouped_votes = grouped_supports
|
grouped_votes = grouped_supports(:created_at)
|
||||||
grouped_votes.each do |group, votes|
|
accumulate_supports(grouped_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
|
|
||||||
|
|
||||||
fill_holes(grouped_votes)
|
fill_holes(grouped_votes)
|
||||||
end
|
end
|
||||||
|
|
||||||
def grouped_supports
|
|
||||||
supports.group_by { |v| grouping_key_for(v.created_at) }
|
|
||||||
end
|
|
||||||
|
|
||||||
def grouping_key_for(created_at)
|
|
||||||
return "#{created_at.to_date.cweek}/#{created_at.to_date.year}" if params[:group_by] == 'week'
|
|
||||||
return "#{created_at.to_date.year}-#{created_at.to_date.month}" if params[:group_by] == 'month'
|
|
||||||
|
|
||||||
created_at.to_date
|
|
||||||
end
|
|
||||||
|
|
||||||
def fill_holes(grouped_votes)
|
|
||||||
(start_date(proposal.published_at.to_date)..end_date).step(interval).each do |date|
|
|
||||||
missing_key = grouping_key_for(date)
|
|
||||||
next if grouped_votes.key? missing_key
|
|
||||||
|
|
||||||
previous_key = previous_key_for(date)
|
|
||||||
previous_value = if grouped_votes.key? previous_key
|
|
||||||
grouped_votes[previous_key]
|
|
||||||
else
|
|
||||||
0
|
|
||||||
end
|
|
||||||
|
|
||||||
grouped_votes[missing_key] = previous_value
|
|
||||||
end
|
|
||||||
|
|
||||||
grouped_votes
|
|
||||||
end
|
|
||||||
|
|
||||||
def previous_key_for(date)
|
|
||||||
grouping_key_for(date - interval)
|
|
||||||
end
|
|
||||||
|
|
||||||
def interval
|
|
||||||
return 1.week if params[:group_by] == 'week'
|
|
||||||
return 1.month if params[:group_by] == 'month'
|
|
||||||
1.day
|
|
||||||
end
|
|
||||||
|
|
||||||
def supports
|
def supports
|
||||||
@supports ||= Vote
|
@supports ||= Vote
|
||||||
.where(votable: proposal, created_at: start_date.beginning_of_day..end_date.end_of_day)
|
.where(votable: proposal, created_at: start_date.beginning_of_day..end_date.end_of_day)
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
require 'rails_helper'
|
require 'rails_helper'
|
||||||
|
|
||||||
describe "Retrieves number of supports for the successful proposal" do
|
describe "Retrieves number of supports for the successful proposal" do
|
||||||
let(:created_at) { DateTime.parse("2018-01-01 12:00:00") }
|
let(:created_at) { Time.now.beginning_of_day - 9.days }
|
||||||
let(:proposal) { create(:proposal, created_at: created_at) }
|
let(:proposal) { create(:proposal, created_at: created_at, published_at: created_at) }
|
||||||
|
|
||||||
before do
|
before do
|
||||||
@successful_proposal_id = Setting['proposals.successful_proposal_id']
|
@successful_proposal_id = Setting['proposals.successful_proposal_id']
|
||||||
@@ -32,7 +32,7 @@ describe "Retrieves number of supports for the successful proposal" do
|
|||||||
json = JSON.parse(response.body, symbolize_names: true)
|
json = JSON.parse(response.body, symbolize_names: true)
|
||||||
|
|
||||||
expect(response).to have_http_status(200)
|
expect(response).to have_http_status(200)
|
||||||
expect(json.length).to eq(8)
|
expect(json.length).to eq(10)
|
||||||
expect(json.values.last).to eq(8)
|
expect(json.values.last).to eq(8)
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -52,7 +52,8 @@ describe "Retrieves number of supports for the successful proposal" do
|
|||||||
json = JSON.parse(response.body, symbolize_names: true)
|
json = JSON.parse(response.body, symbolize_names: true)
|
||||||
|
|
||||||
expect(response).to have_http_status(200)
|
expect(response).to have_http_status(200)
|
||||||
expect(json.length).to eq(1)
|
expect(json.length).to be >= 1
|
||||||
|
expect(json.length).to be <= 2
|
||||||
expect(json.values.last).to eq(8)
|
expect(json.values.last).to eq(8)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
require 'rails_helper'
|
require 'rails_helper'
|
||||||
|
|
||||||
describe "Retrieves number of supports for a proposal" do
|
describe "Retrieves number of supports for a proposal" do
|
||||||
let(:created_at) { DateTime.parse("2018-01-01 12:00:00") }
|
let(:created_at) { Time.now - 9.days }
|
||||||
let(:proposal) { create(:proposal, created_at: created_at, published_at: created_at) }
|
let(:proposal) { create(:proposal, created_at: created_at, published_at: created_at) }
|
||||||
|
|
||||||
before do
|
before do
|
||||||
@@ -19,7 +19,7 @@ describe "Retrieves number of supports for a proposal" do
|
|||||||
json = JSON.parse(response.body, symbolize_names: true)
|
json = JSON.parse(response.body, symbolize_names: true)
|
||||||
|
|
||||||
expect(response).to have_http_status(200)
|
expect(response).to have_http_status(200)
|
||||||
expect(json.length).to eq(8)
|
expect(json.length).to eq(10)
|
||||||
expect(json.values.last).to eq(8)
|
expect(json.values.last).to eq(8)
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -39,7 +39,8 @@ describe "Retrieves number of supports for a proposal" do
|
|||||||
json = JSON.parse(response.body, symbolize_names: true)
|
json = JSON.parse(response.body, symbolize_names: true)
|
||||||
|
|
||||||
expect(response).to have_http_status(200)
|
expect(response).to have_http_status(200)
|
||||||
expect(json.length).to eq(1)
|
expect(json.length).to be >= 1
|
||||||
|
expect(json.length).to be <= 2
|
||||||
expect(json.values.last).to eq(8)
|
expect(json.values.last).to eq(8)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user