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



42
43
44
# File 'app/models/caboose/post.rb', line 42

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

.uri(post) ⇒ Object



46
47
48
49
50
51
52
53
54
# File 'app/models/caboose/post.rb', line 46

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



34
35
36
# File 'app/models/caboose/post.rb', line 34

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

#set_slug_and_uri(str) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'app/models/caboose/post.rb', line 56

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



38
39
40
# File 'app/models/caboose/post.rb', line 38

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