Created
October 24, 2014 15:38
-
-
Save janlimpens/bdfccb76e7db4ea7a8a9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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