Skip to content

Instantly share code, notes, and snippets.

@vinniefranco
Created January 25, 2011 23:18
Show Gist options
  • Save vinniefranco/795911 to your computer and use it in GitHub Desktop.
Save vinniefranco/795911 to your computer and use it in GitHub Desktop.
class PhotoGallery < ActiveRecord::Base
# Legacy work
self.inheritance_column = 'inheritance_type'
set_table_name :galleries
alias_attribute :created_at, :dcreate
belongs_to :client,
:conditions => { :table_name => :galleries,
:type => :photos }
# Poster image for gallery
has_one :image,
:conditions => { :table_name => :galleries },
:foreign_key => "table_id"
accepts_nested_attributes_for :image
# Child Galleries
has_many :galleries,
:class_name => "PhotoGallery",
:foreign_key => "parent_id"
# Parent Gallery
belongs_to :gallery,
:class_name => "PhotoGallery"
# Photos in gallery. They aren't the actual photos they require yet another join to images.
has_many :photos,
:conditions => { :is_deleted => false },
:foreign_key => "gallery_id"
scope :current_clients_root_galleries, lambda { |client| where(:client_id => client,
:type => 'photo',
:parent_id => nil)
.includes(:image)
.includes(:galleries)
.includes(:photos)
.order("dcreate DESC") }
scope :current_clients_galleries, lambda { |client| where(:client_id => client, :type => 'photo')
.includes(:image)
.includes(:galleries)
.order("title ASC") }
class << self
def find_client_gallery(id, client_id)
where(:id => id,:client_id => client_id).first
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment