Module: Surveyor::SurveyorControllerMethods

Extended by:
ActiveSupport::Concern
Included in:
SurveyorController
Defined in:
lib/surveyor/surveyor_controller_methods.rb

Instance Method Summary collapse

Instance Method Details

#createObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/surveyor/surveyor_controller_methods.rb', line 23

def create
  surveys = Survey.where(:access_code => params[:survey_code]).order("survey_version DESC")
  if params[:survey_version].blank?
    @survey = surveys.first
  else
    @survey = surveys.where(:survey_version => params[:survey_version]).first
  end
  @response_set = ResponseSet.
    create(:survey => @survey, :user_id => (@current_user.nil? ? @current_user : @current_user.id))
  if (@survey && @response_set)
    flash[:notice] = t('surveyor.survey_started_success')
    redirect_to(surveyor.edit_my_survey_path(
      :survey_code => @survey.access_code, :response_set_code  => @response_set.access_code))
  else
    flash[:notice] = t('surveyor.Unable_to_find_that_survey')
    redirect_to surveyor_index
  end
end

#editObject



60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/surveyor/surveyor_controller_methods.rb', line 60

def edit
  # @response_set is set in before_action - set_response_set_and_render_context
  if @response_set
    @sections = SurveySection.where(survey_id: @response_set.survey_id).includes([:survey, {questions: [{answers: :question}, {question_group: :dependency}, :dependency]}])
    @section = (section_id_from(params) ? @sections.where(id: section_id_from(params)).first : @sections.first) || @sections.first
    @survey = @section.survey
    set_dependents
  else
    flash[:notice] = t('surveyor.unable_to_find_your_responses')
    redirect_to surveyor_index
  end
end

#exportObject



131
132
133
134
135
136
# File 'lib/surveyor/surveyor_controller_methods.rb', line 131

def export
  surveys = Survey.where(:access_code => params[:survey_code]).order("survey_version DESC")
  s = params[:survey_version].blank? ? surveys.first : surveys.where(:survey_version => params[:survey_version]).first
  render_404 and return if s.blank?
  @survey = s.filtered_for_json
end

#load_and_update_response_set_with_retries(remaining = 2) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
# File 'lib/surveyor/surveyor_controller_methods.rb', line 99

def load_and_update_response_set_with_retries(remaining=2)
  begin
    load_and_update_response_set
  rescue ActiveRecord::StatementInvalid => e
    if remaining > 0
      load_and_update_response_set_with_retries(remaining - 1)
    else
      raise e
    end
  end
end

#newObject

Actions



18
19
20
21
# File 'lib/surveyor/surveyor_controller_methods.rb', line 18

def new
  @surveys_by_access_code = Survey.order("created_at DESC, survey_version DESC").to_a.group_by(&:access_code)
  redirect_to surveyor_index unless surveyor_index == surveyor.available_surveys_path
end

#render_404Object



138
139
140
141
# File 'lib/surveyor/surveyor_controller_methods.rb', line 138

def render_404
  head :status => 404
  true
end

#showObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/surveyor/surveyor_controller_methods.rb', line 42

def show
  # @response_set is set in before_action - set_response_set_and_render_context
  if @response_set
    @survey = @response_set.survey
    respond_to do |format|
      format.html #{render :action => :show}
      format.csv {
        send_data(@response_set.to_csv, :type => 'text/csv; charset=utf-8; header=present',
          :filename => "#{@response_set.updated_at.strftime('%Y-%m-%d')}_#{@response_set.access_code}.csv")
      }
      format.json
    end
  else
    flash[:notice] = t('surveyor.unable_to_find_your_responses')
    redirect_to surveyor_index
  end
end

#updateObject



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/surveyor/surveyor_controller_methods.rb', line 73

def update
  question_ids_for_dependencies = (params[:r] || []).map{|k,v| v["question_id"] }.compact.uniq
  saved = load_and_update_response_set_with_retries

  return redirect_with_message(surveyor_finish, :notice, t('surveyor.completed_survey')) if saved && params[:finish]

  respond_to do |format|
    format.html do
      if @response_set.nil?
        return redirect_with_message(surveyor.available_surveys_path, :notice, t('surveyor.unable_to_find_your_responses'))
      else
        flash[:notice] = t('surveyor.unable_to_update_survey') unless saved
        redirect_to surveyor.edit_my_survey_path(:anchor => anchor_from(params[:section]), :section => section_id_from(params))
      end
    end
    format.js do
      if @response_set
        render :json => @response_set.reload.all_dependencies(question_ids_for_dependencies)
      else
        render :text => "No response set #{params[:response_set_code]}",
          :status => 404
      end
    end
  end
end

#url_optionsObject



143
144
145
# File 'lib/surveyor/surveyor_controller_methods.rb', line 143

def url_options
  ((I18n.locale == I18n.default_locale) ? {} : {:locale => I18n.locale}).merge(super)
end