class Comic < ActiveRecord::Base acts_as_cute_admin :display_name => :number, :order_scope => :publish_on before_validation_on_create :assign_number validates_presence_of :number validates_uniqueness_of :number if Rails.env.production? || Rails.env.preproduction? has_attached_file :image, :storage => :s3, :s3_credentials => "#{Rails.root}/config/s3.yml", :s3_headers => { 'Expires' => 1.year.from_now.httpdate }, :url => ':s3_url', :path => "comics/:id/:basename_:style_:timestamp.:extension", :styles => { :web => "930x930>" } else has_attached_file :image, :path => ":rails_root/public/system/comics/:id/:basename_:style.:extension", :url => "/system/comics/:id/:basename_:style.:extension", :styles => { :web => "930x930>" } end named_scope :published, lambda { { :conditions => ["publish_on <= ?", Date.today] } } named_scope :find_latest, :order => "publish_on DESC", :limit => 10 named_scope :find_previous, lambda { |comic| { :conditions => ["publish_on < ?", comic.publish_on], :order => "publish_on DESC", :limit => 1 } } named_scope :find_next, lambda { |comic| { :conditions => ["publish_on > ?", comic.publish_on], :order => "publish_on ASC", :limit => 1 } } named_scope :ordered, :order => "publish_on" named_scope :reverse_ordered, :order => "publish_on DESC" def self.latest find_latest.published.first end def to_param number.to_s end def assign_number self.number = get_new_number unless number.present? && number > 0 end def get_new_number self.class.find_latest.any? ? self.class.find_latest.first.number + 1 : 1 end def previous self.class.find_previous(self).published.first end def next self.class.find_next(self).published.first end def name_or_date name.present? ? name : I18n.l(publish_on, :format => :cute_with_dayname) end end