lib/servel/index.rb in servel-0.13.0 vs lib/servel/index.rb in servel-0.14.0
- old
+ new
@@ -18,24 +18,26 @@
def render_cache_key
@render_cache_key ||= [@fs_path.to_s, @fs_path.mtime.to_i, sort_method, sort_direction].join("-")
end
def locals
- entries = @fs_path.children.map { |path| Servel::EntryFactory.for(path) }.compact
-
{
url_root: @url_root,
url_path: @url_path,
- directories: directories(entries),
- files: files(entries),
+ entries: entries,
sort: {
method: sort_method,
direction: sort_direction
}
}
end
+ def entries
+ children = @fs_path.children.map { |path| Servel::EntryFactory.for(path) }.compact
+ special_entries + apply_sort(children.select(&:directory?)) + apply_sort(children.select(&:file?))
+ end
+
def sort_method
param = @params["_servel_sort_method"]
param = "name" unless SORT_METHODS.include?(param)
param
end
@@ -44,27 +46,22 @@
param = @params["_servel_sort_direction"]
param = "asc" unless SORT_DIRECTIONS.include?(param)
param
end
- def directories(entries)
- list = apply_sort(entries.select { |entry| entry.directory? })
+ def special_entries
+ list = []
+ list << Servel::EntryFactory.home("/") if @url_root != ""
unless @url_path == "/"
- list.unshift(Servel::EntryFactory.parent("../"))
- list.unshift(Servel::EntryFactory.top(@url_root == "" ? "/" : @url_root))
+ list << Servel::EntryFactory.top(@url_root == "" ? "/" : @url_root)
+ list << Servel::EntryFactory.parent("../")
end
- list.unshift(Servel::EntryFactory.home("/")) if @url_root != ""
-
list
end
- def files(entries)
- apply_sort(entries.select { |entry| entry.file? })
- end
-
def apply_sort(entries)
entries = case sort_method
when "name"
Naturalsorter::Sorter.sort_by_method(entries, :name, true)
when "mtime"
@@ -77,7 +74,7 @@
entries.reverse! if sort_direction == "desc"
entries
end
- instrument :render, :locals, :directories, :files, :apply_sort
+ instrument :render, :locals, :entries, :apply_sort
end
\ No newline at end of file