Sha256: c86d92d18204443edea3a1623d357075682e9e401039a1f91a1a2da4fb1ab69f

Contents?: true

Size: 987 Bytes

Versions: 3

Compression:

Stored size: 987 Bytes

Contents

class AccountChart
  include Reporta::Report

  filter :gender, collection: %w(Any Male Female), include_blank: false

  x_axis :months, as: :date, format: "%Y-%m-%d"
  y_axis :accounts, title: 'Total Accounts'

  line_chart :sign_ups
  column_chart :average_age, line_width: 20, align: :left

  def months
    @months ||= (1..12).map { |month| Date.new(Date.today.year, month) }
  end

  def sign_ups(month)
    total = accounts_by_month[month.to_date].to_a.size
    [month.to_time.to_i * 1000, total]
  end

  def average_age(month)
    accounts = accounts_by_month[month.to_date]
    average = accounts.sum(&:age) / accounts.size
    [month.to_time.to_i * 1000, average]
  end

  private

  def accounts_by_month
    @accounts_by_month ||= begin
      users = User.all
      if gender.present? && gender != 'Any'
        users = users.where(gender: gender)
      end
      users.group_by do |account|
        account.created_at.beginning_of_month.to_date
      end
    end
  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
reporta-modules-0.0.3 spec/dummy/app/models/account_chart.rb
reporta-modules-0.0.2 spec/dummy/app/models/account_chart.rb
reporta-modules-0.0.1 spec/dummy/app/models/account_chart.rb