+++ /dev/null
-module ControlGroupHelper
- def control_group_class(resource, attr)
- errors = resource.errors
- cls = ['control-group']
- p [attr, errors[attr]]
- if not errors[attr].empty?
- cls << 'error'
- end
- cls
- end
-end
#alert= t("errors.messages.not_saved", :count => @achievement.errors.count, :resource => @achievement.class.model_name.human.downcase)
= form_for(@achievement, :html => { :class => 'submit-form' }) do |f|
- %div{:class => control_group_class(@achievement, :task_id)}
- = f.label :task_id
- .controls
- = f.select :task_id, Task.all.map { |t| [t.name, t.id] }
- - @achievement.errors[:task_id].each do |msg|
- %span.help-inline= @achievement.errors.full_message(:task_id, msg)
- = render :partial => 'shared/control_group', :locals => { :form => f, :attr => :result, :field_type => :text_field }
+ = f.control_group :task_id do |g|
+ = g.label
+ = g.controls do |c|
+ = c.select Task.all.map { |t| [t.name, t.id] }
+ = f.control_group :result do |g|
+ = g.label
+ = g.controls do |c|
+ = c.text_field
= f.submit 'Save'
= link_to 'Cancel', cancel_path, :class => 'btn'
= form_for(@lecture, :html => { :class => 'submit-form' }) do |f|
%fieldset
- = render :partial => 'shared/control_group', :locals => { :form => f, :attr => :name, :field_type => :text_field }
+ = f.control_group :name do |g|
+ = g.label
+ = g.controls do |c|
+ = c.text_field
= f.submit 'Save'
= link_to 'Cancel', root_path, :class => 'btn'
+++ /dev/null
-%div{:class => control_group_class(form.object, attr)}
- %span= form.object
- = form.label attr
- .controls
- = form.send field_type, attr
- - form.object.errors[attr].each do |msg|
- %span.help-inline= form.object.errors.full_message attr, msg
= task_form_for(@apk, @task) do |f|
%fieldset
- = render :partial => 'shared/control_group', :locals => { :form => f, :attr => :name, :field_type => :text_field }
+ = f.control_group :name do |g|
+ = g.label
+ = g.controls do |c|
+ = c.text_field
= fields_for @apk do |f2|
- = render :partial => 'shared/control_group', :locals => { :form => f2, :attr => :name, :field_type => :text_field }
+ = f2.control_group :name do |g|
+ = g.label
+ = g.controls do |c|
+ = c.text_field
= fields_for :apkfile, @apk do |f3|
- = render :partial => 'shared/control_group', :locals => { :form => f3, :attr => :file, :field_type => :file_field }
+ = f3.control_group :file do |g|
+ = g.label
+ = g.controls do |c|
+ = c.file_field
= f.submit 'Save'
= link_to 'Cancel', cancel_path, :class => 'btn'
= form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => { :class => 'submit-form' }) do |f|
%fieldset
- [[:email, :email_field], [:password, :password_field], [:password_confirmation, :password_field], [:name, :text_field]].each do |attr, type|
- = render :partial => 'shared/control_group', :locals => { :form => f, :attr => attr, :field_type => type }
+ = f.control_group attr do |g|
+ = g.label
+ = g.controls do |c|
+ = c.send type
= f.submit t('devise.sign_up')
= link_to t('devise.sign_in'), new_session_path(resource), :class => 'btn'
= form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => { :class => 'submit-form' }) do |f|
%fieldset
- [[:email, :email_field], [:password, :password_field], [:password_confirmation, :password_field], [:student_number, :text_field]].each do |attr, type|
- = render :partial => 'shared/control_group', :locals => { :form => f, :attr => attr, :field_type => type }
+ = f.control_group attr do |g|
+ = g.label
+ = g.controls do |c|
+ = c.send type
= f.submit t('devise.sign_up')
= link_to t('devise.sign_in'), new_session_path(resource), :class => 'btn'
--- /dev/null
+ActiveSupport.on_load(:action_view) do
+ ActionView::Base.default_form_builder = BootstrapFormBuilder
+end
--- /dev/null
+class BootstrapFormBuilder < ActionView::Helpers::FormBuilder
+ def control_group(method, &blk)
+ errors = object.errors
+ cls = ['control-group']
+ if not errors[method].empty?
+ cls << 'error'
+ end
+ builder = ControlGroupBuilder.new object_name, object, @template, method
+ @template.content_tag(:div, :class => cls) do
+ blk.call builder
+ end
+ end
+
+ class ControlGroupBuilder
+ def initialize(object_name, object, template, method)
+ @object_name = object_name
+ @object = object
+ @template = template
+ @method = method
+ end
+
+ def label
+ @template.label @object_name, @method
+ end
+
+ def controls(&blk)
+ builder = ControlsBuilder.new @object_name, @object, @template, @method
+ @template.content_tag(:div, :class => 'controls') do
+ blk.call builder
+ end
+ end
+ end
+
+ class ControlsBuilder
+ def initialize(object_name, object, template, method)
+ @object_name = object_name
+ @object = object
+ @template = template
+ @method = method
+ end
+
+ [
+ :email_field,
+ :file_field,
+ :password_field,
+ :text_field,
+ ].each do |sym|
+ define_method sym do
+ form sym
+ end
+ end
+
+ def select(choices)
+ form :select, choices
+ end
+
+ private
+
+ def form(field_type, *args)
+ buf = @template.send field_type, @object_name, @method, *args
+ @object.errors[@method].each do |msg|
+ full_msg = @object.errors.full_message(@method, msg)
+ buf.concat @template.content_tag(:span, full_msg, :class => 'help-inline')
+ end
+ buf
+ end
+ end
+end