Module: CmseditHelper
- Included in:
- DcPollRenderer
- Defined in:
- app/helpers/cmsedit_helper.rb
Overview
CmseditHelper module defines helper methods used by cmsedit actions. Output is controlled by data found in 3 major sections of DRG CMS form: index, result_set and form sections.
Instance Attribute Summary collapse
-
#js ⇒ Object
readonly
javascript part created by form helpers.
Instance Method Summary collapse
-
#_get_user_for(field_name) ⇒ Object
Returns username for id.
-
#dc_actions_for_form ⇒ Object
Creates actions div for edit form.
-
#dc_actions_for_index ⇒ Object
Creates action div for cmsedit index action.
-
#dc_actions_for_result(document) ⇒ Object
Creates actions that could be performed on single row of result set.
-
#dc_check_and_default(value, default, values = nil) ⇒ Object
Checks if value is defined and sets default.
-
#dc_clicks_for_result(document) ⇒ Object
Creates div with documents of current result set.
-
#dc_columns_for_result(document) ⇒ Object
Creates column for each field of result set document.
-
#dc_div_filter ⇒ Object
Creates filter div for cmsedit index/filter action.
-
#dc_document_statistics ⇒ Object
Creates current document statistics div (created_by, created_at, .…) at the bottom of edit form.
-
#dc_fields_for_form ⇒ Object
Creates edit form div.
-
#dc_fields_for_tab(fields) ⇒ Object
Creates input fields for one tab.
-
#dc_format_value(value, format = nil) ⇒ Object
Formats value according to format supplied or data type.
-
#dc_header_for_result ⇒ Object
Creates header div for result set.
-
#dc_link_or_ajax(yaml, parms) ⇒ Object
Creates code for link or ajax action type.
-
#dc_row_for_result(document) ⇒ Object
Creates tr code for each row of result set.
-
#dc_style_or_class(selector, yaml, value, record) ⇒ Object
Defines style or class for row (tr) or column (td).
-
#dc_table_title_for_result(result = nil) ⇒ Object
Creates title div for cmsedit index result set records.
-
#top_bottom_line(yaml, columns = 2) ⇒ Object
Creates top or bottom horizontal line on form.
Instance Attribute Details
#js ⇒ Object (readonly)
javascript part created by form helpers
32 33 34 |
# File 'app/helpers/cmsedit_helper.rb', line 32 def js @js end |
Instance Method Details
#_get_user_for(field_name) ⇒ Object
Returns username for id. Subroutine of dc_document_statistics
731 732 733 734 735 736 737 |
# File 'app/helpers/cmsedit_helper.rb', line 731 def _get_user_for(field_name) #:nodoc: if @record[field_name] u = DcUser.find(@record[field_name]) return u ? u.name : @record[field_name] end # nil end |
#dc_actions_for_form ⇒ Object
Creates actions div for edit form.
Displaying readonly form turned out to be challenge. For now when readonly parameter has value 2, back link will force readonly form. Value 1 or not set will result in normal link.
422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 |
# File 'app/helpers/cmsedit_helper.rb', line 422 def dc_actions_for_form() # create standard actions std_actions = {' 1' => 'back', ' 2' => {'type' => 'submit', 'caption' => 'drgcms.save'}, ' 3' => {'type' => 'submit', 'caption' => 'drgcms.save&back'} } # when edit only unless @record.id.nil? std_actions.merge!({' 6' => 'new'} ) std_actions.merge!(@record.active ? {' 5' => 'disable'} : {' 5' => 'enable'} ) if @record.respond_to?('active') end actions = @form['form']['actions'] # shortcut for actions: standard actions = nil if actions.class == String && actions == 'standard' # standard actions actions = std_actions if actions.nil? # readonly actions = {' 1' => 'back'} if @form['readonly'] # Actions are strictly forbidden if @form['form']['actions'] and dc_dont?(@form['form']['actions']) actions = [] elsif actions['standard'] actions.merge!(std_actions) actions['standard'] = nil end # Update save and save&back actions.each do |k,v| if v.class == String if v.match(/save\&back/i) actions[k] = {'type' => 'submit', 'caption' => 'drgcms.save&back'} elsif v == 'save' actions[k] = {'type' => 'submit', 'caption' => 'drgcms.save'} end end end # Sort so that standard actions come first actions = actions.to_a.sort {|x,y| x[0].to_s <=> y[0].to_s} # Add spinner to the beginning c = %Q[<td id="dc-spinner" class="div-hidden">#{fa_icon('spinner lg spin')}</td>] actions.each do |element| session[:form_processing] = "form:actions: #{element}" v = element[1] next if v.nil? # yes it happends p "Using text option in actions_for form is replaced with caption. Table #{@form['table']}" if v['text'] # on_save_ok should't go inside td tags if (element[0] == 'on_save_ok') then c << hidden_field_tag(:on_save_ok, v) next end # parms = @parms.clone if v.class == String next if params[:readonly] and !(v == 'back') c << '<td class="dc-link dc-animate">' c << case when (v == 'back' or v == 'cancle') then # If return_to is present link directly to URL if parms['xreturn_to'] # disabled for now dc_link_to( 'drgcms.back','arrow-left', parms['return_to'] ) else parms['action'] = 'index' parms['readonly'] = parms['readonly'].to_s.to_i < 2 ? nil : 1 dc_link_to( 'drgcms.back','arrow-left', parms ) end when v == 'delete' then parms['operation'] = v parms['id'] = @record.id dc_link_to( 'drgcms.delete','remove', parms, data: { confirm: t('drgcms.confirm_delete') }, method: :delete ) when v == 'new' then parms['action'] = v dc_link_to( 'drgcms.new', 'plus', parms) when (v == 'enable' or v == 'disable') then parms['operation'] = v parms['id'] = @record.id icon = (v == 'enable' ? 'thumbs-o-up' : 'thumbs-o-down') dc_link_to( "drgcms.#{v}",icon, parms, method: :delete ) when v == 'edit' then parms['operation'] = v parms['id'] = @record.id dc_link_to( "drgcms.#{v}",v, parms ) else "err1 #{element[0]}=>#{v}" end c << '</td>' # non standard actions else c << case # submit button when v['type'] == 'submit' caption = v['caption'] || 'drgcms.save' icon = v['icon'] || 'save' '<td class="dc-link-submit dc-animate">' + dc_submit_tag(caption, icon, {:data => v['params'], :title => v['title']}) + '</td>' # delete with some sugar added when v['type'] == 'delete' parms['id'] = @record.id parms.merge!(v['params']) caption = v['caption'] || 'drgcms.delete' icon = v['icon'] || 'remove' '<td class="dc-link dc-animate">' + dc_link_to( caption, icon, parms, data: t('drgcms.confirm_delete'), method: :delete ) + '</td>' # ajax or link button when v['type'] == 'ajax' || v['type'] == 'link' parms = {} # direct url if v['url'] parms['controller'] = v['url'] parms['idr'] = @record.id # make url else parms['controller'] = v['controller'] parms['action'] = v['action'] parms['table'] = v['table'] parms['formname'] = v['formname'] parms['id'] = @record.id # additional parameters v['params'].each { |k,v| parms[k] = v } if v['params'] end # Error if controller param is missing if parms['controller'].nil? "<td>#{t('drgcms.error')}</td>" else v['caption'] ||= v['text'] caption = t("#{v['caption'].downcase}", v['caption']) url = url_for(parms) p url request = v['request'] || v['method'] || 'get' icon = v['icon'] ? "#{fa_icon(v['icon'])} " : '' if v['type'] == 'ajax' # ajax button %Q[<td class="dc-link-ajax dc-animate" id="dc-submit-ajax" data-url="#{url}" data-request="#{request}" title="#{v['title']}">#{icon}#{caption}</td>] else # link button # %Q[<td class="dc-link dc-animate" title="#{v['title']}><a href="#{url}">#{icon}#{caption}</a></td>] %Q[<td class="dc-link dc-animate">#{dc_link_to(v['caption'],v['icon'], parms)}</td>] end end # Javascript action when v['type'] == 'script' # v['caption'] ||= 'Caption missing!' # caption = t("#{v['caption'].downcase}", v['caption']) data = {'request' => 'script', 'script' => v['js']} %Q[<td class="dc-link-ajax dc-animate">#{ dc_link_to(v['caption'],v['icon'], '#', data: data ) }</td>] else '<td>err2</td>' end end end c.html_safe end |
#dc_actions_for_index ⇒ Object
Creates action div for cmsedit index action.
37 38 39 40 41 42 43 44 45 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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'app/helpers/cmsedit_helper.rb', line 37 def dc_actions_for_index() return '' if @form['index'].nil? or @form['readonly'] actions = @form['index']['actions'] return '' if actions.nil? or actions.size == 0 # Simulate standard actions actions = {'standard' => true} if actions.class == String && actions == 'standard' std_actions = {' 2' => 'new', ' 3' => 'sort', ' 4' => 'filter' } if actions['standard'] actions.merge!(std_actions) actions['standard'] = nil end # start div with hidden spinner image html = <<EOT <div id="dc-action-menu"> <span id="dc-spinner" class="div-hidden">#{fa_icon('spinner lg spin')}</span> <ul class="dc-action-menu"> EOT # actions.each do |k,v| session[:form_processing] = "index:actions: #{k}=#{v}" next if v.nil? # must be url = @parms.clone yaml = v.class == String ? {'type' => v} : v # if single definition simulate type parameter action = yaml['type'].to_s.downcase if action == 'url' dc_deprecate "action: url will be deprecated. Use action: link in index: actions" action = 'link' end # if return_to is present link directly to URL if action == 'link' and yaml['url'] url = yaml['url'] else url['controller'] = yaml['controller'] if yaml['controller'] url['action'] = yaml['action'] || action url['table'] = yaml['table'] if yaml['table'] url['formname'] = yaml['formname'] if yaml['formname'] end # html link options yhtml = yaml['html'] || {} yhtml['title'] = yaml['title'] if yaml['title'] html << '<li class="dc-animate">' # html << case # sort when action == 'sort' then choices = [['id','id']] if @form['index']['sort'] @form['index']['sort'].split(',').each do |s| s.strip! choices << [ t("helpers.label.#{@form['table']}.#{s}"), s ] end end fa_icon('sort-alpha-asc') + ' ' + t('drgcms.sort') + ' ' + select('sort', 'sort', choices, { include_blank: true }, { class: 'drgcms_sort', 'data-table' => @form['table']} ) # filter when action == 'filter' then caption = t('drgcms.filter') caption << ' ' + fa_icon('caret-down lg') + DcFilter.(self) # add filter OFF link s = session[@form['table']] if s and s[:filter] caption << ' ' + dc_link_to(nil,'remove lg', controller: 'cmsedit', filter: 'off', table: @form['table'], title: 'drgcms.filter_off') end caption # new when action == 'new' then caption = yaml['caption'] || 'drgcms.new' dc_link_to(caption,'plus', url, yhtml ) # menu when action == 'menu' then caption = t(v['caption'], v['caption']) + ' ' + fa_icon('caret-down lg') caption + eval(v['eval']) else caption = yaml['caption'] || yaml['text'] icon = yaml['icon'] ? yaml['icon'] : action dc_link_to(caption, icon, url, yhtml) end html << '</li>' end html << '</ul>' html << DcFilter.get_filter_field(self) html << '</div>' html.html_safe end |
#dc_actions_for_result(document) ⇒ Object
Creates actions that could be performed on single row of result set.
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 |
# File 'app/helpers/cmsedit_helper.rb', line 195 def dc_actions_for_result(document) actions = @form['result_set']['actions'] return '' if actions.nil? or @form['readonly'] # standard actions actions = {'standard' => true} if actions.class == String && actions == 'standard' std_actions = {' 2' => 'edit', ' 3' => 'delete'} actions.merge!(std_actions) if actions['standard'] # width = @form['result_set']['actions_width'] || 20*actions.size html = "<td style=\"width: #{width}px;\">" actions.each do |k,v| session[:form_processing] = "result_set:actions: #{k}=#{v}" next if k == 'standard' # ignore standard definition parms = @parms.clone yaml = v.class == String ? {'type' => v} : v # if single definition simulate type parameter html << case when yaml['type'] == 'edit' then parms['action'] = 'edit' parms['id'] = document.id dc_link_to( nil, 'pencil lg', parms ) when yaml['type'] == 'duplicate' then parms['id'] = document.id # duplicate string will be added to these fields. parms['dup_fields'] = yaml['dup_fields'] parms['action'] = 'create' dc_link_to( nil, 'copy lg', parms, data: { confirm: t('drgcms.confirm_dup') }, method: :post ) when yaml['type'] == 'delete' then parms['action'] = 'destroy' parms['id'] = document.id dc_link_to( nil, 'remove lg', parms, data: { confirm: t('drgcms.confirm_delete') }, method: :delete ) # undocumented so far when yaml['type'] == 'edit_embedded' parms['controller'] = 'cmsedit' parms['table'] += ";#{yaml['table']}" parms['ids'] ||= '' parms['ids'] += "#{document.id};" dc_link_to( nil, 'table lg', parms, method: :get ) when yaml['type'] == 'link' || yaml['type'] == 'ajax' then if yaml['url'] parms['controller'] = yaml['url'] parms['idr'] = document.id else parms['id'] = document.id end parms['controller'] = yaml['controller'] if yaml['controller'] parms['action'] = yaml['action'] if yaml['action'] parms['table'] = yaml['table'] if yaml['table'] parms['formname'] = yaml['formname'] if yaml['formname'] parms['target'] = yaml['target'] if yaml['target'] dc_link_or_ajax(yaml, parms) else # error. yaml['type'].to_s end end html << '</td>' html.html_safe end |
#dc_check_and_default(value, default, values = nil) ⇒ Object
Checks if value is defined and sets default. If values are sent it also checks if value is found in values. If not it will report error and set value to default. Subroutine of dc_fields_for_tab.
579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 |
# File 'app/helpers/cmsedit_helper.rb', line 579 def dc_check_and_default(value, default, values=nil) #:nodoc: return default if value.nil? # check if value is within allowed values if values if !values.index(value) # parameters should be in downcase. Check downcase version. if n = values.index(value.downcase) return values[n] else logger.error("DRG Forms: Value #{value} not within values [#{values.join(',')}]. Default #{default} used!") return default end end end value end |
#dc_clicks_for_result(document) ⇒ Object
Creates div with documents of current result set.
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 |
# File 'app/helpers/cmsedit_helper.rb', line 291 def dc_clicks_for_result(document) html = '' if @form['result_set']['dblclick'] yaml = @form['result_set']['dblclick'] opts = {} opts[:controller] = yaml['controller'] || 'cmsedit' opts[:action] = yaml['action'] opts[:table] = yaml['table'] opts[:formname] = yaml['formname'] opts[:method] = yaml['method'] || 'get' opts[:id] = document['id'] html << ' data-dblclick=' + url_for(opts) else html << (' data-dblclick=' + url_for(action: 'show', controller: 'cmsedit', id: document, readonly: (params[:readonly] ? 2 : 1), table: params[:table], formname: params[:formname], ids: params[:ids]) ) if @form['form'] end html end |
#dc_columns_for_result(document) ⇒ Object
Creates column for each field of result set document.
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 |
# File 'app/helpers/cmsedit_helper.rb', line 360 def dc_columns_for_result(document) html = '' if (columns = @form['result_set']['columns']) columns.each do |k,v| session[:form_processing] = "result_set:columns: #{k}=#{v}" # convert shortcut to hash v = {'name' => v} if v.class == String # eval value = if v['eval'] if v['eval'].match('dc_name4_id') a = v['eval'].split(',') if a.size == 3 dc_name4_id(a[1], a[2], nil, document[ v['name'] ]) else dc_name4_id(a[1], a[2], a[3], document[ v['name'] ]) end elsif v['eval'].match('dc_name4_value') dc_name4_value( @form['table'], v['name'], document[ v['name'] ] ) elsif v['eval'].match('eval ') # evaluate with specified parameters else if v['params'] if v['params'] == 'document' # pass document as parameter when all eval( "#{v['eval']} document") else # list of fields delimeted by , params = v['params'].chomp.split(',').inject('') do |result,e| result << (e.match(/\.|\:|\(/) ? e : "document['#{e.strip}']") + ',' end params.chomp!(',') eval( "#{v['eval']} #{params}") end else eval( "#{v['eval']} '#{document[ v['name'] ]}'") end end # as field elsif document.respond_to?(v['name']) dc_format_value(document.send( v['name'] ), v['format']) # as hash (dc_dummy) elsif document.class == Hash document[ v['name'] ] # error else "!!! #{v['name']}" end # td = '<td ' td << dc_style_or_class('class',v['td_class'],value,document) td << dc_style_or_class('style',v['td_style'] || v['style'],value,document) html << "#{td}>#{value}</td>" end end html.html_safe end |
#dc_div_filter ⇒ Object
Creates filter div for cmsedit index/filter action.
127 128 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 |
# File 'app/helpers/cmsedit_helper.rb', line 127 def dc_div_filter() choices = [] filter = (@form['index'] and @form['index']['filter']) ? @form['index']['filter'] + ',' : '' filter << 'id as text_field' # filter id is added by default filter.split(',').each do |f| f.strip! name = f.match(' as ') ? f.split(' ').first : f choices << [ t("helpers.label.#{@form['table']}.#{name}", name), f ] end choices4_operators = t('drgcms.choices4_filter_operators').chomp.split(',').inject([]) {|r,v| r << (v.match(':') ? v.split(':') : v )} # currently selected options if session[@form['table']] and session[@form['table']][:filter] field_name, operators_value, dummy = session[@form['table']][:filter].split("\t") else field_name, operators_value = nil, nil end #{ form_tag :table => @form['table'], filter: :on, filter_input: 1, action: :index, method: :post } url = url_for(:table => @form['table'], filter: :on, filter_input: 1, action: :index, controller: :cmsedit) html =<<EOT <div id="drgcms_filter" class="div-hidden"> <h1>#{t('drgcms.filter_set')}</h1> <table class="dc-menu"><td> #{ select(nil, 'filter_field1', (choices, field_name), { include_blank: true }) } #{ select(nil, 'filter_oper', (choices4_operators, operators_value)) } </td> <td class="dc-link dc-animate drgcms_popup_submit" data-url="#{url}">#{fa_icon('check-square-o')} #{t('drgcms.filter_on')}</td> <td class="dc-link dc-animate">#{dc_link_to('drgcms.filter_off','close', {action: 'index', filter: 'off', :table => @form['table']}) }</td> </table> </div> EOT html.html_safe end |
#dc_document_statistics ⇒ Object
Creates current document statistics div (created_by, created_at, .…) at the bottom of edit form. + lots of more. At the moment also adds icon for dumping current document as json text.
743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 |
# File 'app/helpers/cmsedit_helper.rb', line 743 def dc_document_statistics return '' if @record.new_record? or dc_dont?(@form['form']['info']) html = "<div id='dc-document-info'>#{t('drgcms.doc_info')}</div>" # html = "<div id='dc-document-info'>#{fa_icon 'info-circle 2x'}</div>" html = '<div id="dc-document-info">' + fa_icon('info-circle lg') + '</div>' # html << "<div id='dc-document-info-popup' class='div-hidden'><table>" u = _get_user_for('created_by') html << "<tr><td>#{t('drgcms.created_by', 'Created by')}: </td><td><b>#{u}</td></tr>" if u u = _get_user_for('updated_by') html << "<tr><td>#{t('drgcms.updated_by', 'Updated by')}: </td><td><b>#{u}</td></tr>" if u html << "<tr><td>#{t('drgcms.created_at', 'Created at')}: </td><td><b>#{dc_format_value(@record.created_at)}</td></tr>" if @record['created_at'] html << "<tr><td>#{t('drgcms.updated_at', 'Updated at')}: </td><td><b>#{dc_format_value(@record.updated_at)}</td></tr>" if @record['updated_at'] html << '</table>' # Copy to clipboard icon parms = params.clone parms[:controller] = 'dc_common' parms[:action] = 'copy_clipboard' url = url_for(parms) # caption = image_tag('drg_cms/copy.png', title: t('drgcms.doc_copy_clipboard')) # html << %Q[<hr><img class="dc-link-img dc-link-ajax dc-animate" data-url="#{url}" data-request="get" #{caption}] html << fa_icon('copy 2x', class: 'dc-link-img dc-link-ajax dc-animate', 'data-url' => url, 'data-request' => 'get', title: t('drgcms.doc_copy_clipboard') ) (html << '</div>').html_safe # html.html_safe end |
#dc_fields_for_form ⇒ Object
Creates edit form div.
688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 |
# File 'app/helpers/cmsedit_helper.rb', line 688 def dc_fields_for_form() html, tabs, tdata = '',[], '' # Only fields defined if (fields = @form['form']['fields']) html << "<div id='data_fields' " + (@form['form']['height'] ? "style=\"height: #{@form['form']['height']}px;\">" : '>') html << dc_fields_for_tab(fields) + '</div>' else # there are multiple tabs on form first = true # first tab @form['form']['tabs'].keys.sort.each do |tabname| next if tabname.match('actions') # Tricky. If field name is not on the tab skip to next tab if params[:edit_only] is_on_tab = false @form['form']['tabs'][tabname].each {|k,v| is_on_tab = true if params[:edit_only] == v['name'] } next unless is_on_tab end # first div is displayed all other are hidden tdata << "<div id='data_#{tabname}'" tdata << ' class="div-hidden"' unless first tdata << " style=\"height: #{@form['form']['height']}px;\"" if @form['form']['height'] tdata << ">#{dc_fields_for_tab(@form['form']['tabs'][tabname])}</div>" tabs << tabname first = false end # make it all work together html << '<ul class="dc-form-ul" >' first = true # first tab must be selected tabs.each do |tab| html << "<li id='li_#{tab}' data-div='#{tab}' class='dc-form-li #{'dc-form-li-selected' if first }'>#{t_name(tab, tab)}</li>" first = false end html << '</ul>' html << tdata end # add last_updated_at hidden field so controller can check if record was updated in during editing html << hidden_field(nil, :last_updated_at, :value => @record.updated_at.to_i) if @record.respond_to?(:updated_at) html.html_safe end |
#dc_fields_for_tab(fields) ⇒ Object
Creates input fields for one tab. Subroutine of dc_fields_for_form.
611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 |
# File 'app/helpers/cmsedit_helper.rb', line 611 def dc_fields_for_tab(fields) #:nodoc: @js ||= '' html = '<table class="dc-form-table">' labels_pos = dc_check_and_default(@form['form']['labels_pos'], 'right', ['top','left','right']) current_column = 0 hidden_fields = '' odd_even = nil reset_cycle() # options and fields must be separated before sorting = fields.select {|field| field.class != Fixnum } columns = .try(:[],'columns') || 1 # Select form fields and sort them by key form_fields = fields.select {|field| field.class == Fixnum } form_fields.to_a.sort.each do |element| = element.last session[:form_processing] = "form:fields: #{element.first}=#{}" # ignore if edit_only singe field is required next if params[:edit_only] and params[:edit_only] != ['name'] # hidden_fields. Add them at the end if ['type'] == 'hidden_field' hidden_fields << DrgcmsFormFields::HiddenField.new(self, @record, ).render next end # initialize when column is 0 if current_column == 0 odd_even = cycle('odd','even') current_column = columns end # label text = if ['text'] t(['text'], ['text']) else t_name(['name'], ['name'].capitalize.gsub('_',' ') ) end # help text can be defined in form or in translations starting with helpers. or as helpers.help.collection.field help = if ['help'] ['help'].match('helpers.') ? t(['help']) : ['help'] end help ||= t('helpers.help.' + @form['table'] + '.' + ['name'],' ') # create field object from class and call its render method klas_string = ['type'].camelize field_html = if DrgcmsFormFields.const_defined?(klas_string) # check if field type is defined klas = DrgcmsFormFields.const_get(klas_string) field = klas.new(self, @record, ).render @js << field.js field.html else # litle error string "Error: Code for field type #{['type']} not defined!" end # Line separator html << top_bottom_line(['top-line'], columns) if ['top-line'] html << '<tr>' if current_column == columns colspan = ['colspan'] || 1 # html << if labels_pos == 'top' %Q[<td class="dc-form-label-top dc-color-#{odd_even} dc-align-left" title="#{help}" colspan="#{colspan*2}"> <div><label for="record_#{['name']}">#{text} </label></div> <div id="td_record_#{['name']}">#{field_html}</div></td> ] else %Q[<td class="dc-form-label dc-color-#{odd_even} dc-align-#{labels_pos}" title="#{help}"> <label for="record_#{['name']}">#{text} </label></td> <td id="td_record_#{['name']}" class="dc-form-field dc-color-#{odd_even}" colspan="#{colspan*2 - 1}">#{field_html} </td> ] end # check if must go to next line current_column -= colspan html << '</tr>' if current_column == 0 html << top_bottom_line(['bottom-line'], columns) if ['bottom-line'] end html << '</table>' << hidden_fields end |
#dc_format_value(value, format = nil) ⇒ Object
Formats value according to format supplied or data type. There is lots of things missing here.
315 316 317 318 319 320 321 322 323 324 325 326 327 328 |
# File 'app/helpers/cmsedit_helper.rb', line 315 def dc_format_value(value, format=nil) # :TODO: Enable formating numbers. return '' if value.nil? klass = value.class.to_s if klass.match('Time') format ||= t('time.formats.default') value.strftime(format) elsif klass.match('Date') format ||= t('date.formats.default') value.strftime(format) else value.to_s end end |
#dc_header_for_result ⇒ Object
Creates header div for result set.
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 |
# File 'app/helpers/cmsedit_helper.rb', line 256 def dc_header_for_result() c = '' actions = @form['result_set']['actions'] c = '<th> </th>' unless actions.nil? or @form['readonly'] # preparation for sort icon sort_field, sort_direction = nil, nil if session[@form['table']] sort_field, sort_direction = session[@form['table']][:sort].to_s.split(' ') end # if (columns = @form['result_set']['columns']) columns.each do |k,v| session[:form_processing] = "result_set:columns: #{k}=#{v}" th = '<th ' v = {'name' => v} if v.class == String caption = v['caption'] || t("helpers.label.#{@form['table']}.#{v['name']}") # no sorting when embedded field or custom filter is active if @tables.size == 1 and @form['result_set']['filter'].nil? icon = 'sort lg' if v['name'] == sort_field icon = sort_direction == '1' ? 'sort-alpha-asc lg' : 'sort-alpha-desc lg' end th << ">#{dc_link_to(caption, icon, sort: v['name'], table: @tables[0][1], action: :index )}</th>" else th << ">#{caption}</th>" end c << th end end c.html_safe end |
#dc_link_or_ajax(yaml, parms) ⇒ Object
Creates code for link or ajax action type. Subroutine of dc_actions_for_result.
177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'app/helpers/cmsedit_helper.rb', line 177 def dc_link_or_ajax(yaml, parms) #:nodoc: rest = {} rest['method'] = yaml['method'] || yaml['request'] || 'get' rest['caption'] = yaml['caption'] || yaml['text'] rest['class'] = rest['class'].to_s + ' dc-animate' rest['title'] = yaml['title'] dc_deprecate "Form: result_set:action:text directive will be deprecated. Use caption instead of text." if yaml['text'] if yaml['type'] == 'link' dc_link_to(yaml['caption'], yaml['icon'], parms, rest ) else '' end end |
#dc_row_for_result(document) ⇒ Object
Creates tr code for each row of result set.
351 352 353 354 355 |
# File 'app/helpers/cmsedit_helper.rb', line 351 def dc_row_for_result(document) clas = "dc-#{cycle('odd','even')} " + dc_style_or_class(nil,@form['result_set']['tr_class'],nil,document) style = dc_style_or_class('style',@form['result_set']['tr_style'],nil,document) "<tr class=\"#{clas}\" #{dc_clicks_for_result(document)} #{style}>".html_safe end |
#dc_style_or_class(selector, yaml, value, record) ⇒ Object
Defines style or class for row (tr) or column (td)
333 334 335 336 337 338 339 340 341 342 343 344 345 346 |
# File 'app/helpers/cmsedit_helper.rb', line 333 def dc_style_or_class(selector, yaml, value, record) return '' if yaml.nil? # alias record and value so both names can be used field = value document = record html = selector ? "#{selector}=\"" : '' html << if yaml.class == String yaml else (yaml['eval'] ? eval(yaml['eval']) : '') rescue 'background-color:red;' end html << '"' if selector html end |
#dc_table_title_for_result(result = nil) ⇒ Object
Creates title div for cmsedit index result set records. Title div also includes paging options.
165 166 167 168 169 170 171 172 |
# File 'app/helpers/cmsedit_helper.rb', line 165 def dc_table_title_for_result(result=nil) title = if @form['title'] # form has title section t(@form['title'],@form['title']) else # get name from translations t('helpers.label.' + @form['table'] + '.tabletitle', @form['table']) end dc_table_title(title, result) end |
#top_bottom_line(yaml, columns = 2) ⇒ Object
Creates top or bottom horizontal line on form.
599 600 601 602 603 604 605 606 |
# File 'app/helpers/cmsedit_helper.rb', line 599 def top_bottom_line(yaml, columns=2) if yaml.class == Hash clas = yaml['class'] style = yaml['style'] end clas ||= 'dc-separator' "<tr><td colspan=\"#{columns*2}\" class=\"#{clas}\" style=\"#{style}\"></td></tr>" end |