Module: Surveyor::SurveyorControllerMethods

Included in:
SurveyorController
Defined in:
lib/surveyor/surveyor_controller_methods.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



7
8
9
10
11
12
13
# File 'lib/surveyor/surveyor_controller_methods.rb', line 7

def self.included(base)
  base.send :before_filter, :get_current_user, :only => [:new, :create]
  base.send :before_filter, :determine_if_javascript_is_enabled, :only => [:create, :update]
  base.send :before_filter, :set_response_set_and_render_context, :only => [:edit, :show]
  base.send :layout, 'surveyor_default'
  base.send :before_filter, :set_locale
end

Instance Method Details

#createObject



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

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



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

def edit
  # @response_set is set in before_filter - set_response_set_and_render_context
  if @response_set
    @survey = Survey.with_sections.find_by_id(@response_set.survey_id)
    @sections = @survey.sections
    @section = @sections.with_includes.find(section_id_from(params) || :first) || @sections.with_includes.first
    set_dependents
  else
    flash[:notice] = t('surveyor.unable_to_find_your_responses')
    redirect_to surveyor_index
  end
end

#exportObject



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

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



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

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



16
17
18
19
# File 'lib/surveyor/surveyor_controller_methods.rb', line 16

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

#render_404Object



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

def render_404
  head :status => 404
  true
end

#showObject



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

def show
  # @response_set is set in before_filter - 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



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

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



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

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