Module: PopulateMe::DocumentMixins::Outcasting

Included in:
PopulateMe::Document
Defined in:
lib/populate_me/document_mixins/outcasting.rb

Instance Method Summary collapse

Instance Method Details

#outcast(field, item, o = {}) ⇒ Object

This module prepares the field for being send to the Admin API and build the form. It compiles the value and all other info in a hash. Therefore, it is a complement to the AdminAdapter module.



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/populate_me/document_mixins/outcasting.rb', line 10

def outcast field, item, o={}
  item = item.dup
  item[:input_name] = "#{o[:input_name_prefix]}[#{item[:field_name]}]"
  unless item[:type]==:list
    WebUtils.ensure_key! item, :input_value, self.__send__(field)
  end
  meth = "outcast_#{item[:type]}".to_sym
  if respond_to?(meth)
    __send__(meth, field, item, o) 
  else
    item
  end
end

#outcast_attachment(field, item, o = {}) ⇒ Object



74
75
76
77
78
79
# File 'lib/populate_me/document_mixins/outcasting.rb', line 74

def outcast_attachment field, item, o={}
  item = item.dup
  item[:url] = self.attachment(field).url
  item[:multiple] = (self.new? and self.class.batch_field == field)
  item
end

#outcast_list(field, item, o = {}) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/populate_me/document_mixins/outcasting.rb', line 32

def outcast_list field, item, o={}
  item = item.dup
  item[:items] = self.__send__(field).map do |nested|
   nested.to_admin_form(o.merge(input_name_prefix: item[:input_name]+'[]'))
  end
  item
end

#outcast_price(field, item, o = {}) ⇒ Object



81
82
83
84
85
86
87
# File 'lib/populate_me/document_mixins/outcasting.rb', line 81

def outcast_price field, item, o={}
  item = item.dup
  if item[:input_value].is_a?(Integer)
    item[:input_value] = WebUtils.display_price item[:input_value]
  end
  item
end

#outcast_select(field, item, o = {}) ⇒ Object



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
# File 'lib/populate_me/document_mixins/outcasting.rb', line 40

def outcast_select field, item, o={}
  item = item.dup
  unless item[:select_options].nil?
    if item[:multiple]==true
      item[:input_name] = item[:input_name]+'[]'
    end
    opts = WebUtils.deep_copy(WebUtils.get_value(item[:select_options],self))
    opts.map! do |opt|
      if opt.is_a?(String)||opt.is_a?(Symbol)
        opt = [opt.to_s.capitalize,opt]
      end
      if opt.is_a?(Array)
        opt = {description: opt[0].to_s, value: opt[1].to_s}
      end
      if item[:input_value].respond_to?(:include?)
        opt[:selected] = true if item[:input_value].include?(opt[:value])
      else
        opt[:selected] = true if item[:input_value]==opt[:value]
      end
      opt
    end
    if item[:multiple]
      (item[:input_value]||[]).reverse.each do |iv|
        opt = opts.find{|opt| opt[:value]==iv }
        opts.unshift(opts.delete(opt)) unless opt.nil?
      end
    end
    item[:select_options] = opts
    item
  else
    item
  end
end

#outcast_string(field, item, o = {}) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/populate_me/document_mixins/outcasting.rb', line 24

def outcast_string field, item, o={}
  if item.key? :autocomplete
    item = item.dup
    item[:autocomplete] = WebUtils.deep_copy(WebUtils.get_value(item[:autocomplete],self))
  end
  item
end