Class: Storytime::Post

Inherits:
ActiveRecord::Base
  • Object
show all
Extended by:
FriendlyId
Includes:
ActionView::Helpers::SanitizeHelper, Concerns::HasVersions, PostPartialInheritance, PostTags, ScopedToSite
Defined in:
app/models/storytime/post.rb

Direct Known Subclasses

BlogPost, Page

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Concerns::HasVersions

#activate_version, #create_version, #draft_content, #latest_version, #publish!, #published, #published=, #published?

Instance Attribute Details

#previewObject

Returns the value of attribute preview.



17
18
19
# File 'app/models/storytime/post.rb', line 17

def preview
  @preview
end

Class Method Details

.find_preview(id) ⇒ Object



37
38
39
40
41
42
43
44
45
46
# File 'app/models/storytime/post.rb', line 37

def find_preview(id)
  post = Post.friendly.find(id)

  if post.present?
    post.content = post.preview_content
    post.preview = true
  end

  post
end

.human_nameObject



33
34
35
# File 'app/models/storytime/post.rb', line 33

def human_name
  @human_name ||= type_name.humanize.split(" ").map(&:capitalize).join(" ")
end

.policy_classObject



29
30
31
# File 'app/models/storytime/post.rb', line 29

def policy_class
  Storytime::PostPolicy
end

.type_nameObject



48
49
50
# File 'app/models/storytime/post.rb', line 48

def type_name
  to_s.split("::").last.underscore
end

Instance Method Details

#author_nameObject



70
71
72
# File 'app/models/storytime/post.rb', line 70

def author_name
  user.storytime_name.blank? ? user.email : user.storytime_name
end

#human_nameObject



62
63
64
# File 'app/models/storytime/post.rb', line 62

def human_name
  self.class.human_name
end

#normalize_friendly_id(value) ⇒ Object



82
83
84
# File 'app/models/storytime/post.rb', line 82

def normalize_friendly_id(value)
  value.split("/").map(&:parameterize).join("/")
end

#post_ancestor_klassesObject



95
96
97
# File 'app/models/storytime/post.rb', line 95

def post_ancestor_klasses
  self.class.ancestors.grep(Class).split(ActiveRecord::Base).first
end

#preview_contentObject



58
59
60
# File 'app/models/storytime/post.rb', line 58

def preview_content
  autosave.present? ? autosave.content : latest_version.content
end

#sanitize_contentObject



91
92
93
# File 'app/models/storytime/post.rb', line 91

def sanitize_content
  self.draft_content = Storytime.post_sanitizer.call(self.draft_content) unless Storytime.post_sanitizer.blank?
end

#should_generate_new_friendly_id?Boolean

Returns:

  • (Boolean)


86
87
88
89
# File 'app/models/storytime/post.rb', line 86

def should_generate_new_friendly_id?
  current_slug = self.slug == "" ? nil : self.slug
  slug.blank? || slug_changed? || (current_slug.nil? && published_at_changed? && published_at_change.first.nil?)
end

#slug_candidatesObject



74
75
76
77
78
79
80
# File 'app/models/storytime/post.rb', line 74

def slug_candidates
  if slug.blank?
    [:title]
  elsif slug_changed? 
    [:slug]
  end
end

#to_partial_pathObject

END class << self



54
55
56
# File 'app/models/storytime/post.rb', line 54

def to_partial_path
  self.class._to_partial_path(site)
end

#type_nameObject



66
67
68
# File 'app/models/storytime/post.rb', line 66

def type_name
  self.class.type_name
end