Created
March 31, 2014 17:12
-
-
Save biscuitvile/9897165 to your computer and use it in GitHub Desktop.
Advanced Form Builder
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| = form_for [:admin, @account], url: :admin_settings, class: 'settings' do |f| | |
| %section.panel.panel_form | |
| = f.input :store_name | |
| = f.input :description, type: :textarea | |
| = f.input :contact_email | |
| %section.panel | |
| %p.input.text | |
| = f.submit t('admin.settings.save'), class: 'action_button success' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class FormBuilder < ActionView::Helpers::FormBuilder | |
| def label(attribute, text = nil, options = {}, &block) | |
| text = (text || attribute.to_s.humanize.titleize) | |
| unless object.nil? | |
| errors = object.errors[attribute.to_sym] | |
| if errors.present? | |
| text += " #{errors.is_a?(Array) ? errors.first : errors}" | |
| end | |
| end | |
| super(attribute, text.html_safe, options, &block) | |
| end | |
| def wrapper(attribute, kind = :text, &block) | |
| html_class = 'input ' + kind.to_s | |
| html_class += ' input_focus' if object.public_send(attribute).present? | |
| html_class += ' input_error' if object.errors[attribute.to_sym].present? | |
| @template.content_tag "p", class: html_class, &block | |
| end | |
| def input(attribute, args={}, &block) | |
| kind = args.fetch(:type, :text) | |
| wrapper attribute, kind do | |
| (label attribute) + | |
| (text_field attribute) | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment