Rudimentary export

This commit is contained in:
Petko Bordjukov 2015-10-06 21:37:53 +03:00
parent 5cc4a043aa
commit 420a42bdb1
7 changed files with 33 additions and 0 deletions

View File

@ -1,3 +1,5 @@
require 'csv'
class ApplicationController < ActionController::Base class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception. # Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead. # For APIs, you may want to use :null_session instead.

View File

@ -1,4 +1,9 @@
class HomeController < ApplicationController class HomeController < ApplicationController
def index def index
end end
def ratings
@talks = Talk.ordered_by_rating
@ratings = Ratings.new
end
end end

16
app/models/ratings.rb Normal file
View File

@ -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

View File

@ -1,4 +1,9 @@
class Talk < ActiveResource::Base class Talk < ActiveResource::Base
self.site = "https://cfp.openfest.org/api/conferences/2" self.site = "https://cfp.openfest.org/api/conferences/2"
self.element_name = "event" self.element_name = "event"
def self.ordered_by_rating
ratings = Ratings.new
all.sort { |left, right| ratings[right.id] <=> ratings[left.id] }
end
end end

View File

@ -0,0 +1 @@
<%= CSV.generate_line([talk.id, talk.event_type.name, talk.title, talk.subtitle, @ratings[talk.id]]).html_safe -%>

View File

@ -0,0 +1,3 @@
<%- headers = ['ID', 'Type', 'Title', 'Subtitle', 'Votes'] -%>
<%= CSV.generate_line headers -%>
<%= render partial: 'talk', collection: @talks %>

View File

@ -1,4 +1,5 @@
Rails.application.routes.draw do Rails.application.routes.draw do
resources :talk_preferences, only: [:index, :show, :create, :update] resources :talk_preferences, only: [:index, :show, :create, :update]
root to: 'home#index' root to: 'home#index'
get 'ratings' => 'home#ratings'
end end