class Page < ActiveRecord::Base extend FriendlyId friendly_id :title, use: :slugged before_save :set_template_path after_create :create_template_file after_update :move_template_file after_destroy :remove_template_file validates :title, :slug, uniqueness: true, presence: true has_many :content_blocks private def set_template_path @old_template_path = attribute_was(:template_path) self.template_path = "#{Rails.root}/app/views/pages/#{self.slug}.html.erb" end def create_template_file %x(touch #{self.template_path}) end def move_template_file %x(mv #{@old_template_path} #{self.template_path}) end def remove_template_file %x(rm #{self.template_path}) end end