Sha256: 1bfde8ba9e2ac5fa279ba78ef9ff291b42610794344ea4f3c26b1763908a84c4

Contents?: true

Size: 1.49 KB

Versions: 5

Compression:

Stored size: 1.49 KB

Contents

class ContentType
  
  include Locomotive::Mongoid::Document
  
  ## fields ##
  field :name
  field :description
  field :slug
  field :order_by
  field :highlighted_field_name
  
  ## associations ##
  belongs_to_related :site
  embeds_many :contents, :class_name => 'ContentInstance'

  ## callbacks ##
  before_validate :normalize_slug
  before_save :set_default_values
  
  ## validations ##
  validates_presence_of :site, :name, :slug
  validates_uniqueness_of :slug, :scope => :site
  validates_size_of :content_custom_fields, :minimum => 1, :message => :array_too_short
  
  ## behaviours ##
  custom_fields_for :contents
  
  ## methods ##
  
  def ordered_contents(conditions = {})
    column = self.order_by.to_sym
    
    (if conditions.nil? || conditions.empty?
      self.contents
    else
      self.contents.where(conditions)
    end).sort { |a, b| (a.send(column) || 0) <=> (b.send(column) || 0) }
  end
  
  def sort_contents!(order)
    order.split(',').each_with_index do |id, position|
      self.contents.find(id)._position_in_list = position
    end
    self.save
  end
  
  def highlighted_field
    self.content_custom_fields.detect { |f| f._name == self.highlighted_field_name }
  end
  
  protected
  
  def set_default_values
    self.order_by ||= 'updated_at'
    self.highlighted_field_name ||= self.content_custom_fields.first._name
  end
  
  def normalize_slug
    self.slug = self.name.clone if self.slug.blank? && self.name.present?    
    self.slug.slugify! if self.slug.present?
  end
  
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
locomotive_cms-0.0.1.4 app/models/content_type.rb
locomotive_cms-0.0.1.3 app/models/content_type.rb
locomotive_cms-0.0.1.2 app/models/content_type.rb
locomotive_cms-0.0.1.1 app/models/content_type.rb
locomotive_cms-0.0.1 app/models/content_type.rb