Skip to content

Instantly share code, notes, and snippets.

@CodeOfficer
Created January 19, 2009 20:09
Show Gist options
  • Save CodeOfficer/49149 to your computer and use it in GitHub Desktop.
Save CodeOfficer/49149 to your computer and use it in GitHub Desktop.
class Page < ActiveRecord::Base
acts_as_taggable_on :tags
has_many :containers, :as => :containable, :dependent => :destroy, :order => "position"
has_many :comments, :as => :commentable, :dependent => :destroy
before_validation :generate_slug
before_validation :set_dates
validates_presence_of :title
validates_uniqueness_of :slug
attr_accessor :order_of_containers
class << self
def find_for_index
find(:all, :order => 'published_at DESC')
end
def find_by_permalink(slug, options = {})
page = find_by_slug(slug, options) || raise(ActiveRecord::RecordNotFound)
end
end
def set_dates
self.published_at = Time.now
end
def is_showable_by?(current_user)
return true if published?
return true if current_user && current_user.has_role?('admin')
return false
end
def is_createable_by?(current_user)
return true if current_user && current_user.has_role?('admin')
return false
end
def is_editable_by?(current_user)
return true if current_user && current_user.has_role?('admin')
return false
end
def is_destroyable_by?(current_user)
return true if current_user && current_user.has_role?('admin')
return false
end
def generate_slug
self.slug = self.title.dup # if self.slug.blank?
self.slug.slugorize!
end
def published?
published_at <= Time.now
end
def publish!
published_at = Time.now
save
reload
end
# TODO how to account for validations? as this is for js
def order_of_containers=(order_of_containers)
new_ordering = parse_order_of_containers(order_of_containers)
new_ordering.each_with_index do |container_id, index|
c = containers.find(container_id)
c.position = index + 1
c.save(false)
end
end
def parse_order_of_containers(order='')
order = order.delete('li_container_')
order = order.split(',')
order = order.collect(&:to_i)
order = order.select { |i| container_ids.include? i }
raise "OUCH" if order.blank?
order
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment