Skip to content

Commit

Permalink
add conference exports model to conference
Browse files Browse the repository at this point in the history
  • Loading branch information
manno committed Mar 31, 2014
1 parent 9e07949 commit bb944d8
Show file tree
Hide file tree
Showing 13 changed files with 171 additions and 25 deletions.
8 changes: 4 additions & 4 deletions app/controllers/schedule_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,15 @@ def static_export
authorize! :read, @conference

StaticProgramExportJob.new.async.perform @conference, check_conference_locale(params[:export_locale])
redirect_to schedule_path, notice: 'Static schedule export started. Please reload this page after a minute.'
redirect_to schedule_static_export_path, notice: 'Static schedule export started. Please reload this page after a minute.'
end

def download_static_export
authorize! :read, @conference

out_path = StaticProgramExport.filename @conference, check_conference_locale(params[:export_locale])
if File.readable? out_path
send_file out_path, type: "application/x-tar-gz"
conference_export = @conference.conference_export(check_conference_locale(params[:export_locale]))
if conference_export.present? and File.readable? conference_export.tarball.path
send_file conference_export.tarball.path, type: "application/x-tar-gz"
else
redirect_to schedule_path, notice: 'No export found to download.'
end
Expand Down
8 changes: 6 additions & 2 deletions app/jobs/static_program_export_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@ class StaticProgramExportJob
require "static_program_export"
include SuckerPunch::Job


def perform(conference, locale='en')
ENV['CONFERENCE'] = conference.acronym
ENV['CONFERENCE_LOCALE'] = locale
ENV['RAILS_ENV'] = Rails.env
`rake frab:static_program_export`

exporter = StaticProgramExport.new(conference, locale)
exporter.create_tarball
file = exporter.create_tarball

conference_export = ConferenceExport.where(conference_id: conference.id, locale: locale).first_or_create
conference_export.update_attributes tarball: File.open(file)
end

end
Expand Down
5 changes: 5 additions & 0 deletions app/models/conference.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class Conference < ActiveRecord::Base
has_many :languages, as: :attachable, dependent: :destroy
has_many :rooms, dependent: :destroy
has_many :tracks, dependent: :destroy
has_many :conference_exports, dependent: :destroy
has_one :call_for_papers, dependent: :destroy
has_one :ticket_server, dependent: :destroy

Expand Down Expand Up @@ -99,6 +100,10 @@ def export_url
"/#{EXPORT_PATH}/#{self.acronym}"
end

def conference_export(locale='en')
ConferenceExport.where(conference_id: self.id, locale: locale).try(:first)
end

def language_breakdown(accepted_only = false)
result = Array.new
if accepted_only
Expand Down
8 changes: 8 additions & 0 deletions app/models/conference_export.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class ConferenceExport < ActiveRecord::Base
belongs_to :conference
attr_accessible :locale, :tarball
has_attached_file :tarball
validates_attachment_content_type :tarball, content_type: [/gzip/]
validates_presence_of :locale, :conference

end
6 changes: 3 additions & 3 deletions app/views/schedule/html_exports.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@
%th Download static schedule export
%tbody
- @conference.language_codes.each do |locale|
- filename = StaticProgramExport.filename(@conference, locale)
- conference_export = @conference.conference_export(locale)
%tr
%td
= action_button '', "create #{locale}", schedule_static_export_path(export_locale: locale), :hint => "Create a static HTML export of your program schedule."
%td
- if File.readable? filename
- date = l(File.ctime(filename), :format => :pretty_datetime)
- if conference_export.present? and File.readable? conference_export.tarball.path
- date = l(Time.at(conference_export.tarball.updated_at), :format => :pretty_datetime)
= link_to "#{date} - #{locale}", schedule_download_static_export_path(export_locale: locale)

14 changes: 14 additions & 0 deletions db/migrate/20140329001424_create_conference_exports.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class CreateConferenceExports < ActiveRecord::Migration
def change
create_table :conference_exports do |t|
t.string :locale
t.references :conference
t.string :tarball_file_name
t.string :tarball_content_type
t.integer :tarball_file_size
t.datetime :tarball_updated_at
t.timestamps
end
add_index :conference_exports, :conference_id
end
end
15 changes: 14 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.

ActiveRecord::Schema.define(:version => 20140322223731) do
ActiveRecord::Schema.define(:version => 20140329001424) do

create_table "availabilities", :force => true do |t|
t.integer "person_id"
Expand Down Expand Up @@ -40,6 +40,19 @@

add_index "call_for_papers", ["start_date", "end_date"], :name => "index_call_for_papers_on_dates"

create_table "conference_exports", :force => true do |t|
t.string "locale"
t.integer "conference_id"
t.string "tarball_file_name"
t.string "tarball_content_type"
t.integer "tarball_file_size"
t.datetime "tarball_updated_at"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end

add_index "conference_exports", ["conference_id"], :name => "index_conference_exports_on_conference_id"

create_table "conference_users", :force => true do |t|
t.string "role"
t.integer "user_id"
Expand Down
42 changes: 27 additions & 15 deletions lib/static_program_export.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,29 @@ class StaticProgramExport
def initialize(conference, locale = 'en')
@conference = conference
@locale = locale

@asset_paths = []
@base_directory = EXPORT_PATH.join(@conference.acronym)
@base_url = URI.parse(@conference.program_export_base_url).path
@base_url += '/' unless @base_url.end_with?('/')
@original_schedule_public = @conference.schedule_public

@session = ActionDispatch::Integration::Session.new(Frab::Application)
@session.host = Settings.host
@session.https! if Settings['protocol'] == "https"
end

def self.filename(conference, locale = 'en')
EXPORT_PATH.join("#{conference.acronym}-#{locale}.tar.gz")
end

def create_tarball
out_file = StaticProgramExport.filename(@conference, @locale)
out_file = filename(@conference, @locale)
if File.exist? out_file
File.unlink out_file
end
system( 'tar', *['-cpz', '-f', out_file.to_s, '-C', EXPORT_PATH.to_s, @conference.acronym].flatten )
out_file.to_s
end

# only run by rake task, cannot run in the same thread as rails
def run_export
fail "No conference found!" if @conference.nil?

@asset_paths = []
@base_directory = EXPORT_PATH.join(@conference.acronym)
@base_url = get_base_url
@original_schedule_public = @conference.schedule_public

@session = ActionDispatch::Integration::Session.new(Frab::Application)
@session.host = Settings.host
@session.https! if Settings['protocol'] == "https"
ActiveRecord::Base.transaction do
unlock_schedule unless @original_schedule_public

Expand All @@ -44,6 +42,20 @@ def run_export

private

def get_base_url
if @conference.program_export_base_url.present?
base_url = URI.parse(@conference.program_export_base_url).path
base_url += '/' unless @base_url.end_with?('/')
base_url
else
"/"
end
end

def filename(conference, locale = 'en')
EXPORT_PATH.join("#{@conference.acronym}-#{@locale}.tar.gz")
end

def setup_directories
FileUtils.rm_r(@base_directory, secure: true) if File.exist? @base_directory
FileUtils.mkdir_p(@base_directory)
Expand Down
6 changes: 6 additions & 0 deletions test/factories.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,12 @@
factory :conference_reviewer, traits: [:conference_reviewer_role]
end

factory :conference_export do
conference
locale 'en'
tarball { File.open(File.join(Rails.root, 'test', 'fixtures', 'tarball.tar.gz')) }
end

factory :person do
email { Factory.next(:email) }
public_name "Fred Besen"
Expand Down
Binary file added test/fixtures/tarball.tar.gz
Binary file not shown.
26 changes: 26 additions & 0 deletions test/integration/static_program_export_task_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
require 'test_helper'

class StaticProgramExportTask < ActionDispatch::IntegrationTest

setup do
@conference = create(:three_day_conference)
@target_dir = File.join(Rails.root, 'tmp', 'static_export', @conference.acronym)
end

test "can run program export task" do
locale = 'en'

load File.join(Rails.root, 'lib', 'tasks', 'static_program_export.rake')
Rake::Task.define_task(:environment)
ENV['CONFERENCE'] = @conference.acronym
ENV['CONFERENCE_LOCALE'] = locale
ENV['RAILS_ENV'] = Rails.env
Rake.application.invoke_task "frab:static_program_export"

assert File.directory? @target_dir
end

teardown do
FileUtils.remove_dir @target_dir
end
end
33 changes: 33 additions & 0 deletions test/unit/conference_export_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
require 'test_helper'

class ConferenceExportTest < ActiveSupport::TestCase

test "can create a conference export" do
conference_export = FactoryGirl.create :conference_export
assert_not_nil conference_export.tarball
assert File.readable? conference_export.tarball.path
assert_not_nil conference_export.conference
assert_not_nil conference_export.id
end

test "can update a conference export attachment" do
file = File.open(File.join(Rails.root, 'test', 'fixtures', 'tarball.tar.gz'))
conference_export = FactoryGirl.create :conference_export, tarball: file
conference_export.save
assert File.readable? conference_export.tarball.path
end

test "can update conference export with tarball" do
conference = FactoryGirl.create(:three_day_conference)
locale = 'en'
file = File.join(Rails.root, 'test', 'fixtures', 'tarball.tar.gz')

assert_difference 'ConferenceExport.count' do
conference_export = ConferenceExport.where(conference_id: conference.id, locale: locale).first_or_create
conference_export.update_attributes tarball: File.open(file)
end
conference_export = ConferenceExport.where(conference_id: conference.id, locale: locale).first
assert_not_nil conference_export.tarball
end

end
25 changes: 25 additions & 0 deletions test/unit/static_program_export_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require 'test_helper'

class StaticProgramExportTest < ActiveSupport::TestCase
setup do
@conference = create(:three_day_conference)
@locale = 'en'
@target_dir = File.join(Rails.root, 'tmp', 'static_export', @conference.acronym)
end

test "static exporter can create a tarball" do
exporter = StaticProgramExport.new(@conference, @locale)
FileUtils.mkdir_p File.join(StaticProgramExport::EXPORT_PATH, @conference.acronym)
assert_equal exporter.create_tarball, File.join(Rails.root, 'tmp', 'static_export', @conference.acronym + '-en.tar.gz')
end

test "static exporter can run export" do
exporter = StaticProgramExport.new(@conference, @locale)
exporter.run_export
assert File.directory? @target_dir
end

teardown do
FileUtils.remove_dir @target_dir
end
end

0 comments on commit bb944d8

Please sign in to comment.