From db36fa09c6a87c43323c5d2b151c77e3af8578df Mon Sep 17 00:00:00 2001 From: Petko Bordjukov Date: Tue, 13 Oct 2015 14:34:45 +0300 Subject: [PATCH] Add export functionality --- app/controllers/home_controller.rb | 5 +++++ app/models/talk_preference.rb | 4 ++++ app/views/home/_talk_preference.csv.erb | 1 + app/views/home/export.csv.erb | 3 +++ config/routes.rb | 1 + 5 files changed, 14 insertions(+) create mode 100644 app/views/home/_talk_preference.csv.erb create mode 100644 app/views/home/export.csv.erb diff --git a/app/controllers/home_controller.rb b/app/controllers/home_controller.rb index 9073dca..1485932 100644 --- a/app/controllers/home_controller.rb +++ b/app/controllers/home_controller.rb @@ -6,4 +6,9 @@ class HomeController < ApplicationController @talks = Talk.ordered_by_rating @ratings = Ratings.new end + + def export + @talks = Talk.all + @talk_preferences = TalkPreference.all + end end diff --git a/app/models/talk_preference.rb b/app/models/talk_preference.rb index 2797c48..523424c 100644 --- a/app/models/talk_preference.rb +++ b/app/models/talk_preference.rb @@ -5,6 +5,10 @@ class TalkPreference < ActiveRecord::Base before_create :assign_new_unique_id + def include?(id) + talks.include? id.to_s + end + private def assign_new_unique_id diff --git a/app/views/home/_talk_preference.csv.erb b/app/views/home/_talk_preference.csv.erb new file mode 100644 index 0000000..961f575 --- /dev/null +++ b/app/views/home/_talk_preference.csv.erb @@ -0,0 +1 @@ +<%= CSV.generate_line([talk_preference.hashed_unique_id, talk_preference.created_at] + @talks.map(&:id).map { |talk| talk_preference.include? talk }).html_safe -%> diff --git a/app/views/home/export.csv.erb b/app/views/home/export.csv.erb new file mode 100644 index 0000000..ee7483a --- /dev/null +++ b/app/views/home/export.csv.erb @@ -0,0 +1,3 @@ +<%- headers = ['ID', 'Created at'] + @talks.map(&:id) -%> +<%= CSV.generate_line headers -%> +<%= render partial: 'talk_preference', collection: @talk_preferences %> diff --git a/config/routes.rb b/config/routes.rb index bcd85b9..1a04cd5 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -2,4 +2,5 @@ Rails.application.routes.draw do resources :talk_preferences, only: [:index, :show, :create, :update] root to: 'home#index' get 'ratings' => 'home#ratings' + get 'export' => 'home#export' end