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
# 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'
end

Instance Method Details

#createObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/surveyor/surveyor_controller_methods.rb', line 28

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(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



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/surveyor/surveyor_controller_methods.rb', line 65

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
    if params[:section]
      @section = @sections.with_includes.find(section_id_from(params[:section])) || @sections.with_includes.first
    else
      @section = @sections.with_includes.first
    end
    set_dependents
  else
    flash[:notice] = t('surveyor.unable_to_find_your_responses')
    redirect_to surveyor_index
  end
end

#exportObject



141
142
143
144
145
146
147
148
149
# File 'lib/surveyor/surveyor_controller_methods.rb', line 141

def export
  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
  render_404 and return if @survey.blank?
end

#load_and_update_response_set_with_retries(remaining = 2) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
# File 'lib/surveyor/surveyor_controller_methods.rb', line 108

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



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/surveyor/surveyor_controller_methods.rb', line 15

def new
  @surveys = Survey.find(:all)
  @codes = @surveys.inject({}) do |codes,s|
    codes[s.access_code] ||= {}
    codes[s.access_code][:title] = s.title
    codes[s.access_code][:survey_versions] ||= []
    codes[s.access_code][:survey_versions] << s.survey_version
    codes
  end
  @title = "You can take these surveys"
  redirect_to surveyor_index unless surveyor_index == available_surveys_path
end

#render_404Object



151
152
153
154
# File 'lib/surveyor/surveyor_controller_methods.rb', line 151

def render_404
  head :status => 404
  true
end

#showObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/surveyor/surveyor_controller_methods.rb', line 47

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



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/surveyor/surveyor_controller_methods.rb', line 82

def update
  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(available_surveys_path, :notice, t('surveyor.unable_to_find_your_responses'))
      else
        flash[:notice] = t('surveyor.unable_to_update_survey') unless saved
        redirect_to edit_my_survey_path(
          :anchor => anchor_from(params[:section]), :section => section_id_from(params[:section]))
      end
    end
    format.js do
      if @response_set
        render :json => @response_set.reload.all_dependencies
      else
        render :text => "No response set #{params[:response_set_code]}",
          :status => 404
      end
    end
  end
end