OSDN Git Service

Define and use custom FormBuilder
authoreagletmt <eagletmt@gmail.com>
Thu, 1 Nov 2012 11:30:03 +0000 (20:30 +0900)
committereagletmt <eagletmt@gmail.com>
Thu, 1 Nov 2012 11:31:34 +0000 (20:31 +0900)
CutenServer/app/helpers/control_group_helper.rb [deleted file]
CutenServer/app/views/achievements/_form.html.haml
CutenServer/app/views/lectures/_form.html.haml
CutenServer/app/views/shared/_control_group.html.haml [deleted file]
CutenServer/app/views/tasks/_form.html.haml
CutenServer/app/views/teachers/registrations/new.html.haml
CutenServer/app/views/users/registrations/new.html.haml
CutenServer/config/initializers/custom_form_builder.rb [new file with mode: 0644]
CutenServer/extras/bootstrap_form_builder.rb [new file with mode: 0644]

diff --git a/CutenServer/app/helpers/control_group_helper.rb b/CutenServer/app/helpers/control_group_helper.rb
deleted file mode 100644 (file)
index d70a3d6..0000000
+++ /dev/null
@@ -1,11 +0,0 @@
-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
index 91a8187..859c1cc 100644 (file)
@@ -2,12 +2,13 @@
   #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'
index c746aaf..d9a25a6 100644 (file)
@@ -3,6 +3,9 @@
 
 = 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'
diff --git a/CutenServer/app/views/shared/_control_group.html.haml b/CutenServer/app/views/shared/_control_group.html.haml
deleted file mode 100644 (file)
index 48bd71d..0000000
+++ /dev/null
@@ -1,7 +0,0 @@
-%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
index 092e782..a7fc814 100644 (file)
@@ -3,10 +3,19 @@
 
 = 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'
index 8733439..e145875 100644 (file)
@@ -4,6 +4,9 @@
 = 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'
index 54c5852..eeb21db 100644 (file)
@@ -4,6 +4,9 @@
 = 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'
diff --git a/CutenServer/config/initializers/custom_form_builder.rb b/CutenServer/config/initializers/custom_form_builder.rb
new file mode 100644 (file)
index 0000000..22ddf43
--- /dev/null
@@ -0,0 +1,3 @@
+ActiveSupport.on_load(:action_view) do
+  ActionView::Base.default_form_builder = BootstrapFormBuilder
+end
diff --git a/CutenServer/extras/bootstrap_form_builder.rb b/CutenServer/extras/bootstrap_form_builder.rb
new file mode 100644 (file)
index 0000000..3bceaa9
--- /dev/null
@@ -0,0 +1,68 @@
+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