From 420a42bdb138c328d61cc64c4580ba5451c2d958 Mon Sep 17 00:00:00 2001 From: Petko Bordjukov Date: Tue, 6 Oct 2015 21:37:53 +0300 Subject: [PATCH] Rudimentary export --- app/controllers/application_controller.rb | 2 ++ app/controllers/home_controller.rb | 5 +++++ app/models/ratings.rb | 16 ++++++++++++++++ app/models/talk.rb | 5 +++++ app/views/home/_talk.csv.erb | 1 + app/views/home/ratings.csv.erb | 3 +++ config/routes.rb | 1 + 7 files changed, 33 insertions(+) create mode 100644 app/models/ratings.rb create mode 100644 app/views/home/_talk.csv.erb create mode 100644 app/views/home/ratings.csv.erb diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 1dec6a9..5e12622 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,3 +1,5 @@ +require 'csv' + class ApplicationController < ActionController::Base # Prevent CSRF attacks by raising an exception. # For APIs, you may want to use :null_session instead. diff --git a/app/controllers/home_controller.rb b/app/controllers/home_controller.rb index 95f2992..9073dca 100644 --- a/app/controllers/home_controller.rb +++ b/app/controllers/home_controller.rb @@ -1,4 +1,9 @@ class HomeController < ApplicationController def index end + + def ratings + @talks = Talk.ordered_by_rating + @ratings = Ratings.new + end end diff --git a/app/models/ratings.rb b/app/models/ratings.rb new file mode 100644 index 0000000..1ba0a49 --- /dev/null +++ b/app/models/ratings.rb @@ -0,0 +1,16 @@ +class Ratings + @ratings = Hash.new(0) + + def initialize + ratings = TalkPreference.all.pluck(:talks).reduce(Hash.new(0)) do |result, talks| + talks.map(&:to_i).each { |talk| result[talk] += 1 } + result + end.to_h + + @ratings = Hash.new(0).merge!(ratings) + end + + def [](id) + @ratings[id] + end +end diff --git a/app/models/talk.rb b/app/models/talk.rb index 306b47d..42695da 100644 --- a/app/models/talk.rb +++ b/app/models/talk.rb @@ -1,4 +1,9 @@ class Talk < ActiveResource::Base self.site = "https://cfp.openfest.org/api/conferences/2" self.element_name = "event" + + def self.ordered_by_rating + ratings = Ratings.new + all.sort { |left, right| ratings[right.id] <=> ratings[left.id] } + end end diff --git a/app/views/home/_talk.csv.erb b/app/views/home/_talk.csv.erb new file mode 100644 index 0000000..b2a63e8 --- /dev/null +++ b/app/views/home/_talk.csv.erb @@ -0,0 +1 @@ +<%= CSV.generate_line([talk.id, talk.event_type.name, talk.title, talk.subtitle, @ratings[talk.id]]).html_safe -%> diff --git a/app/views/home/ratings.csv.erb b/app/views/home/ratings.csv.erb new file mode 100644 index 0000000..2a8e6ef --- /dev/null +++ b/app/views/home/ratings.csv.erb @@ -0,0 +1,3 @@ +<%- headers = ['ID', 'Type', 'Title', 'Subtitle', 'Votes'] -%> +<%= CSV.generate_line headers -%> +<%= render partial: 'talk', collection: @talks %> diff --git a/config/routes.rb b/config/routes.rb index b7097bb..bcd85b9 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,4 +1,5 @@ Rails.application.routes.draw do resources :talk_preferences, only: [:index, :show, :create, :update] root to: 'home#index' + get 'ratings' => 'home#ratings' end