cirandas.net

ref: master

app/helpers/custom_fields_helper.rb


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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
module CustomFieldsHelper

  def format_name(format)
    names = {}
    names['string'] = _('String')
    names['text'] = _('Text')
    names['date'] = _('Date')
    names['numeric'] = _('Numeric')
    names['link'] = _('Link')
    names['list'] = _('List')
    names['checkbox'] = _('Checkbox')
    names[format]
  end

  def custom_field_forms(customized_type)
    forms = []
    forms << [_('String'), form_for_format(customized_type,'string')]
    forms << [_('Text'), form_for_format(customized_type,'text')]
    forms << [_('Date'), form_for_format(customized_type,'date')]
    forms << [_('Numeric'), form_for_format(customized_type,'numeric')]
    forms << [_('Link'), form_for_format(customized_type,'link')]
    forms << [_('List'), form_for_format(customized_type,'list')]
    forms << [_('Checkbox'), form_for_format(customized_type,'checkbox')]
    forms
  end

  def render_extras_field(id, extra=nil, field=nil)
    if extra.nil?
      CGI::escapeHTML((render(:partial => 'features/custom_fields/extras_field', :locals => {:id => id, :extra => nil, :field => field})))
    else
      render :partial => 'features/custom_fields/extras_field', :locals => {:id => id, :extra => extra, :field => field}
    end
  end

  def form_for_field(field, customized_type)
    render :partial => 'features/custom_fields/form', :locals => {:field => field}
  end

  def display_custom_field_value(custom_field_value)
    value_for_format custom_field_value.custom_field.format, custom_field_value.value
  end

  def display_value_for_custom_field(custom_field, value)
    value_for_format custom_field.format, value
  end

  def value_for_format format, value
    case format
    when 'text', 'list', 'numeric', 'date', 'string'
      value
    when 'checkbox'
      value == "1" ? _('Yes') : _('No')
    when 'link'
      url = value[/\Ahttps?:\/\//i] ? value : "http://#{value}"
      link_to(value, url, :target => '_blank')
    end

  end

  private

  def form_for_format(customized_type, format)
    field = CustomField.new(:format => format, :customized_type => customized_type, :environment => environment)
    CGI::escapeHTML((render(:partial => 'features/custom_fields/form', :locals => {:field => field}))).html_safe
  end
end