Skip to content

Commit

Permalink
implemented odt export for cv, refs #21518
Browse files Browse the repository at this point in the history
  • Loading branch information
bihorco36 committed Nov 17, 2016
1 parent 7c54914 commit f9cebb9
Show file tree
Hide file tree
Showing 10 changed files with 193 additions and 7 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ gem 'annotate'
gem 'devise'
gem 'devise_token_auth'
gem 'faker'
gem 'odf-report'
gem 'mysql2'
gem 'net-ldap', '~> 0.14.0'
gem 'omniauth'
Expand Down
7 changes: 6 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ GEM
nio4r (1.2.1)
nokogiri (1.6.8.1)
mini_portile2 (~> 2.1.0)
odf-report (0.5.1)
nokogiri (>= 1.5.0)
rubyzip (~> 1.1.0)
omniauth (1.3.1)
hashie (>= 1.2, < 4)
rack (>= 1.0, < 3)
Expand Down Expand Up @@ -182,6 +185,7 @@ GEM
unicode-display_width (~> 1.0, >= 1.0.1)
ruby-progressbar (1.8.1)
rubyntlm (0.3.4)
rubyzip (1.1.7)
seed-fu (2.3.6)
activerecord (>= 3.1)
activesupport (>= 3.1)
Expand Down Expand Up @@ -232,6 +236,7 @@ DEPENDENCIES
listen (~> 3.0.5)
mysql2
net-ldap (~> 0.14.0)
odf-report
omniauth
omniauth-ldap
pg (= 0.19.0.pre20160409114042)
Expand All @@ -250,4 +255,4 @@ DEPENDENCIES
tzinfo-data

BUNDLED WITH
1.13.5
1.13.6
22 changes: 22 additions & 0 deletions app/controllers/people_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,26 @@ class PeopleController < CrudController
def index
super(render_options: { include: [:status] })
end

def show
format_odt? ? export : super
end

private

def export
person = Person.find(params[:id])
odt_file = person.export
send_data odt_file.generate, type: 'application/vnd.oasis.opendocument.text',
disposition: 'attachment',
filename: filename(person.name)
end

def filename(name)
"#{name.downcase.tr(' ', '_')}_cv.odt"
end

def format_odt?
response.request.filtered_parameters['format'] == 'odt'
end
end
2 changes: 2 additions & 0 deletions app/models/person.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
#

class Person < ApplicationRecord
include Export

has_many :projects, dependent: :destroy
has_many :activities, dependent: :destroy
has_many :advanced_trainings, dependent: :destroy
Expand Down
106 changes: 106 additions & 0 deletions app/models/person/export.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
module Person::Export
def export
ODFReport::Report.new('lib/templates/cv_template.odt') do |r|
insert_general_sections(r)
insert_personalien(r)
insert_competences(r)
insert_advanced_trainings(r)
insert_educations(r)
insert_activities(r)
insert_projects(r)
end
end

private

def insert_general_sections(r)
r.add_field(:client, 'mg')
r.add_field(:project, 'pcv')
r.add_field(:section, 'dev1')
r.add_field(:name, name)
r.add_field(:title_function, role)

r.add_field(:header_info, "#{name} - Version 1.0")

r.add_field(:date, Date.today.strftime('%d.%m.%Y'))
r.add_field(:version, '1.0')
r.add_field(:comment, 'Aktuelle Ausgabe')
end

def insert_personalien(r)
r.add_field(:title, title)
r.add_field(:birthdate, Date.parse(birthdate.to_s).strftime('%d.%m.%Y'))
r.add_field(:origin, origin)
r.add_field(:language, language)
end

def insert_competences(r)
bullet = "\u2022"
competences_string = ''
competences.each do |c|
competences_string << "#{bullet} #{c.description}\n"
end
r.add_field(:competences, competences_string)
end

def insert_educations(r)
educations_list = educations.collect do |e|
{ year: formatted_year(e), title: e.title }
end

r.add_table('EDUCATIONS', educations_list, header: true) do |t|
t.add_column(:year, :year)
t.add_column(:education, :title)
end
end

def insert_advanced_trainings(r)
advanced_trainings_list = advanced_trainings.collect do |at|
{ year: formatted_year(at), description: at.description }
end

r.add_table('ADVANCED_TRAININGS', advanced_trainings_list, header: true) do |t|
t.add_column(:year, :year)
t.add_column(:advanced_training, :description)
end
end

def insert_activities(r)
activities_list = activities.collect do |a|
{ year: formatted_year(a), description: a.description }
end

r.add_table('ACTIVITIES', activities_list, header: true) do |t|
t.add_column(:year, :year)
t.add_column(:activity, :description)
end
end

def insert_projects(r)
projects_list = projects.collect do |p|
{ year: formatted_year(p), project: project_description(p)}
end

r.add_table('PROJECTS', projects_list, header: true) do |t|
t.add_column(:year, :year)
t.add_column(:project_description, :project)
end
end

def project_description(p)
str = ''
str << "#{p.title}\n\n"
str << "#{p.description}\n\n"
str << "#{p.role}\n\n"
str << "#{p.technology}"
end

def formatted_year(obj)
if(obj.year_from == obj.year_to)
obj.year_to
else
"#{obj.year_from} - #{obj.year_to}"
end
end

end
Binary file added lib/templates/cv_template (copy).zip
Binary file not shown.
Binary file added lib/templates/cv_template.odt
Binary file not shown.
29 changes: 29 additions & 0 deletions spec/controllers/people_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,35 @@
require 'rails_helper'

describe PeopleController do
describe 'Export person as odt' do
it 'returns bob' do
bob = people(:bob)

expect_any_instance_of(Person)
.to receive(:export)
.exactly(1).times
.and_call_original

expect_any_instance_of(ODFReport::Report)
.to receive(:add_field)
.exactly(14).times
.and_call_original

expect_any_instance_of(ODFReport::Report)
.to receive(:add_table)
.exactly(4).times
.and_call_original

process :show, method: :get, format: 'odt', params: { id: bob.id }
end

it 'check filename' do
process :show, method: :get, format: 'odt', params: { id: people(:bob).id }
expect(@response['Content-Disposition']).to match(
/filename="bob_anderson_cv.odt"/)
end
end

describe 'GET index' do
it 'returns all people without nested models' do
keys = %w(id birthdate profile_picture language location martial_status
Expand Down
12 changes: 6 additions & 6 deletions spec/fixtures/advanced_trainings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
course:
description: course about how to clean
updated_at: 2016-10-20 13:16:37
updated_by: MyString
updated_by: bob
created_at: 2016-10-20 13:16:37
year_from:
year_to:
year_from: 2010
year_to: 2012
person: bob

education:
description: was nice
updated_at: 2016-10-20 13:16:37
updated_by: MyString
updated_by: alice
created_at: 2016-10-20 13:16:37
year_from:
year_to:
year_from: 2011
year_to: 2015
person: alice
21 changes: 21 additions & 0 deletions spec/models/person_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,27 @@
describe Person do
fixtures :people

context 'fomatting' do
it 'just one year if year from and to are the same' do
bob = people(:bob)
activity = bob.activities.first
activity.update_attributes(year_to: 2000)

formatted_year = bob.send(:formatted_year, activity)

expect(formatted_year).to eq(2000)
end

it 'returns year from and to are not the same' do
bob = people(:bob)
activity = bob.activities.first

formatted_year = bob.send(:formatted_year, activity)

expect(formatted_year).to eq('2000 - 2010')
end
end

context 'variations' do
before do
@bob = people(:bob)
Expand Down

0 comments on commit f9cebb9

Please sign in to comment.