Class: AgileCommonController
- Inherits:
-
AgileApplicationController
- Object
- ActionController::Base
- AgileApplicationController
- AgileCommonController
- Defined in:
- app/controllers/agile_common_controller.rb
Overview
AgileCommonController holds some common actions used by AgileRails.
Instance Method Summary collapse
-
#add_json_ld_schema ⇒ Object
Will add new json_ld element with blank structure into ar_json_ld field on a document.
-
#autocomplete ⇒ Object
This action is called on ajax autocomplete call.
-
#copy_clipboard ⇒ Object
Copy current record to clipboard as json text.
-
#help ⇒ Object
Will provide help data.
-
#logout ⇒ Object
Default user logout action.
-
#paste_clipboard ⇒ Object
Paste data from clipboard into text_area and update documents in destination database.
-
#poll_submit ⇒ Object
Save poll results to ar_poll_results table.
-
#process_login ⇒ Object
Default user login action.
-
#restore_from_journal ⇒ Object
Action for restoring document data from journal document.
-
#toggle_edit_mode ⇒ Object
Toggle CMS edit mode.This action is called when user clicks CMS icon on top left corner of the browser.
Methods inherited from AgileApplicationController
#agile_dump, #agile_edit_mode?, #agile_get_site, #agile_process_default_request, #agile_render_404, #agile_user_has_role?, #agile_visit_log, #set_page_title
Instance Method Details
#add_json_ld_schema ⇒ Object
Will add new json_ld element with blank structure into ar_json_ld field on a document.
212 213 214 215 216 217 218 219 220 221 222 223 |
# File 'app/controllers/agile_common_controller.rb', line 212 def add_json_ld_schema edited_document = ArJsonLd.find_document_by_ids(AgileHelper.table_param(params), params[:ids]) yaml = YAML.load_file( AgileHelper.form_file_find('json_ld_schema') ) schema_data = yaml[params[:schema]] # Existing document if edited_document.ar_json_lds.find_by(type: "@#{params[:schema]}") return render json: {'msg_error' => t('helpers.help.ar_json_ld.add_error', schema: params[:schema] ) } else add_empty_json_ld_schema(edited_document, schema_data, params[:schema], params[:schema], yaml) end render json: {'reload_' => 1} end |
#autocomplete ⇒ Object
This action is called on ajax autocomplete call. It checks if user has rights to view data.
URL parameters:
- table
-
Table (table) model name in lower case indicating table which will be searched.
- id
-
Name of id key field that will be returned. Default is ‘_id’
- input
-
Search data entered in input field.
- search
-
when passed without dot it defines field name on which search
will be performed. When passed with dot class_method.method_name is assumed. Method name will be parsed and any class with class method name can be evaluated. Class method must accept input parameter and return array [ [_id, value],.. ] which will be used in autocomplete field.
Return: JSON array [label, value, id] of first 20 documents that confirm to query.
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'app/controllers/agile_common_controller.rb', line 46 def autocomplete # table parameter must be defined. If not, get it from search parameter if params['table'].nil? && params['search'].match(/\./) name = params['search'].split('.').first params['table'] = name.underscore end if params['table'].match('_control') # depends on ar_memory permissions. Should probably be logged on. return render json: { label: t('agile.not_authorized') } unless agile_user_can(ArPermission::CAN_VIEW, 'ar_memory') else return render json: { label: t('agile.not_authorized') } unless agile_user_can(ArPermission::CAN_VIEW) end table = params['table'].classify.constantize input = params['input'].gsub(/\(|\)|\[|\]|\{|\|\.|\,|\}|\%|\*|\:|\;/, '') # remove special characters # call method in class. if search parameter contains . search is defined in a method result = if params['search'].match(/\./) name, method = params['search'].split('.') table.send(method, input).map do |e| { label: e[0], value: e[0], id: (e[1] || e[0]).to_s } end # standard search in table by field_name else table.where("lower(#{params['search']}) like ?", "%#{input.downcase}%"). limit(20).map do |e| { label: e[params['search']], value: e[params['search']], id: e.id } end end render json: result end |
#copy_clipboard ⇒ Object
Copy current record to clipboard as json text. It will actually ouput an window with data formatted as json.
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'app/controllers/agile_common_controller.rb', line 151 def copy_clipboard # Only administrators can perform this operation return render(plain: t('agile.not_authorized') ) unless agile_user_has_role?('admin') respond_to do |format| # just open new window to same url and come back with html request format.json { agile_render_ajax(operation: 'window', url: request.url ) } format.html do table = AgileHelper.table_param(params).split(';').last record = agile_document_find(table, params[:id]) text = %(<style>body {font-family: monospace;}</style><pre> JSON:<br>[#{table},#{params[:id]},#{params[:ids]}]<br>#{record.to_json}<br><br> YAML:<br>#{record.attributes.to_yaml.gsub("\n", '<br>')}</pre>) render plain: text end end end |
#help ⇒ Object
Will provide help data
228 229 230 231 232 233 234 235 236 237 238 |
# File 'app/controllers/agile_common_controller.rb', line 228 def help agile_form_read form_name = AgileHelper.form_param(params) || AgileHelper.table_param(params) help_file_name = @form['help'] || @form['extend'] || form_name help_file_name = AgileApplicationController.find_help_file(help_file_name) @help = YAML.load_file(help_file_name) if help_file_name # no auto generated help on index action return render json: {} if params[:type] == 'index' && @help.nil? render json: { popup_help: render_to_string(partial: 'help') } end |
#logout ⇒ Object
Default user logout action.
112 113 114 115 |
# File 'app/controllers/agile_common_controller.rb', line 112 def logout clear_login_data redirect_to(params[:return_to] || '/', allow_other_host: true) end |
#paste_clipboard ⇒ Object
Paste data from clipboard into text_area and update documents in destination database. This action is called twice. First time for displaying text_area field and second time ajax call for processing data.
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
# File 'app/controllers/agile_common_controller.rb', line 176 def paste_clipboard # Only administrators can perform this operation return render(plain: t('agile.not_authorized') ) unless agile_user_has_role?('admin') result = '' respond_to do |format| # just open new window to same url and come back with html request format.html { return render('paste_clipboard', layout: ' agile') } format.json { table, id, ids = nil params[:data].split("\n").each do |line| line.chomp! next if line.size < 5 # empty line. Skip begin if line[0] == '[' # id(s) result += "<br>#{line}" line = line[/\[(.*?)\]/, 1] # just what is between [] table, id, ids = line.split(',') elsif line[0] == '{' # document data result += paste_document_process(line, table, id, ids) end rescue Exception => e result += " Runtime error. #{e.}\n" break end end } end agile_render_ajax(div: 'result', value: result ) end |
#poll_submit ⇒ Object
Save poll results to ar_poll_results table
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 |
# File 'app/controllers/agile_common_controller.rb', line 243 def poll_submit record = params[:record] poll = ArPoll.find(params[:poll_id]) || ArPoll.find_by(name: params[:poll_id]) result = poll.save_results(record) # call additional service run = params[:record][:run] || params[:run] redirect_to(params[:return_to] || '/') and return if run.blank? service_response = send(run, result) respond_to do |format| format.html do # submit # return on poll-top params[:return_to] += '#poll-top' unless params[:return_to].match('#poll-top') # response should be in same format as ajax response. Extract error and info service_response = {} unless service_response.class == Hash flash[:error] = service_response['msg_error'] flash[:info] = service_response['msg_info'] redirect_to params[:return_to] end format.js do # ajax submit render json: service_response || { ok: 1 } end end end |
#process_login ⇒ Object
Default user login action.
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'app/controllers/agile_common_controller.rb', line 92 def process_login # Somebody is probably playing return agile_render_404 unless ( params[:record] && params[:record][:username] && params[:record][:password] ) if params[:record][:password].present? # must not be empty user = ArUser.find_by(username: params[:record][:username], active: true) if user&.authenticate(params[:record][:password]) set_login_data(user, params[:record][:remember_me].to_i == 1) return redirect_to(params[:return_to] || '/', allow_other_host: true) else clear_login_data # on the safe side end end flash[:error] = t('agile.invalid_username') redirect_to(params[:return_to] || '/', allow_other_host: true) end |
#restore_from_journal ⇒ Object
Action for restoring document data from journal document.
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'app/controllers/agile_common_controller.rb', line 120 def restore_from_journal # Only administrators can perform this operation unless agile_user_has_role?('admin') return render plain: { 'msg_info' => (t ('agile.not_authorized')) }.to_json end # selected fields to hash restore = {} params[:select].each { |k, v| restore[k] = v if v == '1' } result = if restore.size == 0 { 'msg_error' => (t ('agile.ar_journal.zero_selected')) } else journal_rec = ArJournal.find(params[:id]) # update hash with data to be restored JSON.parse(journal_rec.diff).each { |k, v| restore[k] = v.first if restore[k] } # table model and document id table = journal_rec.tables.classify.constantize id = journal_rec.ids record = table.find(id) # restore and save values restore.each { |field, value| record.send("#{field}=",value) } record.save # TODO Error checking { 'msg_info' => (t ('agile.ar_journal.restored')) } end render plain: result.to_json end |
#toggle_edit_mode ⇒ Object
Toggle CMS edit mode.This action is called when user clicks CMS icon on top left corner of the browser.
81 82 83 84 85 86 87 |
# File 'app/controllers/agile_common_controller.rb', line 81 def toggle_edit_mode session[:edit_mode] ||= 0 return agile_render_404 if session[:edit_mode] < 1 # called without logged in session[:edit_mode] = (session[:edit_mode] == 1) ? 2 : 1 redirect_to (params[:return_to] || '/') end |