Class: Fe::Admin::ElementsController
- Inherits:
-
ApplicationController
- Object
- ApplicationController
- Fe::Admin::ElementsController
- Defined in:
- app/controllers/fe/admin/elements_controller.rb
Instance Method Summary collapse
- #copy_existing ⇒ Object
-
#create ⇒ Object
POST /elements.
-
#destroy ⇒ Object
DELETE /elements/1 DELETE /elements/1.xml.
- #drop ⇒ Object
- #duplicate ⇒ Object
-
#edit ⇒ Object
GET /element/1/edit.
-
#extra_element_params ⇒ Object
give enclosing apps a way to permit their own element attributes by overriding this method.
- #new ⇒ Object
- #remove_from_grid ⇒ Object
- #reorder ⇒ Object
-
#update ⇒ Object
PUT /elements/1.
- #use_existing ⇒ Object
Instance Method Details
#copy_existing ⇒ Object
41 42 43 44 45 46 47 48 |
# File 'app/controllers/fe/admin/elements_controller.rb', line 41 def copy_existing @element = Fe::Element.find(params[:id]) # NOTE the enclosing app might want to override this method and check that they have access to the questionnaire that the existing element is used on # duplicate the elements @element = @element.duplicate(@page) @element.update_attribute(:share, false) @page_element = Fe::PageElement.where(element: @element, page: @page).first_or_create render :create end |
#create ⇒ Object
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.js.erb' } end end end |
#destroy ⇒ Object
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 = @page.all_elements.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(question_grid_id: nil, conditional_id: nil) else @element.destroy end respond_to do |format| format.js end end |
#drop ⇒ Object
129 130 131 132 133 134 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 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'app/controllers/fe/admin/elements_controller.rb', line 129 def drop element = @page.all_elements.find(params[:draggable_element].split('_')[1]) # element being dropped target = @page.all_elements.find(params[:id]) if [params[:before], params[:after]].include?('true') # move the element out of its parent and back onto the page directly, placing it before the target page_element = Fe::PageElement.where(page_id: @page.id, element_id: element.id).first_or_create @page.page_elements << page_element parent_element = element.question_grid || element.choice_field parent_page_element = @page.page_elements.find_by(element_id: parent_element.id) if params[:before] page_element.insert_at(parent_page_element.position) else page_element.insert_at(parent_page_element.position + 1) end # remove question grid / choice_field ref since it's directly on the page now element.update(question_grid_id: nil, choice_field_id: nil) return end case target.class.to_s when 'Fe::QuestionGrid', 'Fe::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 'Fe::ChoiceField' # abort if the element is already in this box if element.choice_field_id == params[:id].to_i render nothing: true else element.choice_field_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 |
#duplicate ⇒ Object
187 188 189 190 191 192 193 |
# File 'app/controllers/fe/admin/elements_controller.rb', line 187 def duplicate element = @page.all_elements.find(params[:id]) @element = element.duplicate(@page, element.question_grid || element.question_grid_with_total || element.choice_field) respond_to do |format| format.js end end |
#edit ⇒ Object
GET /element/1/edit
8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'app/controllers/fe/admin/elements_controller.rb', line 8 def edit @element = @page.all_elements.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 |
#extra_element_params ⇒ Object
give enclosing apps a way to permit their own element attributes by overriding this method
196 197 198 |
# File 'app/controllers/fe/admin/elements_controller.rb', line 196 def extra_element_params [] end |
#new ⇒ Object
22 23 24 25 26 27 28 29 |
# File 'app/controllers/fe/admin/elements_controller.rb', line 22 def new @questions = params[:element_type].constantize.active.shared.order('label') @style = element_params[:style] if @style @questions = @questions.where(style: @style).to_a.uniq end end |
#remove_from_grid ⇒ Object
173 174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'app/controllers/fe/admin/elements_controller.rb', line 173 def remove_from_grid element = @page.all_elements.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 |
#reorder ⇒ Object
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.permit!.to_h.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 @page.all_elements.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 |
#update ⇒ Object
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 = @page.all_elements.find(params[:id]) respond_to do |format| if @element.update(element_params) format.js else format.js { render action: 'error.js.erb' } end end end |
#use_existing ⇒ Object
31 32 33 34 35 36 37 38 39 |
# File 'app/controllers/fe/admin/elements_controller.rb', line 31 def use_existing @element = Fe::Element.find(params[:id]) # NOTE the enclosing app might want to override this method and check that they have access to the questionnaire that the existing element is used on # 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 |