Sha256: d6b3e9581e3aac9576cbfd43d69b5cab66053fd459e9343c0a9fd6ced7b52523

Contents?: true

Size: 803 Bytes

Versions: 3

Compression:

Stored size: 803 Bytes

Contents

class Page < ActiveRecord::Base
  extend FriendlyId
  friendly_id :title, use: :slugged

  after_create :create_template
  after_update :rename_template
  after_destroy :remove_template

  validates :title, :slug, uniqueness: true
  validates :title, presence: true
  validates :slug, presence: true, on: :update

  has_many :content_blocks

  default_scope order('title')

  def template_path
    template_path_from_slug(slug)
  end

  private

  def create_template
    %x(touch #{self.template_path})
  end

  def rename_template
    old_template_path = template_path_from_slug(slug_was)
    %x(mv #{old_template_path} #{template_path})
  end

  def remove_template
    %x(rm #{template_path})
  end

  def template_path_from_slug(slug)
    "#{Rails.root}/app/views/pages/#{slug}.html.erb"
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
manifest-rails-0.1.2 app/models/page.rb
manifest-rails-0.1.1 app/models/page.rb
manifest-rails-0.1.0 app/models/page.rb