Module: BelongsToControl

Defined in:
app/controls/belongs_to_control.rb

Overview

Controls for belongs_to AgileRails Form field.

Instance Method Summary collapse

Instance Method Details

#before_saveObject

Before save, set required fields



49
50
51
52
53
54
55
56
57
58
59
# File 'app/controls/belongs_to_control.rb', line 49

def before_save
  if @record.respond_to?(:parent_type)
    @record.parent_type = @tables.first[0].to_s
    @record.parent_id   = params[:ids]
  else
    parent_table_index = @tables.size - 2
    belongs_to_field = "#{params[:belongs_to] || @tables[parent_table_index][1]}_id="
    ids = params[:ids].split(';')
    @record.send(belongs_to_field, ids[parent_table_index])
  end
end

#default_filterObject

Default filter for selecting related records



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'app/controls/belongs_to_control.rb', line 32

def default_filter
  model = @tables.last[0]
  return YAML.load(@record[params[:belongs_to]]) if model == 'ar_memory'

  if model.column_names.include? 'parent_type'
    model.where(parent_type: @tables.first[0].to_s, parent_id: params[:ids])
  else
    parent_table_index = @tables.size - 2
    belongs_to_field = "#{(params[:belongs_to] || @tables[parent_table_index][1])}_id"
    ids = params[:ids].split(';')
    model.where(belongs_to_field => ids[parent_table_index])
  end
end