diff --git a/db/manifest b/db/manifest index dcb427176..24b1ab8bf 100755 --- a/db/manifest +++ b/db/manifest @@ -107,4 +107,5 @@ recordings_all_discarded.sql recordings_via_admin_web.sql relax_band_model_varchar.sql add_piano.sql -feed.sql \ No newline at end of file +feed.sql +feed_use_recording.sql \ No newline at end of file diff --git a/db/up/feed_use_recording.sql b/db/up/feed_use_recording.sql new file mode 100644 index 000000000..598c7e495 --- /dev/null +++ b/db/up/feed_use_recording.sql @@ -0,0 +1,4 @@ +DELETE from feeds; + +ALTER TABLE feeds DROP COLUMN claimed_recording_id; +ALTER TABLE feeds ADD COLUMN recording_id VARCHAR(64) UNIQUE REFERENCES recordings(id) ON DELETE CASCADE; \ No newline at end of file diff --git a/ruby/lib/jam_ruby/models/claimed_recording.rb b/ruby/lib/jam_ruby/models/claimed_recording.rb index 7fce422b9..80446170f 100644 --- a/ruby/lib/jam_ruby/models/claimed_recording.rb +++ b/ruby/lib/jam_ruby/models/claimed_recording.rb @@ -9,7 +9,6 @@ module JamRuby 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", :inverse_of => :shareable, :foreign_key => 'shareable_id' - has_one :feed, :class_name => "JamRuby::Feed", :inverse_of => :claimed_recording, :foreign_key => 'claimed_recording_id', :dependent => :destroy validates :name, no_profanity: true, length: {minimum: 3, maximum: 64}, presence: true @@ -21,17 +20,11 @@ module JamRuby validates_uniqueness_of :user_id, :scope => :recording_id validate :user_belongs_to_recording - before_create :add_to_feed before_create :generate_share_token SHARE_TOKEN_LENGTH = 8 - def add_to_feed - feed = Feed.new - feed.claimed_recording = self - end - def user_belongs_to_recording if user && recording && !recording.users.exists?(user) errors.add(:user, ValidationMessages::NOT_PART_OF_RECORDING) diff --git a/ruby/lib/jam_ruby/models/feed.rb b/ruby/lib/jam_ruby/models/feed.rb index 0277d49eb..4909c2fe7 100644 --- a/ruby/lib/jam_ruby/models/feed.rb +++ b/ruby/lib/jam_ruby/models/feed.rb @@ -2,11 +2,11 @@ module JamRuby class Feed < ActiveRecord::Base - belongs_to :claimed_recording, class_name: "JamRuby::ClaimedRecording", inverse_of: :feed, foreign_key: 'claimed_recording_id' + belongs_to :recording, class_name: "JamRuby::Recording", inverse_of: :feed, foreign_key: 'recording_id' belongs_to :music_session_history, class_name: "JamRuby::MusicSessionHistory", inverse_of: :feed, foreign_key: 'music_session_id' def self.index(params = {}) - Feed.includes(:claimed_recording).includes(:music_session_history).order('created_at DESC').limit(20) + Feed.includes(:recording).includes(:music_session_history).order('created_at DESC').limit(20) end end end diff --git a/ruby/lib/jam_ruby/models/recording.rb b/ruby/lib/jam_ruby/models/recording.rb index 631431928..8efd4e73f 100644 --- a/ruby/lib/jam_ruby/models/recording.rb +++ b/ruby/lib/jam_ruby/models/recording.rb @@ -12,6 +12,7 @@ module JamRuby has_many :comments, :class_name => "JamRuby::RecordingComment", :foreign_key => "recording_id" has_many :likes, :class_name => "JamRuby::RecordingLiker", :foreign_key => "recording_id" has_many :plays, :class_name => "JamRuby::RecordingPlay", :foreign_key => "recording_id" + has_one :feed, :class_name => "JamRuby::Feed", :inverse_of => :recording, :foreign_key => 'recording_id', :dependent => :destroy belongs_to :owner, :class_name => "JamRuby::User", :inverse_of => :owned_recordings, :foreign_key => 'owner_id' belongs_to :band, :class_name => "JamRuby::Band", :inverse_of => :recordings @@ -26,6 +27,13 @@ module JamRuby validate :only_one_mix before_save :sanitize_active_admin + before_create :add_to_feed + + + def add_to_feed + feed = Feed.new + feed.recording = self + end def sanitize_active_admin self.owner_id = nil if self.owner_id == '' diff --git a/ruby/spec/factories.rb b/ruby/spec/factories.rb index 769248fcd..cd5b935ff 100644 --- a/ruby/spec/factories.rb +++ b/ruby/spec/factories.rb @@ -154,7 +154,7 @@ FactoryGirl.define do association :user, factory: :user before(:create) { |claimed_recording| - claimed_recording.recording = FactoryGirl.create(:recording_with_track, owner: claimed_recording.user) + claimed_recording.recording = FactoryGirl.create(:recording_with_track, owner: claimed_recording.user) unless claimed_recording.recording } end diff --git a/ruby/spec/jam_ruby/models/feed_spec.rb b/ruby/spec/jam_ruby/models/feed_spec.rb index 825a8a6c6..96d584e75 100644 --- a/ruby/spec/jam_ruby/models/feed_spec.rb +++ b/ruby/spec/jam_ruby/models/feed_spec.rb @@ -8,10 +8,24 @@ describe Feed do it "one claimed recording" do claimed_recording = FactoryGirl.create(:claimed_recording) + MusicSessionHistory.delete_all # the factory makes a music_session while making the recording/claimed_recording feeds = Feed.index() - feeds.length.should == 2 # the factory makes a music_session while making the recording/claimed_recording - feeds[0].music_session_history == claimed_recording.recording.music_session.music_session_history - feeds[1].claimed_recording == claimed_recording + feeds.length.should == 1 + feeds[0].recording == claimed_recording.recording + end + + it "two claimed recordings for the same recording should only return one" do + recording = FactoryGirl.create(:claimed_recording).recording + second_track = FactoryGirl.create(:recorded_track, recording: recording) + recording.recorded_tracks << second_track + FactoryGirl.create(:claimed_recording, recording: recording, user: second_track.user) + MusicSessionHistory.delete_all + + # verify the mess above only made one recording + Recording.count.should == 1 + + feeds = Feed.index() + feeds.length.should == 1 end it "one music session" do diff --git a/web/app/views/api_feeds/show.rabl b/web/app/views/api_feeds/show.rabl index 53c537b32..cc2f58239 100644 --- a/web/app/views/api_feeds/show.rabl +++ b/web/app/views/api_feeds/show.rabl @@ -20,61 +20,63 @@ glue :music_session_history do end end -glue :claimed_recording do +glue :recording do node :type do |i| - 'claimed_recording' + 'recording' end - attributes :id, :name, :description, :is_public, :is_downloadable, :genre_id + attributes :id, :band, :created_at, :duration, :comment_count, :like_count, :play_count - node :share_url do |claimed_recording| - unless claimed_recording.share_token.nil? - share_token_url(claimed_recording.share_token.token) - end - end + child(:band => :band) { + attributes :id, :name, :location, :photo_url + } - child(:recording => :recording) { - attributes :id, :created_at, :duration, :comment_count, :like_count, :play_count + child(:owner => :owner) { + attributes :id, :name, :location, :photo_url + } - child(:band => :band) { - attributes :id, :name, :location, :photo_url - } + child(:mixes => :mixes) { + attributes :id, :is_completed - child(:owner => :owner) { - attributes :id, :name, :location, :photo_url - } + node :mp3_url do |mix| + mix[:mp3_url] + end - child(:mixes => :mixes) { - attributes :id, :is_completed + node :ogg_url do |mix| + mix[:ogg_url] + end + } - node :mp3_url do |mix| - mix[:mp3_url] - end + child(:recorded_tracks => :recorded_tracks) { + attributes :id, :fully_uploaded, :client_track_id, :client_id, :instrument_id - node :ogg_url do |mix| - mix[:ogg_url] - end - } - - child(:recorded_tracks => :recorded_tracks) { - attributes :id, :fully_uploaded, :client_track_id, :client_id, :instrument_id - - node :url do |recorded_track| + node :url do |recorded_track| recorded_track[:url] - end + end - child(:user => :user) { - attributes :id, :first_name, :last_name, :name, :city, :state, :country, :location, :photo_url - } - } - - child(:comments => :comments) { - attributes :comment, :created_at - - child(:user => :creator) { - attributes :id, :first_name, :last_name, :photo_url - } + child(:user => :user) { + attributes :id, :first_name, :last_name, :city, :state, :country, :location, :photo_url } } + + child(:comments => :comments) { + attributes :comment, :created_at + + child(:user => :creator) { + attributes :id, :first_name, :last_name, :photo_url + } + } + + child(:claimed_recordings => :claimed_recordings) { + + attributes :id, :name, :description, :is_public, :is_downloadable, :genre_id + + node :share_url do |claimed_recording| + unless claimed_recording.share_token.nil? + share_token_url(claimed_recording.share_token.token) + end + end + } + end diff --git a/web/app/views/api_recordings/show.rabl b/web/app/views/api_recordings/show.rabl index 9da99c52a..79c73f610 100644 --- a/web/app/views/api_recordings/show.rabl +++ b/web/app/views/api_recordings/show.rabl @@ -2,6 +2,26 @@ object @recording attributes :id, :band, :created_at, :duration, :comment_count, :like_count, :play_count +child(:band => :band) { + attributes :id, :name, :location, :photo_url +} + +child(:owner => :owner) { + attributes :id, :name, :location, :photo_url +} + +child(:mixes => :mixes) { + attributes :id, :is_completed + + node :mp3_url do |mix| + mix[:mp3_url] + end + + node :ogg_url do |mix| + mix[:ogg_url] + end +} + child(:recorded_tracks => :recorded_tracks) { attributes :id, :fully_uploaded, :client_track_id, :client_id, :instrument_id @@ -10,6 +30,25 @@ child(:recorded_tracks => :recorded_tracks) { end child(:user => :user) { - attributes :id, :first_name, :last_name, :city, :state, :country, :photo_url + attributes :id, :first_name, :last_name, :city, :state, :country, :location, :photo_url } } + +child(:comments => :comments) { + attributes :comment, :created_at + + child(:user => :creator) { + attributes :id, :first_name, :last_name, :photo_url + } +} + +child(:claimed_recordings => :claimed_recordings) { + + attributes :id, :name, :description, :is_public, :is_downloadable, :genre_id + + node :share_url do |claimed_recording| + unless claimed_recording.share_token.nil? + share_token_url(claimed_recording.share_token.token) + end + end +}