cirandas.net

ref: master

app/models/concerns/acts_as_having_posts.rb


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
module ActsAsHavingPosts

  module ClassMethods
    def acts_as_having_posts(scope = nil)
      has_many :posts, -> {
        s = order('published_at DESC, id DESC').where('articles.type != ?', 'RssFeed')
        s = s.instance_exec(&scope) if scope
        s
      }, class_name: 'Article', foreign_key: 'parent_id', source: :children

      attr_accessor :feed_attrs

      after_create do |blog|
        blog.children << RssFeed.new(:name => 'feed', :profile => blog.profile)
        blog.feed = blog.feed_attrs
      end

      settings_items :posts_per_page, :type => :integer, :default => 5

      self.send(:include, ActsAsHavingPosts)
    end
  end

  def has_posts?
    true
  end

  def feed
    children.where(:type => 'RssFeed').first
  end

  def feed=(attrs)
    if attrs
      if self.feed
        self.feed.update(attrs)
      else
        self.feed_attrs = attrs
      end
    end
    self.feed
  end

  def name=(value)
    self.set_name(value)
    self.slug = self.slug.blank? ? self.name.to_slug : self.slug.to_slug
  end

end