Skip to content

Instantly share code, notes, and snippets.

@pcantrell
Created October 18, 2013 18:11
Show Gist options
  • Save pcantrell/7045692 to your computer and use it in GitHub Desktop.
Save pcantrell/7045692 to your computer and use it in GitHub Desktop.
class Recording < ActiveRecord::Base
has_many :artistic_contributions, dependent: :destroy, include: [:artistic_role, :artist]
has_many :artists, through: :artistic_contributions
has_many :editions, class_name: 'RecordingEdition', dependent: :destroy
has_many :blog_posts, foreign_key: :podcast_recording_id
validates :title, :base_file_name, presence: true
before_save :refresh_editions
acts_as_indexed :fields => [:title]
accepts_nested_attributes_for :artistic_contributions, allow_destroy: true
def composer_contributions
artistic_contributions.select do |c|
c.artistic_role.name_in_credits == 'composer'
end
end
def performer_contributions
artistic_contributions - composer_contributions
end
def edition(*flavors)
flavors.each do |flavor|
editions.each do |edition|
return edition if edition.flavor == flavor
end
end
nil
end
def refresh_editions
self.editions.destroy_all
RecordingEdition::FLAVORS.each do |flavor|
edition = RecordingEdition.new(recording: self)
edition.flavor = flavor
editions << edition if edition.exists?
end
if editions.empty?
errors.add 'base_file_name', "does not exist"
end
end
def primary_blog_post
@primary_blog_post ||= blog_posts.order(:created_at).last
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment