fotonauts-facebooker 1.0.67

Sign up to get free protection for your applications and to get access to all the features.
Files changed (156) hide show
  1. data/.autotest +15 -0
  2. data/CHANGELOG.rdoc +24 -0
  3. data/COPYING.rdoc +19 -0
  4. data/Manifest.txt +152 -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 +45 -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 +275 -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 +37 -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/comment.rb +9 -0
  66. data/lib/facebooker/models/cookie.rb +10 -0
  67. data/lib/facebooker/models/education_info.rb +11 -0
  68. data/lib/facebooker/models/event.rb +28 -0
  69. data/lib/facebooker/models/family_relative_info.rb +7 -0
  70. data/lib/facebooker/models/friend_list.rb +16 -0
  71. data/lib/facebooker/models/group.rb +36 -0
  72. data/lib/facebooker/models/info_item.rb +10 -0
  73. data/lib/facebooker/models/info_section.rb +10 -0
  74. data/lib/facebooker/models/location.rb +8 -0
  75. data/lib/facebooker/models/message_thread.rb +89 -0
  76. data/lib/facebooker/models/notifications.rb +17 -0
  77. data/lib/facebooker/models/page.rb +46 -0
  78. data/lib/facebooker/models/photo.rb +19 -0
  79. data/lib/facebooker/models/tag.rb +12 -0
  80. data/lib/facebooker/models/user.rb +751 -0
  81. data/lib/facebooker/models/video.rb +9 -0
  82. data/lib/facebooker/models/work_info.rb +10 -0
  83. data/lib/facebooker/parser.rb +970 -0
  84. data/lib/facebooker/rails/backwards_compatible_param_checks.rb +31 -0
  85. data/lib/facebooker/rails/controller.rb +353 -0
  86. data/lib/facebooker/rails/cucumber.rb +28 -0
  87. data/lib/facebooker/rails/cucumber/world.rb +40 -0
  88. data/lib/facebooker/rails/extensions/action_controller.rb +48 -0
  89. data/lib/facebooker/rails/extensions/rack_setup.rb +16 -0
  90. data/lib/facebooker/rails/extensions/routing.rb +15 -0
  91. data/lib/facebooker/rails/facebook_form_builder.rb +141 -0
  92. data/lib/facebooker/rails/facebook_pretty_errors.rb +22 -0
  93. data/lib/facebooker/rails/facebook_request_fix.rb +28 -0
  94. data/lib/facebooker/rails/facebook_request_fix_2-3.rb +31 -0
  95. data/lib/facebooker/rails/facebook_session_handling.rb +68 -0
  96. data/lib/facebooker/rails/facebook_url_helper.rb +192 -0
  97. data/lib/facebooker/rails/facebook_url_rewriting.rb +60 -0
  98. data/lib/facebooker/rails/helpers.rb +835 -0
  99. data/lib/facebooker/rails/helpers/fb_connect.rb +165 -0
  100. data/lib/facebooker/rails/helpers/stream_publish.rb +22 -0
  101. data/lib/facebooker/rails/integration_session.rb +38 -0
  102. data/lib/facebooker/rails/profile_publisher_extensions.rb +42 -0
  103. data/lib/facebooker/rails/publisher.rb +608 -0
  104. data/lib/facebooker/rails/routing.rb +49 -0
  105. data/lib/facebooker/rails/test_helpers.rb +68 -0
  106. data/lib/facebooker/rails/utilities.rb +22 -0
  107. data/lib/facebooker/server_cache.rb +24 -0
  108. data/lib/facebooker/service.rb +103 -0
  109. data/lib/facebooker/service/base_service.rb +19 -0
  110. data/lib/facebooker/service/curl_service.rb +44 -0
  111. data/lib/facebooker/service/net_http_service.rb +12 -0
  112. data/lib/facebooker/service/typhoeus_multi_service.rb +27 -0
  113. data/lib/facebooker/service/typhoeus_service.rb +17 -0
  114. data/lib/facebooker/session.rb +786 -0
  115. data/lib/facebooker/stream_post.rb +19 -0
  116. data/lib/facebooker/version.rb +9 -0
  117. data/lib/net/http_multipart_post.rb +123 -0
  118. data/lib/rack/facebook.rb +89 -0
  119. data/lib/rack/facebook_session.rb +21 -0
  120. data/lib/tasks/facebooker.rake +19 -0
  121. data/lib/tasks/facebooker.rb +2 -0
  122. data/lib/tasks/tunnel.rake +46 -0
  123. data/rails/init.rb +1 -0
  124. data/setup.rb +1585 -0
  125. data/templates/layout.erb +24 -0
  126. data/test/facebooker/adapters_test.rb +191 -0
  127. data/test/facebooker/admin_test.rb +102 -0
  128. data/test/facebooker/application_test.rb +110 -0
  129. data/test/facebooker/attachment_test.rb +72 -0
  130. data/test/facebooker/batch_request_test.rb +83 -0
  131. data/test/facebooker/data_test.rb +86 -0
  132. data/test/facebooker/logging_test.rb +43 -0
  133. data/test/facebooker/mobile_test.rb +45 -0
  134. data/test/facebooker/model_test.rb +133 -0
  135. data/test/facebooker/models/event_test.rb +15 -0
  136. data/test/facebooker/models/page_test.rb +56 -0
  137. data/test/facebooker/models/photo_test.rb +16 -0
  138. data/test/facebooker/models/user_test.rb +1074 -0
  139. data/test/facebooker/rails/facebook_request_fix_2-3_test.rb +25 -0
  140. data/test/facebooker/rails/facebook_url_rewriting_test.rb +76 -0
  141. data/test/facebooker/rails/integration_session_test.rb +13 -0
  142. data/test/facebooker/rails/publisher_test.rb +538 -0
  143. data/test/facebooker/rails_integration_test.rb +1543 -0
  144. data/test/facebooker/server_cache_test.rb +44 -0
  145. data/test/facebooker/service_test.rb +58 -0
  146. data/test/facebooker/session_test.rb +883 -0
  147. data/test/facebooker_test.rb +1263 -0
  148. data/test/fixtures/multipart_post_body_with_only_parameters.txt +33 -0
  149. data/test/fixtures/multipart_post_body_with_single_file.txt +38 -0
  150. data/test/fixtures/multipart_post_body_with_single_file_that_has_nil_key.txt +38 -0
  151. data/test/net/http_multipart_post_test.rb +52 -0
  152. data/test/rack/facebook_session_test.rb +34 -0
  153. data/test/rack/facebook_test.rb +73 -0
  154. data/test/rails_test_helper.rb +36 -0
  155. data/test/test_helper.rb +74 -0
  156. metadata +303 -0
@@ -0,0 +1,48 @@
1
+ module ::ActionController
2
+ class Base
3
+ def self.inherited_with_facebooker(subclass)
4
+ inherited_without_facebooker(subclass)
5
+ if subclass.to_s == "ApplicationController"
6
+ subclass.send(:include,Facebooker::Rails::Controller)
7
+ subclass.helper Facebooker::Rails::Helpers
8
+ end
9
+ end
10
+ class << self
11
+ alias_method_chain :inherited, :facebooker
12
+ end
13
+ end
14
+ end
15
+
16
+
17
+ # When making get requests, Facebook sends fb_sig parameters both in the query string
18
+ # and also in the post body. We want to ignore the query string ones because they are one
19
+ # request out of date
20
+ # We only do thise when there are POST parameters so that IFrame linkage still works
21
+ if Rails.version < '2.3'
22
+ class ActionController::AbstractRequest
23
+ def query_parameters_with_facebooker
24
+ if request_parameters.blank?
25
+ query_parameters_without_facebooker
26
+ else
27
+ (query_parameters_without_facebooker||{}).reject {|key,value| key.to_s =~ /^fb_sig/}
28
+ end
29
+ end
30
+
31
+ alias_method_chain :query_parameters, :facebooker
32
+ end
33
+ else
34
+ class ActionController::Request
35
+ def query_parameters_with_facebooker
36
+ if request_parameters.blank?
37
+ query_parameters_without_facebooker
38
+ else
39
+ (query_parameters_without_facebooker||{}).reject {|key,value| key.to_s =~ /^fb_sig/}
40
+ end
41
+ end
42
+
43
+ alias_method_chain :query_parameters, :facebooker
44
+ end
45
+ end
46
+
47
+ Mime::Type.register_alias "text/html", :fbml
48
+ Mime::Type.register_alias "text/javascript", :fbjs
@@ -0,0 +1,16 @@
1
+ # Somewhere in 2.3 RewindableInput was removed- rack supports it natively
2
+ require 'rack/facebook'
3
+ require 'rack/facebook_session'
4
+
5
+ ActionController::Dispatcher.middleware.insert_before(
6
+ ActionController::ParamsParser,
7
+ Rack::Facebook
8
+ )
9
+
10
+ #use this if you aren't using the cookie store and want to use
11
+ # the facebook session key for your session id
12
+ # ActionController::Dispatcher.middleware.insert_before(
13
+ # ActionController::Base.session_store,
14
+ # Rack::FacebookSession,
15
+ # ActionController::Base.session_options[:key]
16
+ # )
@@ -0,0 +1,15 @@
1
+ class ActionController::Routing::Route
2
+ def recognition_conditions_with_facebooker
3
+ defaults = recognition_conditions_without_facebooker
4
+ defaults << " env[:canvas] == conditions[:canvas] " if conditions[:canvas]
5
+ defaults
6
+ end
7
+ alias_method_chain :recognition_conditions, :facebooker
8
+ end
9
+
10
+ # We turn off route optimization to make named routes use our code for figuring out if they should go to the session
11
+ ActionController::Base::optimise_named_routes = false
12
+
13
+ # pull :canvas=> into env in routing to allow for conditions
14
+ ActionController::Routing::RouteSet.send :include, Facebooker::Rails::Routing::RouteSetExtensions
15
+ ActionController::Routing::RouteSet::Mapper.send :include, Facebooker::Rails::Routing::MapperExtensions
@@ -0,0 +1,141 @@
1
+ module Facebooker
2
+ module Rails
3
+ class FacebookFormBuilder < ActionView::Helpers::FormBuilder
4
+
5
+
6
+ second_param = %w(password_field file_field check_box date_select datetime_select time_select)
7
+ third_param = %w(radio_button country_select select time_zone_select)
8
+ fifth_param = %w(collection_select)
9
+
10
+ def self.create_with_offset(name,offset)
11
+ define_method name do |field,*args|
12
+ options = args[offset] || {}
13
+ build_shell(field,options.with_indifferent_access) do
14
+ super(field,*args)
15
+ end
16
+ end
17
+ end
18
+
19
+ second_param.each do |name|
20
+ create_with_offset(name,0)
21
+ end
22
+ third_param.each do |name|
23
+ create_with_offset(name,1)
24
+ end
25
+ fifth_param.each do |name|
26
+ create_with_offset(name,3)
27
+ end
28
+
29
+ def build_shell(field,options)
30
+ @template.content_tag "fb:editor-custom", :label=>label_for(field,options) do
31
+ yield
32
+ end
33
+ end
34
+
35
+ def label_for(field,options)
36
+ options[:label] || field.to_s.humanize
37
+ end
38
+
39
+ def text(string,options={})
40
+ @template.content_tag "fb:editor-custom",string, :label=>label_for("",options)
41
+ end
42
+
43
+
44
+ def text_field(method, options = {})
45
+ options = options.with_indifferent_access
46
+ options[:label] ||= label_for(method,options)
47
+ add_default_name_and_id(options,method)
48
+ options["value"] ||= value_before_type_cast(object,method)
49
+ @template.content_tag("fb:editor-text","",options)
50
+ end
51
+
52
+
53
+ def text_area(method, options = {})
54
+ options[:label] ||= label_for(method,options)
55
+ add_default_name_and_id(options,method)
56
+ @template.content_tag("fb:editor-textarea",value_before_type_cast(object,method),options)
57
+ end
58
+
59
+ #
60
+ # Build a text input area that uses typeahed
61
+ # options are like collection_select
62
+ def collection_typeahead(method,collection,value_method,text_method,options={})
63
+ build_shell(method,options) do
64
+ collection_typeahead_internal(method,collection,value_method,text_method,options)
65
+ end
66
+ end
67
+
68
+ def collection_typeahead_internal(method,collection,value_method,text_method,options={})
69
+ option_values = collection.map do |item|
70
+ value=item.send(value_method)
71
+ text=item.send(text_method)
72
+ @template.content_tag "fb:typeahead-option",text,:value=>value
73
+ end.join
74
+ add_default_name_and_id(options,method)
75
+ options["value"] ||= value_before_type_cast(object,method)
76
+ @template.content_tag("fb:typeahead-input",option_values,options)
77
+ end
78
+
79
+ def value_before_type_cast(object,method)
80
+ unless object.nil?
81
+ method_name = method.to_s
82
+ object.respond_to?(method_name + "_before_type_cast") ?
83
+ object.send(method_name + "_before_type_cast") :
84
+ object.send(method_name)
85
+ end
86
+ end
87
+
88
+ def multi_friend_input(options={})
89
+ build_shell(:friends,options) do
90
+ @template.content_tag("fb:multi-friend-input","",options)
91
+ end
92
+ end
93
+
94
+ def buttons(*names)
95
+ buttons=names.map do |name|
96
+ create_button(name)
97
+ end.join
98
+
99
+ @template.content_tag "fb:editor-buttonset",buttons
100
+ end
101
+
102
+ def create_button(name)
103
+ @template.content_tag("fb:editor-button","",:value=>name,:name=>"commit")
104
+ end
105
+
106
+ def add_default_name_and_id(options, method)
107
+ @method_name = method
108
+ if options.has_key?("index")
109
+ options["name"] ||= tag_name_with_index(options["index"])
110
+ options["id"] ||= tag_id_with_index(options["index"])
111
+ options.delete("index")
112
+ else
113
+ options["name"] ||= tag_name + (options.has_key?('multiple') ? '[]' : '')
114
+ options["id"] ||= "#{sanitized_object_name}_#{sanitized_method_name}"
115
+ end
116
+ end
117
+
118
+
119
+ private
120
+ def tag_name
121
+ "#{@object_name.to_s}[#{sanitized_method_name}]"
122
+ end
123
+
124
+ def tag_name_with_index(index)
125
+ "#{@object_name.to_s}[#{index}][#{sanitized_method_name}]"
126
+ end
127
+
128
+ def tag_id_with_index(index)
129
+ "#{sanitized_object_name}_#{index}_#{sanitized_method_name}"
130
+ end
131
+
132
+ def sanitized_object_name
133
+ @object_name.to_s.gsub(/\]\[|[^-a-zA-Z0-9:.]/, "_").sub(/_$/, "")
134
+ end
135
+
136
+ def sanitized_method_name
137
+ @method_name.to_s.sub(/\?$/,"")
138
+ end
139
+ end
140
+ end
141
+ end
@@ -0,0 +1,22 @@
1
+ class ActionController::Base
2
+ def rescues_path_with_facebooker(template_name)
3
+ t = "#{RAILS_ROOT}/vendor/plugins/facebooker/templates/#{template_name}.erb"
4
+ if pretty_facebook_errors? && File.exist?(t)
5
+ t
6
+ else
7
+ rescues_path_without_facebooker(template_name)
8
+ end
9
+ end
10
+ alias_method_chain :rescues_path, :facebooker
11
+
12
+ def response_code_for_rescue_with_facebooker(exception)
13
+ pretty_facebook_errors? ? 200 : response_code_for_rescue_without_facebooker(exception)
14
+ end
15
+ alias_method_chain :response_code_for_rescue, :facebooker
16
+
17
+
18
+ def pretty_facebook_errors?
19
+ Facebooker.facebooker_config['pretty_errors'] ||
20
+ (Facebooker.facebooker_config['pretty_errors'].nil? && RAILS_ENV=="development")
21
+ end
22
+ end
@@ -0,0 +1,28 @@
1
+ module ::ActionController
2
+ class AbstractRequest
3
+ include Facebooker::Rails::BackwardsCompatibleParamChecks
4
+
5
+ def request_method_with_facebooker
6
+ if parameters[:_method].blank?
7
+ if %w{GET HEAD}.include?(parameters[:fb_sig_request_method])
8
+ parameters[:_method] = parameters[:fb_sig_request_method]
9
+ end
10
+ end
11
+ request_method_without_facebooker
12
+ end
13
+
14
+ if new.methods.include?("request_method")
15
+ alias_method_chain :request_method, :facebooker
16
+ end
17
+
18
+ def xml_http_request_with_facebooker?
19
+ one_or_true(parameters["fb_sig_is_mockajax"]) ||
20
+ one_or_true(parameters["fb_sig_is_ajax"]) ||
21
+ xml_http_request_without_facebooker?
22
+ end
23
+ alias_method_chain :xml_http_request?, :facebooker
24
+ # we have to re-alias xhr? since it was pointing to the old method
25
+ alias :xhr? :xml_http_request?
26
+
27
+ end
28
+ end
@@ -0,0 +1,31 @@
1
+ module ::ActionController
2
+
3
+ class Request
4
+
5
+ include Facebooker::Rails::BackwardsCompatibleParamChecks
6
+
7
+ def request_method_with_facebooker
8
+ if parameters[:_method].blank?
9
+ if %w{GET HEAD}.include?(parameters[:fb_sig_request_method])
10
+ parameters[:_method] = parameters[:fb_sig_request_method]
11
+ end
12
+ end
13
+ request_method_without_facebooker
14
+ end
15
+
16
+ if new({}).methods.include?("request_method")
17
+ alias_method_chain :request_method, :facebooker
18
+ end
19
+
20
+ def xml_http_request_with_facebooker?
21
+ one_or_true(parameters["fb_sig_is_mockajax"]) ||
22
+ one_or_true(parameters["fb_sig_is_ajax"]) ||
23
+ xml_http_request_without_facebooker?
24
+ end
25
+ alias_method_chain :xml_http_request?, :facebooker
26
+
27
+ # we have to re-alias xhr? since it was pointing to the old method
28
+ alias :xhr? :xml_http_request?
29
+
30
+ end
31
+ end
@@ -0,0 +1,68 @@
1
+ module ActionController
2
+ class CgiRequest
3
+ alias :initialize_aliased_by_facebooker :initialize
4
+
5
+ def initialize(cgi, session_options = {})
6
+ initialize_aliased_by_facebooker(cgi, session_options)
7
+ @cgi.instance_variable_set("@request_params", request_parameters.merge(query_parameters))
8
+ end
9
+
10
+ DEFAULT_SESSION_OPTIONS[:cookie_only] = false
11
+ end
12
+ end
13
+
14
+ module ActionController
15
+ class RackRequest < AbstractRequest #:nodoc:
16
+ alias :initialize_aliased_by_facebooker :initialize
17
+
18
+ def initialize(cgi, session_options = {})
19
+ initialize_aliased_by_facebooker(cgi, session_options)
20
+ @cgi.instance_variable_set("@request_params", request_parameters.merge(query_parameters))
21
+ end
22
+ end
23
+ end
24
+
25
+ class CGI
26
+ class Session
27
+ alias :initialize_aliased_by_facebooker :initialize
28
+ attr_reader :request, :initialization_options
29
+
30
+ def initialize(request, option={})
31
+ @request = request
32
+ @initialization_options = option
33
+ option['session_id'] ||= set_session_id
34
+ initialize_aliased_by_facebooker(request, option)
35
+ end
36
+
37
+ def set_session_id
38
+ if session_key_should_be_set_with_facebook_session_key?
39
+ request_parameters[facebook_session_key]
40
+ else
41
+ request_parameters[session_key]
42
+ end
43
+ end
44
+
45
+ def request_parameters
46
+ request.instance_variable_get("@request_params")
47
+ end
48
+
49
+ def session_key_should_be_set_with_facebook_session_key?
50
+ request_parameters[session_key].blank? && !request_parameters[facebook_session_key].blank?
51
+ end
52
+
53
+ def session_key
54
+ initialization_options['session_key'] || '_session_id'
55
+ end
56
+
57
+ def facebook_session_key
58
+ 'fb_sig_session_key'
59
+ end
60
+
61
+ alias :create_new_id_aliased_by_facebooker :create_new_id
62
+
63
+ def create_new_id
64
+ @new_session = true
65
+ @session_id || create_new_id_aliased_by_facebooker
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,192 @@
1
+ # Extends the ActionView::Helpers::UrlHelper module. See it for details on
2
+ # the usual url helper methods: url_for, link_to, button_to, etc.
3
+ #
4
+ # Mostly, the changes sanitize javascript into facebook javascript.
5
+ # It sanitizes link_to solely by altering the private methods:
6
+ # convert_options_to_javascript!, confirm_javascript_function, and
7
+ # method_javascript_function. For button_to, it alters button_to
8
+ # itself, as well as confirm_javascript_function. No other methods
9
+ # need to be changed because of Facebook javascript.
10
+ #
11
+ # For button_to and link_to, adds alternate confirm options for facebook.
12
+ # ==== Options
13
+ # * <tt>:confirm => 'question?'</tt> - This will add a JavaScript confirm
14
+ # prompt with the question specified.
15
+ #
16
+ # Example:
17
+ # # Generates: <a href="http://rubyforge.org/projects/facebooker" onclick="
18
+ # # var dlg = new Dialog().showChoice('Please Confirm', 'Go to Facebooker?').setStyle();
19
+ # # var a=this;dlg.onconfirm = function() {
20
+ # # document.setLocation(a.getHref());
21
+ # # }; return false;">Facebooker</a>
22
+ # link_to("Facebooker", "http://rubyforge.org/projects/facebooker", :confirm=>"Go to Facebooker?")
23
+ #
24
+ # Alternatively, options[:confirm] may be specified.
25
+ # See the Facebook page http://wiki.developers.facebook.com/index.php/FBJS.
26
+ # These options are:
27
+ # <tt>:title</tt>:: Specifies the title of the Facebook dialog. Default is "Please Confirm".
28
+ # <tt>:content</tt>:: Specifies the title of the Facebook dialog. Default is "Are you sure?".
29
+ #
30
+ # Example:
31
+ # # Generates: <a href="http://rubyforge.org/projects/facebooker" onclick="
32
+ # # var dlg = new Dialog().showChoice('the page says:', 'Go to Facebooker?').setStyle();
33
+ # # var a=this;dlg.onconfirm = function() {
34
+ # # document.setLocation(a.getHref());
35
+ # # }; return false;">Facebooker</a>
36
+ # link_to("Facebooker", "http://rubyforge.org/projects/facebooker", :confirm=>{:title=>"the page says:", :content=>"Go to Facebooker?"})
37
+ #
38
+ # Any other options passed are assumed to be css styles.
39
+ # Again, see the Facebook page http://wiki.developers.facebook.com/index.php/FBJS.
40
+ #
41
+ # Example:
42
+ # # Generates: <a href="http://rubyforge.org/projects/facebooker" onclick="
43
+ # # var dlg = new Dialog().showChoice('the page says:', 'Are you sure?').setStyle({color: 'pink', width: '200px'});
44
+ # # var a=this;dlg.onconfirm = function() {
45
+ # # document.setLocation(a.getHref());
46
+ # # }; return false;">Facebooker</a>
47
+ # link_to("Facebooker", "http://rubyforge.org/projects/facebooker", :confirm=>{:title=>"the page says:, :color=>"pink", :width=>"200px"})
48
+ module ActionView
49
+ module Helpers
50
+ module UrlHelper
51
+ # Alters one and only one line of the Rails button_to. See below.
52
+ def button_to_with_facebooker(name, options={}, html_options = {})
53
+ if !respond_to?(:request_comes_from_facebook?) || !request_comes_from_facebook?
54
+ button_to_without_facebooker(name,options,html_options)
55
+ else
56
+ html_options = html_options.stringify_keys
57
+ convert_boolean_attributes!(html_options, %w( disabled ))
58
+
59
+ method_tag = ''
60
+ if (method = html_options.delete('method')) && %w{put delete}.include?(method.to_s)
61
+ method_tag = tag('input', :type => 'hidden', :name => '_method', :value => method.to_s)
62
+ end
63
+
64
+ form_method = method.to_s == 'get' ? 'get' : 'post'
65
+
66
+ request_token_tag = ''
67
+ if form_method == 'post' && protect_against_forgery?
68
+ request_token_tag = tag(:input, :type => "hidden", :name => request_forgery_protection_token.to_s, :value => form_authenticity_token)
69
+ end
70
+
71
+ if confirm = html_options.delete("confirm")
72
+ # this line is the only change => html_options["onclick"] = "return #{confirm_javascript_function(confirm)}"
73
+ html_options["onclick"] = "#{confirm_javascript_function(confirm, 'a.getForm().submit();')}return false;"
74
+ end
75
+
76
+ url = options.is_a?(String) ? options : self.url_for(options)
77
+ name ||= url
78
+
79
+ html_options.merge!("type" => "submit", "value" => name)
80
+
81
+ "<form method=\"#{form_method}\" action=\"#{escape_once url}\" class=\"button-to\"><div>" +
82
+ method_tag + tag("input", html_options) + request_token_tag + "</div></form>"
83
+ end
84
+ end
85
+
86
+ alias_method_chain :button_to, :facebooker
87
+
88
+ private
89
+
90
+ # Altered to throw an error on :popup and sanitize the javascript
91
+ # for Facebook.
92
+ def convert_options_to_javascript_with_facebooker!(html_options, url ='')
93
+ if !respond_to?(:request_comes_from_facebook?) || !request_comes_from_facebook?
94
+ convert_options_to_javascript_without_facebooker!(html_options,url)
95
+ else
96
+ confirm, popup = html_options.delete("confirm"), html_options.delete("popup")
97
+
98
+ method, href = html_options.delete("method"), html_options['href']
99
+
100
+ html_options["onclick"] = case
101
+ when popup
102
+ raise ActionView::ActionViewError, "You can't use :popup"
103
+ when method # or maybe (confirm and method)
104
+ "#{method_javascript_function(method, url, href, confirm)}return false;"
105
+ when confirm # and only confirm
106
+ "#{confirm_javascript_function(confirm)}return false;"
107
+ else
108
+ html_options["onclick"]
109
+ end
110
+ end
111
+ end
112
+
113
+ alias_method_chain :convert_options_to_javascript!, :facebooker
114
+
115
+
116
+ # Overrides a private method that link_to calls via convert_options_to_javascript! and
117
+ # also, button_to calls directly. For Facebook, confirm can be a hash of options to
118
+ # stylize the Facebook dialog. Takes :title, :content, :style options. See
119
+ # the Facebook page http://wiki.developers.facebook.com/index.php/FBJS for valid
120
+ # style formats like "color: 'black', background: 'white'" or like "'color','black'".
121
+ #
122
+ # == Examples ==
123
+ #
124
+ # link_to("Facebooker", "http://rubyforge.org/projects/facebooker", :confirm=>"Go to Facebooker?")
125
+ # link_to("Facebooker", "http://rubyforge.org/projects/facebooker", :confirm=>{:title=>"the page says:, :content=>"Go to Facebooker?"})
126
+ # link_to("Facebooker", "http://rubyforge.org/projects/facebooker", :confirm=>{:title=>"the page says:, :content=>"Go to Facebooker?", :color=>"pink"})
127
+ def confirm_javascript_function_with_facebooker(confirm, fun = nil)
128
+ if !respond_to?(:request_comes_from_facebook?) || !request_comes_from_facebook?
129
+ confirm_javascript_function_without_facebooker(confirm)
130
+ else
131
+ if(confirm.is_a?(Hash))
132
+ confirm_options = confirm.stringify_keys
133
+ title = confirm_options.delete("title") || "Please Confirm"
134
+ content = confirm_options.delete("content") || "Are you sure?"
135
+ button_confirm = confirm_options.delete("button_confirm") || "Okay"
136
+ button_cancel = confirm_options.delete("button_cancel") || "Cancel"
137
+ style = confirm_options.empty? ? "" : convert_options_to_css(confirm_options)
138
+ else
139
+ title,content,style,button_confirm,button_cancel = 'Please Confirm', confirm, "", "Okay", "Cancel"
140
+ end
141
+ "var dlg = new Dialog().showChoice('#{escape_javascript(title.to_s)}','#{escape_javascript(content.to_s)}','#{escape_javascript(button_confirm.to_s)}','#{escape_javascript(button_cancel.to_s)}').setStyle(#{style});"+
142
+ "var a=this;dlg.onconfirm = function() { #{fun ? fun : 'document.setLocation(a.getHref());'} };"
143
+ end
144
+ end
145
+
146
+ alias_method_chain :confirm_javascript_function, :facebooker
147
+
148
+ def convert_options_to_css(options)
149
+ key_pair = options.shift
150
+ style = "{#{key_pair[0]}: '#{key_pair[1]}'"
151
+ for key in options.keys
152
+ style << ", #{key}: '#{options[key]}'"
153
+ end
154
+ style << "}"
155
+ end
156
+
157
+ # Dynamically creates a form for link_to with method. Calls confirm_javascript_function if and
158
+ # only if (confirm && method) for link_to
159
+ def method_javascript_function_with_facebooker(method, url = '', href = nil, confirm = nil)
160
+ if !respond_to?(:request_comes_from_facebook?) || !request_comes_from_facebook?
161
+ method_javascript_function_without_facebooker(method,url,href)
162
+ else
163
+ action = (href && url.size > 0) ? "'#{url}'" : 'a.getHref()'
164
+ submit_function =
165
+ "var f = document.createElement('form'); f.setStyle('display','none'); " +
166
+ "a.getParentNode().appendChild(f); f.setMethod('POST'); f.setAction(#{action});"
167
+
168
+ unless method == :post
169
+ submit_function << "var m = document.createElement('input'); m.setType('hidden'); "
170
+ submit_function << "m.setName('_method'); m.setValue('#{method}'); f.appendChild(m);"
171
+ end
172
+
173
+ if protect_against_forgery?
174
+ submit_function << "var s = document.createElement('input'); s.setType('hidden'); "
175
+ submit_function << "s.setName('#{request_forgery_protection_token}'); s.setValue('#{escape_javascript form_authenticity_token}'); f.appendChild(s);"
176
+ end
177
+ submit_function << "f.submit();"
178
+
179
+ if(confirm)
180
+ confirm_javascript_function(confirm, submit_function)
181
+ else
182
+ "var a=this;" + submit_function
183
+ end
184
+ end
185
+ end
186
+
187
+ alias_method_chain :method_javascript_function, :facebooker
188
+
189
+ end
190
+ end
191
+ end
192
+