Add admin attachments page

This commit is contained in:
Rei
2014-02-18 21:26:44 +08:00
parent 398864dfaa
commit 88e35ee2ec
8 changed files with 69 additions and 2 deletions

View 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

View File

@@ -1,3 +1,5 @@
class Attachment < ActiveRecord::Base
mount_uploader :file, FileUploader
belongs_to :user
end

View File

@@ -0,0 +1,3 @@
$('#attachment-<%= @attachment.id %>').fadeOut(function() {
$(this).remove();
});

View 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'

View File

@@ -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

View File

@@ -99,6 +99,8 @@ Rails.application.routes.draw do
patch :restore
end
end
resources :attachments, only: [:index, :destroy]
end
if Rails.env.development?

View 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

View File

@@ -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