Skip to content

Commit 5ef0ebe

Browse files
committed
Merge branch 'master' into 29_cart_and_collections
2 parents c43eb34 + 58afc29 commit 5ef0ebe

File tree

8 files changed

+147
-9
lines changed

8 files changed

+147
-9
lines changed

app/controllers/exercises_controller.rb

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,15 @@ def exercises_all
9292
end
9393

9494
def push_external
95-
#account_link = AccountLink.find(params[:account_link][:id]);
96-
#oauth2Client = OAuth2::Client.new('client_id', 'client_secret', :site => account_link.push_url)
97-
#oauth2_token = account_link[:oauth2_token]
98-
#token = OAuth2::AccessToken.from_hash(oauth2Client, :access_token => oauth2_token)
99-
#token.post(account_link.push_url)
100-
#redirect_to @exercise, notice: ('Exercise pushed to ' + account_link.readable)
101-
redirect_to @exercise, notice: 'Exercise was successfully exported.'
95+
account_link = AccountLink.find(params[:account_link][:id]);
96+
oauth2Client = OAuth2::Client.new('client_id', 'client_secret', :site => account_link.push_url)
97+
oauth2_token = account_link[:oauth2_token]
98+
token = OAuth2::AccessToken.from_hash(oauth2Client, :access_token => oauth2_token)
99+
logger.fatal('@exercise.to_proforma_xml')
100+
logger.fatal(@exercise.to_proforma_xml)
101+
logger.fatal('@exercise.to_proforma_xml')
102+
token.post(account_link.push_url, {body: @exercise.to_proforma_xml})
103+
redirect_to @exercise, notice: ('Exercise pushed to ' + account_link.readable)
102104
end
103105

104106
private

app/models/exercise.rb

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

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

db/schema.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#
1212
# It's strongly recommended that you check this file into your version control system.
1313

14-
ActiveRecord::Schema.define(version: 20160212110642) do
14+
ActiveRecord::Schema.define(version: 20160213122258) do
1515

1616
create_table "account_links", force: :cascade do |t|
1717
t.datetime "created_at", null: false
@@ -109,6 +109,7 @@
109109
t.integer "exercise_id"
110110
t.datetime "created_at", null: false
111111
t.datetime "updated_at", null: false
112+
t.string "file_name"
112113
end
113114

114115
add_index "exercise_files", ["exercise_id"], name: "index_exercise_files_on_exercise_id"

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)