item.rb 2.95 KB
Newer Older
Uģis Ozols's avatar
Uģis Ozols committed
1
module Refinery
Garrett Heinlen's avatar
Garrett Heinlen committed
2
  module News
3
    class Item < Refinery::Core::BaseModel
Uģis Ozols's avatar
Uģis Ozols committed
4
      extend FriendlyId
Uģis Ozols's avatar
Uģis Ozols committed
5

Martin Markech's avatar
Martin Markech committed
6
      translates :title, :body, :teaser
Uģis Ozols's avatar
Uģis Ozols committed
7

Garrett Heinlen's avatar
Garrett Heinlen committed
8
      attr_accessor :locale # to hold temporarily
9

Martin Markech's avatar
Martin Markech committed
10
11
12
13
14
15
16
      #has_many   :image_pages, :class_name => '::Refinery::ImagePage' , :as => :page
      #has_many   :images, :through => :image_pages
      belongs_to :image, :class_name => '::Refinery::Image'

      attr_accessible :title, :body,  :source, :publish_date, :expiration_date, :position, :image_id, :teaser
      #accepts_nested_attributes_for  :image_pages

17
18
      class Translation
        attr_accessible :locale
19
      end
20

Martin Markech's avatar
Martin Markech committed
21
22
      validates :title, :publish_date, :presence => true

Uģis Ozols's avatar
Uģis Ozols committed
23

Uģis Ozols's avatar
Uģis Ozols committed
24
      friendly_id :title, :use => [:slugged]
Uģis Ozols's avatar
Uģis Ozols committed
25

Martin Markech's avatar
Martin Markech committed
26
      acts_as_indexed :fields => [:title, :body, :teaser]
Uģis Ozols's avatar
Uģis Ozols committed
27

28
29
30
31
32
      default_scope order('position ASC, publish_date DESC')

      def to_param
        "#{id}-#{slug}"
      end
Uģis Ozols's avatar
Uģis Ozols committed
33

Garrett Heinlen's avatar
Garrett Heinlen committed
34
35
36
      def not_published? # has the published date not yet arrived?
        publish_date > Time.now
      end
Uģis Ozols's avatar
Uģis Ozols committed
37

Garrett Heinlen's avatar
Garrett Heinlen committed
38
39
40
41
42
43
44
45
      def next
        self.class.next(self).first
      end

      def prev
        self.class.previous(self).first
      end

Martin Markech's avatar
Martin Markech committed
46
47
48
49
      def contains_body?
        body.present? && body != "<br>"
      end

Garrett Heinlen's avatar
Garrett Heinlen committed
50
      class << self
51
        def by_archive(archive_date)
52
          where(:publish_date => archive_date.beginning_of_month..archive_date.end_of_month)
53
54
55
        end

        def by_year(archive_year)
56
          where(:publish_date => archive_year.beginning_of_year..archive_year.end_of_year)
57
58
59
60
61
62
63
        end

        def all_previous
          where(['publish_date <= ?', Time.now.beginning_of_month])
        end

        def next(item)
Garrett Heinlen's avatar
Garrett Heinlen committed
64
          self.send(:with_exclusive_scope) do
65
            where("publish_date > ?", item.publish_date).order("publish_date ASC")
Garrett Heinlen's avatar
Garrett Heinlen committed
66
67
68
          end
        end

69
        def previous(item)
70
          where("publish_date < ?", item.publish_date)
71
72
73
        end

        def not_expired
74
          where(arel_table[:expiration_date].eq(nil).or(arel_table[:expiration_date].gt(Time.now)))
75
76
77
78
79
80
81
82
83
84
85
        end

        def published
          not_expired.where("publish_date < ?", Time.now)
        end

        def latest(limit = 10)
          published.limit(limit)
        end

        def live
86
          not_expired.where("publish_date <= ?", Time.now)
87
88
89
90
91
        end

        # rejects any page that has not been translated to the current locale.
        def translated
          includes(:translations).where(
Uģis Ozols's avatar
Uģis Ozols committed
92
            translation_class.arel_table[:locale].eq(::Globalize.locale)
93
          ).where(
Philip Arndt's avatar
Philip Arndt committed
94
            arel_table[:id].eq(translation_class.arel_table[:refinery_news_item_id])
95
96
97
          )
        end

Garrett Heinlen's avatar
Garrett Heinlen committed
98
        def teasers_enabled?
Uģis Ozols's avatar
Uģis Ozols committed
99
          Refinery::Setting.find_or_set(:teasers_enabled, true, :scoping => 'news')
Garrett Heinlen's avatar
Garrett Heinlen committed
100
101
102
        end

        def teaser_enabled_toggle!
Uģis Ozols's avatar
Uģis Ozols committed
103
104
          currently = Refinery::Setting.find_or_set(:teasers_enabled, true, :scoping => 'news')
          Refinery::Setting.set(:teasers_enabled, :value => !currently, :scoping => 'news')
Garrett Heinlen's avatar
Garrett Heinlen committed
105
106
107
        end
      end

Garrett Heinlen's avatar
Garrett Heinlen committed
108
    end
Uģis Ozols's avatar
Uģis Ozols committed
109
110
  end
end