Class: ActiveadminBlog::BlogPost

Inherits:
Object
  • Object
show all
Includes:
ActionView::Helpers::TextHelper, Mongoid::Document, Mongoid::Search, Mongoid::Timestamps
Defined in:
app/models/activeadmin_blog/blog_post.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.archiveObject



82
83
84
85
86
# File 'app/models/activeadmin_blog/blog_post.rb', line 82

def self.archive
  self.all.collect do |p|
    [p.date.month, p.date.year]
  end.uniq
end

.blog_search(query) ⇒ Object



78
79
80
# File 'app/models/activeadmin_blog/blog_post.rb', line 78

def self.blog_search(query)
  self.search(query).published
end

.published_in_category(category_slug) ⇒ Object

Class methods



63
64
65
66
# File 'app/models/activeadmin_blog/blog_post.rb', line 63

def self.published_in_category(category_slug)
  category = ActiveadminBlog::BlogCategory.find_by(slug: category_slug)
  category.blog_posts.published
end

.published_in_month(month, year) ⇒ Object



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

def self.published_in_month(month, year)
  begin
    start_date = Date.new(year, month, 1)
    end_date   = start_date + 1.month
  rescue
    self.published
  end
  self.published.where(:date=>{'$gte' => start_date,'$lt' => end_date})
end

.tagged_with(tag) ⇒ Object



88
89
90
# File 'app/models/activeadmin_blog/blog_post.rb', line 88

def self.tagged_with(tag)
  self.published
end

Instance Method Details

#excerptObject



45
46
47
48
49
50
51
52
# File 'app/models/activeadmin_blog/blog_post.rb', line 45

def excerpt
  html = Nokogiri::HTML(content)
  begin
    html.css('p').select{|p| not p.content.empty? }.first.content
  rescue
    ""
  end
end

Helpers

Returns:

  • (Boolean)


41
42
43
# File 'app/models/activeadmin_blog/blog_post.rb', line 41

def has_featured_image?
  not featured_image.to_s.empty?
end

#new?Boolean

Mongoid 3.x

Returns:

  • (Boolean)


58
59
60
# File 'app/models/activeadmin_blog/blog_post.rb', line 58

def new? # Mongoid 3.x
  new_record?
end

#page_descriptionObject



54
55
56
# File 'app/models/activeadmin_blog/blog_post.rb', line 54

def page_description
  Nokogiri::HTML(excerpt).text
end