Class: Caboose::Post

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/caboose/post.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.slug(str) ⇒ Object


44
45
46
# File 'app/models/caboose/post.rb', line 44

def self.slug(str)    
  return str.downcase.gsub(' ', '-').gsub(/[^\w-]/, '')        
end

.uri(post) ⇒ Object


92
93
94
95
96
97
98
99
100
# File 'app/models/caboose/post.rb', line 92

def self.uri(post)
  str = "/posts/#x{post.created_at.strftime('%Y/%m/%d')}/#{post.slug}"
  i = 2
  while Caboose::Post.where("site_id = ? and id <> ? and uri = ?", post.site_id, post.id, str).exists?
    str = "/posts/#{post.created_at.strftime('%Y/%m/%d')}/#{post.slug}-#{i}"
    i = i + 1
  end      
  return str
end

Instance Method Details

#blockObject


36
37
38
# File 'app/models/caboose/post.rb', line 36

def block
  Caboose::Block.where("post_id = ? and parent_id is null", self.id).first
end

#custom_field_value(key) ⇒ Object


118
119
120
121
122
123
124
125
126
# File 'app/models/caboose/post.rb', line 118

def custom_field_value(key)
  fv = Caboose::PostCustomFieldValue.where(:post_id => self.id, :key => key).first
  if fv.nil?
    f = Caboose::PostCustomField.where(:site_id => self.site_id, :key => key).first
    return nil if f.nil?
    fv = Caboose::PostCustomFieldValue.create(:post_id => self.id, :post_custom_field_id => f.id, :key => key, :value => f.default_value, :sort_order => f.sort_order)
  end
  return fv.value
end

#is_publishedObject


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

def is_published
  Caboose::Block.where(:post_id => self.id).where('status != ?','published').count == 0 if !self.id.nil?
end

#publishObject


52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'app/models/caboose/post.rb', line 52

def publish
  if !self.id.nil?
    Caboose::Block.where(:post_id => self.id).where('status = ? OR status = ?','edited','added').all.each do |b|
      if b.new_value == 'EMPTY'
        b.value = nil
      elsif !b.new_value.blank?
        b.value = b.new_value
      end
      if b.new_media_id == 0
        b.media_id = nil
      elsif !b.new_media_id.blank?
        b.media_id = b.new_media_id
      end
      b.sort_order = b.new_sort_order if !b.new_sort_order.blank?
      b.parent_id = b.new_parent_id if !b.new_parent_id.blank?
      b.status = 'published'
      b.new_value = nil
      b.new_media_id = nil
      b.new_sort_order = nil
      b.new_parent_id = nil
      b.save
    end
    deleted_blocks = Caboose::Block.where(:post_id => self.id, :status => 'deleted').pluck(:id)
    dids = deleted_blocks.blank? ? 0 : deleted_blocks
    Caboose::Block.where("id in (?) or parent_id in (?)",dids,dids).destroy_all
    Caboose::Block.where(:post_id => self.id, :status => nil).update_all(:status => 'published')
    self.status = 'published'
    self.save
  end
end

#revertObject


83
84
85
86
87
88
89
90
# File 'app/models/caboose/post.rb', line 83

def revert
  if !self.id.nil?
    Caboose::Block.where(:post_id => self.id).where(:status => 'added').destroy_all
    Caboose::Block.where(:post_id => self.id).update_all("status = 'published', new_value = null, new_media_id = null, new_sort_order = sort_order, new_parent_id = null")
    self.status = 'published'
    self.save
  end
end

#set_slug_and_uri(str) ⇒ Object


102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'app/models/caboose/post.rb', line 102

def set_slug_and_uri(str)
  d = self.created_at.strftime('%Y/%m/%d')
  s = Caboose::Post.slug(str)
  new_slug = "#{s}"
  new_uri = "/posts/#{d}/#{new_slug}"
  i = 2
  while Caboose::Post.where("site_id = ? and id <> ? and uri = ?", self.site_id, self.id, new_uri).exists?
    new_slug = "#{s}-#{i}"
    new_uri = "/posts/#{d}/#{new_slug}"      
    i = i + 1
  end
  self.slug = new_slug
  self.uri = new_uri
  self.save        
end

#top_level_blocksObject


40
41
42
# File 'app/models/caboose/post.rb', line 40

def top_level_blocks
  Caboose::Block.where("post_id = ? and parent_id is null", self.id).reorder(:sort_order).all
end

#verify_custom_field_values_existObject


128
129
130
131
132
133
# File 'app/models/caboose/post.rb', line 128

def verify_custom_field_values_exist
  Caboose::PostCustomField.where(:site_id => self.site_id).all.each do |f|
    fv = Caboose::PostCustomFieldValue.where(:post_id => self.id, :post_custom_field_id => f.id).first                  
    Caboose::PostCustomFieldValue.create(:post_id => self.id, :post_custom_field_id => f.id, :key => f.key, :value => f.default_value, :sort_order => f.sort_order) if fv.nil?
  end
end