Skip to content

Commit 608d281

Browse files
committed
Merge pull request #48 from Dantemss/publish_all_task
Added rake task to publish all unpublished objects in the database
2 parents b4f9569 + 7f4af7f commit 608d281

File tree

10 files changed

+123
-18
lines changed

10 files changed

+123
-18
lines changed

app/routines/exercises/import/excel.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
# Imports an Excel file
2-
# Arguments are, in order:
3-
# filename, author's user id, copyright holder's user id, skip_first_row
42

53
# Reads xls format
64
require 'spreadsheet'

app/routines/exercises/import/zip.rb

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
# Imports a zip file
2-
# The zip file should an Excel spreadsheet and, optionally, image files
3-
# Arguments are, in order:
4-
# zip_filename, excel_filename (in the zip file),
5-
# author's user id, copyright holder's user id, skip_first_row
2+
# The zip file should contain an Excel spreadsheet and, optionally, image files
63

74
module Exercises
85
module Import
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Publishes all unpublished exercises and solutions
2+
3+
module Publishables
4+
module Publish
5+
class All
6+
7+
lev_routine
8+
9+
protected
10+
11+
def exec
12+
publishables = Publication.where(published_at: nil).to_a
13+
Rails.logger.info "Publishing #{publishables.length} objects"
14+
15+
publishables.each do |p|
16+
p.publish
17+
p.save!
18+
end
19+
end
20+
end
21+
end
22+
end

lib/tasks/exercises_import.rake renamed to lib/tasks/exercises/import.rake

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
1-
# Imports unicode tab-delimited txt file saved from Excel
2-
# Arguments are, in order:
3-
# filename, author's user id, copyright holder's user id,
4-
# skip_first_row, column separator and row separator
5-
# Example: rake exercises:import:unicode[exercises.txt,1,2]
6-
# will import exercises from exercises.txt and
7-
# assign the user with ID 1 as the author and
8-
# solution author, and the user with ID 2 as the CR holder
9-
101
namespace :exercises do
112
namespace :import do
12-
3+
# Imports exercises from a spreadsheet
4+
# Arguments are, in order:
5+
# filename, author's user id, copyright holder's user id, skip_first_row
6+
# Example: rake exercises:import:excel[exercises.xlsx,1,2]
7+
# will import exercises from exercises.xlsx and
8+
# assign the user with ID 1 as the author and
9+
# solution author, and the user with ID 2 as the CR holder
1310
desc "import an Excel file"
1411
task :excel, [:filename, :author_id, :ch_id, :skip_first_row] => :environment do |t, args|
1512
# Output import logging info to the console (except in the test environment)
@@ -24,6 +21,13 @@ namespace :exercises do
2421
end
2522
end
2623

24+
# Imports exercises from a zip file
25+
# Arguments are, in order:
26+
# zip_filename, excel_filename, author's user id, copyright holder's user id, skip_first_row
27+
# Example: rake exercises:import:zip[exercises.zip,exercises.xlsx,1,2]
28+
# will import exercises from exercises.xlsx (within exercises.zip) and
29+
# assign the user with ID 1 as the author and
30+
# solution author, and the user with ID 2 as the CR holder
2731
desc "import a zip file"
2832
task :zip, [:zip_filename, :excel_filename,
2933
:author_id, :ch_id, :skip_first_row] => :environment do |t, args|
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
namespace :publishables do
2+
namespace :publish do
3+
4+
# Publishes all unpublished exercises and solutions
5+
# Takes no arguments
6+
# Example: rake publishables:publish:all
7+
# will publish all unpublished exercises and solutions
8+
desc "publishes all unpublished exercises and solutions"
9+
task :all => :environment do |t, args|
10+
# Output import logging info to the console (except in the test environment)
11+
original_logger = Rails.logger
12+
begin
13+
Rails.logger = ActiveSupport::Logger.new(STDOUT) unless Rails.env.test?
14+
15+
Publishables::Publish::All.call
16+
ensure
17+
# Restore original logger
18+
Rails.logger = original_logger
19+
end
20+
end
21+
22+
end
23+
end

spec/factories/exercises.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
trait :published do
2222
after(:build) do |exercise|
23-
exercise.publication.published_at = Time.now
23+
exercise.publication.publish
2424
end
2525
end
2626
end

spec/factories/solutions.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,15 @@
33
question
44
solution_type SolutionType::EXAMPLE
55
content "Worked example!"
6+
7+
after(:build) do |solution, evaluator|
8+
solution.publication ||= build(:publication, publishable: solution)
9+
end
10+
11+
trait :published do
12+
after(:build) do |solution|
13+
solution.publication.publish
14+
end
15+
end
616
end
717
end

spec/lib/tasks/exercises_import_rake_spec.rb renamed to spec/lib/tasks/exercises/import_rake_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
describe 'exercises import' do
55
before :all do
6-
Rake.application.rake_require "tasks/exercises_import"
6+
Rake.application.rake_require "tasks/exercises/import"
77
Rake::Task.define_task(:environment)
88
end
99

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
require 'rails_helper'
2+
require 'rake'
3+
4+
describe 'publishables publish' do
5+
before :all do
6+
Rake.application.rake_require "tasks/publishables/publish"
7+
Rake::Task.define_task(:environment)
8+
end
9+
10+
context 'all' do
11+
let :run_rake_task do
12+
Rake::Task["publishables:publish:all"].reenable
13+
Rake.application.invoke_task "publishables:publish:all"
14+
end
15+
16+
it 'calls Publishables::Publish::All' do
17+
expect(Publishables::Publish::All).to receive(:call)
18+
run_rake_task
19+
end
20+
end
21+
end
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
require 'rails_helper'
2+
3+
module Publishables::Publish
4+
RSpec.describe All do
5+
let!(:published_exercise) { FactoryGirl.create :exercise, :published }
6+
let!(:published_solution) { FactoryGirl.create :solution, :published }
7+
8+
let!(:exercise_1) { FactoryGirl.create :exercise }
9+
let!(:exercise_2) { FactoryGirl.create :exercise }
10+
11+
let!(:solution_1) { FactoryGirl.create :solution }
12+
let!(:solution_2) { FactoryGirl.create :solution }
13+
14+
it 'publishes all unpublished publishables' do
15+
exercise_published_at = published_exercise.published_at
16+
solution_published_at = published_solution.published_at
17+
18+
Publishables::Publish::All.call
19+
20+
expect(published_exercise.published_at).to eq (exercise_published_at)
21+
expect(published_solution.published_at).to eq (solution_published_at)
22+
23+
expect(exercise_1.reload.is_published?).to eq true
24+
expect(exercise_2.reload.is_published?).to eq true
25+
26+
expect(solution_1.reload.is_published?).to eq true
27+
expect(solution_2.reload.is_published?).to eq true
28+
end
29+
end
30+
end

0 commit comments

Comments
 (0)