Sha256: b98f2cdbbfb00b0be9f11d360426cc51558f00be9566ae2d362ba50f1b275c61

Contents?: true

Size: 1.07 KB

Versions: 1

Compression:

Stored size: 1.07 KB

Contents

class SpudPhotoAlbum < ActiveRecord::Base
  has_many :spud_photo_albums_photos, dependent: :destroy
  has_many :photos,
           -> { order('spud_photo_albums_photos.sort_order asc') },
           through: :spud_photo_albums_photos,
           source: :spud_photo

  has_many :spud_photo_galleries_albums
  has_many :galleries,
           through: :spud_photo_galleries_albums,
           source: :spud_photo_gallery

  validates :title, :url_name, presence: true
  validates :title, :url_name, uniqueness: true
  before_validation :set_url_name
  after_save :update_photo_order

  scope :ordered, -> { order('created_at desc') }

  def top_photo_url(style)
    return photos.first.photo.url(style) unless photos.empty?
  end

  def photos_available
    if photo_ids.any?
      SpudPhoto.where('id not in (?)', photo_ids)
    else
      SpudPhoto.all
    end
  end

  private

  def set_url_name
    self.url_name = title.parameterize
  end

  def update_photo_order
    # order = 0
    # self.photos.each do |p|
    #   p.update_attribute(:order, order)
    #   order += 1
    # end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
tb_photos-1.2.0 app/models/spud_photo_album.rb