Module: Cms::Concerns::CanBeAddressable

Defined in:
lib/cms/concerns/can_be_addressable.rb

Instance Method Summary collapse

Instance Method Details

#addressable?Boolean

Returns Until is_addressable is called, this will always be false.

Returns:

  • (Boolean)

    Until is_addressable is called, this will always be false.



57
58
59
# File 'lib/cms/concerns/can_be_addressable.rb', line 57

def addressable?
  false
end

#is_addressable(options = {}) ⇒ Object

Adds Addressable behavior to a model. This allows models to be inserted into the sitemap, having parent sections. By default, this method is available to all ActiveRecord::Base classes.

Parameters:

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

    a customizable set of options

Options Hash (options):

  • :path (String)

    The base path where instances will be placed.

  • :no_dynamic_path (String)

    Set as true if the Record has a :path attribute managed as a column in the db. (Default: false)

  • :destroy_if (Symbol)

    Name of a custom method used to determine when this object should be destroyed. Rather than dependant: destroy to determine if the section node should be destroyed when this object is.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/cms/concerns/can_be_addressable.rb', line 12

def is_addressable(options={})
  has_one_options = {as: :node, inverse_of: :node, class_name: 'Cms::SectionNode', validate: true}
  unless options[:destroy_if]
    has_one_options[:dependent] = :destroy
  else
    before_destroy options[:destroy_if]
    after_destroy :destroy_node
  end

  has_one :section_node, has_one_options
  # For reasons that aren't clear, just using :autosave doesn't work.
  after_save do
    if section_node && section_node.changed?
      section_node.save
    end
  end

  after_validation do
    # Copy errors from association for slug
    if section_node && !section_node.valid?
      section_node.errors[:slug].each do |message|
        errors.add(:slug, message)
      end
    end
  end

  include Cms::Concerns::Addressable
  extend Cms::Concerns::Addressable::ClassMethods
  include Cms::Concerns::Addressable::NodeAccessors
  include Cms::Concerns::Addressable::MarkAsDirty
  extend Cms::Configuration::ConfigurableTemplate

  if options[:path]
    @path = options[:path]
    include GenericSitemapBehavior
  end

  @template = options[:template]

  unless options[:no_dynamic_path]
    include Addressable::DynamicPath
  end
end

#requires_slug?Boolean

Returns Some addressable content types don’t require a slug.

Returns:

  • (Boolean)

    Some addressable content types don’t require a slug.



62
63
64
# File 'lib/cms/concerns/can_be_addressable.rb', line 62

def requires_slug?
  false
end