Class: ScaffoldMarkup::Builders::FormBuilder
- Inherits:
-
BaseBuilder
- Object
- BaseBuilder
- ScaffoldMarkup::Builders::FormBuilder
- Defined in:
- lib/scaffold_markup/builders/form_builder.rb
Instance Attribute Summary collapse
-
#model ⇒ Object
readonly
Returns the value of attribute model.
Attributes inherited from BaseBuilder
Instance Method Summary collapse
- #actions(&block) ⇒ Object
- #association(association_name, options = {}) ⇒ Object
- #display(attribute) ⇒ Object
- #flash_errors ⇒ Object
-
#initialize(template, model, &block) ⇒ FormBuilder
constructor
A new instance of FormBuilder.
- #text(attribute, options = {}) ⇒ Object
- #to_s ⇒ Object
Methods inherited from BaseBuilder
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
#model ⇒ Object (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, ={}) _self = self attribute = model.class.reflections[association_name].foreign_key ControlGroup.new("#{model.class.human_attribute_name(association_name)}#{[:required] ? ' (*)' : ''}", :class => [: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 [: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_errors ⇒ Object
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..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, ={}) _self = self ControlGroup.new("#{model.class.human_attribute_name(attribute)}#{[:required] ? ' (*)' : ''}", :class => [: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 [:required] end.html_safe end |
#to_s ⇒ Object
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 |