facebooker-fbg 1.0.59a

Sign up to get free protection for your applications and to get access to all the features.
Files changed (153) hide show
  1. data/.autotest +15 -0
  2. data/CHANGELOG.rdoc +24 -0
  3. data/COPYING.rdoc +19 -0
  4. data/Manifest.txt +149 -0
  5. data/README.rdoc +119 -0
  6. data/Rakefile +94 -0
  7. data/TODO.rdoc +4 -0
  8. data/examples/desktop_login.rb +14 -0
  9. data/facebooker.gemspec +42 -0
  10. data/generators/facebook/facebook_generator.rb +14 -0
  11. data/generators/facebook/templates/config/facebooker.yml +49 -0
  12. data/generators/facebook/templates/public/javascripts/facebooker.js +332 -0
  13. data/generators/facebook_controller/USAGE +33 -0
  14. data/generators/facebook_controller/facebook_controller_generator.rb +40 -0
  15. data/generators/facebook_controller/templates/controller.rb +7 -0
  16. data/generators/facebook_controller/templates/functional_test.rb +11 -0
  17. data/generators/facebook_controller/templates/helper.rb +2 -0
  18. data/generators/facebook_controller/templates/view.fbml.erb +2 -0
  19. data/generators/facebook_controller/templates/view.html.erb +2 -0
  20. data/generators/facebook_publisher/facebook_publisher_generator.rb +14 -0
  21. data/generators/facebook_publisher/templates/create_facebook_templates.rb +15 -0
  22. data/generators/facebook_publisher/templates/publisher.rb +3 -0
  23. data/generators/facebook_scaffold/USAGE +27 -0
  24. data/generators/facebook_scaffold/facebook_scaffold_generator.rb +118 -0
  25. data/generators/facebook_scaffold/templates/controller.rb +93 -0
  26. data/generators/facebook_scaffold/templates/facebook_style.css +2579 -0
  27. data/generators/facebook_scaffold/templates/functional_test.rb +89 -0
  28. data/generators/facebook_scaffold/templates/helper.rb +2 -0
  29. data/generators/facebook_scaffold/templates/layout.fbml.erb +6 -0
  30. data/generators/facebook_scaffold/templates/layout.html.erb +17 -0
  31. data/generators/facebook_scaffold/templates/style.css +74 -0
  32. data/generators/facebook_scaffold/templates/view_edit.fbml.erb +13 -0
  33. data/generators/facebook_scaffold/templates/view_edit.html.erb +18 -0
  34. data/generators/facebook_scaffold/templates/view_index.fbml.erb +24 -0
  35. data/generators/facebook_scaffold/templates/view_index.html.erb +24 -0
  36. data/generators/facebook_scaffold/templates/view_new.fbml.erb +12 -0
  37. data/generators/facebook_scaffold/templates/view_new.html.erb +17 -0
  38. data/generators/facebook_scaffold/templates/view_show.fbml.erb +10 -0
  39. data/generators/facebook_scaffold/templates/view_show.html.erb +10 -0
  40. data/generators/publisher/publisher_generator.rb +14 -0
  41. data/generators/xd_receiver/templates/xd_receiver.html +10 -0
  42. data/generators/xd_receiver/templates/xd_receiver_ssl.html +10 -0
  43. data/generators/xd_receiver/xd_receiver_generator.rb +10 -0
  44. data/init.rb +26 -0
  45. data/install.rb +12 -0
  46. data/lib/facebooker.rb +254 -0
  47. data/lib/facebooker/adapters/adapter_base.rb +91 -0
  48. data/lib/facebooker/adapters/bebo_adapter.rb +77 -0
  49. data/lib/facebooker/adapters/facebook_adapter.rb +60 -0
  50. data/lib/facebooker/admin.rb +42 -0
  51. data/lib/facebooker/application.rb +13 -0
  52. data/lib/facebooker/attachment.rb +51 -0
  53. data/lib/facebooker/batch_request.rb +45 -0
  54. data/lib/facebooker/data.rb +57 -0
  55. data/lib/facebooker/feed.rb +78 -0
  56. data/lib/facebooker/logging.rb +44 -0
  57. data/lib/facebooker/mobile.rb +20 -0
  58. data/lib/facebooker/mock/service.rb +50 -0
  59. data/lib/facebooker/mock/session.rb +18 -0
  60. data/lib/facebooker/model.rb +139 -0
  61. data/lib/facebooker/models/affiliation.rb +10 -0
  62. data/lib/facebooker/models/album.rb +11 -0
  63. data/lib/facebooker/models/applicationproperties.rb +39 -0
  64. data/lib/facebooker/models/applicationrestrictions.rb +10 -0
  65. data/lib/facebooker/models/cookie.rb +10 -0
  66. data/lib/facebooker/models/education_info.rb +11 -0
  67. data/lib/facebooker/models/event.rb +28 -0
  68. data/lib/facebooker/models/family_relative_info.rb +7 -0
  69. data/lib/facebooker/models/friend_list.rb +16 -0
  70. data/lib/facebooker/models/group.rb +36 -0
  71. data/lib/facebooker/models/info_item.rb +10 -0
  72. data/lib/facebooker/models/info_section.rb +10 -0
  73. data/lib/facebooker/models/location.rb +8 -0
  74. data/lib/facebooker/models/notifications.rb +17 -0
  75. data/lib/facebooker/models/page.rb +44 -0
  76. data/lib/facebooker/models/photo.rb +19 -0
  77. data/lib/facebooker/models/tag.rb +12 -0
  78. data/lib/facebooker/models/user.rb +575 -0
  79. data/lib/facebooker/models/video.rb +9 -0
  80. data/lib/facebooker/models/work_info.rb +10 -0
  81. data/lib/facebooker/parser.rb +724 -0
  82. data/lib/facebooker/rails/backwards_compatible_param_checks.rb +31 -0
  83. data/lib/facebooker/rails/controller.rb +372 -0
  84. data/lib/facebooker/rails/cucumber.rb +28 -0
  85. data/lib/facebooker/rails/cucumber/world.rb +40 -0
  86. data/lib/facebooker/rails/extensions/action_controller.rb +48 -0
  87. data/lib/facebooker/rails/extensions/rack_setup.rb +14 -0
  88. data/lib/facebooker/rails/extensions/routing.rb +15 -0
  89. data/lib/facebooker/rails/facebook_form_builder.rb +141 -0
  90. data/lib/facebooker/rails/facebook_pretty_errors.rb +22 -0
  91. data/lib/facebooker/rails/facebook_request_fix.rb +28 -0
  92. data/lib/facebooker/rails/facebook_request_fix_2-3.rb +31 -0
  93. data/lib/facebooker/rails/facebook_session_handling.rb +68 -0
  94. data/lib/facebooker/rails/facebook_url_helper.rb +192 -0
  95. data/lib/facebooker/rails/facebook_url_rewriting.rb +60 -0
  96. data/lib/facebooker/rails/helpers.rb +829 -0
  97. data/lib/facebooker/rails/helpers/fb_connect.rb +144 -0
  98. data/lib/facebooker/rails/integration_session.rb +38 -0
  99. data/lib/facebooker/rails/profile_publisher_extensions.rb +42 -0
  100. data/lib/facebooker/rails/publisher.rb +599 -0
  101. data/lib/facebooker/rails/routing.rb +49 -0
  102. data/lib/facebooker/rails/test_helpers.rb +68 -0
  103. data/lib/facebooker/rails/utilities.rb +22 -0
  104. data/lib/facebooker/server_cache.rb +24 -0
  105. data/lib/facebooker/service.rb +102 -0
  106. data/lib/facebooker/service/base_service.rb +19 -0
  107. data/lib/facebooker/service/curl_service.rb +44 -0
  108. data/lib/facebooker/service/net_http_service.rb +12 -0
  109. data/lib/facebooker/service/typhoeus_multi_service.rb +27 -0
  110. data/lib/facebooker/service/typhoeus_service.rb +17 -0
  111. data/lib/facebooker/session.rb +732 -0
  112. data/lib/facebooker/stream_post.rb +19 -0
  113. data/lib/facebooker/version.rb +9 -0
  114. data/lib/net/http_multipart_post.rb +123 -0
  115. data/lib/rack/facebook.rb +89 -0
  116. data/lib/rack/facebook_session.rb +21 -0
  117. data/lib/tasks/facebooker.rake +18 -0
  118. data/lib/tasks/facebooker.rb +2 -0
  119. data/lib/tasks/tunnel.rake +46 -0
  120. data/rails/init.rb +1 -0
  121. data/setup.rb +1585 -0
  122. data/templates/layout.erb +24 -0
  123. data/test/facebooker/adapters_test.rb +191 -0
  124. data/test/facebooker/admin_test.rb +102 -0
  125. data/test/facebooker/application_test.rb +41 -0
  126. data/test/facebooker/attachment_test.rb +72 -0
  127. data/test/facebooker/batch_request_test.rb +83 -0
  128. data/test/facebooker/data_test.rb +86 -0
  129. data/test/facebooker/logging_test.rb +43 -0
  130. data/test/facebooker/mobile_test.rb +45 -0
  131. data/test/facebooker/model_test.rb +133 -0
  132. data/test/facebooker/models/event_test.rb +15 -0
  133. data/test/facebooker/models/page_test.rb +51 -0
  134. data/test/facebooker/models/photo_test.rb +16 -0
  135. data/test/facebooker/models/user_test.rb +430 -0
  136. data/test/facebooker/rails/facebook_request_fix_2-3_test.rb +25 -0
  137. data/test/facebooker/rails/facebook_url_rewriting_test.rb +76 -0
  138. data/test/facebooker/rails/integration_session_test.rb +13 -0
  139. data/test/facebooker/rails/publisher_test.rb +532 -0
  140. data/test/facebooker/rails_integration_test.rb +1524 -0
  141. data/test/facebooker/server_cache_test.rb +44 -0
  142. data/test/facebooker/service_test.rb +58 -0
  143. data/test/facebooker/session_test.rb +883 -0
  144. data/test/facebooker_test.rb +951 -0
  145. data/test/fixtures/multipart_post_body_with_only_parameters.txt +33 -0
  146. data/test/fixtures/multipart_post_body_with_single_file.txt +38 -0
  147. data/test/fixtures/multipart_post_body_with_single_file_that_has_nil_key.txt +38 -0
  148. data/test/net/http_multipart_post_test.rb +52 -0
  149. data/test/rack/facebook_session_test.rb +34 -0
  150. data/test/rack/facebook_test.rb +73 -0
  151. data/test/rails_test_helper.rb +35 -0
  152. data/test/test_helper.rb +74 -0
  153. metadata +271 -0
@@ -0,0 +1,60 @@
1
+ module ::ActionController
2
+ if Rails.version < '2.3'
3
+ class AbstractRequest
4
+ def relative_url_root
5
+ Facebooker.path_prefix
6
+ end
7
+ end
8
+ else
9
+ class Request
10
+ def relative_url_root
11
+ Facebooker.path_prefix
12
+ end
13
+ end
14
+ end
15
+
16
+ class Base
17
+ class << self
18
+ alias :old_relative_url_root :relative_url_root
19
+ def relative_url_root
20
+ Facebooker.path_prefix
21
+ end
22
+ end
23
+ end
24
+
25
+ class UrlRewriter
26
+ include Facebooker::Rails::BackwardsCompatibleParamChecks
27
+
28
+ RESERVED_OPTIONS << :canvas
29
+
30
+ def link_to_new_canvas?
31
+ one_or_true @request.parameters["fb_sig_in_new_facebook"]
32
+ end
33
+
34
+ def link_to_canvas?(params, options)
35
+ option_override = options[:canvas]
36
+ return false if option_override == false # important to check for false. nil should use default behavior
37
+ option_override || (can_safely_access_request_parameters? && (one_or_true(@request.parameters["fb_sig_in_canvas"]) || one_or_true(@request.parameters[:fb_sig_in_canvas]) || one_or_true(@request.parameters["fb_sig_is_ajax"]) ))
38
+ end
39
+
40
+ #rails blindly tries to merge things that may be nil into the parameters. Make sure this won't break
41
+ def can_safely_access_request_parameters?
42
+ @request.request_parameters
43
+ end
44
+
45
+ def rewrite_url_with_facebooker(*args)
46
+ options = args.first.is_a?(Hash) ? args.first : args.last
47
+ is_link_to_canvas = link_to_canvas?(@request.request_parameters, options)
48
+ if is_link_to_canvas && !options.has_key?(:host)
49
+ options[:host] = Facebooker.canvas_server_base
50
+ end
51
+ options.delete(:canvas)
52
+ Facebooker.request_for_canvas(is_link_to_canvas) do
53
+ rewrite_url_without_facebooker(*args)
54
+ end
55
+ end
56
+
57
+ alias_method_chain :rewrite_url, :facebooker
58
+
59
+ end
60
+ end
@@ -0,0 +1,829 @@
1
+ require 'action_pack'
2
+
3
+ module Facebooker
4
+ module Rails
5
+
6
+ # Facebook specific helpers for creating FBML
7
+ #
8
+ # All helpers that take a user as a parameter will get the Facebook UID from the facebook_id attribute if it exists.
9
+ # It will use to_s if the facebook_id attribute is not present.
10
+ #
11
+ module Helpers
12
+
13
+ include Facebooker::Rails::Helpers::FbConnect
14
+
15
+ def versioned_concat(string,binding)
16
+ if ignore_binding?
17
+ concat(string)
18
+ else
19
+ concat(string,binding)
20
+ end
21
+ end
22
+
23
+ # Create an fb:dialog
24
+ # id must be a unique name e.g. "my_dialog"
25
+ # cancel_button is true or false
26
+ def fb_dialog( id, cancel_button, &block )
27
+ content = capture(&block)
28
+ cancel_button = cancel_button ? 1 : 0 unless cancel_button == 0
29
+ versioned_concat( content_tag("fb:dialog", content, {:id => id, :cancel_button => cancel_button}), block.binding )
30
+ end
31
+
32
+ def fbjs_library
33
+ "<script>var _token = '#{form_authenticity_token}';var _hostname = '#{ActionController::Base.asset_host}'</script>"+
34
+ "#{javascript_include_tag 'facebooker'}"
35
+ end
36
+
37
+ def fb_iframe(src, options = {})
38
+ content_tag "fb:iframe", '', options.merge({ :src => src })
39
+ end
40
+
41
+ def fb_swf(src, options = {})
42
+ tag "fb:swf", options.merge(:swfsrc => src)
43
+ end
44
+
45
+ def fb_dialog_title( title )
46
+ content_tag "fb:dialog-title", title
47
+ end
48
+
49
+ def fb_dialog_content( &block )
50
+ content = capture(&block)
51
+ versioned_concat( content_tag("fb:dialog-content", content), block.binding )
52
+ end
53
+
54
+ def fb_dialog_button( type, value, options={} )
55
+ options.assert_valid_keys FB_DIALOG_BUTTON_VALID_OPTION_KEYS
56
+ options.merge! :type => type, :value => value
57
+ tag "fb:dialog-button", options
58
+ end
59
+
60
+ FB_DIALOG_BUTTON_VALID_OPTION_KEYS = [:close_dialog, :href, :form_id, :clickrewriteurl, :clickrewriteid, :clickrewriteform]
61
+
62
+ def fb_show_feed_dialog(action, user_message = "", prompt = "", callback = nil)
63
+ update_page do |page|
64
+ page.call "Facebook.showFeedDialog",action.template_id,action.data,action.body_general,action.target_ids,callback,prompt,user_message
65
+ end
66
+ end
67
+
68
+
69
+ # Create an fb:request-form without a selector
70
+ #
71
+ # The block passed to this tag is used as the content of the form
72
+ #
73
+ # The message param is the name sent to content_for that specifies the body of the message
74
+ #
75
+ # For example,
76
+ #
77
+ # <% content_for("invite_message") do %>
78
+ # This gets sent in the invite. <%= fb_req_choice("with a button!",new_poke_path) %>
79
+ # <% end %>
80
+ # <% fb_request_form("Poke","invite_message",create_poke_path) do %>
81
+ # Send a poke to: <%= fb_friend_selector %> <br />
82
+ # <%= fb_request_form_submit %>
83
+ # <% end %>
84
+ def fb_request_form(type,message_param,url,options={},&block)
85
+ content = capture(&block)
86
+ message = @template.instance_variable_get("@content_for_#{message_param}")
87
+ versioned_concat(content_tag("fb:request-form", content + token_tag,
88
+ {:action=>url,:method=>"post",:invite=>true,:type=>type,:content=>message}.merge(options)),
89
+ block.binding)
90
+ end
91
+
92
+ # Create a submit button for an <fb:request-form>
93
+ # If the request is for an individual user you can optionally
94
+ # Provide the user and a label for the request button.
95
+ # For example
96
+ # <% content_for("invite_user") do %>
97
+ # This gets sent in the invite. <%= fb_req_choice("Come join us!",new_invite_path) %>
98
+ # <% end %>
99
+ # <% fb_request_form("Invite","invite_user",create_invite_path) do %>
100
+ # Invite <%= fb_name(@facebook_user.friends.first.id)%> to the party <br />
101
+ # <%= fb_request_form_submit(:uid => @facebook_user.friends.first.id, :label => "Invite %n") %>
102
+ # <% end %>
103
+ # <em>See:</em> http://wiki.developers.facebook.com/index.php/Fb:request-form-submit for options
104
+ def fb_request_form_submit(options={})
105
+ tag("fb:request-form-submit",stringify_vals(options))
106
+ end
107
+
108
+
109
+ # Create an fb:request-form with an fb_multi_friend_selector inside
110
+ #
111
+ # The content of the block are used as the message on the form, the options hash is passed onto fb_multi_friend_selector.
112
+ #
113
+ # For example:
114
+ # <% fb_multi_friend_request("Poke","Choose some friends to Poke",create_poke_path,:exclude_ids => "123456789,987654321") do %>
115
+ # If you select some friends, they will see this message.
116
+ # <%= fb_req_choice("They will get this button, too",new_poke_path) %>
117
+ # <% end %>
118
+ def fb_multi_friend_request(type,friend_selector_message,url, fb_multi_friend_selector_options = {},&block)
119
+ content = capture(&block)
120
+ versioned_concat(content_tag("fb:request-form",
121
+ fb_multi_friend_selector(friend_selector_message, fb_multi_friend_selector_options) + token_tag,
122
+ {:action=>url,:method=>"post",:invite=>true,:type=>type,:content=>content}
123
+ ),
124
+ block.binding)
125
+ end
126
+
127
+ # Render an <fb:friend-selector> element
128
+ # <em>See:</em> http://wiki.developers.facebook.com/index.php/Fb:friend-selector for options
129
+ #
130
+ def fb_friend_selector(options={})
131
+ tag("fb:friend-selector",stringify_vals(options))
132
+ end
133
+
134
+ # Render an <fb:multi-friend-input> element
135
+ # <em> See: </em> http://wiki.developers.facebook.com/index.php/Fb:multi-friend-input for options
136
+ def fb_multi_friend_input(options={})
137
+ tag "fb:multi-friend-input",stringify_vals(options)
138
+ end
139
+
140
+ # Render an <fb:multi-friend-selector> with the passed in welcome message
141
+ # Full version shows all profile pics for friends.
142
+ # <em> See: </em> http://wiki.developers.facebook.com/index.php/Fb:multi-friend-selector for options
143
+ # <em> Note: </em> I don't think the block is used here.
144
+ def fb_multi_friend_selector(message,options={},&block)
145
+ options = options.dup
146
+ tag("fb:multi-friend-selector",stringify_vals({:showborder=>false,:actiontext=>message,:max=>20}.merge(options)))
147
+ end
148
+
149
+ # Render a condensed <fb:multi-friend-selector> with the passed in welcome message
150
+ # Condensed version show checkboxes for each friend.
151
+ # <em> See: </em> http://wiki.developers.facebook.com/index.php/Fb:multi-friend-selector_%28condensed%29 for options
152
+ # <em> Note: </em> I don't think the block is used here.
153
+ def fb_multi_friend_selector_condensed(options={},&block)
154
+ options = options.dup
155
+ tag("fb:multi-friend-selector",stringify_vals(options.merge(:condensed=>true)))
156
+ end
157
+
158
+ # Render a button in a request using the <fb:req-choice> tag
159
+ # url must be an absolute url
160
+ # This should be used inside the block of an fb_multi_friend_request
161
+ def fb_req_choice(label,url)
162
+ tag "fb:req-choice",:label=>label,:url=>url
163
+ end
164
+
165
+ # Create a facebook form using <fb:editor>
166
+ #
167
+ # It yields a form builder that will convert the standard rails form helpers
168
+ # into the facebook specific version.
169
+ #
170
+ # Example:
171
+ # <% facebook_form_for(:poke,@poke,:url => create_poke_path) do |f| %>
172
+ # <%= f.text_field :message, :label=>"message" %>
173
+ # <%= f.buttons "Save Poke" %>
174
+ # <% end %>
175
+ #
176
+ # will generate
177
+ #
178
+ # <fb:editor action="/pokes/create">
179
+ # <fb:editor-text name="poke[message]" id="poke_message" value="" label="message" />
180
+ # <fb:editor-buttonset>
181
+ # <fb:editor-button label="Save Poke"
182
+ # </fb:editor-buttonset>
183
+ # </fb:editor>
184
+ def facebook_form_for( record_or_name_or_array,*args, &proc)
185
+
186
+ raise ArgumentError, "Missing block" unless block_given?
187
+ options = args.last.is_a?(Hash) ? args.pop : {}
188
+
189
+ case record_or_name_or_array
190
+ when String, Symbol
191
+ object_name = record_or_name_or_array
192
+ when Array
193
+ object = record_or_name_or_array.last
194
+ object_name = ActionController::RecordIdentifier.singular_class_name(object)
195
+ apply_form_for_options!(record_or_name_or_array, options)
196
+ args.unshift object
197
+ else
198
+ object = record_or_name_or_array
199
+ object_name = ActionController::RecordIdentifier.singular_class_name(object)
200
+ apply_form_for_options!([object], options)
201
+ args.unshift object
202
+ end
203
+ method = (options[:html]||{})[:method]
204
+ options[:builder] ||= Facebooker::Rails::FacebookFormBuilder
205
+ editor_options={}
206
+
207
+ action=options.delete(:url)
208
+ editor_options[:action]= action unless action.blank?
209
+ width=options.delete(:width)
210
+ editor_options[:width]=width unless width.blank?
211
+ width=options.delete(:labelwidth)
212
+ editor_options[:labelwidth]=width unless width.blank?
213
+
214
+ versioned_concat(tag("fb:editor",editor_options,true) , proc.binding)
215
+ versioned_concat(tag(:input,{:type=>"hidden",:name=>:_method, :value=>method},false), proc.binding) unless method.blank?
216
+ versioned_concat(token_tag, proc.binding)
217
+ fields_for( object_name,*(args << options), &proc)
218
+ versioned_concat("</fb:editor>",proc.binding)
219
+ end
220
+
221
+ # Render an fb:application-name tag
222
+ #
223
+ # This renders the current application name via fbml. See
224
+ # http://wiki.developers.facebook.com/index.php/Fb:application-name
225
+ # for a full description.
226
+ #
227
+ def fb_application_name(options={})
228
+ tag "fb:application-name", stringify_vals(options)
229
+ end
230
+
231
+ # Render an fb:name tag for the given user
232
+ # This renders the name of the user specified. You can use this tag as both subject and object of
233
+ # a sentence. <em> See </em> http://wiki.developers.facebook.com/index.php/Fb:name for full description.
234
+ # Use this tag on FBML pages instead of retrieving the user's info and rendering the name explicitly.
235
+ #
236
+ def fb_name(user, options={})
237
+ options = options.dup
238
+ options.transform_keys!(FB_NAME_OPTION_KEYS_TO_TRANSFORM)
239
+ options.assert_valid_keys(FB_NAME_VALID_OPTION_KEYS)
240
+ options.merge!(:uid => cast_to_facebook_id(user))
241
+ content_tag("fb:name",nil, stringify_vals(options))
242
+ end
243
+
244
+ FB_NAME_OPTION_KEYS_TO_TRANSFORM = {:first_name_only => :firstnameonly,
245
+ :last_name_only => :lastnameonly,
246
+ :show_network => :shownetwork,
247
+ :use_you => :useyou,
248
+ :if_cant_see => :ifcantsee,
249
+ :subject_id => :subjectid}
250
+ FB_NAME_VALID_OPTION_KEYS = [:firstnameonly, :linked, :lastnameonly, :possessive, :reflexive,
251
+ :shownetwork, :useyou, :ifcantsee, :capitalize, :subjectid]
252
+
253
+ # Render an <fb:pronoun> tag for the specified user
254
+ # Options give flexibility for placing in any part of a sentence.
255
+ # <em> See </em> http://wiki.developers.facebook.com/index.php/Fb:pronoun for complete list of options.
256
+ #
257
+ def fb_pronoun(user, options={})
258
+ options = options.dup
259
+ options.transform_keys!(FB_PRONOUN_OPTION_KEYS_TO_TRANSFORM)
260
+ options.assert_valid_keys(FB_PRONOUN_VALID_OPTION_KEYS)
261
+ options.merge!(:uid => cast_to_facebook_id(user))
262
+ content_tag("fb:pronoun",nil, stringify_vals(options))
263
+ end
264
+
265
+ FB_PRONOUN_OPTION_KEYS_TO_TRANSFORM = {:use_you => :useyou, :use_they => :usethey}
266
+ FB_PRONOUN_VALID_OPTION_KEYS = [:useyou, :possessive, :reflexive, :objective,
267
+ :usethey, :capitalize]
268
+
269
+ # Render an fb:ref tag.
270
+ # Options must contain either url or handle.
271
+ # * <em> url </em> The URL from which to fetch the FBML. You may need to call fbml.refreshRefUrl to refresh cache
272
+ # * <em> handle </em> The string previously set by fbml.setRefHandle that identifies the FBML
273
+ # <em> See </em> http://wiki.developers.facebook.com/index.php/Fb:ref for complete description
274
+ def fb_ref(options)
275
+ options.assert_valid_keys(FB_REF_VALID_OPTION_KEYS)
276
+ validate_fb_ref_has_either_url_or_handle(options)
277
+ validate_fb_ref_does_not_have_both_url_and_handle(options)
278
+ tag("fb:ref", stringify_vals(options))
279
+ end
280
+
281
+ def validate_fb_ref_has_either_url_or_handle(options)
282
+ unless options.has_key?(:url) || options.has_key?(:handle)
283
+ raise ArgumentError, "fb_ref requires :url or :handle"
284
+ end
285
+ end
286
+
287
+ def validate_fb_ref_does_not_have_both_url_and_handle(options)
288
+ if options.has_key?(:url) && options.has_key?(:handle)
289
+ raise ArgumentError, "fb_ref may not have both :url and :handle"
290
+ end
291
+ end
292
+
293
+ FB_REF_VALID_OPTION_KEYS = [:url, :handle]
294
+
295
+ # Render an <fb:profile-pic> for the specified user.
296
+ #
297
+ # You can optionally specify the size using the :size=> option. Valid
298
+ # sizes are :thumb, :small, :normal and :square. Or, you can specify
299
+ # width and height settings instead, just like an img tag.
300
+ #
301
+ # You can optionally specify whether or not to include the facebook icon
302
+ # overlay using the :facebook_logo => true option. Default is false.
303
+ #
304
+ def fb_profile_pic(user, options={})
305
+ options = options.dup
306
+ validate_fb_profile_pic_size(options)
307
+ options.transform_keys!(FB_PROFILE_PIC_OPTION_KEYS_TO_TRANSFORM)
308
+ options.assert_valid_keys(FB_PROFILE_PIC_VALID_OPTION_KEYS)
309
+ options.merge!(:uid => cast_to_facebook_id(user))
310
+ content_tag("fb:profile-pic", nil,stringify_vals(options))
311
+ end
312
+
313
+ FB_PROFILE_PIC_OPTION_KEYS_TO_TRANSFORM = {:facebook_logo => 'facebook-logo'}
314
+ FB_PROFILE_PIC_VALID_OPTION_KEYS = [:size, :linked, 'facebook-logo', :width, :height]
315
+
316
+
317
+ # Render an fb:photo tag.
318
+ # photo is either a Facebooker::Photo or an id of a Facebook photo or an object that responds to photo_id.
319
+ # <em> See: </em> http://wiki.developers.facebook.com/index.php/Fb:photo for complete list of options.
320
+ def fb_photo(photo, options={})
321
+ options = options.dup
322
+ options.assert_valid_keys(FB_PHOTO_VALID_OPTION_KEYS)
323
+ options.merge!(:pid => cast_to_photo_id(photo))
324
+ validate_fb_photo_size(options)
325
+ validate_fb_photo_align_value(options)
326
+ content_tag("fb:photo",nil, stringify_vals(options))
327
+ end
328
+
329
+ FB_PHOTO_VALID_OPTION_KEYS = [:uid, :size, :align]
330
+
331
+ def cast_to_photo_id(object)
332
+ object.respond_to?(:photo_id) ? object.photo_id : object
333
+ end
334
+
335
+ VALID_FB_SHARED_PHOTO_SIZES = [:thumb, :small, :normal, :square]
336
+ VALID_FB_PHOTO_SIZES = VALID_FB_SHARED_PHOTO_SIZES
337
+ VALID_FB_PROFILE_PIC_SIZES = VALID_FB_SHARED_PHOTO_SIZES
338
+ VALID_PERMISSIONS=[:email, :offline_access, :status_update, :photo_upload, :create_listing, :create_event, :rsvp_event, :sms, :video_upload,
339
+ :publish_stream, :read_stream]
340
+
341
+ # Render an fb:tabs tag containing some number of fb:tab_item tags.
342
+ # Example:
343
+ # <% fb_tabs do %>
344
+ # <%= fb_tab_item("Home", "home") %>
345
+ # <%= fb_tab_item("Office", "office") %>
346
+ # <% end %>
347
+ def fb_tabs(&block)
348
+ content = capture(&block)
349
+ versioned_concat(content_tag("fb:tabs", content), block.binding)
350
+ end
351
+
352
+ # Render an fb:tab_item tag.
353
+ # Use this in conjunction with fb_tabs
354
+ # Options can contains :selected => true to indicate that a tab is the current tab.
355
+ # <em> See: </em> http://wiki.developers.facebook.com/index.php/Fb:tab-item for complete list of options
356
+ def fb_tab_item(title, url, options={})
357
+ options= options.dup
358
+ options.assert_valid_keys(FB_TAB_ITEM_VALID_OPTION_KEYS)
359
+ options.merge!(:title => title, :href => url)
360
+ validate_fb_tab_item_align_value(options)
361
+ tag("fb:tab-item", stringify_vals(options))
362
+ end
363
+
364
+ FB_TAB_ITEM_VALID_OPTION_KEYS = [:align, :selected]
365
+
366
+ def validate_fb_tab_item_align_value(options)
367
+ if options.has_key?(:align) && !VALID_FB_TAB_ITEM_ALIGN_VALUES.include?(options[:align].to_sym)
368
+ raise(ArgumentError, "Unknown value for align: #{options[:align]}")
369
+ end
370
+ end
371
+
372
+ def validate_fb_photo_align_value(options)
373
+ if options.has_key?(:align) && !VALID_FB_PHOTO_ALIGN_VALUES.include?(options[:align].to_sym)
374
+ raise(ArgumentError, "Unknown value for align: #{options[:align]}")
375
+ end
376
+ end
377
+
378
+ VALID_FB_SHARED_ALIGN_VALUES = [:left, :right]
379
+ VALID_FB_PHOTO_ALIGN_VALUES = VALID_FB_SHARED_ALIGN_VALUES
380
+ VALID_FB_TAB_ITEM_ALIGN_VALUES = VALID_FB_SHARED_ALIGN_VALUES
381
+
382
+
383
+ # Create a Facebook wall. It can contain fb_wall_posts
384
+ #
385
+ # For Example:
386
+ # <% fb_wall do %>
387
+ # <%= fb_wall_post(@user,"This is my message") %>
388
+ # <%= fb_wall_post(@otheruser,"This is another message") %>
389
+ # <% end %>
390
+ def fb_wall(&proc)
391
+ content = capture(&proc)
392
+ versioned_concat(content_tag("fb:wall",content,{}),proc.binding)
393
+ end
394
+
395
+ # Render an <fb:wallpost> tag
396
+ # TODO: Optionally takes a time parameter t = int The current time, which is displayed in epoch seconds.
397
+ def fb_wallpost(user,message)
398
+ content_tag("fb:wallpost",message,:uid=>cast_to_facebook_id(user))
399
+ end
400
+ alias_method :fb_wall_post, :fb_wallpost
401
+
402
+ # Render an <fb:error> tag
403
+ # If message and text are present then this will render fb:error and fb:message tag
404
+ # TODO: Optionally takes a decoration tag with value of 'no_padding' or 'shorten'
405
+ def fb_error(message, text=nil)
406
+ fb_status_msg("error", message, text)
407
+ end
408
+
409
+ # Render an <fb:explanation> tag
410
+ # If message and text are present then this will render fb:error and fb:message tag
411
+ # TODO: Optionally takes a decoration tag with value of 'no_padding' or 'shorten'
412
+ def fb_explanation(message, text=nil)
413
+ fb_status_msg("explanation", message, text)
414
+ end
415
+
416
+ # Render an <fb:success> tag
417
+ # If message and text are present then this will render fb:error and fb:message tag
418
+ # TODO: Optionally takes a decoration tag with value of 'no_padding' or 'shorten'
419
+ def fb_success(message, text=nil)
420
+ fb_status_msg("success", message, text)
421
+ end
422
+
423
+ # Render flash values as <fb:message> and <fb:error> tags
424
+ #
425
+ # values in flash[:notice] will be rendered as an <fb:message>
426
+ #
427
+ # values in flash[:error] will be rendered as an <fb:error>
428
+ # TODO: Allow flash[:info] to render fb_explanation
429
+ def facebook_messages
430
+ message=""
431
+ unless flash[:notice].blank?
432
+ message += fb_success(*flash[:notice])
433
+ end
434
+ unless flash[:error].blank?
435
+ message += fb_error(*flash[:error])
436
+ end
437
+ message
438
+ end
439
+
440
+ # Create a dashboard. It can contain fb_action, fb_help, and fb_create_button
441
+ #
442
+ # For Example:
443
+ # <% fb_dashboard do %>
444
+ # <%= APP_NAME %>
445
+ # <%= fb_action 'My Matches', search_path %>
446
+ # <%= fb_help 'Feedback', "http://www.facebook.com/apps/application.php?id=6236036681" %>
447
+ # <%= fb_create_button 'Invite Friends', main_path %>
448
+ # <% end %>
449
+ def fb_dashboard(&proc)
450
+ if block_given?
451
+ content = capture(&proc)
452
+ versioned_concat(content_tag("fb:dashboard",content,{}),proc.binding)
453
+ else
454
+ content_tag("fb:dashboard",content,{})
455
+ end
456
+ end
457
+
458
+ # Content for the wide profile box goes in this tag
459
+ def fb_wide(&proc)
460
+ content = capture(&proc)
461
+ versioned_concat(content_tag("fb:wide", content, {}), proc.binding)
462
+ end
463
+
464
+ # Content for the narrow profile box goes in this tag
465
+ def fb_narrow(&proc)
466
+ content = capture(&proc)
467
+ versioned_concat(content_tag("fb:narrow", content, {}), proc.binding)
468
+ end
469
+
470
+ # Renders an action using the <fb:action> tag
471
+ def fb_action(name, url)
472
+ "<fb:action href=\"#{url_for(url)}\">#{name}</fb:action>"
473
+ end
474
+
475
+ # Render a <fb:help> tag
476
+ # For use inside <fb:dashboard>
477
+ def fb_help(name, url)
478
+ "<fb:help href=\"#{url_for(url)}\">#{name}</fb:help>"
479
+ end
480
+
481
+ # Render a <fb:create-button> tag
482
+ # For use inside <fb:dashboard>
483
+ def fb_create_button(name, url)
484
+ "<fb:create-button href=\"#{url_for(url)}\">#{name}</fb:create-button>"
485
+ end
486
+
487
+ # Create a comment area
488
+ # All the data for this content area is stored on the facebook servers.
489
+ # <em>See:</em> http://wiki.developers.facebook.com/index.php/Fb:comments for full details
490
+ def fb_comments(xid,canpost=true,candelete=false,numposts=5,options={})
491
+ options = options.dup
492
+ title = (title = options.delete(:title)) ? fb_title(title) : nil
493
+ content_tag "fb:comments",title,stringify_vals(options.merge(:xid=>xid,:canpost=>canpost.to_s,:candelete=>candelete.to_s,:numposts=>numposts))
494
+ end
495
+
496
+ # Adds a title to the title bar
497
+ #
498
+ # Facebook | App Name | This is the canvas page window title
499
+ #
500
+ # +title+: This is the canvas page window
501
+ def fb_title(title)
502
+ "<fb:title>#{title}</fb:title>"
503
+ end
504
+
505
+ # Create a Google Analytics tag
506
+ #
507
+ # +uacct+: Your Urchin/Google Analytics account ID.
508
+ def fb_google_analytics(uacct, options={})
509
+ options = options.dup
510
+ tag "fb:google-analytics", stringify_vals(options.merge(:uacct => uacct))
511
+ end
512
+
513
+ # Render if-is-app-user tag
514
+ # This tag renders the enclosing content only if the user specified has accepted the terms of service for the application.
515
+ # Use fb_if_user_has_added_app to determine wether the user has added the app.
516
+ # Example:
517
+ # <% fb_if_is_app_user(@facebook_user) do %>
518
+ # Thanks for accepting our terms of service!
519
+ # <% fb_else do %>
520
+ # Hey you haven't agreed to our terms. <%= link_to("Please accept our terms of service.", :action => "terms_of_service") %>
521
+ # <% end %>
522
+ #<% end %>
523
+ def fb_if_is_app_user(user=nil,options={},&proc)
524
+ content = capture(&proc)
525
+ options = options.dup
526
+ options.merge!(:uid=>cast_to_facebook_id(user)) if user
527
+ versioned_concat(content_tag("fb:if-is-app-user",content,stringify_vals(options)),proc.binding)
528
+ end
529
+
530
+ # Render if-user-has-added-app tag
531
+ # This tag renders the enclosing content only if the user specified has installed the application
532
+ #
533
+ # Example:
534
+ # <% fb_if_user_has_added_app(@facebook_user) do %>
535
+ # Hey you are an app user!
536
+ # <% fb_else do %>
537
+ # Hey you aren't an app user. <%= link_to("Add App and see the other side.", :action => "added_app") %>
538
+ # <% end %>
539
+ #<% end %>
540
+ def fb_if_user_has_added_app(user,options={},&proc)
541
+ content = capture(&proc)
542
+ options = options.dup
543
+ versioned_concat(content_tag("fb:if-user-has-added-app", content, stringify_vals(options.merge(:uid=>cast_to_facebook_id(user)))),proc.binding)
544
+ end
545
+
546
+ # Render fb:if-is-user tag
547
+ # This tag only renders enclosing content if the user is one of those specified
548
+ # user can be a single user or an Array of users
549
+ # Example:
550
+ # <% fb_if_is_user(@check_user) do %>
551
+ # <%= fb_name(@facebook_user) %> are one of the users. <%= link_to("Check the other side", :action => "friend") %>
552
+ # <% fb_else do %>
553
+ # <%= fb_name(@facebook_user) %> are not one of the users <%= fb_name(@check_user) %>
554
+ # <%= link_to("Check the other side", :action => "you") %>
555
+ # <% end %>
556
+ # <% end %>
557
+ def fb_if_is_user(user,&proc)
558
+ content = capture(&proc)
559
+ user = [user] unless user.is_a? Array
560
+ user_list=user.map{|u| cast_to_facebook_id(u)}.join(",")
561
+ versioned_concat(content_tag("fb:if-is-user",content,{:uid=>user_list}),proc.binding)
562
+ end
563
+
564
+ # Render fb:else tag
565
+ # Must be used within if block such as fb_if_is_user or fb_if_is_app_user . See example in fb_if_is_app_user
566
+ def fb_else(&proc)
567
+ content = capture(&proc)
568
+ versioned_concat(content_tag("fb:else",content),proc.binding)
569
+ end
570
+
571
+ #
572
+ # Return the URL for the about page of the application
573
+ def fb_about_url
574
+ "http://#{Facebooker.www_server_base_url}/apps/application.php?api_key=#{Facebooker.api_key}"
575
+ end
576
+
577
+ #
578
+ # Embed a discussion board named xid on the current page
579
+ # <em>See</em http://wiki.developers.facebook.com/index.php/Fb:board for more details
580
+ # Options are:
581
+ # * canpost
582
+ # * candelete
583
+ # * canmark
584
+ # * cancreatet
585
+ # * numtopics
586
+ # * callbackurl
587
+ # * returnurl
588
+ #
589
+ def fb_board(xid,options={})
590
+ options = options.dup
591
+ title = (title = options.delete(:title)) ? fb_title(title) : nil
592
+ content_tag("fb:board", title, stringify_vals(options.merge(:xid=>xid)))
593
+ end
594
+
595
+ # Renders an 'Add to Profile' button
596
+ # The button allows a user to add condensed profile box to the main profile
597
+ def fb_add_profile_section
598
+ tag "fb:add-section-button",:section=>"profile"
599
+ end
600
+
601
+ # Renders an 'Add to Info' button
602
+ # The button allows a user to add an application info section to her Info tab
603
+ def fb_add_info_section
604
+ tag "fb:add-section-button",:section=>"info"
605
+ end
606
+
607
+ # Renders a link that, when clicked, initiates a dialog requesting the specified extended permission from the user.
608
+ #
609
+ # You can prompt a user with the following permissions:
610
+ # * email
611
+ # * read_stream
612
+ # * publish_stream
613
+ # * offline_access
614
+ # * status_update
615
+ # * photo_upload
616
+ # * create_event
617
+ # * rsvp_event
618
+ # * sms
619
+ # * video_upload
620
+ # * create_note
621
+ # * share_item
622
+ # Example:
623
+ # <%= fb_prompt_permission('email', "Would you like to receive email from our application?" ) %>
624
+ #
625
+ # See http://wiki.developers.facebook.com/index.php/Fb:prompt-permission for
626
+ # more details. Correct as of 7th June 2009.
627
+ #
628
+ def fb_prompt_permission(permission,message,callback=nil)
629
+ raise(ArgumentError, "Unknown value for permission: #{permission}") unless VALID_PERMISSIONS.include?(permission.to_sym)
630
+ args={:perms=>permission}
631
+ args[:next_fbjs]=callback unless callback.nil?
632
+ content_tag("fb:prompt-permission",message,args)
633
+ end
634
+
635
+ # Renders a link to prompt for multiple permissions at once.
636
+ #
637
+ # Example:
638
+ # <%= fb_prompt_permissions(['email','offline_access','status_update'], 'Would you like to grant some permissions?')
639
+ def fb_prompt_permissions(permissions,message,callback=nil)
640
+ permissions.each do |p|
641
+ raise(ArgumentError, "Unknown value for permission: #{p}") unless VALID_PERMISSIONS.include?(p.to_sym)
642
+ end
643
+ args={:perms=>permissions*','}
644
+ args[:next_fbjs]=callback unless callback.nil?
645
+ content_tag("fb:prompt-permission",message,args)
646
+ end
647
+
648
+ # Renders an <fb:eventlink /> tag that displays the event name and links to the event's page.
649
+ def fb_eventlink(eid)
650
+ content_tag "fb:eventlink",nil,:eid=>eid
651
+ end
652
+
653
+ # Renders an <fb:grouplink /> tag that displays the group name and links to the group's page.
654
+ def fb_grouplink(gid)
655
+ content_tag "fb:grouplink",nil,:gid=>gid
656
+ end
657
+
658
+ # Returns the status of the user
659
+ def fb_user_status(user,linked=true)
660
+ content_tag "fb:user-status",nil,stringify_vals(:uid=>cast_to_facebook_id(user), :linked=>linked)
661
+ end
662
+
663
+ # Renders a standard 'Share' button for the specified URL.
664
+ def fb_share_button(url)
665
+ content_tag "fb:share-button",nil,:class=>"url",:href=>url
666
+ end
667
+
668
+ # Renders the FBML on a Facebook server inside an iframe.
669
+ #
670
+ # Meant to be used for a Facebook Connect site or an iframe application
671
+ def fb_serverfbml(options={},&proc)
672
+ inner = capture(&proc)
673
+ versioned_concat(content_tag("fb:serverfbml",inner,options), proc.binding)
674
+ end
675
+
676
+ def fb_container(options={},&proc)
677
+ inner = capture(&proc)
678
+ versioned_concat(content_tag("fb:container",inner,options), proc.binding)
679
+ end
680
+
681
+ # Renders an fb:time element
682
+ #
683
+ # Example:
684
+ # <%= fb_time(Time.now, :tz => 'America/New York', :preposition => true) %>
685
+ #
686
+ # See http://wiki.developers.facebook.com/index.php/Fb:time for
687
+ # more details
688
+ def fb_time(time, options={})
689
+ tag "fb:time",stringify_vals({:t => time.to_i}.merge(options))
690
+ end
691
+
692
+ # Renders a fb:intl element
693
+ #
694
+ # Example:
695
+ # <%= fb_intl('Age', :desc => 'Label for the age form field', :delimiters => '[]') %>
696
+ #
697
+ # See http://wiki.developers.facebook.com/index.php/Fb:intl for
698
+ # more details
699
+ def fb_intl(text=nil, options={}, &proc)
700
+ raise ArgumentError, "Missing block or text" unless block_given? or text
701
+ if block_given?
702
+ versioned_concat(fb_intl(capture(&proc), options))
703
+ else
704
+ content_tag("fb:intl", text, stringify_vals(options))
705
+ end
706
+ end
707
+
708
+ # Renders a fb:intl-token element
709
+ #
710
+ # Example:
711
+ # <%= fb_intl-token('number', 5) %>
712
+ #
713
+ # See http://wiki.developers.facebook.com/index.php/Fb:intl-token for
714
+ # more details
715
+ def fb_intl_token(name, text=nil, &proc)
716
+ raise ArgumentError, "Missing block or text" unless block_given? or text
717
+ if block_given?
718
+ versioned_concat(fb_intl_token(name, capture(&proc)))
719
+ else
720
+ content_tag("fb:intl-token", text, stringify_vals({:name => name}))
721
+ end
722
+ end
723
+
724
+ # Renders a fb:date element
725
+ #
726
+ # Example:
727
+ # <%= fb_date(Time.now, :format => 'verbose', :tz => 'America/New York') %>
728
+ #
729
+ # See http://wiki.developers.facebook.com/index.php/Fb:date for
730
+ # more details
731
+ def fb_date(time, options={})
732
+ tag "fb:date", stringify_vals({:t => time.to_i}.merge(options))
733
+ end
734
+
735
+ # Renders the Facebook bookmark button
736
+ #
737
+ # See http://wiki.developers.facebook.com/index.php/Fb:bookmark for
738
+ # more details
739
+ def fb_bookmark_button
740
+ content_tag "fb:bookmark"
741
+ end
742
+
743
+ # Renders a fb:fbml-attribute element
744
+ #
745
+ # Example:
746
+ # <%= fb_fbml_attribute('title', Education) %>
747
+ #
748
+ # The options hash is passed to the fb:intl element that is generated inside this element
749
+ # and can have the keys available for the fb:intl element.
750
+ #
751
+ # See http://wiki.developers.facebook.com/index.php/Fb:fbml-attribute for
752
+ # more details
753
+ def fb_fbml_attribute(name, text, options={})
754
+ content_tag("fb:fbml-attribute", fb_intl(text, options), stringify_vals({:name => name}))
755
+ end
756
+
757
+ protected
758
+
759
+ def cast_to_facebook_id(object)
760
+ Facebooker::User.cast_to_facebook_id(object)
761
+ end
762
+
763
+ def validate_fb_profile_pic_size(options)
764
+ if options.has_key?(:size) && !VALID_FB_PROFILE_PIC_SIZES.include?(options[:size].to_sym)
765
+ raise(ArgumentError, "Unknown value for size: #{options[:size]}")
766
+ end
767
+ end
768
+
769
+ def validate_fb_photo_size(options)
770
+ if options.has_key?(:size) && !VALID_FB_PHOTO_SIZES.include?(options[:size].to_sym)
771
+ raise(ArgumentError, "Unknown value for size: #{options[:size]}")
772
+ end
773
+ end
774
+
775
+ private
776
+ def stringify_vals(hash)
777
+ result={}
778
+ hash.each do |key,value|
779
+ result[key]=value.to_s
780
+ end
781
+ result
782
+ end
783
+
784
+ def fb_status_msg(type, message, text)
785
+ if text.blank?
786
+ tag("fb:#{type}", :message => message)
787
+ else
788
+ content_tag("fb:#{type}", content_tag("fb:message", message) + text)
789
+ end
790
+ end
791
+
792
+ def token_tag
793
+ unless protect_against_forgery?
794
+ ''
795
+ else
796
+ tag(:input, :type => "hidden", :name => request_forgery_protection_token.to_s, :value => form_authenticity_token)
797
+ end
798
+ end
799
+
800
+ def ignore_binding?
801
+ ActionPack::VERSION::MAJOR >= 2 && ActionPack::VERSION::MINOR >= 2
802
+ end
803
+ end
804
+ end
805
+ end
806
+
807
+ class Hash
808
+ def transform_keys!(transformation_hash)
809
+ transformation_hash.each_pair{|key, value| transform_key!(key, value)}
810
+ end
811
+
812
+
813
+ def transform_key!(old_key, new_key)
814
+ swapkey!(new_key, old_key)
815
+ end
816
+
817
+ ### This method is lifted from Ruby Facets core
818
+ def swapkey!( newkey, oldkey )
819
+ self[newkey] = self.delete(oldkey) if self.has_key?(oldkey)
820
+ self
821
+ end
822
+
823
+ # We can allow css attributes.
824
+ FB_ALWAYS_VALID_OPTION_KEYS = [:class, :style]
825
+ def assert_valid_keys(*valid_keys)
826
+ unknown_keys = keys - [valid_keys + FB_ALWAYS_VALID_OPTION_KEYS].flatten
827
+ raise(ArgumentError, "Unknown key(s): #{unknown_keys.join(", ")}") unless unknown_keys.empty?
828
+ end
829
+ end