PageRenderTime 23ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/plugins/facebooker/lib/facebooker/rails/facebook_form_builder.rb

http://github.com/timothyf/EngineY
Ruby | 141 lines | 113 code | 25 blank | 3 comment | 3 complexity | 22e0c5f8ae10ad83e0258c9f08646594 MD5 | raw file
  1. module Facebooker
  2. module Rails
  3. class FacebookFormBuilder < ActionView::Helpers::FormBuilder
  4. second_param = %w(password_field file_field check_box date_select datetime_select time_select)
  5. third_param = %w(radio_button country_select select time_zone_select)
  6. fifth_param = %w(collection_select)
  7. def self.create_with_offset(name,offset)
  8. define_method name do |field,*args|
  9. options = args[offset] || {}
  10. build_shell(field,options.with_indifferent_access) do
  11. super
  12. end
  13. end
  14. end
  15. second_param.each do |name|
  16. create_with_offset(name,0)
  17. end
  18. third_param.each do |name|
  19. create_with_offset(name,1)
  20. end
  21. fifth_param.each do |name|
  22. create_with_offset(name,3)
  23. end
  24. def build_shell(field,options)
  25. @template.content_tag "fb:editor-custom", :label=>label_for(field,options) do
  26. yield
  27. end
  28. end
  29. def label_for(field,options)
  30. options[:label] || field.to_s.humanize
  31. end
  32. def text(string,options={})
  33. @template.content_tag "fb:editor-custom",string, :label=>label_for("",options)
  34. end
  35. def text_field(method, options = {})
  36. options = options.with_indifferent_access
  37. options[:label] ||= label_for(method,options)
  38. add_default_name_and_id(options,method)
  39. options["value"] ||= value_before_type_cast(object,method)
  40. @template.content_tag("fb:editor-text","",options)
  41. end
  42. def text_area(method, options = {})
  43. options[:label] ||= label_for(method,options)
  44. add_default_name_and_id(options,method)
  45. @template.content_tag("fb:editor-textarea",value_before_type_cast(object,method),options)
  46. end
  47. #
  48. # Build a text input area that uses typeahed
  49. # options are like collection_select
  50. def collection_typeahead(method,collection,value_method,text_method,options={})
  51. build_shell(method,options) do
  52. collection_typeahead_internal(method,collection,value_method,text_method,options)
  53. end
  54. end
  55. def collection_typeahead_internal(method,collection,value_method,text_method,options={})
  56. option_values = collection.map do |item|
  57. value=item.send(value_method)
  58. text=item.send(text_method)
  59. @template.content_tag "fb:typeahead-option",text,:value=>value
  60. end.join
  61. add_default_name_and_id(options,method)
  62. options["value"] ||= value_before_type_cast(object,method)
  63. @template.content_tag("fb:typeahead-input",option_values,options)
  64. end
  65. def value_before_type_cast(object,method)
  66. unless object.nil?
  67. method_name = method.to_s
  68. object.respond_to?(method_name + "_before_type_cast") ?
  69. object.send(method_name + "_before_type_cast") :
  70. object.send(method_name)
  71. end
  72. end
  73. def multi_friend_input(options={})
  74. build_shell(:friends,options) do
  75. @template.content_tag("fb:multi-friend-input","",options)
  76. end
  77. end
  78. def buttons(*names)
  79. buttons=names.map do |name|
  80. create_button(name)
  81. end.join
  82. @template.content_tag "fb:editor-buttonset",buttons
  83. end
  84. def create_button(name)
  85. @template.content_tag("fb:editor-button","",:value=>name,:name=>"commit")
  86. end
  87. def add_default_name_and_id(options, method)
  88. @method_name = method
  89. if options.has_key?("index")
  90. options["name"] ||= tag_name_with_index(options["index"])
  91. options["id"] ||= tag_id_with_index(options["index"])
  92. options.delete("index")
  93. else
  94. options["name"] ||= tag_name + (options.has_key?('multiple') ? '[]' : '')
  95. options["id"] ||= "#{sanitized_object_name}_#{sanitized_method_name}"
  96. end
  97. end
  98. private
  99. def tag_name
  100. "#{@object_name.to_s}[#{sanitized_method_name}]"
  101. end
  102. def tag_name_with_index(index)
  103. "#{@object_name.to_s}[#{index}][#{sanitized_method_name}]"
  104. end
  105. def tag_id_with_index(index)
  106. "#{sanitized_object_name}_#{index}_#{sanitized_method_name}"
  107. end
  108. def sanitized_object_name
  109. @object_name.to_s.gsub(/\]\[|[^-a-zA-Z0-9:.]/, "_").sub(/_$/, "")
  110. end
  111. def sanitized_method_name
  112. @method_name.to_s.sub(/\?$/,"")
  113. end
  114. end
  115. end
  116. end