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


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

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

.by_title(title) ⇒ Object


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

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

.by_year(date) ⇒ Object


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

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)

163
164
165
# File 'app/models/refinery/blog/post.rb', line 163

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

.newest_firstObject


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

def newest_first
  order("published_at DESC")
end

.next(current_record) ⇒ Object


149
150
151
152
153
# File 'app/models/refinery/blog/post.rb', line 149

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

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

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

.previous(item) ⇒ Object


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

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

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


155
156
157
158
159
# File 'app/models/refinery/blog/post.rb', line 155

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


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

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

.recent(count) ⇒ Object


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

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

.teaser_enabled_toggle!Object


171
172
173
174
# File 'app/models/refinery/blog/post.rb', line 171

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)

167
168
169
# File 'app/models/refinery/blog/post.rb', line 167

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

.uncategorizedObject


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

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.


98
99
100
101
102
103
104
105
106
107
108
109
# File 'app/models/refinery/blog/post.rb', line 98

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)

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

def author_required?
  !Refinery::Blog.user_class.nil?
end

#author_usernameObject


91
92
93
# File 'app/models/refinery/blog/post.rb', line 91

def author_username
  author.try(:username) || username
end

#friendly_id_sourceObject


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

def friendly_id_source
  custom_url.presence || title
end

#live?Boolean

Returns:

  • (Boolean)

83
84
85
# File 'app/models/refinery/blog/post.rb', line 83

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

#nextObject


75
76
77
# File 'app/models/refinery/blog/post.rb', line 75

def next
  self.class.next(self)
end

#prevObject


79
80
81
# File 'app/models/refinery/blog/post.rb', line 79

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)

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

def should_generate_new_friendly_id?
  saved_change_to_attribute?(:custom_url) || saved_change_to_attribute?(:title)
end

#validating_source_urls?Boolean

Returns:

  • (Boolean)

25
26
27
# File 'app/models/refinery/blog/post.rb', line 25

def validating_source_urls?
  Refinery::Blog.validate_source_url
end