Module: Showbuilder::ShowModelForm

Included in:
Showbuilder
Defined in:
lib/showbuilder/show_model_form.rb

Instance Method Summary collapse

Instance Method Details

#model_error_messages(object) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/showbuilder/show_model_form.rb', line 34

def model_error_messages(object)
  messages = object.errors.messages.map do |key, value|
    value
  end
  messages.flatten!
  return if messages.empty?

  contents_tag(:div, :class => "alert alert-error") do |contents|
    contents << (:a, '&times'.html_safe, :class=>'close' , "data-dismiss" => :alert)
    contents << (:strong, flash[:error], :class => "alert-heading") if flash[:error]
    contents << (:ul) do
      list_items = messages.map do |msg|
        (:li, msg)
      end
      list_items.join.html_safe
    end
  end
end

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

show_model_form @customer do |form|

form.show_text_input            :name
form.show_email_input           :email
form.show_password_input        :password

end

dependents: puts @customer.class.to_s.underscore # customer I18n.t(‘customer.name’) I18n.t(“customer.email’) I18n.t(‘customer.password’)



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/showbuilder/show_model_form.rb', line 19

def show_model_form(models, options ={}, &block)
  models                 = Array.wrap(models)
  options                = options || {}
  options[:builder]      = options[:builder] || Showbuilder::Builders::ModelFormBuilder
  options[:html]         = options[:html] || {}
  options[:html][:class] = "#{options[:html][:class]} form-horizontal"

  self.form_for(models, options) do |form|
    self.html_contents do |contents|
      contents << self.model_error_messages(models.last)
      contents << capture(form, &block)
    end
  end
end