Class: PollsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/polls_controller.rb

Overview

Manage polls creation within the sharebox site

Instance Method Summary collapse

Methods inherited from ApplicationController

#check_lang, #prepare_attached_docs_request

Instance Method Details

#check_adminObject

check if user has admin rights
All the views and features related to polls are destinated only for admins



11
12
13
14
15
16
# File 'app/controllers/polls_controller.rb', line 11

def check_admin
  unless current_user.is_admin?
    flash[:notice] = t('sb.no_permission')
    redirect_to root_url
  end
end

#createObject

Create the poll
possible only if there is a title, a description and at least 1 question (open or closed)
duplicate questions will be rejected and the poll creation will fail



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'app/controllers/polls_controller.rb', line 109

def create
  check_admin
  puts(poll_params["name"])
  @poll = current_user.polls.new(poll_params)
  if @poll.description == ""
    flash[:notice] = t('sb.missing_required_fields')
  elsif @poll.name == ""
    flash[:notice] = t('sb.missing_required_fields')
  elsif @poll.open_names == "" && @poll.closed_names == ""
    flash[:notice] = t('sb.missing_required_fields')
  else
    @poll.closed_names_number=@poll.closed_names.split(";").length
    @poll.open_names_number=@poll.open_names.split(";").length
    array = @poll.get_names
    if array.uniq.count != array.size
      flash[:notice] = t('sb.same_questions')
    else
      @poll.save
      flash[:notice] = t('sb.created')
    end
  end
  render 'new'
  #redirect_to root_url
end

#destroyObject

Destroy a poll/survey
Every folder related to this poll will be updated (reinitialize poll_id)
All satisfaction answers related to the poll will be deleted cause they all belong to a poll



138
139
140
141
142
143
144
145
146
147
148
# File 'app/controllers/polls_controller.rb', line 138

def destroy
  check_admin
  @poll = Poll.find_by_id(params[:id])
  Folder.where(poll_id: @poll.id).each do |f|
    f.poll_id = nil
    f.save
  end
  @poll.destroy
  flash[:notice] = t('sb.deleted')
  redirect_to root_url
end

#editObject

Show the edit form in order for the admin to update existing polls (title, description…)
This view allows you to edit a poll.



42
43
44
45
46
47
48
49
# File 'app/controllers/polls_controller.rb', line 42

def edit
  check_admin
  @poll = Poll.find_by_id(params[:id])
  unless @poll
    flash[:notice] = t('sb.inexisting')
    redirect_to root_url
  end
end

#getpollsObject

if route is /getpolls return all the polls in the colibri
if route is /getpolls?mynums=1 return poll numbers containing satisfactions answers out of the folders/assets system for the current_user



21
22
23
24
25
26
27
28
29
# File 'app/controllers/polls_controller.rb', line 21

def getpolls
  if params[:mynums].to_i==1
    poll_ids=current_user.satisfactions.where("folder_id < ?",0).pluck("DISTINCT poll_id")
    render json: poll_ids
  else
    allpolls = Poll.all.order("id DESC")
    render json: allpolls
  end
end

#indexObject

the index route leads to the satisfactions exploitation main dashboard where everything is done with ajax



33
34
35
36
37
# File 'app/controllers/polls_controller.rb', line 33

def index
  unless current_user.belongs_to_team? || current_user.is_admin?
    redirect_to root_url
  end
end

#newObject

Show the ‘new’ form in order for the admin to create new polls
open & closed questions can be defined, via two different textarea
Questions must be separated by “;”



86
87
88
89
# File 'app/controllers/polls_controller.rb', line 86

def new
  check_admin
  @poll = current_user.polls.new
end

#poll_paramsObject (private)



151
152
153
# File 'app/controllers/polls_controller.rb', line 151

def poll_params
  params.require(:poll).permit(:name, :description, :closed_names, :open_names, :closed_names_number, :open_names_number)
end

#showObject

not used actually
should be recoded (?) to implement the same result as the route localhost:3000/satisfactions/run/poll_id?blabla



95
96
97
98
99
100
101
102
103
# File 'app/controllers/polls_controller.rb', line 95

def show
  poll = Poll.find_by_id(params[:id])
  unless poll
    flash[:notice] = t('sb.inexisting')
    redirect_to browse_path
  else
    redirect_to root_url
  end
end

#updateObject

Saves the changes
You can delete or add open & closed questions



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'app/controllers/polls_controller.rb', line 54

def update
  check_admin
  @poll = Poll.find_by_id(params[:id])
  array = poll_params[:closed_names].split(";") + poll_params[:open_names].split(";")
  if array.uniq.count != array.size
    flash[:notice] = t('sb.same_questions')
  elsif params[:poll][:description] == ""
    flash[:notice] = t('sb.missing_required_fields')
  elsif params[:poll][:name] == ""
    flash[:notice] = t('sb.missing_required_fields')
  elsif params[:poll][:open_names] == "" && params[:poll][:closed_names] == ""
    flash[:notice] = t('sb.missing_required_fields')
  else
    params[:poll][:closed_names].strip!
    params[:poll][:open_names].strip!
    params[:poll][:closed_names_number]=params[:poll][:closed_names].split(";").length
    params[:poll][:open_names_number]=params[:poll][:open_names].split(";").length
    flash[:notice]= "#{params[:poll][:closed_names_number]} #{t('sb.closed_questions')} #{params[:poll][:open_names_number]} #{t('sb.open_questions')}"
    if @poll.update(poll_params)
      flash[:notice] = "#{flash[:notice]} - #{t('sb.updated')}"
    else
      flash[:notice] = "#{flash[:notice]} - #{t('sb.not_updated')}"
    end
  end
  #redirect_to root_url
  render 'edit'
end