Class: ScaffoldMarkup::Builders::FormBuilder

Inherits:
BaseBuilder
  • Object
show all
Defined in:
lib/scaffold_markup/builders/form_builder.rb

Instance Attribute Summary collapse

Attributes inherited from BaseBuilder

#block, #template, #url

Instance Method Summary collapse

Methods inherited from BaseBuilder

#html_safe

Constructor Details

#initialize(template, model, &block) ⇒ FormBuilder

Returns a new instance of FormBuilder.



6
7
8
9
# File 'lib/scaffold_markup/builders/form_builder.rb', line 6

def initialize(template, model, &block)
  super(template, &block)
  @model = model
end

Instance Attribute Details

#modelObject (readonly)

Returns the value of attribute model.



4
5
6
# File 'lib/scaffold_markup/builders/form_builder.rb', line 4

def model
  @model
end

Instance Method Details

#actions(&block) ⇒ Object



53
54
55
56
# File 'lib/scaffold_markup/builders/form_builder.rb', line 53

def actions(&block)
  #TODO: FormActions tiene que ser una clase de TwitterBootstrapMarkup
  Tag.block(:div, template.capture(self, &block), :class => 'form-actions').html_safe
end

#association(association_name, options = {}) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/scaffold_markup/builders/form_builder.rb', line 38

def association(association_name, options={})
  _self = self
  attribute = model.class.reflections[association_name].foreign_key
  ControlGroup.new("#{model.class.human_attribute_name(association_name)}#{options[:required] ? ' (*)' : ''}", :class => options[:required] ? 'bold' : '') do
    html_attributes = {
        :prompt => '[Select]',
        :selected_value => _self.model.send(attribute),
        :id => "#{_self.model.class.model_name.underscore}_#{attribute}",
        :name => "#{_self.model.class.model_name.underscore}[#{attribute}]"
    }
    select = append Select.new(Hash[_self.model.class.reflections[association_name].klass.all.map { |e| [e.to_s, e.id] }], html_attributes)
    select.attributes[:required] = :required if options[:required]
  end.html_safe
end

#display(attribute) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/scaffold_markup/builders/form_builder.rb', line 22

def display(attribute)
  association = model.class.reflected_association(attribute)
  display_value = association ? model.send(association.name).to_s : model.send(attribute)
  ControlGroup.new(model.class.human_attribute_name(attribute), :class => 'bold') do
    append Tag.block(:span, display_value, :class => 'display')
  end.html_safe
end

#flash_errorsObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/scaffold_markup/builders/form_builder.rb', line 58

def flash_errors
  _self = self
  if model.errors.any?
    errors = Alert.danger_closable do
      append Tag.block(:strong, 'Cant perform requested operation')
      append do
        Tag.block(:ul) do
          _self.model.errors.full_messages.each do |msg|
            append Tag.new(:li, msg.gsub(/\n/, '<br>'))
          end
        end
      end
    end
    template.content_for(:flash, errors.html_safe)
  end
end

#text(attribute, options = {}) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/scaffold_markup/builders/form_builder.rb', line 30

def text(attribute, options={})
  _self = self
  ControlGroup.new("#{model.class.human_attribute_name(attribute)}#{options[:required] ? ' (*)' : ''}", :class => options[:required] ? 'bold' : '') do
    text_box = append Input.text(:id => "#{_self.model.class.model_name.underscore}_#{attribute}", :name => "#{_self.model.class.model_name.underscore}[#{attribute}]", :value => _self.model.send(attribute))
    text_box.attributes[:required] = :required if options[:required]
  end.html_safe
end

#to_sObject



11
12
13
14
15
16
17
18
19
20
# File 'lib/scaffold_markup/builders/form_builder.rb', line 11

def to_s
  _self = self
  html = Form.horizontal(:method => :post, :action => model.new_record? ? url.list_resource(model.class) : url.resource(model), 'accept-charset' => 'UTF-8') do
    append Input.hidden :name => 'authenticity_token', :value => _self.template.form_authenticity_token
    append Input.hidden :name => '_method', :value => :put unless _self.model.new_record?
    append _self.template.capture(_self, &_self.block)
  end.to_s
  flash_errors
  html
end