Class: BaseEditingBootstrap::Forms::Base

Inherits:
ActionView::Helpers::FormBuilder
  • Object
show all
Defined in:
lib/base_editing_bootstrap/forms/base.rb

Instance Method Summary collapse

Instance Method Details

#check_box(method, options = {}, checked_value = "1", unchecked_value = "0") ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/base_editing_bootstrap/forms/base.rb', line 59

def check_box(method, options = {}, checked_value = "1", unchecked_value = "0")
  label = options.extract!(:label)[:label] || nil
  if label
    label_tag = label(label, class: "form-check-label")
  elsif block_given?
    label_tag = yield
  else
    label_tag = nil
  end
  @template.(:div, class: form_style_class_for(method, {class: (options.extract!(:class)[:class] || "")}, base_classes: ["form-check"])) do
    checkbox_class = ["form-check-input"]
    checkbox_class << "is-invalid" if object.errors && object.errors.include?(method)
    super(method, options.reverse_merge(class: checkbox_class.join(" ")), checked_value, unchecked_value) + label_tag
  end
end

#ckeditor_text_area(field, options = {}) ⇒ Object



50
51
52
# File 'lib/base_editing_bootstrap/forms/base.rb', line 50

def ckeditor_text_area(field, options = {})
  text_area(field, options.reverse_merge(data: {controller: "ckeditor"}))
end

#collection_check_boxes(method, collection, value_method, text_method, options = {}, html_options = {}, &block) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/base_editing_bootstrap/forms/base.rb', line 80

def collection_check_boxes(method,
                           collection,
                           value_method,
                           text_method,
                           options = {},
                           html_options = {},
                           &block)
  form_check_classes = (["form-check"] + [(html_options.delete(:form_check_class) { "" }).split(" ").collect(&:strip)]).compact.join(" ")
  super do |builder|
    @template.(:div, class: form_check_classes) do
      builder.check_box(class: "form-check-input") + builder.label(class: "form-check-label")
    end
  end
end

#decimal_field(field, options = {}) ⇒ Object



40
41
42
43
44
45
46
47
48
# File 'lib/base_editing_bootstrap/forms/base.rb', line 40

def decimal_field(field, options = {})
  number_field(field,
               options.reverse_merge(
                 value: @template.number_with_delimiter(object[field].to_f,
                                                        separator: ".", # questo è secondo definizione html5
                                                        delimiter: ""),
                 step: :any
               ))
end

#form_style_class_for(method, options = {}, base_classes: ["form-control"]) ⇒ Object

Costruisce l’array delle classi che devono essere presenti sul campo della form



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/base_editing_bootstrap/forms/base.rb', line 27

def form_style_class_for(method, options = {}, base_classes: ["form-control"])
  classes = base_classes
  if object.errors
    classes << "is-invalid" if object.errors.include?(method)
    # caso in cui il metodo è una relazione
    if method.to_s.match(/(.*)_id\z/)
      classes << "is-invalid" if object.errors.include?(Regexp.last_match(1))
    end
  end
  classes << options[:class].split(" ") if options[:class]
  classes.flatten.compact.uniq.join(" ")
end

#radio_button(method, tag_value, options = {}) ⇒ Object



95
96
97
98
99
100
# File 'lib/base_editing_bootstrap/forms/base.rb', line 95

def radio_button(method, tag_value, options = {})
  @template.(:div, class: "form-check") do
    super(method, tag_value, options.reverse_merge(class: "form-check-input")) +
      label(method, class: "form-check-label")
  end
end

#select(method, choices = nil, options = {}, html_options = {}, &block) ⇒ Object



54
55
56
57
# File 'lib/base_editing_bootstrap/forms/base.rb', line 54

def select(method, choices = nil, options = {}, html_options = {}, &block)
  html_options.merge!(class: form_style_class_for(method, html_options, base_classes: ["form-control", "form-select"]))
  super(method, choices, options, html_options, &block)
end

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

Se necessario modificare il testo dell’ “undo”, basta aggiungere nelle traduzioni nella solita struttura di active record l’attributo :_submit_undo, per il normale submit consiglio la lettura della guida standard di rails ATTENZIONE: nelle classi del bottone undo, abbiamo aggiunto .btn-undo-button

che ascoltiamo dalle modal e utilizziamo per chiudere la modal, al posto
di seguire realmente il link con il browser.


109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/base_editing_bootstrap/forms/base.rb', line 109

def submit(value = nil, options = {})
  @template.(:div, class: "btn-group mr-1") do

    if value.nil? && object.respond_to?(:model_name)
      key = object.respond_to?(:persisted?) ? (object.persisted? ? :update : :create) : :submit
      defaults = []
      tmp = object.class
      while tmp != ::ActiveRecord::Base && tmp.respond_to?(:model_name)
        defaults << :"helpers.submit.#{tmp.model_name.i18n_key}.#{key}"
        tmp = tmp.superclass
      end
      defaults << :"helpers.submit.#{key}"
      defaults << "_NOT_FOUND_TRANSLATION_"

      value = I18n.translate(defaults.shift, model: object.model_name.human, default: defaults)
      value = nil if value == "_NOT_FOUND_TRANSLATION_"
    end

    super(value, options.reverse_merge(class: "btn btn-primary")) +
      @template.link_to(object.class.human_attribute_name(:_submit_undo, default: :undo),
                        @template.index_custom_polymorphic_path(object.class),
                        class: "btn btn-default btn-undo-button")
  end
end

#switch_box(method, options = {}, checked_value = "1", unchecked_value = "0") ⇒ Object



75
76
77
78
# File 'lib/base_editing_bootstrap/forms/base.rb', line 75

def switch_box(method, options = {}, checked_value = "1", unchecked_value = "0")
  options[:class] = (["form-check form-switch"] + (options[:class] || "").split(" ")).join(" ")
  self.check_box(method, options, checked_value, unchecked_value)
end