Skip to content
This repository has been archived by the owner on Aug 11, 2021. It is now read-only.

Commit

Permalink
Merge pull request #36 from gurix/features/session_report
Browse files Browse the repository at this point in the history
Using session reports
  • Loading branch information
gurix committed Jun 5, 2014
2 parents 820a603 + 4378a99 commit 69be514
Show file tree
Hide file tree
Showing 22 changed files with 184 additions and 38 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ spec/dummy/tmp/
spec/dummy/.sass-cache
coverage/
**/*.DS_Store

.DS_Store
4 changes: 3 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
PATH
remote: .
specs:
helena (0.1.0)
helena (0.2.0)
bootstrap-sass
breadcrumbs_on_rails
haml
haml-rails
jquery-rails
liquid
mongoid (~> 4.0.0.beta1)
mongoid-simple-tags
mongoid_orderable
Expand Down Expand Up @@ -120,6 +121,7 @@ GEM
json (1.8.1)
launchy (2.4.2)
addressable (~> 2.3)
liquid (2.6.1)
mail (2.5.4)
mime-types (~> 1.16)
treetop (~> 1.4.8)
Expand Down
1 change: 1 addition & 0 deletions app/assets/stylesheets/helena/admin/versions.css.sass
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@
min-width: 150px

td:last-child
min-width: 150px
text-align: right
40 changes: 30 additions & 10 deletions app/controllers/helena/admin/versions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,29 @@ class VersionsController < Admin::ApplicationController
respond_to :html

before_filter :load_survey, :add_breadcrumbs
before_filter :build_version, only: [:new, :create]
before_filter :load_version, only: [:edit, :update]

def index
@versions = @survey.versions.without_base
end

def create
base_version = @survey.versions.find_by(version: 0)
def new
end

@version = Helena::VersionPublisher.publish(base_version)
@version.notes = version_params['notes']
def create
@version.update_attributes(version_params)
@version.save
notify_successful_create_for(@version.version)
respond_with @version, location: admin_survey_versions_path(@survey)
end

if @version.save
notify_successful_create_for(@version.version)
else
notify_error @version
end
def edit
end

def update
@version.update_attributes version_params
notify_successful_update_for(@version.version)
respond_with @version, location: admin_survey_versions_path(@survey)
end

Expand All @@ -38,13 +44,27 @@ def load_survey
@survey = Helena::Survey.find(params[:survey_id])
end

def load_version
@version = @survey.versions.find(params[:id])
end

def add_breadcrumbs
add_breadcrumb Helena::Survey.model_name.human(count: 2), :admin_surveys_path
add_breadcrumb @survey.name, admin_survey_versions_path(@survey)
end

def version_params
params.require(:version).permit(:notes)
params.require(:version).permit(:notes, :session_report)
end

def default_session_report
render_to_string 'default_session_report', layout: false
end

def build_version
base_version = @survey.versions.find_by(version: 0)
@version = Helena::VersionPublisher.publish(base_version)
@version.session_report = @survey.reload.versions.last.session_report || default_session_report
end
end
end
Expand Down
24 changes: 23 additions & 1 deletion app/controllers/helena/sessions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ class SessionsController < ApplicationController
respond_to :html

before_filter :load_survey
before_filter :load_session, only: [:edit, :update]
before_filter :load_session, only: [:show, :edit, :update]

def show
@template = Liquid::Template.parse(@version.session_report)
render html: @template.render(variable_mapping).html_safe, layout: true
end

def edit
@questions = question_group_questions
Expand Down Expand Up @@ -50,6 +55,10 @@ def session_answers
Hash[@session.answers.map { |answer| [answer.code, answer.value] }]
end

def hashed_session_answers
@session.answers.map { |answer| { code: answer.code, value: answer.value }.deep_stringify_keys }
end

def session_params
if params[:session]
params.require(:session).permit(answers: @question_group.question_codes, question_types: @question_group.question_codes)
Expand Down Expand Up @@ -83,5 +92,18 @@ def answer_errors
end
errors
end

def variable_mapping
variables = {
survey_answers: hashed_session_answers,
survey_title: @version.survey_detail.try(:title),
survey_description: @version.survey_detail.try(:description)
}.deep_stringify_keys

session_answers.each do |answer|
variables[answer.first.to_s] = answer.last
end
variables
end
end
end
9 changes: 3 additions & 6 deletions app/models/helena/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ class Version
include Helena::Concerns::ApplicationModel
include Mongoid::Orderable # Needed, because the embedded objects needs this, see

field :version, type: Integer, default: 0
field :notes, type: String
field :version, type: Integer, default: 0
field :notes, type: String
field :session_report, type: String

embedded_in :survey

Expand All @@ -25,10 +26,6 @@ def question_codes
question_groups.map(&:question_codes).flatten
end

def questions
question_groups.map(&:questions).flatten
end

def question_code_occurences
question_codes.each_with_object(Hash.new(0)) { |word, counts| counts[word] += 1 }
end
Expand Down
9 changes: 6 additions & 3 deletions app/views/helena/admin/sessions/index.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
= session.answers.count

%td
= link_to t('shared.delete'), admin_survey_session_path(@survey, session),
method: :delete, class: 'btn btn-danger', data: { confirm: t('shared.delete_question') }
= link_to t('.edit'), edit_survey_session_path(@survey, session), class: 'btn'
.btn-group
- if session.completed?
= link_to t('shared.show'), survey_session_path(@survey, session), class: 'btn btn-primary'
= link_to t('shared.edit'), edit_survey_session_path(@survey, session), class: 'btn btn-success'
= link_to t('shared.delete'), admin_survey_session_path(@survey, session),
method: :delete, class: 'btn btn-danger', data: { confirm: t('shared.delete_question') }
5 changes: 5 additions & 0 deletions app/views/helena/admin/versions/_form.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
= simple_form_for [:admin, @survey, @version] do |f|
= f.input :notes
= f.input :session_report, as: :text, input_html: { cols: 60, rows: 10 }, hint: t('.session_report_hint')

= f.submit t('shared.save'), class: 'btn btn-success'
17 changes: 17 additions & 0 deletions app/views/helena/admin/versions/default_session_report.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
%h1 {{ survey_title }}
%p {{ survey_description }}
%table
%thead
%tr
%th Code
%th Value
%tbody
{% for answer in survey_answers %}
%tr
%td {{ answer.code }}
%td {{ answer.value }}
{% endfor %}
%tfoot
%tr
%td{ colspan: 2}
{{ survey_answers.size }} answers
3 changes: 3 additions & 0 deletions app/views/helena/admin/versions/edit.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.helena-admin
%h2= t 'shared.edit'
= render 'form'
13 changes: 5 additions & 8 deletions app/views/helena/admin/versions/index.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,9 @@
%td.created_at{ data: { title: Helena::Version.human_attribute_name(:created_at) } }
= time_ago_in_words(version.created_at)
%td
= link_to t('shared.delete'), admin_survey_version_path(@survey, version), method: :delete, class: 'btn btn-danger', data: { confirm: t('shared.delete_question') }

%fieldset
%legend= t '.publish'
= simple_form_for([:admin, @survey, Helena::Version.new]) do |f|
= f.input :notes
%br
= f.submit t('.publish_btn'), class: 'btn btn-success'
.btn-group
= link_to t('shared.edit'), edit_admin_survey_version_path(@survey, version), class: 'btn btn-success'
= link_to t('shared.delete'), admin_survey_version_path(@survey, version), method: :delete, class: 'btn btn-danger', data: { confirm: t('shared.delete_question') }

%a.btn.btn-success{ href: new_admin_survey_version_path(@survey)}
= t 'shared.add'
3 changes: 3 additions & 0 deletions app/views/helena/admin/versions/new.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.helena-admin
%h2= t '.new'
= render 'form'
2 changes: 1 addition & 1 deletion app/views/helena/sessions/edit.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

= hidden_field_tag :question_group, @question_group.id

- if @question_group.previous_items.count == 1
- if @question_group.previous_items.any?
= link_to t('.back'), edit_survey_session_path(@survey, @session, question_group: @question_group.previous_item), class: 'btn'

= f.submit class: 'btn btn-success pull-right', value: @question_group.last? ? t('.save') : t('.next')
Expand Down
1 change: 1 addition & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ en:
save: Save
edit: Edit
add: Add
show: Show
required: '* indicates required fields'
code_hint: The code must consist only of lowercase letters, numbers and underscores. Don't begin with a digit or an underscore and don't end with an underscore.
move_up: Move up
Expand Down
6 changes: 4 additions & 2 deletions config/locales/views/admin/versions/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@ en:
versions:
index:
empty: No versions published at the moment.
publish: Publish changes as new Version...
publish_btn: Publish
new:
new: Publish new version
form:
session_report_hint: 'You can use HTML in combination with liquid markup. Variables: {{title}}, {{description}}, {{answers}} ({{answers[0].code}}, {{answers[0].value}})'
4 changes: 2 additions & 2 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Helena::Engine.routes.draw do
resources :surveys do
resources :sessions, only: [:edit, :update]
resources :sessions, only: [:edit, :update, :show]
end

scope :admin, as: :admin, module: :admin do
Expand All @@ -11,7 +11,7 @@

resources :surveys, concerns: :movable do
resources :sessions, only: [:index, :destroy]
resources :versions, only: [:index, :create, :destroy]
resources :versions
resources :question_groups, concerns: :movable do
resources :questions, concerns: :movable
resource :questions do
Expand Down
6 changes: 6 additions & 0 deletions db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,15 @@ def create_restaurant_survey
generate_sessions(survey, published_version)
end

def default_session_report
haml = File.read(File.dirname(__FILE__) + '/../app/views/helena/admin/versions/default_session_report.html.haml')
Haml::Engine.new(haml).render
end

def publish(version)
published_version = Helena::VersionPublisher.publish(version)
published_version.notes = Faker::Lorem.paragraph(1)
published_version.session_report = default_session_report
published_version.save
published_version
end
Expand Down
1 change: 1 addition & 0 deletions helena.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ survey/test development, longitudinal studies and instant feedback.'
s.add_dependency 'simple_form'
s.add_dependency 'breadcrumbs_on_rails'
s.add_dependency 'rails-i18n'
s.add_dependency 'liquid'

s.add_development_dependency 'rspec-rails'
s.add_development_dependency 'mongoid-rspec'
Expand Down
1 change: 1 addition & 0 deletions lib/helena.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
require 'mongoid'
require 'mongoid_orderable'
require 'mongoid-simple-tags'
require 'liquid'

module Helena
end
2 changes: 1 addition & 1 deletion lib/helena/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Helena
VERSION = '0.1.0'
VERSION = '0.2.0'
end
30 changes: 28 additions & 2 deletions spec/features/helena/admin/manage_version_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,37 @@
end

scenario 'publishing a new version on the base of base version' do
visit helena.admin_survey_versions_path @survey

visit helena.new_admin_survey_version_path @survey

expect(find_field('version_session_report').value).to have_content '{{ survey_title }}'

fill_in 'Notes', with: 'Luke, I am your father!'
fill_in 'version_session_report', with: 'Foo Bar'

expect { click_button 'Save' }.to change { @survey.reload.versions.count }.by(1)

expect(@survey.reload.versions.last.session_report).to eq 'Foo Bar'

visit helena.new_admin_survey_version_path @survey

expect(find_field('version_session_report').value).to have_content 'Foo Bar'
end

scenario 'changing the session report for a version' do
published_version = Helena::VersionPublisher.publish @baseversion
published_version.notes = 'bla bla'
published_version.save

visit helena.edit_admin_survey_version_path(@survey, published_version)

fill_in 'Notes', with: 'Michael Knight, a lone crusader in a dangerous world. The world of the Knight Rider.'
fill_in 'version_session_report', with: 'A Man, a Car'

click_button 'Save'

expect { click_button 'Publish' }.to change { @survey.reload.versions.count }.by(1)
expect(published_version.reload.notes).to eq 'Michael Knight, a lone crusader in a dangerous world. The world of the Knight Rider.'
expect(published_version.reload.session_report).to eq 'A Man, a Car'
end

scenario 'deletes a version' do
Expand Down
Loading

0 comments on commit 69be514

Please sign in to comment.