Sha256: c353145791b14d4f8dcf36263bf4407b0df91168c74581049cfacf216309b221

Contents?: true

Size: 1.67 KB

Versions: 11

Compression:

Stored size: 1.67 KB

Contents

class BrickLayer::Route
  include Mongoid::Document
  include Mongoid::Ancestry
  has_ancestry
  
  field :current_parent_id, type: String
  field :slug,              type: String
  field :data_set_template, type: String
  field :full_path_cache,   type: String
  
  has_one :data_set, class_name: "BrickLayer::DataSet", autosave: true
  accepts_nested_attributes_for :data_set, :allow_destroy => true
  
  validates :slug, :uniqueness => { :scope => :current_parent_id }
  
  before_validation :set_parent
  before_save       :set_full_path_cache
  after_save        :update_routes
  
  # Grab or create root
  def self.root    
    root_exist = BrickLayer::Route.where(:slug => "").count > 0
    
    if root_exist      
      return BrickLayer::Route.where(:slug => "").first
    else
      return BrickLayer::Route.create(:slug => "")
    end
  end
  
  # Display the full path for a route
  def full_path
    self.ancestors.sort_by { |x| x.ancestry.to_s }.map { |x| x.slug }.join("/") + "/#{self.slug}"
  end
  
  # For parent purposes
  def to_s
    slug.blank? ? "home (root)" : slug
  end
    
  # Use this method to keep up with hiearchry for #full_path
  def set_parent
    unless parent_id.blank?
      self.current_parent_id = parent_id.to_s
    end
  end
  
  # Set the full path cache for easy lookup
  def set_full_path_cache
    unless self.slug.blank?
      self.full_path_cache = self.full_path
    else
      self.full_path_cache = ""
    end
  end
  
  # Change parent 
  def change_parent_to(parent_route)
    self.parent_id = parent_route.id
  end
  
  # Update Children Routes
  def update_routes
    unless self.children.blank?
      self.children.each { |r| r.save }
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
brick_layer-0.10.6 app/models/brick_layer/route.rb
brick_layer-0.10.5 app/models/brick_layer/route.rb
brick_layer-0.10.3 app/models/brick_layer/route.rb
brick_layer-0.10.2 app/models/brick_layer/route.rb
brick_layer-0.10.1 app/models/brick_layer/route.rb
brick_layer-0.10.0 app/models/brick_layer/route.rb
brick_layer-0.9.6 app/models/brick_layer/route.rb
brick_layer-0.9.4 app/models/brick_layer/route.rb
brick_layer-0.9.2 app/models/brick_layer/route.rb
brick_layer-0.9.1 app/models/brick_layer/route.rb
brick_layer-0.9.0 app/models/brick_layer/route.rb