Class: Admin::ElementsController

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

Instance Method Summary collapse

Instance Method Details

#createObject

POST /elements



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'app/controllers/qe/admin/elements_controller.rb', line 54

def create
  # TODO do this within the Rails framework
  # @element = params[:element_type].constantize.new(params[:element])

  @element = "Qe::#{params[:element_type]}".constantize.new(params[:element])
  @element.required = true if @element.question?
  @question_sheet = @page.question_sheet
  respond_to do |format|
    # TODO engineer the mass assignment flow of the engine
    if @element.save!(:without_protection => true)
      @page_element = PageElement.create(:element => @element, :page => @page)
      format.js
    else
      format.js { render :action => 'error.js.erb' }
    end
  end
end

#destroyObject

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



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'app/controllers/qe/admin/elements_controller.rb', line 87

def destroy
  @element = Element.find(params[:id])
  # Start by removing the element from the page
  page_element = 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? && (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



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'app/controllers/qe/admin/elements_controller.rb', line 135

def drop
  element = Element.find(params[:draggable_element].split('_')[1])  # element being dropped
  target = Element.find(params[:id])
  # TODO might need to revise due to namespacing
  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
  PageElement.where(:page_id => @page.id, :element_id => element.id).first.try(:destroy)
end

#duplicateObject



175
176
177
178
179
180
181
# File 'app/controllers/qe/admin/elements_controller.rb', line 175

def duplicate
  element = 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



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'app/controllers/qe/admin/elements_controller.rb', line 19

def edit
  @element = 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

GET /elements/new



34
35
36
37
38
39
40
41
# File 'app/controllers/qe/admin/elements_controller.rb', line 34

def new
  # raise params.inspect
  @questions = "Qe::#{params[:element_type]}".constantize.active.order('label')
  params[:element] ||= {}
  if params[:element][:style]
    @questions = @questions.where(:style => params[:element][:style]).all.uniq
  end
end

#remove_from_gridObject



161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'app/controllers/qe/admin/elements_controller.rb', line 161

def remove_from_grid
  element = Element.find(params[:id])
  PageElement.create(:element_id => element.id, :page_id => @page.id) unless 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.conditional_id
    element.set_position(element.choice_field.position(@page), @page) 
    element.conditional_id = nil
  end
  element.save!
  render :action => :drop
end

#reorderObject



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'app/controllers/qe/admin/elements_controller.rb', line 105

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



10
11
12
13
14
15
16
# File 'app/controllers/qe/admin/elements_controller.rb', line 10

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

  respond_to do |format|
    format.js
  end
end

#updateObject

PUT /elements/1



73
74
75
76
77
78
79
80
81
82
83
# File 'app/controllers/qe/admin/elements_controller.rb', line 73

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

  respond_to do |format|
    if @element.update_attributes(params[:element])
      format.js
    else
      format.js { render :action => 'error.js.erb' }
    end
  end
end

#use_existingObject



43
44
45
46
47
48
49
50
51
# File 'app/controllers/qe/admin/elements_controller.rb', line 43

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