Class: Matestack::Ui::VueJs::Components::Form::Checkbox

Inherits:
Base
  • Object
show all
Defined in:
lib/matestack/ui/vue_js/components/form/checkbox.rb

Instance Method Summary collapse

Methods inherited from Base

#attribute_key, #attributes, #change_event, #component_attributes, #display_errors?, #error_class, #error_config, #error_key, #error_tag, #form_context, #get_from_error_config, #id, #init, #init_value, #input_error_class, #input_key, #input_label, #key, #multiple, #name, #placeholder, #render_errors, #type, #v_model_type, #value_type, #wrapper_error_class, #wrapper_tag

Methods inherited from Vue

#base_vue_props, #component_attributes, #component_uid, #create_children, inherited, #initialize, #matestack_ui_vuejs_ref, #vue_component, vue_name, #vue_name

Constructor Details

This class inherits a constructor from Matestack::Ui::VueJs::Vue

Instance Method Details

#checkbox_attributes(item) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/matestack/ui/vue_js/components/form/checkbox.rb', line 44

def checkbox_attributes(item)
  {
    ":id": item_id(item),
    type: :checkbox,
    name: item_label(item),
    "#{value_key(item)}": item_value(item),
    "matestack-ui-vuejs-ref": matestack_ui_vuejs_ref("select.multiple.#{key}"),
    'v-on:change': change_event,
    'init-value': (init_value || []).to_json,
    'v-bind:class': "{ '#{error_class}': #{error_key} }",
    'value-type': value_type(item),
    "#{v_model_type(item)}": input_key,
  }.merge(self.options)
end

#checkbox_optionsObject

checkbox options



82
83
84
# File 'lib/matestack/ui/vue_js/components/form/checkbox.rb', line 82

def checkbox_options
  @checkbox_options ||= options.delete(:options)
end

#component_idObject



24
25
26
# File 'lib/matestack/ui/vue_js/components/form/checkbox.rb', line 24

def component_id
  "checkbox-component-for-#{key}"
end

#init_value_for_single_inputObject



71
72
73
74
75
76
77
78
# File 'lib/matestack/ui/vue_js/components/form/checkbox.rb', line 71

def init_value_for_single_input
  if init_value == true || init_value == 1
    return "true"
  end
  if init_value == false || init_value == 0
    return "false"
  end
end

#item_id(item) ⇒ Object



96
97
98
# File 'lib/matestack/ui/vue_js/components/form/checkbox.rb', line 96

def item_id(item)
  "#{id}+'_#{item_value(item).to_s.gsub(" ", '_')}'"
end

#item_label(item) ⇒ Object



92
93
94
# File 'lib/matestack/ui/vue_js/components/form/checkbox.rb', line 92

def item_label(item)
  item.is_a?(Array) ? item.first : item
end

#item_value(item) ⇒ Object

calculated attributes



88
89
90
# File 'lib/matestack/ui/vue_js/components/form/checkbox.rb', line 88

def item_value(item)
  item.is_a?(Array) ? item.last : item
end

#render_checkbox_optionsObject

checkbox rendering



37
38
39
40
41
42
# File 'lib/matestack/ui/vue_js/components/form/checkbox.rb', line 37

def render_checkbox_options
  checkbox_options.to_a.each do |item|
    input checkbox_attributes(item)
    label item_label(item), ":for": item_id(item)
  end
end

#render_optionsObject



16
17
18
19
20
21
22
# File 'lib/matestack/ui/vue_js/components/form/checkbox.rb', line 16

def render_options
  if checkbox_options
    render_checkbox_options
  else
    render_true_false_checkbox
  end
end

#render_true_false_checkboxObject



59
60
61
62
63
# File 'lib/matestack/ui/vue_js/components/form/checkbox.rb', line 59

def render_true_false_checkbox
  input true_false_checkbox_attributes.merge(type: :hidden, ":id": nil)
  input true_false_checkbox_attributes.merge(type: :checkbox, ":id": item_id(1))
  label input_label, ":for": item_id(1) if input_label
end

#responseObject



9
10
11
12
13
14
# File 'lib/matestack/ui/vue_js/components/form/checkbox.rb', line 9

def response
  div class: 'matestack-ui-core-form-checkbox' do
    render_options
    render_errors
  end
end

#true_false_checkbox_attributesObject



65
66
67
68
69
# File 'lib/matestack/ui/vue_js/components/form/checkbox.rb', line 65

def true_false_checkbox_attributes
  attributes.merge({
    'init-value': init_value_for_single_input,
  })
end

#value_key(value) ⇒ Object



100
101
102
# File 'lib/matestack/ui/vue_js/components/form/checkbox.rb', line 100

def value_key(value)
  value.is_a?(Numeric) ? ':value' : 'value'
end

#vue_propsObject



28
29
30
31
32
33
# File 'lib/matestack/ui/vue_js/components/form/checkbox.rb', line 28

def vue_props
  {
    init_value: init_value,
    key: key,
  }
end