Skip to content

Commit 86d9004

Browse files
author
Theresa
committed
merged master
2 parents 115cc24 + 58afc29 commit 86d9004

File tree

7 files changed

+145
-8
lines changed

7 files changed

+145
-8
lines changed

app/controllers/exercises_controller.rb

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,15 @@ def exercises_all
8383
end
8484

8585
def push_external
86-
#account_link = AccountLink.find(params[:account_link][:id]);
87-
#oauth2Client = OAuth2::Client.new('client_id', 'client_secret', :site => account_link.push_url)
88-
#oauth2_token = account_link[:oauth2_token]
89-
#token = OAuth2::AccessToken.from_hash(oauth2Client, :access_token => oauth2_token)
90-
#token.post(account_link.push_url)
91-
#redirect_to @exercise, notice: ('Exercise pushed to ' + account_link.readable)
92-
redirect_to @exercise, notice: 'Exercise was successfully exported.'
86+
account_link = AccountLink.find(params[:account_link][:id]);
87+
oauth2Client = OAuth2::Client.new('client_id', 'client_secret', :site => account_link.push_url)
88+
oauth2_token = account_link[:oauth2_token]
89+
token = OAuth2::AccessToken.from_hash(oauth2Client, :access_token => oauth2_token)
90+
logger.fatal('@exercise.to_proforma_xml')
91+
logger.fatal(@exercise.to_proforma_xml)
92+
logger.fatal('@exercise.to_proforma_xml')
93+
token.post(account_link.push_url, {body: @exercise.to_proforma_xml})
94+
redirect_to @exercise, notice: ('Exercise pushed to ' + account_link.readable)
9395
end
9496

9597
private

app/models/exercise.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,20 @@ def add_tests(test_array)
9393
end
9494
end
9595

96+
def build_proforma_xml_for_exercise_file(builder, exercise_file)
97+
if exercise_file.main
98+
proforma_file_class = 'template'
99+
else
100+
proforma_file_class = 'internal'
101+
end
102+
103+
builder.file(exercise_file.content,
104+
'filename' => exercise_file.full_file_name,
105+
'id' => exercise_file.id,
106+
'class' => proforma_file_class
107+
)
108+
end
109+
96110
def to_proforma_xml
97111
builder = Nokogiri::XML::Builder.new do |xml|
98112
xml.root('xmlns:p' => 'urn:proforma:task:v0.9.4') {
@@ -103,6 +117,11 @@ def to_proforma_xml
103117
p.send('meta-data') {
104118
p.title(self.title)
105119
}
120+
p.files {
121+
self.exercise_files.all? { |file|
122+
build_proforma_xml_for_exercise_file(p, file)
123+
}
124+
}
106125
}
107126
}
108127
end

app/models/exercise_file.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
11
class ExerciseFile < ActiveRecord::Base
22
belongs_to :exercise
3+
4+
def full_file_name
5+
"#{self.file_name}.#{self.filetype}"
6+
end
7+
38
end
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class AddFileNameToExerciseFile < ActiveRecord::Migration
2+
def change
3+
add_column :exercise_files, :file_name, :string
4+
end
5+
end

spec/factories/exercise.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,36 @@
44
description 'Very descriptive'
55
maxrating 10
66
end
7+
8+
factory :exercise_with_single_java_main_file, class: 'Exercise' do
9+
title 'Some Exercise'
10+
description 'Very descriptive'
11+
after(:create) do |exercise|
12+
create(:single_java_main_file, exercise: exercise)
13+
end
14+
end
15+
716
end
17+
=begin
18+
category1 = LabelCategory.create(name: 'Languages')
19+
l1 = Label.create(name: 'Java', color: '006600', label_category: category1)
20+
test_framework = TestingFramework.create(name: 'JUnit 4')
21+
22+
ExerciseFile.create(main: true, content: "public class AsteriksPattern{ public static void main String[] args) { } }", path: '', solution: false, filetype: 'java', exercise: exercise3)
23+
24+
Test.create(content: "public class AsteriksPattern {
25+
public static void main(String[] args) {
26+
printAsterisk();
27+
}
28+
static void printAsterisk() {
29+
System.out.println('*****');
30+
System.out.println('*****');
31+
System.out.println('*****');
32+
System.out.println('*****');
33+
System.out.println('*****');
34+
}
35+
}", rating: 5, feedback_message: "Dein Pattern sieht noch nicht wie das Asteriks Pattern aus. Schaue es dir nochmal genauer an!", exercise: exercise3, testing_framework: test_framework)
36+
37+
exercise3.labels << l1
38+
39+
=end

spec/factories/exercise_file.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
FactoryGirl.define do
2+
3+
factory :single_java_main_file, class: 'ExerciseFile' do
4+
main true
5+
content "public class AsteriksPattern{ public static void main String[] args) { } }"
6+
file_name 'Main'
7+
path ''
8+
solution false
9+
filetype 'java'
10+
end
11+
12+
end

spec/models/exercise_spec.rb

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
}
1616

1717
it 'has single <p:description> tag which contains description' do
18-
print(xml)
1918
descriptions = xml.xpath('p:task/p:description/text()')
2019
expect(descriptions.size()).to be 1
2120
expect(descriptions[0].content).to eq 'Very descriptive'
@@ -44,4 +43,67 @@
4443

4544
end
4645

46+
describe 'files' do
47+
let(:xml) {
48+
::Nokogiri::XML(
49+
FactoryGirl.create(:only_meta_data).to_proforma_xml
50+
).xpath('/root')[0]
51+
}
52+
53+
context 'no files' do
54+
55+
it 'contains a single empty <p:files>-tag' do
56+
filesContainer = xml.xpath('p:task/p:files')
57+
expect(filesContainer.size()).to be 1
58+
allFiles = xml.xpath('p:task/*/p:file')
59+
expect(allFiles.size).to be 0
60+
end
61+
62+
end
63+
64+
context 'one Java main file' do
65+
let(:xml) {
66+
::Nokogiri::XML(
67+
FactoryGirl.create(:exercise_with_single_java_main_file).to_proforma_xml
68+
).xpath('/root')[0]
69+
}
70+
71+
it 'has single /p:files/p:file tag' do
72+
files = xml.xpath('p:task/p:files/p:file')
73+
expect(files.size()).to be 1
74+
end
75+
76+
it 'p:file tag has class="template"' do
77+
filesClass = xml.xpath('p:task/p:files/p:file/@class').first
78+
expect(filesClass.value).to eq 'template'
79+
end
80+
81+
it 'has attribute id on <p:file>-tag' do
82+
ids = xml.xpath('p:task/p:files/p:file/@id')
83+
expect(ids.size).to be 1
84+
expect(ids.first.value.size).to be > 0
85+
end
86+
87+
it 'has attribute filename on <p:file>-tag with name and extension' do
88+
file_names = xml.xpath('p:task/p:files/p:file/@filename')
89+
expect(file_names.size).to be 1
90+
expect(file_names.first.value).to eq 'Main.java'
91+
end
92+
93+
it 'has attribute class="template" on <p:file>-tag because it is the main file' do
94+
file_names = xml.xpath('p:task/p:files/p:file/@class')
95+
expect(file_names.size).to be 1
96+
expect(file_names.first.value).to eq 'template'
97+
end
98+
99+
it '<p:file> contains file contents as plain text ' do
100+
file_contents = xml.xpath('p:task/p:files/p:file/text()')
101+
expect(file_contents.size).to be 1
102+
expect(file_contents.first.content).to eq 'public class AsteriksPattern{ public static void main String[] args) { } }'
103+
end
104+
105+
end
106+
107+
end
108+
47109
end

0 commit comments

Comments
 (0)