Class: PostController

Inherits:
ApplicationController show all
Defined in:
app/controllers/post_controller.rb

Constant Summary

Constants included from PaginationHelper

PaginationHelper::DEFAULT_OPTIONS, PaginationHelper::OPTIONS

Instance Method Summary collapse

Methods included from PaginationHelper

included, #paginate, validate_options!

Instance Method Details

#destroyObject



102
103
104
105
# File 'app/controllers/post_controller.rb', line 102

def destroy
  current_user.posts.find(@params['id']).destroy rescue nil
  redirect_to_main
end

#destroy_commentObject



112
113
114
115
# File 'app/controllers/post_controller.rb', line 112

def destroy_comment
  Comment.find(@params['id']).destroy rescue nil
  redirect_to @request.env['HTTP_REFERER']
end

#destroyxmlObject



107
108
109
110
# File 'app/controllers/post_controller.rb', line 107

def destroyxml
  @postid = @params['id']
  current_user.posts.find(@params['id']).destroy rescue nil
end

#editObject



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'app/controllers/post_controller.rb', line 79

def edit
  begin
    @post = current_user.posts.find(@params['id'])
  rescue ActiveRecord::RecordNotFound
    redirect_to_main
    return
  end

  if @request.post?
    @post.attributes = @params['post']
    if @post.save
      flash['notice'] = "Post was successfully saved."
      redirect_to_main
    end
  end
end

#indexObject



8
9
10
11
12
# File 'app/controllers/post_controller.rb', line 8

def index
  @post_pages, @posts = paginate :posts,
                                 :order_by   => 'created_at DESC', 
                                 :per_page   => @app_config['main']['num_posts']
end

#newObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'app/controllers/post_controller.rb', line 52

def new
  @post = Post.new

  if @request.post?
    @post.attributes     = @params['post']
    @post.user           = current_user
    @post.allows_comment = current_user.allows_comment?
    if @post.save
      # Hook up the tags
      unless @params['tags'].empty?
        @params['tags'].split(',').collect {|t| t.strip}.uniq.each do |tag|
          thetag = Tag.find_by_tag(tag) || Tag.new('tag' => tag)
          thetag.increment('numitems')
          thetag.save
          @post.tags << thetag
        end
      end
      # Check for trackback.
      send_trackback(@post, @params['tb_url'], @app_config) unless @params['tb_url'].empty?
      redirect_to :controller => 'users', :action => @post.user.username
      return
    end
    @error = @post.errors.full_messages
    return
  end
end

#postxmlObject



96
97
98
99
100
# File 'app/controllers/post_controller.rb', line 96

def postxml
  @post = current_user.posts.find(@params['id']) rescue Post.new
  @post.update_attributes @params['post'] if @request.post?
  render_without_layout 'post/postxml'
end

#rangeObject

This is used for fancy URL viewing with URLs of the format /year/month/day.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'app/controllers/post_controller.rb', line 21

def range
  unless @params['year']
    @error = 'Invalid date.'
    render 'post/error'
    return
  end
  if @params['day'] and !@params['month']
    @error = 'Invalid date.'
    render 'post/error'
    return
  end

  year   = @params['year']
  fmonth = @params['month'] || '01'
  tmonth = @params['month'] ? fmonth : '12'
  fday   = @params['day'] || '01'
  tday   = @params['day'] ? fday : '31'


  tday = @params['day'] ? fday : (Time.mktime(year.to_i, tmonth.to_i + 1, 1) - 1.day).day.to_s rescue fday

  if @params['user']
    userid = User.find_by_username(@params['user']).id
    @posts = Post.find_all(["created_at BETWEEN ? AND ? AND user_id=?", "#{year}-#{fmonth}-#{fday} 00:00:00", "#{year}-#{tmonth}-#{tday} 23:59:59", userid], 'created_at DESC') rescue []
  else
    @posts = Post.find_all(["created_at BETWEEN ? AND ?", "#{year}-#{fmonth}-#{fday} 00:00:00", "#{year}-#{tmonth}-#{tday} 23:59:59"], 'created_at DESC') rescue []
  end

  render_action 'index'
end

#replyObject



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'app/controllers/post_controller.rb', line 132

def reply
  @post = Post.find_by_slug(@params['slug'])
  unless @post.allows_comment?
    redirect_to :controller => 'post', :action => 'view', :user => @post.user.username, :slug => @post.slug
    return
  end

  if @params['id']
    @comment = Comment.find @params['id']
  end


  if @request.post?
    @newcomment = @comment ? @comment.comments.create(@params['newcomment']) : @post.comments.create(@params['newcomment'])
    if @newcomment.save
      redirect_to :controller => 'post', :action => 'view', :user => @post.user.username, :slug => @post.slug
    end
  end

  # A little hack to pre-load the subject line.
  # Do this down here so it doesn't get called for the POST.
  @newcomment = Comment.new('subject' => 'Re: ' + @post.subject)
end

#replyxmlObject



156
157
158
159
160
161
# File 'app/controllers/post_controller.rb', line 156

def replyxml
  @post = Post.find(@params['id'])
  @params['comment']['body'].strip! # what the fuck, safari? \000?
  @comment = @post.comments.create(@params['comment'])
  render_without_layout 'post/replyxml'
end

#toggle_commentingObject



117
118
119
120
121
122
123
# File 'app/controllers/post_controller.rb', line 117

def toggle_commenting
  if (post = current_user.posts.find(@params['id']) rescue nil)
    post.allows_comment = (not post.allows_comment?)
    post.save
  end
  redirect_to @request.env['HTTP_REFERER']
end

#toggle_commentingxmlObject



125
126
127
128
129
130
# File 'app/controllers/post_controller.rb', line 125

def toggle_commentingxml
  if (@post = current_user.posts.find(@params['id']) rescue nil)
    @post.allows_comment = (not @post.allows_comment?)
    @post.save
  end
end

#viewObject



14
15
16
17
18
# File 'app/controllers/post_controller.rb', line 14

def view
  @post = Post.find_by_sql("SELECT p.* FROM posts p, users u WHERE p.slug='#{@params['slug']}' AND u.username='#{@params['user']}' and p.user_id=u.id")[0]
  redirect_to_main and return if @post.nil?
  @comments = @post.find_all_in_comments("comment_id=0")
end