Permalink
Browse files

Actor#cheesecake_json

  • Loading branch information...
1 parent 5afdda4 commit b24c606c1d4a3b4b22f007078b06ccc8cea1b482 @atd atd committed Dec 14, 2011
Showing with 44 additions and 0 deletions.
  1. +1 −0 base/Gemfile
  2. +10 −0 base/app/models/actor.rb
  3. +13 −0 base/app/models/relation/custom.rb
  4. +20 −0 base/spec/models/actor_spec.rb
View
@@ -4,6 +4,7 @@ source "http://rubygems.org"
# use a local code of mailboxer or avatar_for_rails gems
#
# gem 'mailboxer', :path => '../mailboxer'
+# gem 'social_cheesecake', :path => '../../SocialCheesecake/social_cheesecake'
# gem 'avatars_for_rails', :path => '../avatars_for_rails'
gemspec
View
@@ -485,6 +485,16 @@ def to_param
slug
end
+ # JSON compatible with SocialCheesecake
+ def cheesecake_json
+ {
+ :sectors =>
+ relation_customs.map { |r|
+ r.to_cheesecake_hash
+ }
+ }.to_json
+ end
+
private
# After create callback
@@ -96,4 +96,17 @@ def stronger
def stronger_or_equal
path
end
+
+ # JSON compatible with SocialCheesecake
+ def to_cheesecake_hash(options = {})
+ { :name => name }.tap do |hash|
+ if options[:subsector]
+ hash[:actors] = ties.map(&:receiver_id).uniq
+ else
+ hash[:subsectors] = ( weaker.present? ?
+ weaker.map{ |w| w.to_cheesecake_hash(:subsector => true) } :
+ Array.wrap(to_cheesecake_hash(:subsector => true)) )
+ end
+ end
+ end
end
@@ -48,4 +48,24 @@
Actor.find_by_id(a.id).should be_nil
end
+
+ context "cheesecake" do
+ describe "one custom" do
+ before do
+ @user = Factory(:user)
+ end
+
+ it "should build json" do
+ hash = {
+ :sectors => @user.relation_customs.map do |r|
+ { :name => r.name,
+ :subsectors => [ { :name => r.name, :actors => [] } ]
+ }
+ end
+ }
+
+ @user.cheesecake_json.should eq(hash.to_json)
+ end
+ end
+ end
end

0 comments on commit b24c606

Please sign in to comment.