Class: Spotlight::Page

Inherits:
ActiveRecord::Base
  • Object
show all
Extended by:
FriendlyId
Defined in:
app/models/spotlight/page.rb

Overview

Base page class. See AboutPage, FeaturePage, HomePage

Direct Known Subclasses

AboutPage, FeaturePage, HomePage

Constant Summary collapse

MAX_PAGES =
Spotlight::Engine.config.max_pages

Instance Method Summary collapse

Instance Method Details

#about_page?Boolean

Returns:

  • (Boolean)


105
106
107
# File 'app/models/spotlight/page.rb', line 105

def about_page?
  is_a? AboutPage
end

#clone_for_locale(locale) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'app/models/spotlight/page.rb', line 147

def clone_for_locale(locale)
  dup.tap do |np|
    np.locale = locale
    np.default_locale_page = self
    np.published = false
    np.slug = slug

    if !top_level_page? && (parent_translation = parent_page.translated_page_for(locale)).present?
      np.parent_page = parent_translation
    end

    child_pages.for_locale(locale).update(parent_page: np) if top_level_page? && respond_to?(:child_pages)
  end
end

#contentObject



59
60
61
# File 'app/models/spotlight/page.rb', line 59

def content
  @content ||= Spotlight::PageContent.for(self, :content)
end

#content=(content) ⇒ Object



67
68
69
70
71
72
73
74
# File 'app/models/spotlight/page.rb', line 67

def content=(content)
  if content.is_a? Array
    super content.to_json
  else
    super
  end
  content_changed!
end

#content?Boolean Also known as: has_content?

Returns:

  • (Boolean)


76
77
78
# File 'app/models/spotlight/page.rb', line 76

def content?
  self[:content].present? && content.present?
end

#content_changed!Object



55
56
57
# File 'app/models/spotlight/page.rb', line 55

def content_changed!
  @content = nil
end

#content_typeObject



63
64
65
# File 'app/models/spotlight/page.rb', line 63

def content_type
  self[:content_type] || Spotlight::Engine.config.default_page_content_type
end

#display_sidebar?Boolean

Returns:

  • (Boolean)


81
82
83
# File 'app/models/spotlight/page.rb', line 81

def display_sidebar?
  true
end

#feature_page?Boolean

Returns:

  • (Boolean)


101
102
103
# File 'app/models/spotlight/page.rb', line 101

def feature_page?
  is_a? FeaturePage
end


85
86
87
# File 'app/models/spotlight/page.rb', line 85

def featured_image
  nil
end

#home_page?Boolean

Returns:

  • (Boolean)


109
110
111
# File 'app/models/spotlight/page.rb', line 109

def home_page?
  is_a? HomePage
end

#lock!(user) ⇒ Object



131
132
133
# File 'app/models/spotlight/page.rb', line 131

def lock!(user)
  create_lock(by: user).tap(&:current_session!) unless lock.present?
end

#should_display_title?Boolean

Returns:

  • (Boolean)


127
128
129
# File 'app/models/spotlight/page.rb', line 127

def should_display_title?
  title.present?
end

#should_generate_new_friendly_id?Boolean

Returns:

  • (Boolean)


121
122
123
124
125
# File 'app/models/spotlight/page.rb', line 121

def should_generate_new_friendly_id?
  return false if new_record? && slug.present?

  super || (title_changed? && persisted?)
end

#thumbnail_image_urlObject



89
90
91
92
93
# File 'app/models/spotlight/page.rb', line 89

def thumbnail_image_url
  return unless thumbnail&.iiif_url

  thumbnail.iiif_url
end

#titleObject



49
50
51
52
53
# File 'app/models/spotlight/page.rb', line 49

def title
  return super if I18n.locale == I18n.default_locale

  translated_page_for(I18n.locale)&.title || super
end

#to_partial_pathObject

explicitly set the partial path so that we don’t have to duplicate view logic.



97
98
99
# File 'app/models/spotlight/page.rb', line 97

def to_partial_path
  'spotlight/pages/page'
end

#top_level_page?Boolean

Returns:

  • (Boolean)


113
114
115
# File 'app/models/spotlight/page.rb', line 113

def top_level_page?
  try(:parent_page).blank?
end

#top_level_page_or_selfObject



117
118
119
# File 'app/models/spotlight/page.rb', line 117

def top_level_page_or_self
  parent_page || self
end

#translated_page_for(locale) ⇒ Object



141
142
143
144
145
# File 'app/models/spotlight/page.rb', line 141

def translated_page_for(locale)
  return self if locale == self.locale

  translated_pages.for_locale(locale).first
end

#updated_after?(other_page) ⇒ Boolean

Returns:

  • (Boolean)


135
136
137
138
139
# File 'app/models/spotlight/page.rb', line 135

def updated_after?(other_page)
  return false unless other_page

  updated_at > other_page.updated_at
end