Class: Fe::Admin::ElementsController

Inherits:
ApplicationController
  • Object
show all
Defined in:
app/controllers/fe/admin/elements_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject

POST /elements



51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'app/controllers/fe/admin/elements_controller.rb', line 51

def create
  @element = params[:element_type].constantize.new(element_params)
  @element.required = true if @element.question?
  @question_sheet = @page.question_sheet

  respond_to do |format|
    if @element.save!
      @page_element = Fe::PageElement.create(:element => @element, :page => @page)
      format.js
    else
      format.js { render :action => 'error.rjs' }
    end
  end
end

#destroyObject

DELETE /elements/1 DELETE /elements/1.xml



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'app/controllers/fe/admin/elements_controller.rb', line 81

def destroy
  @element = Fe::Element.find(params[:id])
  # Start by removing the element from the page
  page_element = Fe::PageElement.where(:element_id => @element.id, :page_id => @page.id).first
  page_element.destroy if page_element
  
  # If this element is not on any other pages, is not a question or has no answers, Destroy it
  if @element.reuseable? && (Fe::PageElement.where(:element_id => params[:id]).present? || @element.has_response?)
    @element.update_attributes(:question_grid_id => nil, :conditional_id => nil)
  else
    @element.destroy
  end

  respond_to do |format|
    format.js
  end
end

#dropObject



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'app/controllers/fe/admin/elements_controller.rb', line 129

def drop
  element = Fe::Element.find(params[:draggable_element].split('_')[1])  # element being dropped
  target = Fe::Element.find(params[:id])
  case target.class.to_s
  when 'QuestionGrid', 'QuestionGridWithTotal'
    # abort if the element is already in this box
    if element.question_grid_id == params[:id].to_i
      render :nothing => true
    else
      element.question_grid_id = params[:id]
      element.save!
    end
  when 'ChoiceField'
    # abort if the element is already in this box
    if element.conditional_id == params[:id].to_i
      render :nothing => true
    else
      element.conditional_id = params[:id]
      element.save!
    end
  end
  # Remove page element for this page since it's now in a grid
  Fe::PageElement.where(:page_id => @page.id, :element_id => element.id).first.try(:destroy)
end

#duplicateObject



168
169
170
171
172
173
174
# File 'app/controllers/fe/admin/elements_controller.rb', line 168

def duplicate
  element = Fe::Element.find(params[:id])
  @element = element.duplicate(@page, element.question_grid || element.choice_field)
  respond_to do |format|
    format.js 
  end
end

#editObject

GET /element/1/edit



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'app/controllers/fe/admin/elements_controller.rb', line 17

def edit
  @element = Fe::Element.find(params[:id])
  
  # for dependencies
  if @element.question?
    (3 - @element.conditions.length).times { @element.conditions.build }
    @questions_before_this = @page.questions_before_position(@element.position(@page)) 
  end
  
  respond_to do |format|
    format.js
  end
end

#newObject



31
32
33
34
35
36
37
38
# File 'app/controllers/fe/admin/elements_controller.rb', line 31

def new
  @questions = params[:element_type].constantize.active.order('label')

  @style = element_params[:style]
  if @style
    @questions = @questions.where(:style => @style).uniq
  end
end

#remove_from_gridObject



154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'app/controllers/fe/admin/elements_controller.rb', line 154

def remove_from_grid
  element = Fe::Element.find(params[:id])
  Fe::PageElement.create(:element_id => element.id, :page_id => @page.id) unless Fe::PageElement.where(:element_id => element.id, :page_id => @page.id).first
  if element.question_grid_id
    element.set_position(element.question_grid.position(@page), @page) 
    element.question_grid_id = nil
  elsif element.choice_field_id
    element.set_position(element.choice_field.position(@page), @page) 
    element.choice_field_id = nil
  end
  element.save!
  render :action => :drop
end

#reorderObject



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'app/controllers/fe/admin/elements_controller.rb', line 99

def reorder 
  # since we don't know the name of the list, just find the first param that is an array
  params.each_key do |key| 
    if key.include?('questions_list')
      grid_id = key.sub('questions_list_', '').to_i
      # See if we're ordering inside of a grid
      if grid_id > 0
        Fe::Element.find(grid_id).elements.each do |element|
          if index = params[key].index(element.id.to_s)
            element.position = index + 1 
            element.save(:validate => false)
          end
        end
      else
        @page.page_elements.each do |page_element|
          if index = params[key].index(page_element.element_id.to_s)
            page_element.position = index + 1 
            page_element.save(:validate => false)
            @element = page_element.element
          end
        end
      end
    end
  end
  
  respond_to do |format|
    format.js
  end
end

#showObject

GET /elements/1



8
9
10
11
12
13
14
# File 'app/controllers/fe/admin/elements_controller.rb', line 8

def show
  @element = Fe::Element.find(params[:id])

  respond_to do |format|
    format.js
  end
end

#updateObject

PUT /elements/1



67
68
69
70
71
72
73
74
75
76
77
# File 'app/controllers/fe/admin/elements_controller.rb', line 67

def update
  @element = Fe::Element.find(params[:id])

  respond_to do |format|
    if @element.update_attributes(element_params)
      format.js
    else
      format.js { render :action => 'error.rjs' }
    end
  end
end

#use_existingObject



40
41
42
43
44
45
46
47
48
# File 'app/controllers/fe/admin/elements_controller.rb', line 40

def use_existing
  @element = Fe::Element.find(params[:id])
  # Don't put the same question on a questionnaire twice
  unless @page.question_sheet.elements.include?(@element)
    @page_element = Fe::PageElement.create(:element => @element, :page => @page)
  end
  @question_sheet = @page.question_sheet
  render :create
end