Class: Skyline::Article Abstract

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/skyline/article.rb

Overview

This class is abstract.

Subclass and implement the Article interface

Articles are container objects that contain Sections, have history and can (optional) be previewed and published.

Direct Known Subclasses

Page, PageFragment

Defined Under Namespace

Classes: Data

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#becomes_default_variant_after_saveObject

Stores a variant that will become the default variant AFTER save



78
79
80
# File 'app/models/skyline/article.rb', line 78

def becomes_default_variant_after_save
  @becomes_default_variant_after_save
end

Class Method Details

.can_have_multiple_variants?true, false

This method is abstract.

Implement in subclass if needed, true is the default

Can this type of article have multiple variants?

Returns:

  • (true, false)


114
115
116
# File 'app/models/skyline/article.rb', line 114

def can_have_multiple_variants?
  true
end

.lockable?true, false

This method is abstract.

Implement in subclass if needed, true is the default

Can this type of article be locked?

Returns:

  • (true, false)

    Wether or not this article type can be locked



106
107
108
# File 'app/models/skyline/article.rb', line 106

def lockable?
  true
end

.publishable?true, false

This method is abstract.

Implement in subclass if needed, true is the default.

Is this type of article publishable?

Returns:

  • (true, false)

    Wether or not this article type can be published



98
99
100
# File 'app/models/skyline/article.rb', line 98

def publishable?
  true
end

.right_prefixString

This method is abstract.

Implement the value correct value in your subclass, defaults to ‘article’

The prefix to use when determining rights. User#allow? uses this method when called with 2 parameters.

Returns:

  • (String)

    The string to prefix to the right (_create, _update, _delete)



90
91
92
# File 'app/models/skyline/article.rb', line 90

def right_prefix
  "article"      
end

.to_paramObject



81
82
83
# File 'app/models/skyline/article.rb', line 81

def to_param
  self.name.underscore
end

Instance Method Details

#can_have_multiple_variants?Boolean

Can this article instance have multiple variants?

Defaults to Skyline::Article.can_have_multiple_variants?

Returns:

  • (Boolean)

See Also:



203
204
205
# File 'app/models/skyline/article.rb', line 203

def can_have_multiple_variants?
  self.class.can_have_multiple_variants?
end

#data_classClass, false

The class that provides our custom data fields.

TODO : Dataleak waiting to happen (!!)

Returns:

  • (Class, false)

    False if we don’t have an inner Data class, the inner Data class if there is one.



271
272
273
274
275
276
277
# File 'app/models/skyline/article.rb', line 271

def data_class
  # Note: We can't use memoize here, because it freezes the class
  return @_data_class unless @_data_class.nil?
  @_data_class = (self.class.name + "::" + "Data").constantize
rescue NameError
  @_data_class = false
end

#depublishvoid

This method returns an undefined value.

Depublish an article, removes the published_publication if keep_history? is false

Raises:

  • (StandardError)

    If page is persistent and cannot be depulished



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'app/models/skyline/article.rb', line 133

def depublish
  raise StandardError, "can't be depublished because this page is persistent" if self.persistent?
  
  if self.published_publication
    self.run_callbacks :depublication do
      self.published_publication.destroy unless self.keep_history?
      self.published_publication = nil
    end
  end
  
  self.published_publication_data = nil
  self.url_part = "page-#{self.position}"
  
  self.save
end

#depublishable?true, false

Can this article be depublished?

Returns:

  • (true, false)


161
162
163
# File 'app/models/skyline/article.rb', line 161

def depublishable?
  !self.persistent?
end

#destroyfalse, true

Depublish this article and destroy it.

Returns:

  • (false, true)

    True sucessfully destroyed, otherwise false

See Also:

  • Skylien::Article#depublish


153
154
155
156
# File 'app/models/skyline/article.rb', line 153

def destroy
  depublish
  super
end

#destroyable?true, false

Can this article be destroyed? Only works if the article isn’t persisntent and does not have a publication (isn’t published).

Returns:

  • (true, false)


169
170
171
# File 'app/models/skyline/article.rb', line 169

def destroyable?
  !self.persistent? && self.published_publication == nil
end

#dupObject



216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'app/models/skyline/article.rb', line 216

def dup
  s = super.tap do |dup|
    dup.created_at = nil
    dup.updated_at = nil
    dup.default_variant_id = nil
    dup.default_variant_data_id = nil
    dup.default_variant = nil
    dup.default_variant_data = nil
    dup.published_publication_id = nil
    dup.published_publication_data_id = nil
    
    dup.publications.clear
    dup.versions.clear
    dup.variants.clear
        
    self.variants.each do |variant| 
      variant_dup = variant.dup
      dup.variants << variant_dup
      variant_dup.article = dup
      variant_dup.article_id = nil
      self.becomes_default_variant_after_save = variant_dup if variant == self.default_variant
    end
    dup
  end
end

#editable_by?(user) ⇒ true, false

Checks if the page can be edited by a certain user Currently only checks on page locks.

Parameters:

  • user (Skyline::User, Integer)

    The user or user id to check the access for.

Returns:

  • (true, false)

    True if the user can edit this page, false otherwise



248
249
250
251
# File 'app/models/skyline/article.rb', line 248

def editable_by?(user)
  user = user.kind_of?(Skyline::Configuration.user_class) ? user : Skyline::Configuration.user_class.find_by_identification(user)    
  self.locked? && user.allow?(:page_lock) || !self.locked?
end

#keep_history?true, false

Do we need to keep the history of this article?

If true, this article will have multiple publications, if false, there is only one published publication.

Returns:

  • (true, false)

    Default is false



212
213
214
# File 'app/models/skyline/article.rb', line 212

def keep_history?
  false
end

#lockable?Boolean

Can this article instance be locked?

Defaults to the Skyline::Article.lockable?

Returns:

  • (Boolean)

See Also:



194
195
196
# File 'app/models/skyline/article.rb', line 194

def lockable?
  self.class.lockable?
end

#preview_wrapper_pageSkyline::Page?

This method is abstract.

Implement this in a subclass to get the Page from Settings or from somewhere else.

A subclass can return a Page in which the article (ie: NewsItem) will be rendered for previewing

Returns:

  • (Skyline::Page, nil)

    The page to wrap this article in when previewing. Nil if no wrapping is needed.



292
293
294
# File 'app/models/skyline/article.rb', line 292

def preview_wrapper_page
  nil
end

#previewable?true, false

Can this article be previewed? Delegates to Skyline::Article#renderable?

Returns:

  • (true, false)

See Also:



185
186
187
# File 'app/models/skyline/article.rb', line 185

def previewable?
  self.renderable?
end

#published?true, false

Has this article been puslished?

Returns:

  • (true, false)

    True if it has a published_publication, meaning it’s currently published



123
124
125
126
127
# File 'app/models/skyline/article.rb', line 123

def published?
  # Don't use only "self.published_publication" here, it causes way too many lookups
  # If the next test is wrong, than maybe you should wonder why it is wrong? Foreign key left behind?
  self.published_publication_id.present?
end

#renderable?true, false

Can this article be rendered. This basically means wether or not there are any templates for this article.

Returns:

  • (true, false)


177
178
179
# File 'app/models/skyline/article.rb', line 177

def renderable?
  self.renderable_scope.renderer.templates_for(self).any?
end

#renderable_scopeObject

Multisite interface



307
308
309
# File 'app/models/skyline/article.rb', line 307

def renderable_scope
  Skyline::Rendering::Scopes::Wildcard.new
end

#right_prefixObject



280
281
282
# File 'app/models/skyline/article.rb', line 280

def right_prefix
  self.class.right_prefix
end

#set_default_variant(variant) ⇒ Object



260
261
262
263
264
# File 'app/models/skyline/article.rb', line 260

def set_default_variant(variant)
  return false if variant.id == self.default_variant_id && variant.data_id == self.default_variant_data_id
  self.attributes = {:default_variant_id => variant.id, :default_variant_data_id => variant.data_id}
  true
end

#set_default_variant!(variant) ⇒ Object



254
255
256
257
258
# File 'app/models/skyline/article.rb', line 254

def set_default_variant!(variant)
  if set_default_variant(variant)
    self.save(:validate => false)
  end
end

#siteObject

Multisite interface



302
303
304
# File 'app/models/skyline/article.rb', line 302

def site
  Skyline::Site.new
end

#sitesObject

Multisite interface



297
298
299
# File 'app/models/skyline/article.rb', line 297

def sites
  [Skyline::Site.new]
end

#titleObject



284
285
286
# File 'app/models/skyline/article.rb', line 284

def title
  self.id
end