Class: Blogit::Post

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

Constant Summary

AVAILABLE_STATUS =
(Blogit.configuration.hidden_states + Blogit.configuration.active_states)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Class Method Details

+ (Object) active_with_id(id)

Finds an active post with given id

Parameters:

  • id

    The id of the Post to find

Returns:

  • a Blogit::Post

Raises:

  • ActiveRecord::NoMethodError if no Blogit::Post could be found



69
70
71
# File 'app/models/blogit/post.rb', line 69

def self.active_with_id(id)
  active.find(id)
end

+ (Object) for_feed

The posts to be displayed for RSS and XML feeds/sitemaps

Returns:

  • an ActiveRecord::Relation



59
60
61
# File 'app/models/blogit/post.rb', line 59

def self.for_feed
  active.order('created_at DESC')
end

Instance Method Details

- (Object) blogger

The blogger (User, Admin, etc.) who wrote this Post

Returns:

  • a Blogger (polymorphic type)



38
# File 'app/models/blogit/post.rb', line 38

belongs_to :blogger, :polymorphic => true

- (Object) blogger_display_name

The blogger who wrote this Post's display name

Returns:

  • the blogger's display name as a String if it's set.

  • an empty String if blogger is not present.

Raises:

  • a ConfigurationError if the method called is not defined on #blogger



106
107
108
109
110
111
112
113
114
115
# File 'app/models/blogit/post.rb', line 106

def blogger_display_name
  if self.blogger and !self.blogger.respond_to?(Blogit.configuration.blogger_display_name_method)
    raise ConfigurationError,
    "#{self.blogger.class}##{Blogit.configuration.blogger_display_name_method} is not defined"
  elsif self.blogger.nil?
    ""
  else
    blogger.send(Blogit.configuration.blogger_display_name_method)
  end
end

- (Object) blogger_twitter_username

If there's a blogger and that blogger responds to :twitter_username, returns that. Otherwise, returns nil



119
120
121
122
123
# File 'app/models/blogit/post.rb', line 119

def blogger_twitter_username
  if blogger and blogger.respond_to?(:twitter_username)
    blogger.twitter_username
  end
end

- (Object) comments

The Comments written on this Post

Returns:

  • an ActiveRecord::Relation instance



44
# File 'app/models/blogit/post.rb', line 44

has_many :comments, :class_name => "Blogit::Comment"

- (Object) comments=(value)



95
96
97
98
# File 'app/models/blogit/post.rb', line 95

def comments=(value)
  check_comments_config
  super(value)
end

- (Object) published_at

TODO: Get published at working properly!



78
79
80
# File 'app/models/blogit/post.rb', line 78

def published_at
  created_at
end

- (Object) short_body



86
87
88
# File 'app/models/blogit/post.rb', line 86

def short_body
  truncate(body, length: 400, separator: "\n")
end

- (Object) to_param



82
83
84
# File 'app/models/blogit/post.rb', line 82

def to_param
  "#{id}-#{title.parameterize}"
end