cirandas.net

ref: master

app/models/article_block.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
class ArticleBlock < Block

  attr_accessible :article_id

  def self.description
    _('Display one of your contents.')
  end

  def self.short_description
    _('Show one article')
  end

  def self.pretty_name
    _('Article')
  end

  def help
    _('This block displays one of your articles. You can edit the block to select which one of your articles is going to be displayed in the block.')
  end

  def article_id
    self.settings[:article_id]
  end

  def article_id= value
    self.settings[:article_id] = value.blank? ? nil : value.to_i
  end

  def article(reload = false)
    @article = nil if reload
    if @article || article_id
      begin
        @article = Article.find(article_id)
      rescue ActiveRecord::RecordNotFound
        # dangling reference, clear it
        @article = nil
        self.article_id = nil
        self.save!
      end
    end
    @article
  end

  def article=(obj)
    self.article_id = obj.id
    @article = obj
  end

  def available_articles
    return [] if self.owner.nil?
    self.owner.kind_of?(Environment) ? self.owner.portal_community.articles : self.owner.articles
  end

  def posts_per_page
    self.settings[:posts_per_page] or 1
  end

  def posts_per_page= value
    value = value.to_i
    self.settings[:posts_per_page] = value if value > 0
  end

  settings_items :visualization_format, :type => :string, :default => 'short'

  def self.expire_on
      { :profile => [:article], :environment => [:article] }
  end

end