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.with_indifferent_access) do
      super(field,*args)
    end
  end    
end

Instance Method Details

#add_default_name_and_id(options, method) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
# File 'lib/facebooker/rails/facebook_form_builder.rb', line 106

def add_default_name_and_id(options, method)
  @method_name = method
  if options.has_key?("index")
    options["name"] ||= tag_name_with_index(options["index"])
    options["id"]   ||= tag_id_with_index(options["index"])
    options.delete("index")
  else
    options["name"] ||= tag_name + (options.has_key?('multiple') ? '[]' : '')
    options["id"]   ||= "#{sanitized_object_name}_#{sanitized_method_name}"
  end
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



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

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



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

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



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

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



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

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



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

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



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

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
50
# File 'lib/facebooker/rails/facebook_form_builder.rb', line 44

def text_field(method, options = {})
  options = options.with_indifferent_access
  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



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

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