Permalink
Browse files

add order by size/type/date/name in directory view

  • Loading branch information...
shingara committed Apr 4, 2013
1 parent 79ba28e commit aa5a6b44b1415e7a2f78d2a5954a2785b463cd0a
@@ -2,27 +2,35 @@ class DirectoryDecorator < Draper::Decorator
decorates :directory
delegate :full_path
- def url
- h.url_for(
+ def url(params={})
+ h.url_for({
:controller => 'nodes',
:action => 'index',
:path => model.paths,
:host => h.request.host
- )
+ }.merge(params))
end
def name
model.name.blank? ? '.' : model.name
end
- def ls
+ def ls(order='name')
NodeDecorator.decorate_collection(
- model.childs
+ model.order_childs(order)
)
end
def parents
DirectoryDecorator.decorate_collection(model.parents)
end
+ def order_by_link(order)
+ h.link_to I18n.t("decorator.node.order.#{order}"), url(:order => order)
+ end
+
+ def order_active?(order)
+ (order == h.params[:order] || (h.params[:order].blank? && order == 'name')) ? 'active' : ''
+ end
+
end
@@ -19,4 +19,8 @@ def directory_url
DirectoryDecorator.new(model.parent).url
end
+ def created_at
+ I18n.l(model.created_at)
+ end
+
end
View
@@ -13,6 +13,21 @@ def full_path
paths.join('/')
end
+ def valid_order(order='')
+ case order
+ when 'date'
+ 'created_at'
+ when 'type'
+ 'file_type'
+ else
+ order.blank? ? 'name' : order
+ end
+ end
+
+ def order_childs(order='name')
+ childs.order(valid_order(order))
+ end
+
def update_full_path
self.full_path = full_path
end
@@ -10,15 +10,25 @@
= link_to t('.add_directory'), new_directory_url(:dirname => node_decorate.full_path), :class => 'btn btn-primary'
= link_to t('.add_file'), new_file_url(:dirname => node_decorate.full_path), :class => 'btn btn-primary'
+.row
+ %ul.nav.nav-pills.pull-right
+ %li{:class => 'disabled'}=link_to 'Order by :'
+ %li{:class => node_decorate.order_active?('name')}= node_decorate.order_by_link('name')
+ %li{:class => node_decorate.order_active?('type')}= node_decorate.order_by_link('type')
+ %li{:class => node_decorate.order_active?('date')}= node_decorate.order_by_link('date')
+ %li{:class => node_decorate.order_active?('size')}= node_decorate.order_by_link('size')
+
%table.table.table-striped
%thead
%tr
%th Name
%th Type
+ %th Date
%th Size
%tbody
- - node_decorate.ls.each do |n|
+ - node_decorate.ls(params[:order]).each do |n|
%tr
%td= n.name
%td= n.file_type
+ %td= n.created_at
%td= n.size
View
@@ -21,3 +21,11 @@ en:
directories:
new:
create_directory: Create directory
+
+ decorator:
+ node:
+ order:
+ name: Name
+ type: Type
+ date: Date
+ size: Size
@@ -4,3 +4,10 @@ def create_directory(name)
d.save
d
end
+
+def create_directory_from_parent(name, parent)
+ d = Directory.new(:name => name)
+ d.parent = parent
+ d.save
+ d
+end
@@ -0,0 +1,19 @@
+
+def create_content_file(name, content)
+ t = Tempfile.new(name)
+ t.write content
+
+ ActionDispatch::Http::UploadedFile.new({
+ :filename => name,
+ :type => File.extname(name),
+ :head => {},
+ :tempfile => t
+ })
+end
+
+def create_node_from_parent(name, content, parent)
+ Node.create(
+ :dirname => parent.full_path,
+ :content => create_content_file(name, content)
+ )
+end
View
@@ -1,5 +1,6 @@
require 'spec_helper'
require 'fabricators/directory_fabricator'
+require 'fabricators/node_fabricator'
require 'shoulda/matchers/active_model'
require 'shoulda/matchers/active_record'
@@ -61,6 +62,36 @@
end
end
+ describe "#order_childs" do
+ let!(:directory) { create_directory('foo') }
+ let!(:directory_1) { create_directory_from_parent('baz', directory) }
+ let!(:directory_2) { create_directory_from_parent('bar', directory) }
+ let!(:file_1) { create_node_from_parent('hello.png', 'foo bar', directory) }
+ let!(:file_2) { create_node_from_parent('foo.jpg', 'foo', directory) }
+
+ it 'order by name by default' do
+ expect(directory.order_childs).to eq [
+ directory_2, directory_1, file_2, file_1
+ ]
+ end
+
+ it 'order by created_at' do
+ expect(directory.order_childs('date')).to eq [directory_1, directory_2, file_1, file_2]
+ end
+
+ it 'order by type' do
+ expect(directory.order_childs('type')).to eq [
+ file_2, file_1, directory_2, directory_1
+ ]
+ end
+
+ it 'order by size' do
+ expect(directory.order_childs('size')).to eq [
+ directory_2, directory_1, file_2, file_1
+ ]
+ end
+ end
+
describe ".get" do
context "on first level" do
let!(:directory) { create_directory('foo') }

0 comments on commit aa5a6b4

Please sign in to comment.