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.



11
12
13
14
15
# File 'lib/sidebar_field.rb', line 11

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

Instance Attribute Details

#defaultObject

Returns the value of attribute default.



6
7
8
# File 'lib/sidebar_field.rb', line 6

def default
  @default
end

#keyObject

Returns the value of attribute key.



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

def key
  @key
end

#optionsObject

Returns the value of attribute options.



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

def options
  @options
end

Class Method Details

.build(key, default, options) ⇒ Object



103
104
105
# File 'lib/sidebar_field.rb', line 103

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

.class_for(options) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/sidebar_field.rb', line 107

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



37
38
39
# File 'lib/sidebar_field.rb', line 37

def canonicalize(value)
  value
end

#current_value(sidebar) ⇒ Object



41
42
43
# File 'lib/sidebar_field.rb', line 41

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

#input_html(sidebar) ⇒ Object



25
26
27
# File 'lib/sidebar_field.rb', line 25

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

#input_name(sidebar) ⇒ Object



33
34
35
# File 'lib/sidebar_field.rb', line 33

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

#labelObject



17
18
19
# File 'lib/sidebar_field.rb', line 17

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

#label_html(_sidebar) ⇒ Object



21
22
23
# File 'lib/sidebar_field.rb', line 21

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

#line_html(sidebar) ⇒ Object



29
30
31
# File 'lib/sidebar_field.rb', line 29

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