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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#localeObject

Returns the value of attribute locale.



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

def locale
  @locale
end

Class Method Details

.by_archive(date) ⇒ Object



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

def by_archive(date)
  Refinery.deprecate("Refinery::Blog::Post.by_archive(date)", {:replacement => "Refinery::Blog::Post.by_month(date)", :when => 2.2 })
  by_month(date)
end

.by_month(date) ⇒ Object



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

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

.by_year(date) ⇒ Object



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

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

.comments_allowed?Boolean

Returns:

  • (Boolean)


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

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

.find_by_slug_or_id(slug_or_id) ⇒ Object



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

def find_by_slug_or_id(slug_or_id)
  if slug_or_id.friendly_id?
    find_by_slug(slug_or_id)
  else
    find(slug_or_id)
  end
end

.next(current_record) ⇒ Object



126
127
128
# File 'app/models/refinery/blog/post.rb', line 126

def next(current_record)
  where(["published_at > ? and draft = ?", current_record.published_at, false]).reorder('published_at ASC').with_globalize.first
end


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

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

.previous(item) ⇒ Object



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

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

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



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

def published_before(date=Time.now)
  where("published_at < ? and draft = ?", date, false).with_globalize
end

.published_dates_older_than(date) ⇒ Object



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

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

.recent(count) ⇒ Object



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

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

.teaser_enabled_toggle!Object



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

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)


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

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

.uncategorizedObject



122
123
124
# File 'app/models/refinery/blog/post.rb', line 122

def uncategorized
  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.



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

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.
  joins(:translations).where(globalized_conditions).where(conditions).readonly(false)
end

Instance Method Details

#friendly_id_sourceObject



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

def friendly_id_source
  custom_url.presence || title
end

#live?Boolean

Returns:

  • (Boolean)


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

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

#nextObject



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

def next
  self.class.next(self)
end

#prevObject



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

def prev
  self.class.previous(self)
end