Permalink
Browse files

Rohit refactored stuff to be more like widget, some hardcoding left t…

…o be removed
  • Loading branch information...
1 parent ecef489 commit f65abb099bcc8120029ae63f75d8bdce3527a642 @rohitggarg rohitggarg committed Mar 7, 2013
View
6 app/assets/javascripts/facebook_share_widget/facebook_friend.js.coffee
@@ -35,11 +35,11 @@ $ ->
data = $.extend({}, $.parseJSON(template))
data.facebook_id = @id
redirect_uri = window.location.protocol + '//' + window.location.host + '/widget/facebook/share-redirect?facebook_id='+data.facebook_id+'&link='+data.link
-
- window.open @url.replace('{app_id}', data.appId).replace('{to}', data.facebook_id).replace('{link}', encodeURIComponent(data.link)).replace('{redirect_uri}',encodeURIComponent(redirect_uri)), 'sharer', 'toolbar=0,status=0,width=1000,height=600'
-
+
+ FB.ui { method: 'feed', link: data.link, redirect_uri: redirect_uri, to: data.facebook_id }
friend.shared()
friend.success_callback(friend)
+ return
setMessageModel: (model) ->
@messageModel = model
View
9 app/assets/javascripts/facebook_share_widget/facebook_share.js.coffee.erb
@@ -52,12 +52,15 @@ $ ->
self = this
json_message = $.parseJSON($('.template').text())
params = { link: json_message.link }
- if json_message.compId
- params['compId'] = json_message.compId
+ if json_message.personal_dataId
+ params['personal_dataId'] = json_message.personal_dataId
+ if json_message.personal_data_type
+ params['personal_data_type'] = json_message.personal_data_type
@collection.fetch({ data: $.param(params) }).complete ->
+ FB.init({appId: json_message.appId, cookie: true })
self.render()
- render: =>
+ render: () =>
this.$el.html(HandlebarsTemplates['facebook_share_widget/templates/all_friends']())
filteredFriends = @collection.filterBy(@nameSearch.searchFilter())
@appendFriend friend for friend in filteredFriends
View
25 app/controllers/facebook_share_widget/facebook_controller.rb
@@ -1,25 +1,27 @@
class FacebookShareWidget::FacebookController < FacebookShareWidget::ApplicationController
include FacebookShareWidget::FacebookHelper
+ before_filter :sanitize_params, :except => [:share]
+
def index
end
- def employers
+ def personal_data
begin
- @employers = my_employers
- if @employers.empty?
- render :no_employers
+ @personal_data_results = my_personal_data @personal_data_type
+ if @personal_data_results.empty?
+ render :no_personal_data
end
rescue Exception => ex
render json: { message: "You are probably not logged in" }, status: :not_found
end
end
- def change_employer
+ def friends_personal_data
begin
- @employers = friends_employers
- if @employers.empty?
- render :no_friends_employers
+ @personal_data_results = fb_friends_personal_data @personal_data_type
+ if @personal_data_results.empty?
+ render :no_friends_personal_data
end
rescue Exception => ex
render json: { message: "You are probably not logged in" }, status: :not_found
@@ -28,7 +30,7 @@ def change_employer
def friends
begin
- render json: facebook_friends_for_link(params[:link], params[:compId] ? params[:compId].to_i : nil), status: :ok
+ render json: facebook_friends_for_link(params[:link], @personal_dataId, @personal_data_type), status: :ok
rescue Exception => ex
log_exception_and_render_as_json(ex)
end
@@ -48,6 +50,11 @@ def share
end
private
+
+ def sanitize_params
+ @personal_dataId = params[:personal_dataId] ? params[:personal_dataId].to_i : nil
+ @personal_data_type = params[:personal_data_type].present? ? params[:personal_data_type].gsub('$','.') : nil
+ end
def log_exception_and_render_as_json(ex)
Rails.logger.warn ex.message
View
9 app/views/facebook_share_widget/facebook/_employers_list.html.haml
@@ -1,9 +0,0 @@
-%table
- - @employers.each do |employer|
- - employer_id = friends_employers_list ? employer["id"] : employer.identifier
- - employer_name = friends_employers_list ? employer["name"] : employer.name
- %tr
- %td{width: '25px'}
- = radio_button_tag 'compId', employer_id
- %td
- = label_tag "compId_#{employer_id}", employer_name
View
9 app/views/facebook_share_widget/facebook/_personal_data_list.html.haml
@@ -0,0 +1,9 @@
+= hidden_field_tag 'personal_data_type', params[:personal_data_type]
+%table
+ - @personal_data_results.each do |personal_data|
+ - personal_data_id = personal_data["id"]
+ %tr
+ %td{width: '25px'}
+ = radio_button_tag 'personal_dataId', personal_data_id
+ %td
+ = label_tag "personal_dataId_#{personal_data_id}", personal_data["name"]
View
5 app/views/facebook_share_widget/facebook/change_employer.html.haml
@@ -1,5 +0,0 @@
-= render partial: 'employers_list', locals: { friends_employers_list: true }
-:coffeescript
- $ ->
- window.Cs.submit_employer();
- window.Cs.no_employers();
View
6 app/views/facebook_share_widget/facebook/employers.html.haml
@@ -1,6 +0,0 @@
-.employers
- = render partial: 'employers_list', locals: { friends_employers_list: false }
-:coffeescript
- $ ->
- window.Cs.submit_employer();
- window.Cs.employers();
View
4 app/views/facebook_share_widget/facebook/friends_personal_data.html.haml
@@ -0,0 +1,4 @@
+= render partial: 'personal_data_list'
+:coffeescript
+ $ ->
+ window.Cs.fb_share_widget.onload_friends_data()
View
3 app/views/facebook_share_widget/facebook/index.html.haml
@@ -1,2 +1,3 @@
= javascript_include_tag 'facebook_share_widget/facebook_friend_view'
-= facebook_share_widget_helper template: {link: params[:link], name: params[:name], compId: params[:compId], appId: params[:appId] }
+= javascript_include_tag "http://connect.facebook.net/#{I18n.locale}/all.js"
+= facebook_share_widget_helper template: {link: params[:link], name: params[:name], personal_dataId: params[:personal_dataId], appId: params[:appId], personal_data_type: params[:personal_data_type]}
View
5 app/views/facebook_share_widget/facebook/no_employers.html.haml
@@ -1,5 +0,0 @@
-.employers
- Oops! Looks like you have no employers listed in your Facebook profile
-:coffeescript
- $ ->
- window.Cs.employers();
View
4 app/views/facebook_share_widget/facebook/no_friends_employers.html.haml
@@ -1,4 +0,0 @@
-Oops! Looks like your friends have not listed any employers on their Facebook profile
-:coffeescript
- $ ->
- window.Cs.no_employers();
View
4 app/views/facebook_share_widget/facebook/no_friends_personal_data.html.haml
@@ -0,0 +1,4 @@
+= "Oops! Looks like your friends have not listed any #{params[:personal_data_type]} on their Facebook profile"
+:coffeescript
+ $ ->
+ window.Cs.fb_share_widget.onload_no_friends_data();
View
5 app/views/facebook_share_widget/facebook/no_personal_data.html.haml
@@ -0,0 +1,5 @@
+.personal_data
+ = "Oops! Looks like you have no #{params[:personal_data_type]} listed in your Facebook profile"
+:coffeescript
+ $ ->
+ window.Cs.fb_share_widget.onload_no_self_data();
View
5 app/views/facebook_share_widget/facebook/personal_data.html.haml
@@ -0,0 +1,5 @@
+.personal_data
+ = render partial: 'personal_data_list'
+:coffeescript
+ $ ->
+ window.Cs.fb_share_widget.onload_self_data()
View
4 config/routes.rb
@@ -1,7 +1,7 @@
FacebookShareWidget::Engine.routes.draw do
get 'facebook' => 'facebook#index'
get 'facebook/friends' => 'facebook#friends'
- get 'facebook/employers' => 'facebook#employers'
- get 'facebook/change_employer' => 'facebook#change_employer'
+ get 'facebook/:personal_data_type/data' => 'facebook#personal_data'
+ get 'facebook/:personal_data_type/friends_data' => 'facebook#friends_personal_data'
get 'facebook/share-redirect' => 'facebook#share'
end
View
49 lib/facebook_share_widget/facebook_helper.rb
@@ -18,34 +18,45 @@ def facebook_user(facebook_id)
FbGraph::User.new(facebook_id, access_token: self.facebook_access_token)
end
- def my_employers
- Rails.cache.fetch("employers_of_#{self.facebook_access_token}", :expires_in => 24.hours) do
- employers = []
- facebook_me.fetch.work.each do |work_object|
- employers << work_object.employer
+ def my_personal_data personal_data_type
+ Rails.cache.fetch("#{personal_data_type}_of_#{self.facebook_access_token}", :expires_in => 24.hours) do
+ data = []
+ FbGraph::Query.new("SELECT #{personal_data_type} FROM user WHERE uid = me()").
+ fetch(self.facebook_access_token).each do |data_object|
+ data += get_data_array_for(personal_data_type, data_object)
end
- employers
+ data
end
end
+
+ def get_data_array_for personal_data_type, data_object, first=false
+ tokens = personal_data_type.split('.').collect(&:to_sym)
+ iteratable_value = data_object[tokens.delete(tokens[0])]
+ iteratable_value = [iteratable_value[0]] if first
+ tokens.each do |token|
+ iteratable_value = iteratable_value.collect {|a| a[token]}
+ end
+ iteratable_value
+ end
- def friends_employers
- Rails.cache.fetch("friends_for_#{self.facebook_access_token}", :expires_in => 1.hour) do
- employers = Hash.new(0)
- FbGraph::Query.new('SELECT uid, name, work.employer FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1=me()) AND work.employer').
+ def fb_friends_personal_data personal_data_type
+ Rails.cache.fetch("friends_for_#{self.facebook_access_token}_of_#{personal_data_type}", :expires_in => 1.hour) do
+ data = Hash.new(0)
+ FbGraph::Query.new("SELECT uid, name, #{personal_data_type} FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1=me()) AND #{personal_data_type}").
fetch(self.facebook_access_token).each do|f|
- employers[f[:work][0][:employer]] += 1
+ data[get_data_array_for(personal_data_type, f, true)] += 1
end
- Hash[employers.sort {|a,b| b[1]<=>a[1]}].keys[0..4]
+ Hash[data.sort {|a,b| b[1]<=>a[1]}].keys[0..4]
end
end
- def facebook_friends(compId)
- Rails.cache.fetch("friends_for_#{self.facebook_access_token}_at_#{compId}", :expires_in => 1.hour) do
+ def facebook_friends(personal_dataId, personal_data_type)
+ Rails.cache.fetch("friends_for_#{self.facebook_access_token}_for_#{personal_data_type}_as_#{personal_dataId}", :expires_in => 1.hour) do
friends = {}
- if(compId != nil)
- FbGraph::Query.new('SELECT uid, name, work.employer.id FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1=me())').
+ if(personal_dataId != nil)
+ FbGraph::Query.new("SELECT uid, name, #{personal_data_type}.id FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1=me())").
fetch(self.facebook_access_token).each do|f|
- if(f[:work].collect {|a| a[:employer]}.collect {|a| a[:id]}.include?(compId))
+ if(get_data_array_for(personal_data_type, f).collect {|a| a[:id]}.include?(personal_dataId))
friends[f[:uid].to_s] = { id: f[:uid], name: f[:name] }
end
end
@@ -58,8 +69,8 @@ def facebook_friends(compId)
end
end
- def facebook_friends_for_link(url, compId)
- friends = append_shares_loaded(facebook_friends(compId), url)
+ def facebook_friends_for_link(url, personal_dataId, personal_data_type)
+ friends = append_shares_loaded(facebook_friends(personal_dataId, personal_data_type), url)
friends.collect {|key, value| value }
end
View
40 spec/controllers/facebook_controller_spec.rb
@@ -15,7 +15,7 @@
describe "#friends" do
it "should return friend list" do
friends = [{ id: "1", name: "test"}]
- controller.should_receive(:facebook_friends_for_link).with('http://google.com/', nil) { friends }
+ controller.should_receive(:facebook_friends_for_link).with('http://google.com/', nil, nil) { friends }
get :friends, link: 'http://google.com/'
@@ -25,17 +25,17 @@
it "should return friend list with CompId" do
friends = [{ id: "1", name: "test"}]
- controller.should_receive(:facebook_friends_for_link).with('http://google.com/', 1234) { friends }
+ controller.should_receive(:facebook_friends_for_link).with('http://google.com/', 1234, 'a.test') { friends }
- get :friends, link: 'http://google.com/', compId: 1234
+ get :friends, link: 'http://google.com/', personal_dataId: 1234, personal_data_type: 'a$test'
response.should be_successful
response.body.should == friends.to_json
end
it "should return error message on fail" do
error = Exception.new("some error")
- controller.should_receive(:facebook_friends_for_link).with(anything(), anything()).and_raise(error)
+ controller.should_receive(:facebook_friends_for_link).with(anything(), anything(), anything()).and_raise(error)
get :friends
@@ -46,33 +46,33 @@
describe "#employers" do
it "should return employers list" do
employers = [{ id: "1", name: "Thoughtworks" }]
- controller.should_receive(:my_employers) { employers }
+ controller.should_receive(:my_personal_data).with('work.employer') { employers }
- get :employers
+ get :personal_data, personal_data_type: 'work$employer'
response.should be_successful
- response.should render_template "facebook_share_widget/facebook/employers"
- assigns(:employers).to_json.should == employers.to_json
+ response.should render_template "facebook_share_widget/facebook/personal_data"
+ assigns(:personal_data_results).to_json.should == employers.to_json
end
it "should return error message on fail" do
error = Exception.new("some error")
- controller.should_receive(:my_employers).and_raise(error)
+ controller.should_receive(:my_personal_data).with('work.employer').and_raise(error)
- get :employers
+ get :personal_data, personal_data_type: 'work$employer'
response.should_not be_successful
response.body.should == { message: "You are probably not logged in" }.to_json
end
it "should return no employers message when no employers are listed" do
employers = []
- controller.should_receive(:my_employers) { employers }
+ controller.should_receive(:my_personal_data).with('work.employer') { employers }
- get :employers
+ get :personal_data, personal_data_type: 'work$employer'
response.should be_successful
- response.should render_template "facebook_share_widget/facebook/no_employers"
+ response.should render_template "facebook_share_widget/facebook/no_personal_data"
end
end
describe "#share" do
@@ -111,23 +111,23 @@
describe "#change employer" do
it "should return atmost top five employers list" do
employers = [{ id: "1", name: "Thoughtworks" }, {id: "2", name: "ControlShift"}]
- controller.should_receive(:friends_employers) { employers }
+ controller.should_receive(:fb_friends_personal_data).with('work.employer') { employers }
- get :change_employer
+ get :friends_personal_data, personal_data_type: 'work$employer'
response.should be_successful
- response.should render_template "facebook_share_widget/facebook/change_employer"
- assigns(:employers).to_json.should == employers.to_json
+ response.should render_template "facebook_share_widget/facebook/friends_personal_data"
+ assigns(:personal_data_results).to_json.should == employers.to_json
end
it "should return no employers message when no friends' employers are found" do
employers = []
- controller.should_receive(:friends_employers) { employers }
+ controller.should_receive(:fb_friends_personal_data).with('work.employer') { employers }
- get :change_employer
+ get :friends_personal_data, personal_data_type: 'work$employer'
response.should be_successful
- response.should render_template "facebook_share_widget/facebook/no_friends_employers"
+ response.should render_template "facebook_share_widget/facebook/no_friends_personal_data"
end
end
end
View
23 spec/lib/facebook_helpers_spec.rb
@@ -49,10 +49,10 @@ def initialize
it "should get the friends" do
raw_friends = [OpenStruct.new({ identifier: "1", name: "name" })]
- subject.should_receive(:facebook_friends).with(anything()).and_return(raw_friends)
+ subject.should_receive(:facebook_friends).with(anything(), anything()).and_return(raw_friends)
subject.should_receive(:append_shares_loaded).with(raw_friends, 'http://www.google.com/').and_return(raw_friends)
- subject.facebook_friends_for_link('http://www.google.com/',123)
+ subject.facebook_friends_for_link('http://www.google.com/',123,123)
end
it "should append the status" do
@@ -91,7 +91,7 @@ def initialize
me.stub(:friends) { raw_friends }
subject.stub(:facebook_me) { me }
- subject.facebook_friends(nil).should == {"1" => { id: "1", name: "name" }}
+ subject.facebook_friends(nil, nil).should == {"1" => { id: "1", name: "name" }}
end
end
@@ -100,19 +100,17 @@ def initialize
raw_friends = [{ uid: "1", name: "name" ,work: [{employer: {id: 123}}]}]
FbGraph::Query.any_instance.stub(:fetch).with(anything()).and_return(raw_friends)
subject.stub(:facebook_access_token).and_return('test')
- subject.facebook_friends(123).should == {"1" => { id: "1", name: "name" }}
+ subject.facebook_friends(123,"work.employer").should == {"1" => { id: "1", name: "name" }}
end
end
- describe "#my_employers" do
+ describe "#my_personal_data" do
it "should return back list of my employers" do
- raw_employers = [OpenStruct.new(employer: { identifier: "1", name: "name" }, position: {id: "123",
- name: "Senior Consultant"}, start_date: "2012-03")]
- fetch = mock
- fetch.stub(:work) { raw_employers }
- subject.stub(:facebook_me) { mock(fetch: fetch) }
+ raw_employers = [{ uid: "1", name: "name" , work: [{employer: {id: "123",
+ name: "Senior Consultant"}, start_date: "2012-03"}] }]
+ FbGraph::Query.any_instance.stub(:fetch).with(anything()).and_return(raw_employers)
- subject.my_employers.should == [{ identifier: "1", name: "name" }]
+ subject.my_personal_data('work.employer').should == [{ id: "123", name: "Senior Consultant" }]
end
end
@@ -128,8 +126,7 @@ def initialize
{ uid: "8", name: "name8" ,work: [{employer: {id: 66, name: "name66"}}]},
{ uid: "9", name: "name9" ,work: [{employer: {id: 66, name: "name66"}}]}]
FbGraph::Query.any_instance.stub(:fetch).with(anything()).and_return(raw_friends)
- subject.stub(:facebook_access_token).and_return('test')
- subject.friends_employers.size.should == 5
+ subject.fb_friends_personal_data('work.employer').size.should == 5
end
end

1 comment on commit f65abb0

@woodhull
ControlShift member

Neat, I like this commit.

Can you add a line in the README about how to use this personal_data functionality?

Please sign in to comment.