Module: Cadmus::Page::ClassMethods

Defined in:
lib/cadmus/page.rb

Instance Method Summary collapse

Instance Method Details

#cadmus_page(options = {}) ⇒ Object

Sets up a model to behave as a Cadmus page. This will add the following behaviors:

  • A slug and slug generator field using HasSlug
  • A name field that determines the name of the page for administrative UI
  • An optional, polymorphic +parent+ field
  • A scope called +global+ that returns instances of this class that have no parent
  • A +liquid_template+ method that parses the value of this model's +content+ field as a Liquid template
  • Validators that ensure that this page has a name, that this page's slug is unique within the parent object, and that the slug isn't "pages" or "edit" (which are used for admin UI)

Parameters:

  • options (Hash) (defaults to: {})

    options to modify the default behavior

Options Hash (options):

  • :name_field (Object)

    the name of the field to be used as the page name. Defaults to +:name+.

  • :slug_field (Object)

    the name of the field to be used as the page slug. Defaults to +:slug+.

  • :slug_generator_field (Object)

    the name of the field to be used as the slug generator. Defaults to the value of +name_field+ if unspecified.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/cadmus/page.rb', line 28

def cadmus_page(options={})
  options[:slug_generator_field] = options[:name_field] unless options.has_key?(:slug_generator_field)
  has_slug(options)

  cattr_accessor :name_field
  self.name_field = (options.delete(:name_field) || :name).to_s

  belongs_to :parent, :polymorphic => true
          
  validates_presence_of name_field
  validates_uniqueness_of slug_field, :scope => [:parent_id, :parent_type]
  validates_exclusion_of slug_field, :in => %w(pages edit)
  
  scope :global, lambda { where(:parent_id => nil, :parent_type => nil) }
  
  class_eval do
    def liquid_template
      Liquid::Template.parse(content)
    end
  end
end