Browse Source

Louis | Adds reports on number of bikes sold and average price per year

master
Loos 10 years ago
parent
commit
90b059a46d
  1. 2
      app/controllers/reports_controller.rb
  2. 35
      app/models/report.rb
  3. 5
      app/models/reports.rb
  4. 15
      app/views/reports/bikes_sold_per_year.html.haml
  5. 1
      db/schema.rb

2
app/controllers/reports_controller.rb

@ -2,7 +2,7 @@ class ReportsController < ApplicationController
before_action :authenticate_user!
def bikes_sold_per_year
@bikes_sold_per_year = Report.bikes_sold_per_year
@yearly_data = Report.yearly_data
end
end

35
app/models/report.rb

@ -0,0 +1,35 @@
class Report
@@years = Bike.all.each.map{|bike| Time.parse(bike.date_sold).year if bike.date_sold}
def self.bikes_sold_per_year
counts = Hash.new(0)
@@years.each { |year| counts[year] += 1 }
counts
end
def self.average_price_per_year
prices = Bike.all.each.map{|bike| bike.price}
years_prices = @@years.each_with_index.map{|year, index| {prices[index].to_s.to_sym => year}}
merged_years_prices = years_prices.reduce({}, :merge)
years_prices_grouped = merged_years_prices.group_by{|k, v| v}
average_price_array = years_prices_grouped.each.map{ |k, year_values|
year_prices = year_values.each.map{|pair| pair[0]}
float_prices = year_prices.map{|price| price.to_s.to_f}
average_price = float_prices.inject{ |sum, el| el + sum } / float_prices.size
{k => average_price}
}
average_prices = average_price_array.reduce({}, :merge)
end
def self.yearly_data
unique_years = @@years.uniq
bikes_sold_per_year = Report.bikes_sold_per_year()
average_price_per_year = Report.average_price_per_year()
yearly_data_array = unique_years.map{|year| {year => {number: bikes_sold_per_year[year], average_price: average_price_per_year[year]}}}
yearly_data = yearly_data_array.reduce({}, :merge)
Hash[yearly_data.sort]
end
end

5
app/models/reports.rb

@ -1,5 +0,0 @@
class Report
def self.bikes_sold_per_year
"KITTIES"
end
end

15
app/views/reports/bikes_sold_per_year.html.haml

@ -1 +1,14 @@
= @bikes_sold_per_year
.container
%h1 Bikes Sold Per Year
%table.table
%thead
%tr
%th Year
%th Number of Bikes
%th Average Price
%tbody
- @yearly_data.each do |year, data|
%tr
%td= year
%td= data[:number]
%td= sprintf( "%0.02f", data[:average_price])

1
db/schema.rb

@ -33,7 +33,6 @@ ActiveRecord::Schema.define(version: 20140923000417) do
t.string "model"
t.string "bike_type"
t.string "color"
t.string "frame_size"
t.string "serial_number"
t.text "notes"
t.text "tag_info"

Loading…
Cancel
Save