diff --git a/app/controllers/blogposts_controller.rb b/app/controllers/blogposts_controller.rb index 0baebee..6d754de 100644 --- a/app/controllers/blogposts_controller.rb +++ b/app/controllers/blogposts_controller.rb @@ -1,71 +1,50 @@ class BlogpostsController < ApplicationController + before_filter :set_post, except: [:index, :new, :create] + before_filter :auth, except: [:index, :show] + def index @posts = Blogpost.all.reverse end def show - @post = Blogpost.find(params[:id]) @comment = Comment.new(blogpost: @post) end def new - if mod? - @post = Blogpost.new - else - flash[:alert] = "You are not allowed to create a new post!" - redirect_to blogposts_path - end + @post = Blogpost.new end def edit - @post = Blogpost.find(params[:id]) - if mod? - else - flash[:alert] = "You are not allowed to edit this post!" - redirect_to @post - end end def create - if mod? - @post = Blogpost.new(post_params) - @post.user_author = current_user - if @post.save - redirect_to @post, notice: 'Post has been created.' - else - flash[:alert] = "Error creating blogpost" - render action: "new" - end + @post = Blogpost.new(post_params) + @post.user_author = current_user + if @post.save + redirect_to @post, notice: 'Post has been created.' else - flash[:alert] = "You are not allowed to create new posts" - redirect_to blog_path + flash[:alert] = "Error creating blogpost" + render action: "new" end end def update - @post = Blogpost.find(params[:id]) - if mod? || @comment.author.is?(current_user) - @post.user_editor = current_user - if @post.update_attributes(post_params([:user_editor])) - redirect_to @post, notice: 'Post has been updated.' - else - flash[:alert] = "There was a problem while updating the post" - render action: "edit" - end + @post.user_editor = current_user + @post.attributes = post_params([:user_editor]) + if @post.save + redirect_to @post, notice: 'Post has been updated.' + else + flash[:alert] = "There was a problem while updating the post" + render action: "edit" end end def destroy - @post = Blogpost.find(params[:id]) - if mod? - if @post.destroy - flash[:notice] = "Post deleted!" - else - flash[:alert] = "There was a problem while deleting this Post" - end + if @post.destroy + flash[:notice] = "Post deleted!" else - flash[:alert] = "You are not allowed to delete this Post" + flash[:alert] = "There was a problem while deleting this Post" end redirect_to blogposts_path end @@ -78,4 +57,18 @@ class BlogpostsController < ApplicationController a += add params.require(:blogpost).permit(a) end + + def set_post + if params[:id] + @post = Blogpost.find(params[:id]) + end + end + + def auth + unless mod? + flash[:alert] = "You are not allowed to edit posts!" + redirect_to @post ? @post : blogposts_path + end + end + end \ No newline at end of file diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index 77e77cf..a8b9d33 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -29,7 +29,9 @@ class CommentsController < ApplicationController def update @comment = Comment.find(params[:id]) if mod? || @comment.author.is?(current_user) - if @comment.update_attributes(comment_params) + @comment.user_editor = current_user + @comment.attributes = comment_params + if @comment.save flash[:notice] = "Comment updated!" redirect_to blogpost_path(@comment.blogpost) + "#comment-#{@comment.id}" else diff --git a/app/controllers/forumthreads_controller.rb b/app/controllers/forumthreads_controller.rb index 167ac75..d5c9412 100644 --- a/app/controllers/forumthreads_controller.rb +++ b/app/controllers/forumthreads_controller.rb @@ -12,7 +12,8 @@ class ForumthreadsController < ApplicationController def update if mod? || @thread.author.is?(current_user) @thread.user_editor = current_user - if @thread.update_attributes thread_params([:user_editor]) + @thread.attributes = thread_params([:user_editor]) + if @thread.save redirect_to @thread, notice: 'Post has been updated.' else flash[:alert] = "There was a problem while updating the post" diff --git a/app/controllers/info_controller.rb b/app/controllers/info_controller.rb new file mode 100644 index 0000000..a1c9892 --- /dev/null +++ b/app/controllers/info_controller.rb @@ -0,0 +1,68 @@ +class InfoController < ApplicationController + + before_filter :set_info, except: [:index, :new, :create] + before_filter :auth, except: [:index, :show] + + def index + @info = Info.all.sort_by{|i| i.title} + end + + def show + end + + def new + @info = Info.new + end + + def edit + end + + def create + @info = Info.new(info_params) + if @info.save + redirect_to @info, notice: 'Info has been created.' + else + flash[:alert] = "Error creating info" + render action: "new" + end + end + + def update + @info.attributes = info_params() + if @info.save + redirect_to @info, notice: 'Info has been updated.' + else + flash[:alert] = "There was a problem while updating the info" + render action: "edit" + end + end + + def destroy + if @info.destroy + flash[:notice] = "Info deleted!" + else + flash[:alert] = "There was a problem while deleting this info" + end + redirect_to info_index_path + end + + + private + + def info_params(add = []) + a = [:title, :content] + a += add + params.require(:info).permit(a) + end + + def set_info + @info = Info.find(params[:id]) + end + + def auth + unless mod? + flash[:alert] = "You are not allowed to edit info!" + redirect_to @info ? @info : info_index_path + end + end +end \ No newline at end of file diff --git a/app/controllers/threadreplies_controller.rb b/app/controllers/threadreplies_controller.rb index ff90650..f00e5d1 100644 --- a/app/controllers/threadreplies_controller.rb +++ b/app/controllers/threadreplies_controller.rb @@ -1,3 +1,65 @@ class ThreadrepliesController < ApplicationController + def edit + @reply = Threadreply.find(params[:id]) + if mod? || @reply.author.is?(current_user) + else + flash[:alert] = "You are not allowed to edit this reply" + redirect_to @reply.thread + end + end + + def create + thread = Forumthread.find(params[:forumthread_id]) + if thread.can_write?(current_user) + @reply = Threadreply.new(reply_params) + @reply.user_author = current_user + @reply.forumthread = thread + if @reply.save + redirect_to forumthread_path(@reply.thread) + "#reply-#{@reply.id}", notice: 'Reply created!' + else + flash[:alert] = "Could not create reply." + redirect_to Blogpost.find(params[:forumthread_id]) + end + else + flash[:alert] = "You are not allowed to create replies." + redirect_to Blogpost.find(params[:forumthread_id]) + end + end + + def update + @reply = Threadreply.find(params[:id]) + if mod? || @reply.author.is?(current_user) + if @reply.update_attributes(reply_params) + flash[:notice] = "Reply updated!" + redirect_to forumthread_path(@reply.thread) + "#reply-#{@reply.id}" + else + flash[:alert] = "There was a problem while updating your reply" + render action: "edit" + end + else + flash[:alert] = "You are not allowed to edit this reply" + redirect_to @reply.thread + end + end + + def destroy + @reply = Threadreply.find(params[:id]) + if mod? || @reply.author.is?(current_user) + if @reply.destroy + flash[:notice] = "Reply deleted!" + else + flash[:alert] = "There was a problem while deleting this reply" + end + else + flash[:alert] = "You are not allowed to delete this reply" + end + redirect_to @reply.thread + end + + private + + def reply_params + params.require(:threadreply).permit(:content) + end end \ No newline at end of file diff --git a/app/models/comment.rb b/app/models/comment.rb index afc9114..d4829d1 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -1,5 +1,7 @@ class Comment < ActiveRecord::Base + belongs_to :user_author, class_name: "User", foreign_key: "user_author_id" + belongs_to :user_editor, class_name: "User", foreign_key: "user_editor_id" validates_presence_of :content, :author, :blogpost validates_length_of :content, in: 4..1000 diff --git a/app/models/info.rb b/app/models/info.rb new file mode 100644 index 0000000..b8fdfb4 --- /dev/null +++ b/app/models/info.rb @@ -0,0 +1,6 @@ +class Info < ActiveRecord::Base + self.table_name = "info" + + validates_presence_of :title, :content + +end \ No newline at end of file diff --git a/app/views/blogposts/edit.html.erb b/app/views/blogposts/edit.html.erb index 1f7f428..eefc9ee 100644 --- a/app/views/blogposts/edit.html.erb +++ b/app/views/blogposts/edit.html.erb @@ -5,4 +5,4 @@ <%= f.text_area :content, :label => false, input_html: {class: "full-width vertical"} %>

<%= f.submit "Update Post", class: "btn blue left" %>

<% end %> -

<%= button_to "Delete post", @post, :method => "delete", :confirm => "Delete post & comments forever?", class: "btn red right" %>

\ No newline at end of file +

<%= button_to "Delete post", @post, method: "delete", data: {confirm: "Delete post & comments forever?"}, class: "btn red right" %>

\ No newline at end of file diff --git a/app/views/comments/edit.html.erb b/app/views/comments/edit.html.erb index 857e3ec..092f0a3 100644 --- a/app/views/comments/edit.html.erb +++ b/app/views/comments/edit.html.erb @@ -4,4 +4,4 @@ <%= f.input :content, label: false, as: "text", placeholder: "Comment" %>

<%= f.submit "Update Comment", class: "btn blue left" %>

<% end %> -

<%= button_to "Delete comment", [@comment.blogpost, @comment] , method: "delete", confirm: "Delete comment forever?", class: "btn red right" %>

\ No newline at end of file +

<%= button_to "Delete comment", [@comment.blogpost, @comment] , method: "delete", data: {confirm: "Delete comment forever?"}, class: "btn red right" %>

\ No newline at end of file diff --git a/app/views/forumgroups/edit.html.erb b/app/views/forumgroups/edit.html.erb index 5f94ff2..9b4f731 100644 --- a/app/views/forumgroups/edit.html.erb +++ b/app/views/forumgroups/edit.html.erb @@ -34,4 +34,4 @@

<%= f.submit "Update group", class: "btn blue" %>

<% end %> -

<%= button_to "Delete group", @post, :method => "delete", :confirm => "Delete group?\nForums + Threads will not be accessible!", class: "btn red right" %>

\ No newline at end of file +

<%= button_to "Delete group", @post, :method => "delete", data: {confirm: "Delete group?\nForums + Threads will not be accessible!"}, class: "btn red right" %>

\ No newline at end of file diff --git a/app/views/forumthreads/edit.html.erb b/app/views/forumthreads/edit.html.erb index 8b5d74b..9cd84b1 100644 --- a/app/views/forumthreads/edit.html.erb +++ b/app/views/forumthreads/edit.html.erb @@ -1,8 +1,22 @@

Edit thread

-

Note: You can use <%= link_to "Markdown", "https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet", target: "_blank" %>!

-<%= simple_form_for [@thread.forum, @thread] do |f|%> - <%= f.input :title, label: false %> - <%= f.text_area :content, label: false, input_html: {class: "full-width vertical"} %> - <%= f.submit "Update thread", class: "btn blue left" %> +<%= link_to @thread.forum.group, forumgroup_path(@thread.forum.group) %> → <%= link_to @thread.forum, @thread.forum %> → New thread +<%= form_for @thread do |f|%> + + <% if mod? %> + + + + + + + + + <% end %> +
<%= f.label :sticky %><%= f.check_box :sticky %>
<%= f.label :locked %><%= f.check_box :locked %>
+
+ <%= f.text_field :title, placeholder: "Title" %> +
+ <%= f.text_area :content, placeholder: "Text" %> +

<%= f.submit "Update thread", class: "btn blue" %>

<% end %> -<%= button_to "Delete thread", [@thread.forum, @thread], :method => "delete", :confirm => "Delete thread & comments forever?", class: "btn red right" %> \ No newline at end of file +<%= button_to "Delete thread", @thread, :method => "delete", data: {confirm: "Delete thread & comments forever?"}, class: "btn red right" %> \ No newline at end of file diff --git a/app/views/forumthreads/show.html.erb b/app/views/forumthreads/show.html.erb index 4b9c9ba..1cda450 100644 --- a/app/views/forumthreads/show.html.erb +++ b/app/views/forumthreads/show.html.erb @@ -14,11 +14,12 @@

<%= "#{pluralize(@thread.replies.length, 'reply')}." %>

- <% @thread.replies.each do |c| %> - Reply<%# render "threadreplies/reply", :c => c %> + <% @thread.replies.each do |reply| %> + <%= render partial: "threadreplies/reply", locals: {reply: reply} %> <% end %> - <% unless @thread.can_read?(current_user) %> - new - <%# render "threadreplies/new" %> + <% if @thread.can_write?(current_user) %> + <%= render partial: "threadreplies/new", locals: {reply: Threadreply.new(forumthread: @thread)} %> + <% else %> + You cannot reply here. <% end %>
\ No newline at end of file diff --git a/app/views/info/edit.html.erb b/app/views/info/edit.html.erb new file mode 100644 index 0000000..b32d6ae --- /dev/null +++ b/app/views/info/edit.html.erb @@ -0,0 +1,7 @@ +

Edit Info

+<%= form_for @info do |f|%> + <%= f.text_field :title, :label => false %> + <%= f.text_area :content, :label => false, input_html: {class: "full-width vertical"} %> +

<%= f.submit "Update Info", class: "btn blue left" %>

+<% end %> +

<%= button_to "Delete Info", @info, method: "delete", data: {confirm: "Delete Info forever?"}, class: "btn red right" %>

\ No newline at end of file diff --git a/app/views/info/index.html.erb b/app/views/info/index.html.erb new file mode 100644 index 0000000..12c33a9 --- /dev/null +++ b/app/views/info/index.html.erb @@ -0,0 +1,9 @@ +

Info

+ +<% if mod? %> + <%= link_to "New Info", new_info_path, class: "btn blue" %> +<% end %> \ No newline at end of file diff --git a/app/views/info/new.html.erb b/app/views/info/new.html.erb new file mode 100644 index 0000000..11f7aa8 --- /dev/null +++ b/app/views/info/new.html.erb @@ -0,0 +1,6 @@ +

New Info

+<%= form_for @info, url: info_index_path do |f|%> + <%= f.text_field :title, placeholder: "Title" %> + <%= f.text_area :content, placeholder: "Text", input_html: {class: "full-width vertical"} %> +

<%= f.submit "Create Info", class: "btn blue left" %>

+<% end %> \ No newline at end of file diff --git a/app/views/info/show.html.erb b/app/views/info/show.html.erb new file mode 100644 index 0000000..36e6f7d --- /dev/null +++ b/app/views/info/show.html.erb @@ -0,0 +1,3 @@ +<%= link_to "Info", info_index_path %> → <%= @info.title %> +

<%= @info.title %>

+
<%= render_md(@info.content).html_safe %>
\ No newline at end of file diff --git a/app/views/layouts/_head.html.erb b/app/views/layouts/_head.html.erb index 25b89dc..03f017d 100644 --- a/app/views/layouts/_head.html.erb +++ b/app/views/layouts/_head.html.erb @@ -4,7 +4,7 @@