Class: Enki::Base::Post

Inherits:
ActiveRecord::Base
  • Object
show all
Extended by:
PaginationShim
Defined in:
app/models/enki/base/post.rb

Constant Summary collapse

DEFAULT_LIMIT =
15

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from PaginationShim

paginated, paginated_pages

Instance Attribute Details

#minor_editObject

Returns the value of attribute minor_edit.



65
66
67
# File 'app/models/enki/base/post.rb', line 65

def minor_edit
  @minor_edit
end

#published_at_naturalObject

Returns the value of attribute published_at_natural.



78
79
80
# File 'app/models/enki/base/post.rb', line 78

def published_at_natural
  @published_at_natural
end

Class Method Details

.build_for_preview(params) ⇒ Object



84
85
86
87
88
89
90
91
92
93
# File 'app/models/enki/base/post.rb', line 84

def build_for_preview(params)
  post = self.new(params)
  post.generate_slug
  post.set_dates
  post.apply_filter
  TagList.from(params[:tag_list]).each do |tag|
    post.tags << Tag.new(:name => tag)
  end
  post
end

.default_limitObject


Class Methods




53
54
55
# File 'app/models/enki/base/post.rb', line 53

def self.default_limit
  DEFAULT_LIMIT
end

.find_all_grouped_by_monthObject



109
110
111
112
113
114
115
116
117
# File 'app/models/enki/base/post.rb', line 109

def find_all_grouped_by_month
  posts = find(
    :all,
    :order      => 'posts.published_at DESC',
    :conditions => ['published_at < ?', Time.now]
  )
  month = Struct.new(:date, :posts)
  posts.group_by(&:month).inject([]) {|a, v| a << month.new(v[0], v[1])}
end


95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'app/models/enki/base/post.rb', line 95

def find_by_permalink(year, month, day, slug, options = {})
  begin
    day = Time.parse([year, month, day].collect(&:to_i).join("-")).midnight
    post = find_all_by_slug(slug, options).detect do |post|
      [:year, :month, :day].all? {|time|
        post.published_at.send(time) == day.send(time)
      }
    end
  rescue ArgumentError # Invalid time
    post = nil
  end
  post || raise(ActiveRecord::RecordNotFound)
end

.find_recent(options = {}) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'app/models/enki/base/post.rb', line 39

def self.find_recent(options = {})
  tag     = options.delete(:tag)
  limit   = options.delete(:limit) || DEFAULT_LIMIT

  query = tag ? tagged_with(tag) : self
  query = query.only_published.published_desc.limit(limit)

  return options.empty? ? query : all(options)
end

.only_publishedObject



35
36
37
# File 'app/models/enki/base/post.rb', line 35

def self.only_published
  where(['published_at < ?', Time.zone.now])
end

.published_descObject


Scopes




31
32
33
# File 'app/models/enki/base/post.rb', line 31

def self.published_desc
  order('posts.published_at DESC')
end

Instance Method Details

#apply_filterObject



132
133
134
# File 'app/models/enki/base/post.rb', line 132

def apply_filter
  self.body_html = EnkiFormatter.format_as_xhtml(self.body)
end

#denormalize_comments_count!Object



143
144
145
# File 'app/models/enki/base/post.rb', line 143

def denormalize_comments_count!
  Post.update_all(["approved_comments_count = ?", self.approved_comments.count], ["id = ?", self.id])
end

#destroy_with_undoObject



120
121
122
123
124
125
126
# File 'app/models/enki/base/post.rb', line 120

def destroy_with_undo
  transaction do
    undo = DeletePostUndo.create_undo(self)
    self.destroy
    return undo
  end
end

#generate_slugObject



147
148
149
150
# File 'app/models/enki/base/post.rb', line 147

def generate_slug
  self.slug = self.title.dup if self.slug.blank?
  self.slug.slugorize!
end

#minor_edit?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'app/models/enki/base/post.rb', line 70

def minor_edit?
  self.minor_edit == "1"
end

#monthObject



128
129
130
# File 'app/models/enki/base/post.rb', line 128

def month
  published_at.beginning_of_month
end

#published?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'app/models/enki/base/post.rb', line 74

def published?
  published_at?
end

#set_datesObject



136
137
138
139
140
141
# File 'app/models/enki/base/post.rb', line 136

def set_dates
  self.edited_at = Time.now if self.edited_at.nil? || !minor_edit?
  if new_published_at = Chronic.parse(self.published_at_natural)
    self.published_at = new_published_at
  end
end

#validate_published_at_naturalObject


Instance Methods




61
62
63
# File 'app/models/enki/base/post.rb', line 61

def validate_published_at_natural
  errors.add("published_at_natural", "Unable to parse time") unless published?
end