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

#content_typeObject

Return the content type specified by the target_klass_name property.

Returns:

  • (Object)

    The content type or nil if not found



36
37
38
39
40
41
42
# File 'app/models/locomotive/extensions/page/templatized.rb', line 36

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

#fetch_target_entries(conditions = {}) ⇒ Object

Find all the ordered entries of the target klass filtered or not by the conditions passed in parameter.

Parameters:

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

    The conditions used to filter the entries (optional)

Returns:

  • (Object)

    The documents



118
119
120
121
122
123
124
# File 'app/models/locomotive/extensions/page/templatized.rb', line 118

def fetch_target_entries(conditions = {})
  if self.content_type
    self.content_type.ordered_entries(conditions)
  else
    []
  end
end

#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



107
108
109
# File 'app/models/locomotive/extensions/page/templatized.rb', line 107

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::ContentEntry12345' # 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



93
94
95
96
97
98
99
# File 'app/models/locomotive/extensions/page/templatized.rb', line 93

def target_entry_name
  if self.content_type
    self.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::ContentEntry12345'
page.target_klass # <Locomotive::ContentEntry12345...>

Returns:

  • (Class)

    The target class



53
54
55
# File 'app/models/locomotive/extensions/page/templatized.rb', line 53

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 matching a content type



62
63
64
# File 'app/models/locomotive/extensions/page/templatized.rb', line 62

def target_klass_slug
  self.content_type.try(:slug)
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



72
73
74
75
76
77
# File 'app/models/locomotive/extensions/page/templatized.rb', line 72

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