Class: Locomotive::FormBuilder

Inherits:
SimpleForm::FormBuilder
  • Object
show all
Defined in:
lib/locomotive/simple_form.rb

Instance Method Summary collapse

Instance Method Details

#action(options = {}) ⇒ Object



113
114
115
116
117
118
119
120
121
122
# File 'lib/locomotive/simple_form.rb', line 113

def action(options = {})
  action        = object.persisted? ? :update : :create
  label         = options[:label] || translate_button(action)
  loading_text  = translate_button(:loading_text)

  template. :button, label,
    type:   'submit',
    class:  options[:change_class] || "btn btn-primary btn-sm #{options[:class]}",
    data:   { loading_text: loading_text }
end

#actions(options = {}, &block) ⇒ Object



83
84
85
86
87
88
89
90
# File 'lib/locomotive/simple_form.rb', line 83

def actions(options = {}, &block)
  if options[:back_url]
    actions_with_back_button(options)
  else
    options[:class] ||= 'text-right form-actions'
    template.(:div, options, &block)
  end
end

#actions_with_back_button(options = {}) ⇒ Object



92
93
94
95
96
97
98
99
100
# File 'lib/locomotive/simple_form.rb', line 92

def actions_with_back_button(options = {})
  back_button = back_button_action(options)

  template.(:div, action +
    ' '.html_safe +
    translate_button(:or) +
    ' '.html_safe +
    back_button, class: 'text-right form-actions')
end

#back_button_action(options = {}) ⇒ Object



102
103
104
105
106
107
108
109
110
111
# File 'lib/locomotive/simple_form.rb', line 102

def back_button_action(options = {})
  label  = translate_button(:cancel)
  url    = options[:back_url]

  if options[:use_stored_location]
    url = template.last_saved_location(url)
  end

  template.link_to(label, url)
end

#inputs(name = nil, options = {}, &block) ⇒ Object



74
75
76
77
78
79
80
81
# File 'lib/locomotive/simple_form.rb', line 74

def inputs(name = nil, options = {}, &block)
  html = template.(:legend, template.(:span, name))
  html += template.capture(&block)

  options[:class] ||= 'inputs'

  template.(:fieldset, html, options)
end

#lookup_model_namesObject

Extract the model names from the object_name mess, ignoring numeric and explicit child indexes. Prefix it by locomotive

Example:

route[0][1]

“locomotive”, “route”, “blocks”, “blocks_learning_object”, “foo”


141
142
143
144
145
146
147
148
149
150
# File 'lib/locomotive/simple_form.rb', line 141

def lookup_model_names #:nodoc:
  @lookup_model_names ||= begin
    child_index = options[:child_index]
    names = object_name.to_s.scan(/(?!\d)\w+/).flatten
    names.unshift('locomotive')
    names.delete(child_index) if child_index
    names.each { |name| name.gsub!('_attributes', '') }
    names.freeze
  end
end

#submit_text(action = :submit) ⇒ Object



124
125
126
# File 'lib/locomotive/simple_form.rb', line 124

def submit_text(action = :submit)
  translate_button(action)
end

#translate_button(key) ⇒ Object



128
129
130
# File 'lib/locomotive/simple_form.rb', line 128

def translate_button(key)
  template.t("simple_form.buttons.defaults.locomotive.#{key}")
end