Extract method to DRY up month/year grouping in AnnualReport::TimeSeries class (#35113)
				
					
				
			This commit is contained in:
		@@ -17,14 +17,34 @@ class AnnualReport::TimeSeries < AnnualReport::Source
 | 
				
			|||||||
  private
 | 
					  private
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  def statuses_per_month
 | 
					  def statuses_per_month
 | 
				
			||||||
    @statuses_per_month ||= report_statuses.group(:period).pluck(Arel.sql("date_part('month', created_at)::int AS period, count(*)")).to_h
 | 
					    @statuses_per_month ||= report_statuses.group(:period).pluck(date_part_month.as('period'), Arel.star.count).to_h
 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  def following_per_month
 | 
					  def following_per_month
 | 
				
			||||||
    @following_per_month ||= @account.active_relationships.where("date_part('year', created_at) = ?", @year).group(:period).pluck(Arel.sql("date_part('month', created_at)::int AS period, count(*)")).to_h
 | 
					    @following_per_month ||= annual_relationships_by_month(@account.active_relationships)
 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  def followers_per_month
 | 
					  def followers_per_month
 | 
				
			||||||
    @followers_per_month ||= @account.passive_relationships.where("date_part('year', created_at) = ?", @year).group(:period).pluck(Arel.sql("date_part('month', created_at)::int AS period, count(*)")).to_h
 | 
					    @followers_per_month ||= annual_relationships_by_month(@account.passive_relationships)
 | 
				
			||||||
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  def date_part_month
 | 
				
			||||||
 | 
					    Arel.sql(<<~SQL.squish)
 | 
				
			||||||
 | 
					      DATE_PART('month', created_at)::int
 | 
				
			||||||
 | 
					    SQL
 | 
				
			||||||
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  def annual_relationships_by_month(relationships)
 | 
				
			||||||
 | 
					    relationships
 | 
				
			||||||
 | 
					      .where(created_in_year, @year)
 | 
				
			||||||
 | 
					      .group(:period)
 | 
				
			||||||
 | 
					      .pluck(date_part_month.as('period'), Arel.star.count)
 | 
				
			||||||
 | 
					      .to_h
 | 
				
			||||||
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  def created_in_year
 | 
				
			||||||
 | 
					    Arel.sql(<<~SQL.squish)
 | 
				
			||||||
 | 
					      DATE_PART('year', created_at) = ?
 | 
				
			||||||
 | 
					    SQL
 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
end
 | 
					end
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user