Patches: Browse | Submit New | Admin

[#21731] Add value_field to FacebookFormBuilder to make it easy to emulate Facebook forms for read-only data (v2, better)

Date:
2008-08-29 19:06
Priority:
3
Submitted By:
Victor Costan (costan)
Assigned To:
Nobody (None)
Category:
None
State:
Open
Summary:
Add value_field to FacebookFormBuilder to make it easy to emulate Facebook forms for read-only data (v2, better)

Detailed description
Sometimes I like to use facebook_form_for to show data that was previously entered on a Facebook form. It's nice to
have a wrapper for the fb:editor-custom tag that's needed to do that. The patch below adds "value_field",
which shows a field's value instead of an editable field. value_field can also be used in block form, and it wraps the
block's result with the fb:editor-custom tags, just like form_for.

I find this useful for my code, so I hope you'll accept it.

Thanks for the awesome work!

Index: facebooker/lib/facebooker/rails/facebook_form_builder.rb
===================================================================
--- facebooker/lib/facebooker/rails/facebook_form_builder.rb	(revision 400)
+++ facebooker/lib/facebooker/rails/facebook_form_builder.rb	(working copy)
@@ -1,7 +1,7 @@
 module Facebooker
   module Rails
     class FacebookFormBuilder < ActionView::Helpers::FormBuilder
-      
+      include ActionView::Helpers::CaptureHelper      
       
       second_param = %w(password_field file_field check_box date_select datetime_select time_select)
       third_param = %w(radio_button country_select select time_zone_select)
@@ -39,8 +39,25 @@
       def text(string,options={})
         @template.content_tag "fb:editor-custom",string, :label=>label_for("",options)
       end
-      
-      
+            
+      def value_field(method, options = {}, &block)        
+        options[:label] ||= label_for(method,options)
+        add_default_name_and_id(options,method)
+        options["value"] ||= value_before_type_cast(object,method)
+        if block_given?
+          contents = capture(self, &block)
+          @template.concat(
+              @template.content_tag('fb:editor-custom', "", options) {
+                @template.content_tag("div", contents, :style => 'position: relative; top: 2px;')
+              }, block.binding)
+        else
+          @template.content_tag("fb:editor-custom", "", options) do
+            @template.content_tag("div", value_before_type_cast(object, method),
+                                  :style => 'position: relative; top: 2px;')
+          end
+        end
+      end
+
       def text_field(method, options = {})
         options[:label] ||= label_for(method,options)
         add_default_name_and_id(options,method)

Add A Comment: Notepad

Please login


Followup

Message
Date: 2008-09-03 07:31
Sender: Victor Costan

Re-revised patch (after looking at my developer log on Rails
edge):

Index: facebooker/lib/facebooker/rails/facebook_form_builder.rb
=================================================================
==
---
facebooker/lib/facebooker/rails/facebook_form_builder.rb
(revision 400)
+++
facebooker/lib/facebooker/rails/facebook_form_builder.rb
(working copy)
@@ -39,8 +39,25 @@
       def text(string,options={})
         @template.content_tag
"fb:editor-custom",string,
:label=>label_for("",options)
       end
-      
-      
+            
+      def value_field(method, options = {}, &block)        
+        options[:label] ||= label_for(method,options)
+        add_default_name_and_id(options,method)
+        options["value"] ||=
value_before_type_cast(object,method)
+        if block_given?
+          contents = @template.capture(self, &block)
+          @template.concat(
+              @template.content_tag('fb:editor-custom',
"",
options) {
+                @template.content_tag("div",
contents,
:style => 'position: relative; top: 2px;')
+              })
+        else
+          @template.content_tag("fb:editor-custom",
"",
options) do
+            @template.content_tag("div",
value_before_type_cast(object, method),
+                                  :style => 'position:
relative; top: 2px;')
+          end
+        end
+      end
+
       def text_field(method, options = {})
         options[:label] ||= label_for(method,options)
         add_default_name_and_id(options,method)

Date: 2008-09-03 07:10
Sender: Victor Costan

Revised patch (works with Rails 2.1 and Rails edge):



Index: facebooker/lib/facebooker/rails/facebook_form_builder.rb
=================================================================
==
---
facebooker/lib/facebooker/rails/facebook_form_builder.rb
(revision 400)
+++
facebooker/lib/facebooker/rails/facebook_form_builder.rb
(working copy)
@@ -39,8 +39,25 @@
       def text(string,options={})
         @template.content_tag
"fb:editor-custom",string,
:label=>label_for("",options)
       end
-      
-      
+            
+      def value_field(method, options = {}, &block)        
+        options[:label] ||= label_for(method,options)
+        add_default_name_and_id(options,method)
+        options["value"] ||=
value_before_type_cast(object,method)
+        if block_given?
+          contents = @template.capture(self, &block)
+          @template.concat(
+              @template.content_tag('fb:editor-custom',
"",
options) {
+                @template.content_tag("div",
contents,
:style => 'position: relative; top: 2px;')
+              }, block.binding)
+        else
+          @template.content_tag("fb:editor-custom",
"",
options) do
+            @template.content_tag("div",
value_before_type_cast(object, method),
+                                  :style => 'position:
relative; top: 2px;')
+          end
+        end
+      end
+
       def text_field(method, options = {})
         options[:label] ||= label_for(method,options)
         add_default_name_and_id(options,method)

Attached Files:

Name Description Download
No Files Currently Attached

Changes:

No Changes Have Been Made to This Item