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

+ (Object) included(base)



3
4
5
6
# File 'lib/surveyor/surveyor_controller_methods.rb', line 3

def self.included(base)
  base.send :before_filter, :get_current_user, :only => [:new, :create]
  base.send :layout, 'surveyor_default'
end

Instance Method Details

- (Object) create



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

def create
  @survey = Survey.find_by_access_code(params[:survey_code])
  @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

- (Object) edit



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

def edit
  @response_set = ResponseSet.find_by_access_code(params[:response_set_code], :include => {:responses => [:question, :answer]})
  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
    @dependents = (@response_set.unanswered_dependencies - @section.questions) || []
  else
    flash[:notice] = t('surveyor.unable_to_find_your_responses')
    redirect_to surveyor_index
  end
end

- (Object) new

Actions



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

def new
  @surveys = Survey.find(:all)
  @title = "You can take these surveys"
  redirect_to surveyor_index unless surveyor_index == available_surveys_path
end

- (Object) show



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

def show
  @response_set = ResponseSet.find_by_access_code(params[:response_set_code], :include => {:responses => [:question, :answer]})
  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")
      }
    end
  else
    flash[:notice] = t('surveyor.unable_to_find_your_responses')
    redirect_to surveyor_index
  end
end

- (Object) update



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/surveyor/surveyor_controller_methods.rb', line 60

def update
  @response_set = ResponseSet.find_by_access_code(params[:response_set_code], :include => {:responses => :answer}, :lock => true)
  return redirect_with_message(available_surveys_path, :notice, t('surveyor.unable_to_find_your_responses')) if @response_set.blank?
  saved = false
  ActiveRecord::Base.transaction do
    saved = @response_set.update_attributes(:responses_attributes => ResponseSet.reject_or_destroy_blanks(params[:r]))
    @response_set.complete! if saved && params[:finish]
    saved &= @response_set.save
  end
  return redirect_with_message(surveyor_finish, :notice, t('surveyor.completed_survey')) if saved && params[:finish]

  respond_to do |format|
    format.html do
      flash[:notice] = t('surveyor.unable_to_update_survey') unless saved
      redirect_to :action => "edit", :anchor => anchor_from(params[:section]), :params => {:section => section_id_from(params[:section])}
    end
    format.js do
      ids, remove, question_ids = {}, {}, []
      ResponseSet.reject_or_destroy_blanks(params[:r]).each do |k,v|
        ids[k] = @response_set.responses.find(:first, :conditions => v).id if !v.has_key?("id")
        remove[k] = v["id"] if v.has_key?("id") && v.has_key?("_destroy")
        question_ids << v["question_id"]
      end
      render :json => {"ids" => ids, "remove" => remove}.merge(@response_set.reload.all_dependencies(question_ids))
    end
  end
end