Class: OCms::Page

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/o_cms/page.rb

Constant Summary collapse

DRAFT_STATUS =
"draft"
PUBLISHED_STATUS =
"published"
SCHEDULED_STATUS =
"scheduled"
STATUSES =
[['Draft', DRAFT_STATUS],
['Publish', PUBLISHED_STATUS],
['Schedule', SCHEDULED_STATUS]]

Instance Method Summary collapse

Instance Method Details

#assign_attributes(new_attributes) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'app/models/o_cms/page.rb', line 69

def assign_attributes(new_attributes)
  if new_attributes[:status]
    status = new_attributes[:status]
    published_at = new_attributes[:published_at]

    if DRAFT_STATUS == status
      published_at = nil
    elsif PUBLISHED_STATUS == status && published_at.blank?
      published_at = Time.current
    end

    new_attributes.delete(:status)
    new_attributes[:published_at] = published_at
   end

  super
end

#create_meta_descriptionObject



42
43
44
45
# File 'app/models/o_cms/page.rb', line 42

def create_meta_description
  sanitized_body = ActionView::Base.full_sanitizer.sanitize(self.body)
  self.meta_description = "#{sanitized_body.first(156) + ' ...'}" if self.meta_description.blank? && self.body.present?
end

#create_meta_titleObject



38
39
40
# File 'app/models/o_cms/page.rb', line 38

def create_meta_title
  self.meta_title = "#{title}" if self.meta_title.blank? && self.title.present?
end

#create_slugObject



34
35
36
# File 'app/models/o_cms/page.rb', line 34

def create_slug
  self.slug = "#{title.parameterize}" if self.slug.blank? && self.title.present?
end

#draft?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'app/models/o_cms/page.rb', line 47

def draft?
  self.published_at.nil?
end

#published?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'app/models/o_cms/page.rb', line 51

def published?
  self.published_at.nil? ? false : self.published_at <= Time.current
end

#remove_subpage_relationshipsObject



87
88
89
# File 'app/models/o_cms/page.rb', line 87

def remove_subpage_relationships
  self.subpages.clear
end

#scheduled?Boolean

Returns:

  • (Boolean)


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

def scheduled?
  self.published_at.nil? ? false : self.published_at > Time.current
end

#statusObject



59
60
61
62
63
64
65
66
67
# File 'app/models/o_cms/page.rb', line 59

def status
  if self.draft?
    return DRAFT_STATUS
  elsif self.published?
    return PUBLISHED_STATUS
  elsif self.scheduled?
    return SCHEDULED_STATUS
  end
end