Class: Refinery::Blog::Post

Inherits:
ActiveRecord::Base
  • Object
show all
Extended by:
FriendlyId
Defined in:
app/models/refinery/blog/post.rb

Defined Under Namespace

Modules: ShareThis Classes: Translation

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.by_month(date) ⇒ Object


85
86
87
# File 'app/models/refinery/blog/post.rb', line 85

def by_month(date)
  newest_first.where(:published_at => date.beginning_of_month..date.end_of_month)
end

.by_title(title) ⇒ Object


93
94
95
# File 'app/models/refinery/blog/post.rb', line 93

def by_title(title)
  joins(:translations).find_by(:title => title)
end

.by_year(date) ⇒ Object


89
90
91
# File 'app/models/refinery/blog/post.rb', line 89

def by_year(date)
  newest_first.where(:published_at => date.beginning_of_year..date.end_of_year).with_globalize
end

.comments_allowed?Boolean

Returns:

  • (Boolean)

136
137
138
# File 'app/models/refinery/blog/post.rb', line 136

def comments_allowed?
  Refinery::Setting.find_or_set(:comments_allowed, true, :scoping => 'blog')
end

.newest_firstObject


97
98
99
# File 'app/models/refinery/blog/post.rb', line 97

def newest_first
  order("published_at DESC")
end

.next(current_record) ⇒ Object


123
124
125
126
127
# File 'app/models/refinery/blog/post.rb', line 123

def next(current_record)
  where(arel_table[:published_at].gt(current_record.published_at))
    .where(:draft => false)
    .order('published_at ASC').with_globalize.first
end

109
110
111
# File 'app/models/refinery/blog/post.rb', line 109

def popular(count)
  order("access_count DESC").limit(count).with_globalize
end

.previous(item) ⇒ Object


113
114
115
# File 'app/models/refinery/blog/post.rb', line 113

def previous(item)
  newest_first.published_before(item.published_at).first
end

.published_before(date = Time.now) ⇒ Object Also known as: live


129
130
131
132
133
# File 'app/models/refinery/blog/post.rb', line 129

def published_before(date=Time.now)
  where(arel_table[:published_at].lt(date))
    .where(:draft => false)
    .with_globalize
end

.published_dates_older_than(date) ⇒ Object


101
102
103
# File 'app/models/refinery/blog/post.rb', line 101

def published_dates_older_than(date)
  newest_first.published_before(date).select(:published_at).map(&:published_at)
end

.recent(count) ⇒ Object


105
106
107
# File 'app/models/refinery/blog/post.rb', line 105

def recent(count)
  newest_first.live.limit(count)
end

.teaser_enabled_toggle!Object


144
145
146
147
# File 'app/models/refinery/blog/post.rb', line 144

def teaser_enabled_toggle!
  currently = Refinery::Setting.find_or_set(:teasers_enabled, true, :scoping => 'blog')
  Refinery::Setting.set(:teasers_enabled, :value => !currently, :scoping => 'blog')
end

.teasers_enabled?Boolean

Returns:

  • (Boolean)

140
141
142
# File 'app/models/refinery/blog/post.rb', line 140

def teasers_enabled?
  Refinery::Setting.find_or_set(:teasers_enabled, true, :scoping => 'blog')
end

.uncategorizedObject


117
118
119
120
121
# File 'app/models/refinery/blog/post.rb', line 117

def uncategorized
  newest_first.live.includes(:categories).where(
    Refinery::Blog::Categorization.table_name => { :blog_category_id => nil }
  )
end

.with_globalize(conditions = {}) ⇒ Object

Wrap up the logic of finding the pages based on the translations table.


72
73
74
75
76
77
78
79
80
81
82
83
# File 'app/models/refinery/blog/post.rb', line 72

def with_globalize(conditions = {})
  conditions = {:locale => ::Globalize.locale}.merge(conditions)
  globalized_conditions = {}
  conditions.keys.each do |key|
    if (translated_attribute_names.map(&:to_s) | %w(locale)).include?(key.to_s)
      globalized_conditions["#{self.translation_class.table_name}.#{key}"] = conditions.delete(key)
    end
  end
  # A join implies readonly which we don't really want.
  where(conditions).joins(:translations).where(globalized_conditions)
                   .readonly(false)
end

Instance Method Details

#author_required?Boolean

Override this to disable required authors

Returns:

  • (Boolean)

37
38
39
# File 'app/models/refinery/blog/post.rb', line 37

def author_required?
  true
end

#friendly_id_sourceObject


65
66
67
# File 'app/models/refinery/blog/post.rb', line 65

def friendly_id_source
  custom_url.presence || title
end

#live?Boolean

Returns:

  • (Boolean)

61
62
63
# File 'app/models/refinery/blog/post.rb', line 61

def live?
  !draft && published_at <= Time.now
end

#nextObject


53
54
55
# File 'app/models/refinery/blog/post.rb', line 53

def next
  self.class.next(self)
end

#prevObject


57
58
59
# File 'app/models/refinery/blog/post.rb', line 57

def prev
  self.class.previous(self)
end

#should_generate_new_friendly_id?Boolean

If custom_url or title changes tell friendly_id to regenerate slug when saving record

Returns:

  • (Boolean)

43
44
45
# File 'app/models/refinery/blog/post.rb', line 43

def should_generate_new_friendly_id?
  custom_url_changed? || title_changed?
end