From e8b727af015d3d99f6910ca9da829287c8e5b911 Mon Sep 17 00:00:00 2001 From: Harry Bomrah Date: Thu, 28 May 2015 14:19:55 +0800 Subject: [PATCH] group mods for edit post --- app/controllers/admin/groups_controller.rb | 50 +++++++ app/controllers/orbit_group_controller.rb | 4 +- app/models/group.rb | 4 + app/views/admin/groups/_group.html.erb | 6 +- app/views/admin/groups/_group_form.html.erb | 4 +- app/views/admin/groups/_post.html.erb | 107 +++++++------- app/views/admin/groups/_post_form.html.erb | 28 ++-- app/views/admin/groups/editpost.html.erb | 148 ++++++++++++++++++++ app/views/admin/groups/newpost.html.erb | 1 - app/views/admin/groups/show.html.erb | 13 +- config/routes.rb | 2 + 11 files changed, 300 insertions(+), 67 deletions(-) create mode 100644 app/views/admin/groups/editpost.html.erb diff --git a/app/controllers/admin/groups_controller.rb b/app/controllers/admin/groups_controller.rb index 7baca76..8d7dfdd 100644 --- a/app/controllers/admin/groups_controller.rb +++ b/app/controllers/admin/groups_controller.rb @@ -33,6 +33,14 @@ class Admin::GroupsController < OrbitGroupController redirect_to admin_group_path(@group) end + def updatepost + @post = GroupPost.find(params[:id]) + group = @post.group + @post.update_attributes(post_update_params) + @post.save + redirect_to admin_group_path(group) + end + def deletepost gp = GroupPost.find(params[:id]) gp.destroy @@ -52,6 +60,12 @@ class Admin::GroupsController < OrbitGroupController } end + def editpost + render_401 and return if @post.author != current_user.id + @no_breadcrumb = true + @grouppost = @post + end + def show @no_breadcrumb = true @no_filter = true @@ -263,6 +277,42 @@ class Admin::GroupsController < OrbitGroupController p end + def post_update_params + p = params.require(:group_post).permit! + p["author"] = current_user.id + params[:images_to_destroy].each do |img| + gpi = GroupPostImage.find(img) rescue nil + gpi.destroy if !gpi.nil? + end if !params[:images_to_destroy].nil? + + images = @post.group_post_images + + p[:group_post_images].each do |id| + gpi = GroupPostImage.find(id) rescue nil + if !gpi.nil? + images << gpi + end + end if !p[:group_post_images].nil? + + params[:files_to_destroy].each do |fil| + gpf = GroupPostFile.find(fil) rescue nil + gpf.destroy if !gpf.nil? + end if !params[:files_to_destroy].nil? + + files = @post.group_post_files + + p[:group_post_files].each do |id| + gpf = GroupPostFile.find(id) rescue nil + if !gpf.nil? + files << gpf + end + end if !p[:group_post_files].nil? + + p[:group_post_images] = images + p[:group_post_files] = files + p + end + def post_image_params params.require(:group_post_image).permit! end diff --git a/app/controllers/orbit_group_controller.rb b/app/controllers/orbit_group_controller.rb index 7fb729c..9483801 100644 --- a/app/controllers/orbit_group_controller.rb +++ b/app/controllers/orbit_group_controller.rb @@ -10,7 +10,7 @@ class OrbitGroupController < ApplicationController when "newpost", "createpost", "members" uid = params[:group_id].split("-").last @group = Group.find_by(:uid => uid) - when "showpost" + when "showpost", "editpost" uid = params[:id].split("-").last @post = GroupPost.find_by(:uid => uid) @group = @post.group @@ -21,7 +21,7 @@ class OrbitGroupController < ApplicationController @access_right_level = "none" read_or_write = @group.permission rescue "read" case params[:action] - when "show", "showpost", "newpost", "edit", "members" + when "show", "showpost", "newpost", "edit", "members", "editpost" is_member = @group.users.include?(current_user) if @group.admins.include?(current_user.id.to_s) @access_right_level = "admin" diff --git a/app/models/group.rb b/app/models/group.rb index c699203..ea923af 100644 --- a/app/models/group.rb +++ b/app/models/group.rb @@ -20,5 +20,9 @@ class Group scope :archived, ->{ where(archive: true) } scope :not_archived, ->{ where(archive: false) } + def privacy_name + return self.privacy == "closed" ? "private" : "public" + end + end \ No newline at end of file diff --git a/app/views/admin/groups/_group.html.erb b/app/views/admin/groups/_group.html.erb index b8bf856..9abfb38 100644 --- a/app/views/admin/groups/_group.html.erb +++ b/app/views/admin/groups/_group.html.erb @@ -24,8 +24,10 @@ <% end %> -
+

<%= link_to group.title, admin_group_path(group) %>

@@ -44,7 +46,7 @@
  • Privacy : - "><%= group.privacy %> + <%= group.privacy_name %>
  • <% content = group.description %> diff --git a/app/views/admin/groups/_group_form.html.erb b/app/views/admin/groups/_group_form.html.erb index 8471685..cb3d71f 100644 --- a/app/views/admin/groups/_group_form.html.erb +++ b/app/views/admin/groups/_group_form.html.erb @@ -68,10 +68,10 @@
diff --git a/app/views/admin/groups/_post.html.erb b/app/views/admin/groups/_post.html.erb index ae9c972..3aaa789 100644 --- a/app/views/admin/groups/_post.html.erb +++ b/app/views/admin/groups/_post.html.erb @@ -1,58 +1,67 @@
- - + +
- <% if !post.group_post_images.blank? %> - <%= post.title %> - <% else %> - Post image - <% end %> + <% if !post.group_post_images.blank? %> + <%= post.title %> + <% else %> + Post image + <% end %>
-

- <%= post.title %> -

-
- <% content = strip_tags post.content %> - <%= content[0..250] %> - <%= "..." if content.length > 350 %> -
- +

+ <%= post.title %> +

+
+ <% content = strip_tags post.content %> + <%= content[0..250] %> + <%= "..." if content.length > 350 %> +
+
diff --git a/app/views/admin/groups/_post_form.html.erb b/app/views/admin/groups/_post_form.html.erb index f1c7552..9885174 100644 --- a/app/views/admin/groups/_post_form.html.erb +++ b/app/views/admin/groups/_post_form.html.erb @@ -34,13 +34,19 @@
- + <% if params[:action] == "editpost" %> +
+ <% @grouppost.group_post_images.each do |gpi| %> + + + Delete Image + + <% end %> +
+ <% end %>
- <% if params[:action] == "editpost" %> - - <% end %> - <% end %>
<%= render :partial => "post", :collection => @group.group_posts.desc(:created_at) %> diff --git a/config/routes.rb b/config/routes.rb index a8131a8..19cedd7 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -135,6 +135,8 @@ Orbit::Application.routes.draw do post "posts/file" => 'groups#upload_file' get "posts/file/:id" => "groups#download_file" delete "posts/:id" => "groups#deletepost" + get "post/:id/edit" => "groups#editpost" + patch "post/:id/updatepost" => "groups#updatepost" post "posts/comment" => 'groups#new_comment' resources :groups do