Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Rohit/Akriti Added code for selecting friends who work in same compan…

…y, patched current friend's code
  • Loading branch information...
commit efd87923114ee1347aec212787456b2a009899da 1 parent 4d2dfad
@rohitggarg rohitggarg authored
View
6 app/assets/javascripts/facebook_share_widget/facebook_share.js.coffee.erb
@@ -66,7 +66,11 @@ $ ->
load: =>
self = this
- @collection.fetch({ data: $.param({ link: $.parseJSON($('.template').text()).link }) }).complete ->
+ json_message = $.parseJSON($('.template').text())
+ params = { link: json_message.link }
+ if json_message.compId
+ params['compId'] = json_message.compId
+ @collection.fetch({ data: $.param(params) }).complete ->
self.render()
setMessageModel: (friend) =>
View
4 app/controllers/facebook_share_widget/facebook_controller.rb
@@ -14,7 +14,7 @@ def employers
def friends
begin
- render json: facebook_friends_for_link(params[:link]), status: :ok
+ render json: facebook_friends_for_link(params[:link], params[:compId] ? params[:compId].to_i : nil), status: :ok
rescue Exception => ex
log_exception_and_render_as_json(ex)
end
@@ -26,7 +26,7 @@ def share
post(params[:post])
share = FacebookShareWidget::Share.new(user_facebook_id: me.identifier, friend_facebook_id: params[:post][:facebook_id], url: params[:post][:link], message: params[:post][:message])
share.save!
-
+
render json: {}, status: :ok
rescue Exception => ex
log_exception_and_render_as_json(ex)
View
16 app/views/facebook_share_widget/facebook/employers.html.haml
@@ -1,7 +1,9 @@
-%table.employers
- - @employers.each do |employer|
- %tr
- %td{width: '25px'}
- = radio_button_tag 'employer', employer.identifier
- %td
- = label_tag "employer_#{employer.identifier}", employer.name
+= javascript_include_tag 'widget/employers'
+.employers
+ %table
+ - @employers.each do |employer|
+ %tr
+ %td{width: '25px'}
+ = radio_button_tag 'employer', employer.identifier
+ %td
+ = label_tag "employer_#{employer.identifier}", employer.name
View
6 app/views/facebook_share_widget/facebook/index.html.haml
@@ -1,4 +1,2 @@
-- content_for :head do
- = javascript_include_tag 'jquery'
-
-= facebook_share_widget_helper
+= javascript_include_tag 'facebook_share_widget/facebook_friend_view'
+= facebook_share_widget_helper template: {link: params[:link], name: params[:name], compId: params[:compId]}
@woodhull Owner

Did this change drop the include for jQuery? Does that break the widget?

It might be fine to assume that the parent application using the widget already has jQuery in place.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
View
32 lib/facebook_share_widget/facebook_helper.rb
@@ -19,31 +19,41 @@ def facebook_user(facebook_id)
end
def my_employers
- employers = []
- facebook_me.fetch.work.each do |work_object|
- employers << work_object.employer
+ 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
+ end
+ employers
end
@woodhull Owner

Aha! the cache was added! thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
- employers
end
- def facebook_friends
- Rails.cache.fetch("friends_for_#{self.facebook_access_token}", :expires_in => 1.hour) do
+ def facebook_friends(compId)
+ Rails.cache.fetch("friends_for_#{self.facebook_access_token}_at_#{compId}", :expires_in => 1.hour) do
friends = {}
- facebook_me.friends.each do|f|
- friends[f.identifier] = { id: f.identifier, name: f.name }
+ if(compId != nil)
+ FbGraph::Query.new('select uid, name, work.employer.id from user where uid in (select uid2 from friend where uid1=me())').
+ fetch(self.facebook_access_token).each do|f|
@woodhull Owner

app code style is to use capitalization for SQL/FQL reserved words like SELECT. This isn't followed everywhere, but it should be.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ if(f[:work].collect {|a| a[:employer]}.collect {|a| a[:id]}.include?(compId))
+ friends[f[:uid]] = { id: f[:uid], name: f[:name] }
@woodhull Owner

Shouldn't we be able to do this restriction in FQL rather than iterating over the array to see if there is a compId present?

This block of code seems a bit confusing in general. There is an if statement and two collect statements on a single line. I'm not really sure what is going on.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ end
+ end
+ else
+ facebook_me.friends.each do|f|
+ friends[f.identifier] = { id: f.identifier, name: f.name }
+ end
end
friends
end
end
- def facebook_friends_for_link(url)
- friends = append_shares_loaded(facebook_friends, url)
+ def facebook_friends_for_link(url, compId)
+ friends = append_shares_loaded(facebook_friends(compId), url)
friends.collect {|key, value| value }
end
def append_shares_loaded(friends, url)
me = facebook_me.fetch
- friends
shares = FacebookShareWidget::Share.all conditions: {url: url, user_facebook_id: "#{me.identifier}"}
shares.each do | share |
friends[share.friend_facebook_id] = friends[share.friend_facebook_id].merge({:status => 'shared'}) if friends[share.friend_facebook_id]
View
16 spec/controllers/facebook_controller_spec.rb
@@ -14,18 +14,28 @@
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/') { friends }
+ friends = [{ id: "1", name: "test"}]
+ controller.should_receive(:facebook_friends_for_link).with('http://google.com/', nil) { friends }
get :friends, link: 'http://google.com/'
response.should be_successful
response.body.should == friends.to_json
end
+
+ it "should return friend list" do
+ friends = [{ id: "1", name: "test"}]
+ controller.should_receive(:facebook_friends_for_link).with('http://google.com/', 1234) { friends }
+
+ get :friends, link: 'http://google.com/', compId: 1234
+
+ 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()).and_raise(error)
+ controller.should_receive(:facebook_friends_for_link).with(anything(), anything()).and_raise(error)
get :friends
View
15 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).and_return(raw_friends)
+ subject.should_receive(:facebook_friends).with(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/')
+ subject.facebook_friends_for_link('http://www.google.com/',123)
end
it "should append the status" do
@@ -91,7 +91,16 @@ def initialize
me.stub(:friends) { raw_friends }
subject.stub(:facebook_me) { me }
- subject.facebook_friends.should == {"1" => { id: "1", name: "name" }}
+ subject.facebook_friends(nil).should == {"1" => { id: "1", name: "name" }}
+ end
+ end
+
+ describe "#facebook_friends" do
+ it "should get facebook friend list if compId is specified" do
+ 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" }}
end
end
@woodhull

Did this change drop the include for jQuery? Does that break the widget?

It might be fine to assume that the parent application using the widget already has jQuery in place.

@woodhull

Aha! the cache was added! thanks.

@woodhull

app code style is to use capitalization for SQL/FQL reserved words like SELECT. This isn't followed everywhere, but it should be.

@woodhull

Shouldn't we be able to do this restriction in FQL rather than iterating over the array to see if there is a compId present?

This block of code seems a bit confusing in general. There is an if statement and two collect statements on a single line. I'm not really sure what is going on.

Please sign in to comment.
Something went wrong with that request. Please try again.