Class: MongoidForums::PostsController

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

Instance Method Summary collapse

Methods inherited from ApplicationController

#current_ability

Instance Method Details

#createObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'app/controllers/mongoid_forums/posts_controller.rb', line 31

def create
  authorize! :reply, @topic
  @post = @topic.posts.build(post_params)
  @post.user = mongoid_forums_user

  if @post.save
    @topic.alert_subscribers(mongoid_forums_user.id)
    @topic.forum.increment_posts_count
    flash[:notice] = t("mongoid_forums.post.created")
    redirect_to @topic
  else
    flash.now.alert = t("mongoid_forums.post.not_created")
    render :action => "new"
  end
end

#destroyObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'app/controllers/mongoid_forums/posts_controller.rb', line 67

def destroy
  find_post
  if @topic.posts.first == @post
    flash[:alert] = t("mongoid_forums.post.cannot_delete_first_post")
    redirect_to @topic
    return
  end

  authorize! :destroy_post, @topic.forum

  unless @post.owner_or_admin? mongoid_forums_user
    flash[:alert] = t("mongoid_forums.post.cannot_delete")
    redirect_to @topic and return
  end

  if @post.destroy
    flash[:notice] = t("mongoid_forums.post.deleted")
    redirect_to @topic
  else
    flash[:notice] = t("mongoid_forums.post.cannot_delete")
    redirect_to @topic
  end
end

#editObject



50
51
52
53
# File 'app/controllers/mongoid_forums/posts_controller.rb', line 50

def edit
  find_post
  authorize! :edit_post, @topic.forum
end

#newObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'app/controllers/mongoid_forums/posts_controller.rb', line 9

def new
  authorize! :reply, @topic
  @post = @topic.posts.build
  @post.topic = @topic.id
  if params[:reply_to_id]
    find_reply_to_post

    if @reply_to.id && @reply_to.id == @topic.posts.first.id
      flash[:alert] = t("mongoid_forums.post.not_created_quote_original_post")
      redirect_to @topic
      return
    end
  end

  if @topic.locked
    flash[:alert] = t("mongoid_forums.post.not_created_topic_locked")
    redirect_to @topic
    return
  end

end

#showObject



47
48
# File 'app/controllers/mongoid_forums/posts_controller.rb', line 47

def show
end

#updateObject



55
56
57
58
59
60
61
62
63
64
65
# File 'app/controllers/mongoid_forums/posts_controller.rb', line 55

def update
  authorize! :edit_post, @topic.forum
  find_post

  if @post.owner_or_admin?(mongoid_forums_user) && @post.update_attributes(post_params)
    redirect_to @topic, :notice => t('edited', :scope => 'mongoid_forums.post')
  else
    flash.now.alert = t("mongoid_forums.post.not_edited")
    render :action => "edit"
  end
end