mirror of
https://github.com/zhigang1992/tsemple.git
synced 2026-04-29 05:05:25 +08:00
Add admin attachments page
This commit is contained in:
10
app/controllers/admin/attachments_controller.rb
Normal file
10
app/controllers/admin/attachments_controller.rb
Normal file
@@ -0,0 +1,10 @@
|
||||
class Admin::AttachmentsController < Admin::ApplicationController
|
||||
def index
|
||||
@attachments = Attachment.order(id: :desc).page(params[:page])
|
||||
end
|
||||
|
||||
def destroy
|
||||
@attachment = Attachment.find params[:id]
|
||||
@attachment.destroy
|
||||
end
|
||||
end
|
||||
@@ -1,3 +1,5 @@
|
||||
class Attachment < ActiveRecord::Base
|
||||
mount_uploader :file, FileUploader
|
||||
|
||||
belongs_to :user
|
||||
end
|
||||
|
||||
3
app/views/admin/attachments/destroy.js.erb
Normal file
3
app/views/admin/attachments/destroy.js.erb
Normal file
@@ -0,0 +1,3 @@
|
||||
$('#attachment-<%= @attachment.id %>').fadeOut(function() {
|
||||
$(this).remove();
|
||||
});
|
||||
26
app/views/admin/attachments/index.html.slim
Normal file
26
app/views/admin/attachments/index.html.slim
Normal file
@@ -0,0 +1,26 @@
|
||||
.panel.panel-campo
|
||||
.panel-heading.clearfix
|
||||
.pull-right
|
||||
= paginate @attachments, theme: 'campo'
|
||||
h3.panel-title
|
||||
| Attachments
|
||||
|
||||
.panel-body
|
||||
.row
|
||||
- @attachments.in_groups_of(3, false) do |group|
|
||||
- group.each do |attachment|
|
||||
.col-md-4 id="attachment-#{attachment.id}"
|
||||
.thumbnail
|
||||
a href=attachment.file.url
|
||||
img src=attachment.file.url alt=attachment['file']
|
||||
.caption
|
||||
h4 = attachment['file']
|
||||
a.text-muted href=admin_user_path(attachment.user)
|
||||
= attachment.user.name
|
||||
a.pull-right.text-muted href=admin_attachment_path(attachment) data-remote="true" data-method="delete" data-confirm="Are you sure you want to delete this attachment?"
|
||||
i.fa.fa-times
|
||||
.clearfix
|
||||
|
||||
.panel-footer.clearfix
|
||||
.pull-right
|
||||
= paginate @attachments, theme: 'campo'
|
||||
@@ -34,6 +34,8 @@ html
|
||||
a href=admin_topics_path Topics
|
||||
li class=('active' if controller_name == 'comments')
|
||||
a href=admin_comments_path Comments
|
||||
li class=('active' if controller_name == 'attachments')
|
||||
a href=admin_attachments_path Attachment
|
||||
ul.nav.navbar-nav.navbar-right
|
||||
li
|
||||
a href=root_path Back to site
|
||||
|
||||
@@ -99,6 +99,8 @@ Rails.application.routes.draw do
|
||||
patch :restore
|
||||
end
|
||||
end
|
||||
|
||||
resources :attachments, only: [:index, :destroy]
|
||||
end
|
||||
|
||||
if Rails.env.development?
|
||||
|
||||
22
test/controllers/admin/attachments_controller_test.rb
Normal file
22
test/controllers/admin/attachments_controller_test.rb
Normal file
@@ -0,0 +1,22 @@
|
||||
require 'test_helper'
|
||||
|
||||
class Admin::AttachmentsControllerTest < ActionController::TestCase
|
||||
def setup
|
||||
login_as create(:admin)
|
||||
end
|
||||
|
||||
test "should get index" do
|
||||
create :attachment
|
||||
|
||||
get :index
|
||||
assert_response :success, @response.body
|
||||
end
|
||||
|
||||
test "should delete attachment" do
|
||||
attachment = create(:attachment)
|
||||
|
||||
assert_difference "Attachment.count", -1 do
|
||||
xhr :delete, :destroy, id: attachment
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,6 +1,6 @@
|
||||
# Read about factories at https://github.com/thoughtbot/factory_girl
|
||||
|
||||
FactoryGirl.define do
|
||||
factory :attachment do
|
||||
user
|
||||
file File.open('app/assets/images/rails.png')
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user