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.



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

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.



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

def key
  @key
end

#optionsObject

Returns the value of attribute options.



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

def options
  @options
end

Class Method Details

.build(key, default, options) ⇒ Object



100
101
102
# File 'lib/sidebar_field.rb', line 100

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

.class_for(options) ⇒ Object



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

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



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

def canonicalize(value)
  value
end

#current_value(sidebar) ⇒ Object



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

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

#input_html(sidebar) ⇒ Object



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

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

#input_name(sidebar) ⇒ Object



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

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

#labelObject



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

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

#label_html(_sidebar) ⇒ Object



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

def label_html(_sidebar)
  tag.label(label)
end

#line_html(sidebar) ⇒ Object



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

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