diff --git a/ruby/lib/jam_ruby/models/claimed_recording.rb b/ruby/lib/jam_ruby/models/claimed_recording.rb index d85b2c727..9aab4acd7 100644 --- a/ruby/lib/jam_ruby/models/claimed_recording.rb +++ b/ruby/lib/jam_ruby/models/claimed_recording.rb @@ -13,7 +13,7 @@ module JamRuby belongs_to :genre, :class_name => "JamRuby::Genre" has_many :recorded_tracks, :through => :recording, :class_name => "JamRuby::RecordedTrack" has_many :playing_sessions, :class_name => "JamRuby::MusicSession" - has_one :share_token, :class_name => "JamRuby::ShareToken", :as => :shareable + has_one :share_token, :class_name => "JamRuby::ShareToken", :inverse_of => :shareable, :foreign_key => 'shareable_id' before_create :generate_share_token diff --git a/ruby/lib/jam_ruby/models/music_session_history.rb b/ruby/lib/jam_ruby/models/music_session_history.rb index 1e5a1344d..49e8bc420 100644 --- a/ruby/lib/jam_ruby/models/music_session_history.rb +++ b/ruby/lib/jam_ruby/models/music_session_history.rb @@ -22,7 +22,7 @@ module JamRuby has_many :music_session_user_histories, :class_name => "JamRuby::MusicSessionUserHistory", :foreign_key => "music_session_id" has_many :comments, :class_name => "JamRuby::MusicSessionComment", :foreign_key => "music_session_id" has_many :likes, :class_name => "JamRuby::MusicSessionLiker", :foreign_key => "music_session_id" - has_one :share_token, :class_name => "JamRuby::ShareToken", :as => :shareable + has_one :share_token, :class_name => "JamRuby::ShareToken", :inverse_of => :shareable, :foreign_key => 'shareable_id' before_create :generate_share_token @@ -160,7 +160,6 @@ module JamRuby private def generate_share_token - self.id = music_session.id # unify music_session.id and music_session_history.id token = loop do diff --git a/ruby/spec/jam_ruby/models/share_token_spec.rb b/ruby/spec/jam_ruby/models/share_token_spec.rb index bf232e1f8..60abe3d08 100644 --- a/ruby/spec/jam_ruby/models/share_token_spec.rb +++ b/ruby/spec/jam_ruby/models/share_token_spec.rb @@ -13,7 +13,9 @@ describe ShareToken do it "can reference a music session" do music_session.touch # should create a MSH, and a token, too ShareToken.count.should == 1 + music_session.music_session_history.share_token.should_not be_nil token = ShareToken.find_by_shareable_id!(music_session.id) + token.should == music_session.music_session_history.share_token token.shareable_id.should == music_session.id token.shareable_type.should == 'session' end @@ -21,7 +23,9 @@ describe ShareToken do it "can reference a claimed recording" do claimed_recording.touch # should create a share token ShareToken.count.should == 2 # one for MSH, one for recording + claimed_recording.share_token.should_not be_nil token = ShareToken.find_by_shareable_id!(claimed_recording.id) + claimed_recording.share_token.should == token token.shareable_type.should == 'recording' end diff --git a/web/app/helpers/share_token_helper.rb b/web/app/helpers/share_token_helper.rb new file mode 100644 index 000000000..6436a51fa --- /dev/null +++ b/web/app/helpers/share_token_helper.rb @@ -0,0 +1,4 @@ +module ShareTokenHelper + + +end \ No newline at end of file diff --git a/web/app/views/api_claimed_recordings/show.rabl b/web/app/views/api_claimed_recordings/show.rabl index d00a676a0..10964baa6 100644 --- a/web/app/views/api_claimed_recordings/show.rabl +++ b/web/app/views/api_claimed_recordings/show.rabl @@ -6,6 +6,10 @@ object @claimed_recording attributes :id, :name, :description, :is_public, :is_downloadable, :genre_id +node :share_url do|claimed_recording| + share_token_url(claimed_recording.share_token.token) +end + child(:recording => :recording) { attributes :id, :created_at, :duration, :comment_count, :like_count, :play_count diff --git a/web/app/views/api_music_sessions/show.rabl b/web/app/views/api_music_sessions/show.rabl index 3c49ffc1b..a4d7ff6f6 100644 --- a/web/app/views/api_music_sessions/show.rabl +++ b/web/app/views/api_music_sessions/show.rabl @@ -12,6 +12,10 @@ if :is_recording? end end +node :share_url do|music_session| + share_token_url(music_session.music_session_history.share_token.token) +end + child(:connections => :participants) { collection @music_sessions, :object_root => false attributes :ip_address, :client_id diff --git a/web/config/routes.rb b/web/config/routes.rb index 491b63c69..67dba69ef 100644 --- a/web/config/routes.rb +++ b/web/config/routes.rb @@ -65,7 +65,7 @@ SampleApp::Application.routes.draw do match '/listen_in', to: 'spikes#listen_in' # share tokens - match '/s/:id', to: 'share_tokens#shareable_resolver' + match '/s/:id', to: 'share_tokens#shareable_resolver', :as => 'share_token' # password reset match '/request_reset_password' => 'users#request_reset_password', :via => :get diff --git a/web/spec/controllers/share_tokens_controller_spec.rb b/web/spec/controllers/share_tokens_controller_spec.rb index 7be16d2a9..a6965464f 100644 --- a/web/spec/controllers/share_tokens_controller_spec.rb +++ b/web/spec/controllers/share_tokens_controller_spec.rb @@ -1,6 +1,6 @@ require 'spec_helper' -describe ApiSharedTokenController do +describe SharedTokenController do render_views let(:user) { FactoryGirl.create(:user) }