Skip to content

Instantly share code, notes, and snippets.

@janlimpens
Created October 24, 2014 15:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save janlimpens/bdfccb76e7db4ea7a8a9 to your computer and use it in GitHub Desktop.
Save janlimpens/bdfccb76e7db4ea7a8a9 to your computer and use it in GitHub Desktop.
class PortfolioItem
include Mongoid::Document
include Mongoid::Slug
include ComparableByIndex
include CanBePublished
embedded_in :portfolio, inverse_of: :portfolio_items
field :title
slug :title, history: true, scope: :portfolio
field :client, type: String
field :description
field :date, type: Date
embeds_many :images, cascade_callbacks: true
validates_presence_of :title
def ext_title
client.nil? ? title : "#{title} - #{client}"
end
def get_image_index(image)
if image.nil?
return -1
end
idx = images.sorted.published.entries.index { |x| x.id == image.id }
return idx || -1
end
def next_image(image)
index = get_image_index(image)
(index + 1) == images.published.count ? nil : images.sorted.published.entries[index + 1]
end
def previous_image(image)
index = get_image_index(image)
index == 0 ? nil : images.published.sorted.entries[index - 1]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment