Class: BlogBasic::BlogPost

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

Instance Method Summary collapse

Methods included from BlogBasicModelHelper

#code_highlight_and_markdown, #user_image_tag

Instance Method Details

#check_publishedObject



58
59
60
61
62
63
# File 'app/models/blog_basic/blog_post.rb', line 58

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

#formatted_updated_atObject



95
96
97
# File 'app/models/blog_basic/blog_post.rb', line 95

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

#not_resaving?Boolean

Returns:

  • (Boolean)


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

def not_resaving?
  !@resaving
end

#parsed_body(length = 0) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'app/models/blog_basic/blog_post.rb', line 78

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 code_highlight_and_markdown(image_parsed_body)
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



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'app/models/blog_basic/blog_post.rb', line 36

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)


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

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

#to_paramObject

Provide SEO Friendly URL’s



100
101
102
# File 'app/models/blog_basic/blog_post.rb', line 100

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

#user_name(skip_link = false) ⇒ Object



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

def user_name(skip_link=false)
  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