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

+ (Object) create_with_offset(name, offset)



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

- (Object) add_default_name_and_id(options, method)



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

- (Object) build_shell(field, options)



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

- (Object) buttons(*names)



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

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

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

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



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

- (Object) create_button(name)



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

- (Object) label_for(field, options)



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

- (Object) multi_friend_input(options = {})



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

- (Object) text(string, options = {})



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

- (Object) text_area(method, options = {})



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

- (Object) text_field(method, options = {})



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

- (Object) value_before_type_cast(object, method)



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