Class: BlogBasic::BlogPost

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
ActionView::Helpers::TextHelper
Defined in:
app/models/blog_basic/blog_post.rb

Instance Method Summary collapse

Instance Method Details

#check_publishedObject



72
73
74
75
76
77
# File 'app/models/blog_basic/blog_post.rb', line 72

def check_published
  if self.published_change && self.published_change == [false, true]
    # Moved to published state, update published_on
    self.published_at = Time.now
  end
end

#code_highlight(text) ⇒ Object



33
34
35
36
37
# File 'app/models/blog_basic/blog_post.rb', line 33

def code_highlight(text)
  text.gsub(/\<code( lang="(.+?)")?\>(.+?)\<\/code\>/m) do
    CodeRay.scan($3, $2).div(:css => :class)
  end
end

#formatted_updated_atObject



110
111
112
# File 'app/models/blog_basic/blog_post.rb', line 110

def formatted_updated_at
  self.updated_at.strftime(BlogConf.data['post_date_format'] || '%m/%d/%Y at %I:%M%p')
end

#markdown(text, markdown_options = {}) ⇒ Object



29
30
31
# File 'app/models/blog_basic/blog_post.rb', line 29

def markdown(text, markdown_options = {})
  BlueCloth.new(text, markdown_options).to_html
end

#not_resaving?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'app/models/blog_basic/blog_post.rb', line 43

def not_resaving?
  !@resaving
end

#parsed_body(length = 0) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'app/models/blog_basic/blog_post.rb', line 93

def parsed_body(length=0)
  image_parsed_body = self.body.gsub(/[{]blog_image[:]([0-9]+)[:]([a-zA-Z]+)[}]/) do |str|
    puts "IMAGE ID: #{$1.to_i}"
    img = BlogImage.find_by_id($1.to_i)
    logger.debug img.inspect.to_s
    if img
      img.image.url($2)
    else
      ''
    end
  end
  if length > 0
    image_parsed_body =  truncate(image_parsed_body, :length => length, :separator => ' ')
  end
  return render_text(image_parsed_body)
end

#render_text(text, markdown_options = {}) ⇒ Object



39
40
41
# File 'app/models/blog_basic/blog_post.rb', line 39

def render_text(text, markdown_options = {})
  markdown(code_highlight(text), markdown_options)
end

#replace_blog_image_tagsObject

For images that haven’t been uploaded yet, they get a random image id with ‘upload’ infront of it. We replace those with their new image id



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'app/models/blog_basic/blog_post.rb', line 50

def replace_blog_image_tags
  @resaving = true
  self.body.gsub!(/[{]blog_image:upload[0-9]+:[a-zA-Z]+[}]/) do |image_tag|
    random_id, size = image_tag.scan(/upload([0-9]+)[:]([a-zA-Z]+)/).flatten

    new_id = random_id

    matching_image = self.blog_images.reject {|bi| !bi.random_id || bi.random_id != random_id }.first

    if matching_image
      new_id = matching_image.id
    end

    "{blog_image:#{new_id}:#{size}}"
  end

  self.save
  @resaving = false

  return true
end

#show_user?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'app/models/blog_basic/blog_post.rb', line 79

def show_user?
  (!BlogConf.data['show_user_who_published'] || BlogConf.data['show_user_who_published'] == true)
end

#to_paramObject

Provide SEO Friendly URL’s



115
116
117
# File 'app/models/blog_basic/blog_post.rb', line 115

def to_param
  "#{id}-#{title.gsub(/[^a-z0-9]+/i, '-')}"
end

#user_image_tagObject



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'app/models/blog_basic/blog_post.rb', line 119

def user_image_tag
  if self.user && self.user.respond_to?(:blog_image_url) && self.user.blog_image_url
    # Load image from model
    ret = "<img src=\"#{self.user.blog_image_url}\" />"
  elsif BlogConf.data['gravatar']
    # Gravatar
    require 'digest/md5'
    if self.respond_to?(:email) && !self.email.blank?
      email = self.email
    elsif self.user && self.user.respond_to?(:email) && !self.user.email.blank?
      email = self.user.email
    else
      return ''
    end

    hash = Digest::MD5.hexdigest(email.downcase)
    ret = "<img src=\"http://www.gravatar.com/avatar/#{hash}.jpg\" />"
  else
    # No Image
    return ''
  end

  return ret.html_safe if ret.respond_to?(:html_safe)
  return ret
end

#user_name(skip_link = false) ⇒ Object



84
85
86
87
88
89
90
91
# File 'app/models/blog_basic/blog_post.rb', line 84

def user_name(skip_link=false)
  return BlogConf.data['name']
  #if !skip_link && BlogConf.data['link_to_user']
  #  return "<a href=\"/users/#{self.user.id}\">#{CGI.escapeHTML(self.user.name)}</a>"
  #else
  #  return self.user.name
  #end
end