Class: SidebarField

Inherits:
Object
  • Object
show all
Includes:
ActionView::Helpers::FormOptionsHelper, ActionView::Helpers::FormTagHelper, ActionView::Helpers::TagHelper
Defined in:
lib/sidebar_field.rb

Direct Known Subclasses

CheckBoxField, RadioField, SelectField, TextAreaField

Defined Under Namespace

Classes: CheckBoxField, RadioField, SelectField, TextAreaField

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, default, options = {}) ⇒ SidebarField

Returns a new instance of SidebarField.



9
10
11
12
13
# File 'lib/sidebar_field.rb', line 9

def initialize(key, default, options = {})
  @key = key.to_s
  @default = default
  @options = options
end

Instance Attribute Details

#defaultObject

Returns the value of attribute default.



4
5
6
# File 'lib/sidebar_field.rb', line 4

def default
  @default
end

#keyObject

Returns the value of attribute key.



2
3
4
# File 'lib/sidebar_field.rb', line 2

def key
  @key
end

#optionsObject

Returns the value of attribute options.



3
4
5
# File 'lib/sidebar_field.rb', line 3

def options
  @options
end

Class Method Details

.build(key, default, options) ⇒ Object



98
99
100
# File 'lib/sidebar_field.rb', line 98

def self.build(key, default, options)
  class_for(options).new(key, default, options)
end

.class_for(options) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/sidebar_field.rb', line 102

def self.class_for(options)
  case options[:input_type]
  when :text_area
    TextAreaField
  when :textarea
    TextAreaField
  when :radio
    RadioField
  when :checkbox
    CheckBoxField
  when :select
    SelectField
  else
    if options[:choices]
      SelectField
    else
      self
    end
  end
end

Instance Method Details

#canonicalize(value) ⇒ Object



35
36
37
# File 'lib/sidebar_field.rb', line 35

def canonicalize(value)
  value
end

#current_value(sidebar) ⇒ Object



39
40
41
# File 'lib/sidebar_field.rb', line 39

def current_value(sidebar)
  canonicalize(sidebar.config[key])
end

#input_html(sidebar) ⇒ Object



23
24
25
# File 'lib/sidebar_field.rb', line 23

def input_html(sidebar)
  text_field_tag(input_name(sidebar), current_value(sidebar), class: 'form-control')
end

#input_name(sidebar) ⇒ Object



31
32
33
# File 'lib/sidebar_field.rb', line 31

def input_name(sidebar)
  "configure[#{sidebar.id}][#{key}]"
end

#labelObject



15
16
17
# File 'lib/sidebar_field.rb', line 15

def label
  options[:label] || key.humanize.gsub(/url/i, 'URL')
end

#label_html(_sidebar) ⇒ Object



19
20
21
# File 'lib/sidebar_field.rb', line 19

def label_html(_sidebar)
  ('label', label)
end

#line_html(sidebar) ⇒ Object



27
28
29
# File 'lib/sidebar_field.rb', line 27

def line_html(sidebar)
  (:div, label_html(sidebar) + input_html(sidebar), class: 'form-group')
end