Class: AgileFormFields::BelongsTo

Inherits:
AgileFormField show all
Defined in:
app/models/agile_form_fields/belongs_to.rb

Overview

Implementation of belongs_to AgileRails form field. belongs_to form field is used to edit records that are related to currently edited record.

Related records are edited inside iframe.

===Form options:

  • name: field name (required)
  • type: belongs_to (required)
  • form_name: name of form which will be used for editing
  • load: when is embedded iframe loaded. default=on form load, delay=on tab select, always=every time tab is selected)
  • belongs_to: optionaly define name of model, to which edited table is related
  • html: html options (optional)
    • height: height of embedded object in pixels (1000)
    • width: width of embedded object in pixels (500)

Form example:

10:
 name: ar_parts
 type: belongs_to
 form_name: ar_part
 refresh: delay
 belongs_to: ar_page
 html:
   height: 1000

Instance Attribute Summary

Attributes inherited from AgileFormField

#css, #js

Instance Method Summary collapse

Methods inherited from AgileFormField

get_data, #hash_to_options, #html, #initialize, #options_to_hash, #record_text_for, #ro_standard, #set_css_code, #set_default_value, #set_initial_value, #set_style, #t

Constructor Details

This class inherits a constructor from AgileFormFields::AgileFormField

Instance Method Details

#renderObject

Render belongs_to AgileRails form field code



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
# File 'app/models/agile_form_fields/belongs_to.rb', line 56

def render
  # some HTML5 defaults
  @yaml['html'] ||= {}
  @yaml['html']['width'] ||= '99%'

  @yaml['action'] ||= 'index'
  # defaults both way 
  @yaml['table']     ||= @yaml['form_name'] if @yaml['form_name']
  @yaml['form_name'] ||= @yaml['table'] if @yaml['table']

  if @yaml['name'] == @yaml['table'] || @yaml['table'] == 'ar_memory'
    tables = @yaml['table']
    ids    = @record.id
  else
    tables = @env.tables.inject('') { |r, v| r += "#{v[1]};" } + @yaml['table']
    ids    = (@env.ids.present? ? @env.ids.join(';') + ';' : '') + @record.id.to_s
  end
  # message when new record
  if @record.new_record? && tables != 'ar_temp'
    @yaml['html']['srcdoc'] = %(
<div style='font-family: helvetica; font-size: 1.7rem; font-weight: bold; color: #ddd; padding: 1rem'>
  #{I18n.t('agile.iframe_save_to_view')}
</div>)
  end
  html5 = @yaml['html'].map{ |k, v| %(#{k}="#{v}") }.join(' ')

  # edit enabled belong_to form on a readonly form
  readonly = AgileHelper.dont?(@yaml['readonly']) ? nil : @readonly
  opts = { controller: :agile, action: @yaml['action'],
           ids: ids, table: tables, form_name: @yaml['form_name'], 
           field_name: @yaml['name'], iframe: "if_#{@yaml['name']}", readonly: readonly }
  # additional parameters if specified
  @yaml['params'].each { |k, v| opts[k] = @env.agile_value_for_parameter(v) } if @yaml['params']
         
  @html += "<iframe class='iframe_embedded' id='if_#{@yaml['name']}' name='if_#{@yaml['name']}' #{html5}></iframe>"
  return self if @record.new_record? && tables != 'ar_temp'

  url  = @env.url_for(opts)
  attributes = case @yaml['load']
               when nil?, 'default'
                 "'src', '#{url}'"
               when 'delay'
                 "'data-src-#{@yaml['load']}', '#{url}'"
               else
                 "{'data-src-#{@yaml['load']}': '#{url}', src: '#{url}'}"
               end
  @js += %(
$(document).ready( function() {
  $('#if_#{@yaml['name']}').attr(#{attributes});
});)

  self
end