Sha256: 7e8e8ad4a5e3d912e4ab9cb174fab34184ff9dec4b706348d7664746d99c0162
Contents?: true
Size: 1.68 KB
Versions: 16
Compression:
Stored size: 1.68 KB
Contents
module TypeStation module Concerns module PathGenerator extend ActiveSupport::Concern included do field :title, type: String, default: 'Untitled' field :slug, type: String field :path, type: String field :old_paths, type: Array, default: [] # VALIDATIONS validates :slug, uniqueness: true, on: :update validates :path, presence: true, uniqueness: true, on: :update # CALLBACKS before_create :generate_slug before_destroy :destroy_children after_rearrange :rebuild_path # Rebuild self and children paths upon title change before_save :generate_slug, if: :title_changed? after_save :rebuild_child_paths, if: :has_children? end module ClassMethods def find_by_path(path) search_path = File.join('',path) self.or({path: search_path}, {:old_paths.in => [search_path]}).first end end private # Generates a slug based of the title give by the user def generate_slug self.slug = root? ? "" : title.parameterize rebuild_path end # Rebuild a path based of a ancestors slugs def rebuild_path store_old_path self.path = root? ? "/" : self.ancestors_and_self.collect(&:slug).join('/') end def store_old_path if self.path.present? self.old_paths << self.path self.old_paths.uniq end end def rebuild_child_paths self.children.each do |child| if child.respond_to?(:rebuild_path) child.send :rebuild_path child.save end end end end end end
Version data entries
16 entries across 16 versions & 1 rubygems