Class: K3cms::Blog::BlogPost

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/k3cms/blog/blog_post.rb

Instance Method Summary collapse

Instance Method Details

#custom_url?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'app/models/k3cms/blog/blog_post.rb', line 65

def custom_url?
  read_attribute(:url).present?
end

#normalize_friendly_id(text) ⇒ Object



49
50
51
52
# File 'app/models/k3cms/blog/blog_post.rb', line 49

def normalize_friendly_id(text)
  # This uses stringex
  text.to_url
end

#published?Boolean


Returns:

  • (Boolean)


70
71
72
# File 'app/models/k3cms/blog/blog_post.rb', line 70

def published?
  date and Time.zone.now >= date.beginning_of_day
end

#set_defaultsObject



26
27
28
29
30
31
32
33
34
35
36
# File 'app/models/k3cms/blog/blog_post.rb', line 26

def set_defaults
  # Copy summary to body
  default_summary = '<p>Summary description goes here</p>'
  self.body = self.attributes['summary']                   if self.attributes['body'].nil? && self.attributes['summary'].present? && self.attributes['summary'] != default_summary

  if new_record?
    self.title   = 'New Post'                             if self.attributes['title'].nil?
    self.summary = default_summary                        if self.attributes['summary'].nil?
    self.date = Date.tomorrow                             if self.attributes['date'].nil?
  end
end

#to_sObject



38
39
40
# File 'app/models/k3cms/blog/blog_post.rb', line 38

def to_s
  title
end

#urlObject



53
54
55
# File 'app/models/k3cms/blog/blog_post.rb', line 53

def url
  cached_slug
end

#url=(new) ⇒ Object



56
57
58
59
60
61
62
63
64
# File 'app/models/k3cms/blog/blog_post.rb', line 56

def url=(new)
  if new
    new = normalize_friendly_id(new)
  end
  # By checking if new != url, we solve the problem where a user might tab from the title field to the url field, and then when they tab out of the url field, it will try to save the current url as a custom url instead of realizing that this is still an automatic url from the title.
  if new != url # NOT read_attribute(:url) -- we want to compare to the cached_slug based on the title if that's what is currently being used for the slug
    write_attribute(:url, new)
  end
end