Module: Locomotive::Extensions::Page::Templatized

Extended by:
ActiveSupport::Concern
Included in:
Page
Defined in:
app/models/locomotive/extensions/page/templatized.rb

Instance Method Summary collapse

Instance Method Details

#fetch_target_entry(permalink) ⇒ Object

Find the entry both specified by the target klass and identified by the permalink

Parameters:

  • permalink (String)

    The permalink of the entry

Returns:

  • (Object)

    The document



101
102
103
# File 'app/models/locomotive/extensions/page/templatized.rb', line 101

def fetch_target_entry(permalink)
  target_klass.find_by_permalink(permalink)
end

#target_entry_nameString

Give the name which can be used in a liquid template in order to reference an entry. It uses the slug property if the target klass is a Locomotive content type or the class name itself for the other classes.

Examples:


page.target_klass_name = 'Locomotive::Entry12345' # related to the content type Articles
page.target_entry_name = 'article'

page.target_klass_name = 'OurProduct'
page.target_entry_name = 'our_product'

Returns:

  • (String)

    The name in lowercase and underscored



86
87
88
89
90
91
92
93
# File 'app/models/locomotive/extensions/page/templatized.rb', line 86

def target_entry_name
  if self.target_klass_name =~ /^Locomotive::Entry([a-z0-9]+)$/
    @content_type ||= self.site.content_types.find($1)
    @content_type.slug.singularize
  else
    self.target_klass_name.underscore
  end
end

#target_klassClass

Return the class specified by the target_klass_name property

Examples:


page.target_klass_name = 'Locomotive::Entry12345'
page.target_klass # <Locomotive::Entry12345...>

Returns:

  • (Class)

    The target class



41
42
43
# File 'app/models/locomotive/extensions/page/templatized.rb', line 41

def target_klass
  target_klass_name.constantize
end

#target_klass_slugString

Return the slug related to the target_klass. In other words, it returns the slug of the target content type.

Returns:

  • (String)

    The slug of the target class / content type. Nil if no target klass.



50
51
52
53
54
55
56
57
# File 'app/models/locomotive/extensions/page/templatized.rb', line 50

def target_klass_slug
  if self.target_klass_name =~ /^Locomotive::Entry([a-z0-9]+)$/
    @content_type ||= self.site.content_types.find($1)
    @content_type.slug
  else
    nil
  end
end

#target_klass_slug=(slug) ⇒ Object

Set the target klass from the slug of a content type

Parameters:

  • slug (String)

    The slug of the content type

Returns:

  • (Object)

    The content type or nil if not found



65
66
67
68
69
70
# File 'app/models/locomotive/extensions/page/templatized.rb', line 65

def target_klass_slug=(slug)
  if @content_type = self.site.content_types.where(:slug => slug).first
    self.target_klass_name = @content_type.entries_class_name
  end
  @content_type
end