Class: Facebooker::Rails::FacebookFormBuilder

Inherits:
ActionView::Helpers::FormBuilder
  • Object
show all
Defined in:
lib/facebooker/rails/facebook_form_builder.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.create_with_offset(name, offset) ⇒ Object



10
11
12
13
14
15
16
17
# File 'lib/facebooker/rails/facebook_form_builder.rb', line 10

def self.create_with_offset(name,offset)
  define_method name do |field,*args|
    options = args[offset] || {}
    build_shell(field,options) do
      super
    end
  end    
end

Instance Method Details

#add_default_name_and_id(options, method) ⇒ Object



105
106
107
108
# File 'lib/facebooker/rails/facebook_form_builder.rb', line 105

def add_default_name_and_id(options,method)
  options[:name] ||= "#{object.class.name.underscore}[#{method}]"
  options[:id] ||= "#{object.class.name.underscore}_#{method}"
end

#build_shell(field, options) ⇒ Object



29
30
31
32
33
# File 'lib/facebooker/rails/facebook_form_builder.rb', line 29

def build_shell(field,options)
  @template. "fb:editor-custom", :label=>label_for(field,options) do
    yield
  end
end

#buttons(*names) ⇒ Object



93
94
95
96
97
98
99
# File 'lib/facebooker/rails/facebook_form_builder.rb', line 93

def buttons(*names)
  buttons=names.map do |name|
    create_button(name)
  end.join
  
  @template. "fb:editor-buttonset",buttons
end

#collection_typeahead(method, collection, value_method, text_method, options = {}) ⇒ Object

Build a text input area that uses typeahed options are like collection_select



61
62
63
64
65
# File 'lib/facebooker/rails/facebook_form_builder.rb', line 61

def collection_typeahead(method,collection,value_method,text_method,options={})
  build_shell(method,options) do
    collection_typeahead_internal(method,collection,value_method,text_method,options)
  end
end

#collection_typeahead_internal(method, collection, value_method, text_method, options = {}) ⇒ Object



67
68
69
70
71
72
73
74
75
76
# File 'lib/facebooker/rails/facebook_form_builder.rb', line 67

def collection_typeahead_internal(method,collection,value_method,text_method,options={})
  option_values = collection.map do |item|
    value=item.send(value_method)
    text=item.send(text_method)
    @template. "fb:typeahead-option",text,:value=>value
  end.join
  add_default_name_and_id(options,method)
  options["value"] ||= value_before_type_cast(object,method)
  @template.("fb:typeahead-input",option_values,options)        
end

#create_button(name) ⇒ Object



101
102
103
# File 'lib/facebooker/rails/facebook_form_builder.rb', line 101

def create_button(name)
  @template.("fb:editor-button","",:value=>name,:name=>"commit")
end

#label_for(field, options) ⇒ Object



35
36
37
# File 'lib/facebooker/rails/facebook_form_builder.rb', line 35

def label_for(field,options)
  options[:label] || field.to_s.humanize
end

#multi_friend_input(options = {}) ⇒ Object



87
88
89
90
91
# File 'lib/facebooker/rails/facebook_form_builder.rb', line 87

def multi_friend_input(options={})
  build_shell(:friends,options) do
    @template.("fb:multi-friend-input","",options)
  end
end

#text(string, options = {}) ⇒ Object



39
40
41
# File 'lib/facebooker/rails/facebook_form_builder.rb', line 39

def text(string,options={})
  @template. "fb:editor-custom",string, :label=>label_for("",options)
end

#text_area(method, options = {}) ⇒ Object



52
53
54
55
56
# File 'lib/facebooker/rails/facebook_form_builder.rb', line 52

def text_area(method, options = {})
  options[:label] ||= label_for(method,options)
  add_default_name_and_id(options,method)
  @template.("fb:editor-textarea",value_before_type_cast(object,method),options)        
end

#text_field(method, options = {}) ⇒ Object



44
45
46
47
48
49
# File 'lib/facebooker/rails/facebook_form_builder.rb', line 44

def text_field(method, options = {})
  options[:label] ||= label_for(method,options)
  add_default_name_and_id(options,method)
  options["value"] ||= value_before_type_cast(object,method)
  @template.("fb:editor-text","",options)
end

#value_before_type_cast(object, method) ⇒ Object



78
79
80
81
82
83
84
85
# File 'lib/facebooker/rails/facebook_form_builder.rb', line 78

def value_before_type_cast(object,method)
  unless object.nil?
    method_name = method.to_s
    object.respond_to?(method_name + "_before_type_cast") ?
    object.send(method_name + "_before_type_cast") :
    object.send(method_name)
  end
end