Module: Pigeon::ChannelHelper

Includes:
ActionView::Helpers::CaptureHelper, ActionView::Helpers::FormHelper, ActionView::Helpers::FormOptionsHelper
Included in:
Renderer::ChannelRenderer
Defined in:
app/helpers/pigeon/channel_helper.rb

Instance Method Summary collapse

Instance Method Details

#pigeon_attribute(attribute, value = nil, options = {}) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'app/helpers/pigeon/channel_helper.rb', line 52

def pigeon_attribute(attribute, value = nil, options = {})
  scope = options.delete(:scope)
  field_with_errors = options.delete(:field_with_errors)
   :div, options do
    label = pigeon_attribute_label(attribute, scope: scope)
    field = pigeon_attribute_field(attribute, value, scope: scope)
    if field_with_errors.present?
      field =  :div, field, :class => 'field_with_errors'
    end
    if attribute.type == :boolean
      safe_join([field, label])
    elsif attribute.type == :hidden
      field
    else
      safe_join([label, tag(:br), field])
    end
  end
end

#pigeon_attribute_field(attribute, value = nil, options = {}) ⇒ Object



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
# File 'app/helpers/pigeon/channel_helper.rb', line 7

def pigeon_attribute_field(attribute, value = nil, options = {})
  scope = options.delete(:scope)
  field_name = attribute.scoped_name(scope)
  field_value = value || ''
  forced_type = options.delete(:type)
  render_type = (forced_type || attribute.type).to_s

  case render_type
  when "string"
    text_field_tag(field_name, field_value, options)
  when "password"
    password_field_tag(field_name, field_value, options)
  when "integer"
    number_field_tag(field_name, field_value, options)
  when "enum"
    choices = attribute.options
    if choices.length > 0 && choices[0].is_a?(Hash)
      choices = choices.map { |h| [h["display"], h["value"]] }
    end
    select_tag(field_name, options_for_select(choices, field_value), options)
  when "multi"
    choices = attribute.options
    if choices.length > 0 && choices[0].is_a?(Hash)
      choices = choices.map { |h| [h["display"], h["value"]] }
    end
    select_tag(field_name, options_for_select(choices, field_value), { :multiple => true }.merge(options))
  when "timezone"
    select_tag(field_name, time_zone_options_for_select(field_value), options)
  when "boolean"
    check_box_tag(field_name, '1', field_value.present?, options)
  when "hidden"
    hidden_field_tag(field_name, field_value, options)
  else
    text_field_tag(field_name, field_value, 
                   { :type => render_type }.merge(options))
  end
end

#pigeon_attribute_label(attribute, options = {}) ⇒ Object



45
46
47
48
49
50
# File 'app/helpers/pigeon/channel_helper.rb', line 45

def pigeon_attribute_label(attribute, options = {})
  return '' if attribute.type == :hidden
  scope = options.delete(:scope)
  field_name = attribute.scoped_name(scope)
  label_tag(field_name, attribute.label, options)
end

#pigeon_channel_attribute(channel, attr_name, options = {}) ⇒ Object



89
90
91
92
93
94
95
96
97
# File 'app/helpers/pigeon/channel_helper.rb', line 89

def pigeon_channel_attribute(channel, attr_name, options = {})
  value = channel.read_attribute(attr_name)
  attribute = channel.schema.try(:find_attribute, attr_name)
  attribute ||= ChannelAttribute.build_default(attr_name, value)
  if channel.errors.include?(attr_name.to_sym)
    options[:field_with_errors] = true
  end
  pigeon_attribute(attribute, value, options)
end

#pigeon_channel_attribute_field(channel, attr_name, options = {}) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
# File 'app/helpers/pigeon/channel_helper.rb', line 71

def pigeon_channel_attribute_field(channel, attr_name, options = {})
  value = channel.read_attribute(attr_name)
  attribute = channel.schema.try(:find_attribute, attr_name)
  attribute ||= ChannelAttribute.build_default(attr_name, value)
  field = pigeon_attribute_field(attribute, value, options)
  if channel.errors.include?(attr_name.to_sym)
     :div, field, :class => 'field_with_errors'
  else
    field
  end
end

#pigeon_channel_attribute_label(channel, attr_name, options = {}) ⇒ Object



83
84
85
86
87
# File 'app/helpers/pigeon/channel_helper.rb', line 83

def pigeon_channel_attribute_label(channel, attr_name, options = {})
  attribute = channel.schema.try(:find_attribute, attr_name)
  attribute ||= ChannelAttribute.build_default(attr_name)
  pigeon_attribute_label(attribute, options)
end