Sha256: 350fb9fafa475d12bbed897acd4bc966df2e57345c623cb4cb035b0eab7467ae

Contents?: true

Size: 1.2 KB

Versions: 9

Compression:

Stored size: 1.2 KB

Contents

class Page < ActiveRecord::Base
  include Content
  include Nestable
  
  validates_presence_of :uid, :name

  field :name
  field :uid
  field :scope
  index :uid, :unique => true
  
  field :path
  field :resource_type
  
  timestamps
  
  def home?
    path == "/"
  end

  def resourceful?
    resource_type.present?
  end
  
  def to_label
    name
  end

  def resource_class
    if self.resourceful?
      @resource_class ||= resource_type.to_s.camelize.constantize
    end
  end
  
  # eg.
  # # params => { :q => "abc", :a => 5, :slug => "first_post"} 
  # # page.path => "/blog/:slug"
  # post = page.fetch(params)
  # == Post.where(:slug => "first_post").first
  
  def fetch_resource(params = {})
    if self.resourceful?
      meaningful_params = params.stringify_keys.select {|k, v| 
        path_params.include?(k)
      }
      resource_class.where(meaningful_params).first
    end
  end
  
  def path_params
    self.path.scan(/:([A-z_][A-z0-9_]*)/).flatten
  end
  
  def resource_params(object)
    params = {}
    path_params.each { |param|
      params[param.to_sym] = object.send(param)
    }
    params
  end

  has_many :page_parts
  accepts_nested_attributes_for :page_parts, :allow_destroy => true

end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
fullstack-cms-0.2.3 app/models/page.rb
fullstack-cms-0.2.2 app/models/page.rb
fullstack-cms-0.2.1 app/models/page.rb
fullstack-cms-0.1.17 app/models/page.rb
fullstack-cms-0.1.15 app/models/page.rb
fullstack-cms-0.1.14 app/models/page.rb
fullstack-cms-0.1.13 app/models/page.rb
fullstack-cms-0.1.12 app/models/page.rb
fullstack-cms-0.1.11 app/models/page.rb