Module: Alchemy::Page::PageNatures

Extended by:
ActiveSupport::Concern
Included in:
Alchemy::Page
Defined in:
app/models/alchemy/page/page_natures.rb

Instance Method Summary collapse

Instance Method Details

#cache_keyObject

Returns the key that’s taken for cache path.

Uses the published_at value that’s updated when the user publishes the page.

If the page is the current preview it uses the updated_at value as cache key.



108
109
110
111
112
113
114
# File 'app/models/alchemy/page/page_natures.rb', line 108

def cache_key
  if Page.current_preview == self
    "alchemy/pages/#{id}-#{updated_at}"
  else
    "alchemy/pages/#{id}-#{published_at}"
  end
end

#cache_page?Boolean

Returns true if the page cache control headers should be set.

Disable Alchemy’s page caching globally

# config/alchemy/config.yml
...
cache_pages: false

Disable caching on page layout level

# config/alchemy/page_layouts.yml
- name: contact
  cache: false

Note:

This only sets the cache control headers and skips rendering of the page body, if the cache is fresh. This does not disable the fragment caching in the views. So if you don’t want a page and it’s elements to be cached, then be sure to not use <% cache element %> in the views.

Returns:

  • (Boolean)


149
150
151
152
153
# File 'app/models/alchemy/page/page_natures.rb', line 149

def cache_page?
  return false unless caching_enabled?
  page_layout = PageLayout.get(self.page_layout)
  page_layout['cache'] != false && page_layout['searchresults'] != true
end

#contains_feed?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'app/models/alchemy/page/page_natures.rb', line 32

def contains_feed?
  definition["feed"]
end

#controller_and_actionObject



50
51
52
53
54
55
56
57
# File 'app/models/alchemy/page/page_natures.rb', line 50

def controller_and_action
  if has_controller?
    {
      controller: definition["controller"].gsub(/(^\b)/, "/#{$1}"),
      action: definition["action"]
    }
  end
end

#definitionObject

Returns the self#page_layout definition from config/alchemy/page_layouts.yml file.



79
80
81
82
83
84
85
86
87
# File 'app/models/alchemy/page/page_natures.rb', line 79

def definition
  return {} if systempage?
  definition = PageLayout.get(page_layout)
  if definition.nil?
    log_warning "Page definition for `#{page_layout}` not found. Please check `page_layouts.yml` file."
    return {}
  end
  definition
end

#expiration_timeObject



10
11
12
# File 'app/models/alchemy/page/page_natures.rb', line 10

def expiration_time
  public_until? ? public_until - Time.current : nil
end

#folded?(user_id) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
30
# File 'app/models/alchemy/page/page_natures.rb', line 27

def folded?(user_id)
  return unless Alchemy.user_class < ActiveRecord::Base
  folded_pages.where(user_id: user_id, folded: true).any?
end

#has_controller?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'app/models/alchemy/page/page_natures.rb', line 41

def has_controller?
  !PageLayout.get(page_layout).nil? && !PageLayout.get(page_layout)["controller"].blank?
end

#layout_display_nameObject

Returns translated name of the pages page_layout value. Page layout names are defined inside the config/alchemy/page_layouts.yml file. Translate the name in your config/locales language yml file.



92
93
94
# File 'app/models/alchemy/page/page_natures.rb', line 92

def layout_display_name
  Alchemy.t(page_layout, scope: 'page_layout_names')
end

#layout_partial_nameObject

Returns the name for the layout partial



98
99
100
# File 'app/models/alchemy/page/page_natures.rb', line 98

def layout_partial_name
  page_layout.parameterize.underscore
end

#locked?Boolean

True if page locked_at timestamp and locked_by id are set

Returns:

  • (Boolean)


46
47
48
# File 'app/models/alchemy/page/page_natures.rb', line 46

def locked?
  locked_by? && locked_at?
end

#public?Boolean

Returns:

  • (Boolean)


5
6
7
8
# File 'app/models/alchemy/page/page_natures.rb', line 5

def public?
  current_time = Time.current
  already_public_for?(current_time) && still_public_for?(current_time)
end

#published_atObject

We use the published_at value for the cache_key.

If no published_at value is set yet, i.e. because it was never published, we return the updated_at value.



121
122
123
# File 'app/models/alchemy/page/page_natures.rb', line 121

def published_at
  read_attribute(:published_at) || updated_at
end

#redirects_to_external?Boolean

Returns true or false if the pages definition for config/alchemy/page_layouts.yml contains redirects_to_external: true

Returns:

  • (Boolean)


37
38
39
# File 'app/models/alchemy/page/page_natures.rb', line 37

def redirects_to_external?
  !!definition["redirects_to_external"]
end

#rootpage?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'app/models/alchemy/page/page_natures.rb', line 18

def rootpage?
  !new_record? && parent_id.blank?
end

#statusObject

Returns a Hash describing the status of the Page.



61
62
63
64
65
66
67
68
# File 'app/models/alchemy/page/page_natures.rb', line 61

def status
  {
    public: public?,
    visible: visible?,
    locked: locked?,
    restricted: restricted?
  }
end

#status_title(status_type) ⇒ Object

Returns the translated status for given status type.

Parameters:

  • status_type (Symbol)


74
75
76
# File 'app/models/alchemy/page/page_natures.rb', line 74

def status_title(status_type)
  Alchemy.t(status[status_type].to_s, scope: "page_states.#{status_type}")
end

#systempage?Boolean

Returns:

  • (Boolean)


22
23
24
25
# File 'app/models/alchemy/page/page_natures.rb', line 22

def systempage?
  return true if Page.root.nil?
  rootpage? || (parent_id == Page.root.id && !language_root?)
end

#taggable?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'app/models/alchemy/page/page_natures.rb', line 14

def taggable?
  definition['taggable'] == true
end