Skip to content

Commit

Permalink
speed up display of categories in datagroup show page.
Browse files Browse the repository at this point in the history
  • Loading branch information
manxingxing committed Sep 12, 2013
1 parent 2b0e139 commit 8ec22ee
Show file tree
Hide file tree
Showing 11 changed files with 68 additions and 53 deletions.
1 change: 1 addition & 0 deletions app/assets/stylesheets/datagroups.css.scss
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ table#datagroups-table {
col.action {width: 80px;}
tr.datagroup {
td {
padding: 6px;
vertical-align: top;
.indicator-icon {
width: 16px;
Expand Down
28 changes: 27 additions & 1 deletion app/controllers/categories_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,31 @@ class CategoriesController < ApplicationController
skip_before_filter :deny_access_to_all

access_control do
actions :show do
actions :show, :index do
allow logged_in
end
actions :upload_sheetcells, :update_sheetcells do
allow :admin
end
end

def index
@datagroup = Datagroup.find(params[:datagroup_id])
respond_to do |format|
format.csv {
send_data render_categories_csv, :type => "text/csv", :filename=>"#{@datagroup.title}_categories.csv", :disposition => 'attachment'
}
format.js do
validate_sort_params(collection: ['short', 'long', 'description', 'count'], default: 'short')
@categories = @datagroup.categories
.select('id, short, long, description, (select count(sheetcells.id) from sheetcells where sheetcells.category_id = categories.id) as count')
.search(params[:search])
.order("#{params[:sort]} #{params[:direction]}")
.paginate(page: params[:page], per_page: 20)
end
end
end

def show
respond_to do |format|
format.html do
Expand Down Expand Up @@ -60,6 +77,15 @@ def render_sheetcells_csv
end
end

def render_categories_csv
csv_string = CSV.generate do |csv|
csv << ["ID","SHORT","LONG","DESCRIPTION","MERGE ID"]
@datagroup.categories.select([:id, :short, :long, :description]).order(:short).each do |cat|
csv << [cat.id, cat.short, cat.long, cat.description]
end
end
end

def load_category
@category = Category.find(params[:id])
end
Expand Down
35 changes: 6 additions & 29 deletions app/controllers/datagroups_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,27 @@

class DatagroupsController < ApplicationController

before_filter :load_datagroup, :only => [:show, :upload_categories, :update_categories, :edit, :update, :destroy, :datacolumns]
before_filter :load_datagroup, :except => [:index]

skip_before_filter :deny_access_to_all
access_control do
actions :index, :show do
allow logged_in
end
actions :upload_categories, :update_categories, :edit, :update, :destroy, :datacolumns do
allow :admin
allow :admin, :data_admin
end
end

def index
validate_sort_params(collection: ['id', 'title', 'datacolumns_count'], default: 'title')
@datagroups = Datagroup.select('id, title, description, created_at, datacolumns_count').
search(params[:search]).paginate(page: params[:page], per_page: 100, order: "#{params[:sort]} #{params[:direction]}")
@datagroups = Datagroup.select('id, title, description, created_at, datacolumns_count')
.search(params[:search])
.paginate(page: params[:page], per_page: 100, order: "#{params[:sort]} #{params[:direction]}")
end

def show
respond_to do |format|
format.csv do
send_data render_categories_csv, :type => "text/csv", :filename=>"#{@datagroup.title}_categories.csv", :disposition => 'attachment'
end
validate_sort_params(collection: ['short', 'long', 'description', 'count'], default: 'short')
@categories = @datagroup.categories.joins('left join sheetcells on categories.id = sheetcells.category_id').
select('categories.id, short, long, description, count(sheetcells.id) as count').
group('categories.id').search(params[:search]).
paginate(
page: params[:page],
per_page: 20,
order: "#{params[:sort]} #{params[:direction]}"
)
format.html
format.js
end
@datasets = @datagroup.datasets.select('datasets.id, datasets.title').order(:title).uniq
end

def update
Expand Down Expand Up @@ -95,15 +81,6 @@ def datacolumns

private

def render_categories_csv
csv_string = CSV.generate do |csv|
csv << ["ID","SHORT","LONG","DESCRIPTION","MERGE ID"]
@datagroup.categories.select('id,short,long,description').order(:short).each do |cat|
csv << [cat.id, cat.short, cat.long, cat.description]
end
end
end

def load_datagroup
@datagroup = Datagroup.find(params[:id])
end
Expand Down
4 changes: 2 additions & 2 deletions app/views/categories/_category_list.html.haml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#search_form
= form_tag datagroup_path(@datagroup), {:method => :get, :remote => true, :id => "filter"} do
= form_tag datagroup_categories_path(@datagroup), {:method => :get, :remote => true, :id => "filter"} do
- unless params[:search].blank?
= link_to "Clear search", datagroup_path(@datagroup, sort: params[:sort], direction: params[:direction]), :id => "clear_search"
= link_to "Clear search", datagroup_categories_path(@datagroup, sort: params[:sort], direction: params[:direction]), :id => "clear_search"
= hidden_field_tag :direction, params[:direction]
= hidden_field_tag :sort, params[:sort]
= search_field_tag :search, params[:search]
Expand Down
2 changes: 2 additions & 0 deletions app/views/categories/index.js.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
$("#categories_table").html("<%= j(render(:partial => "category_list")) %>");
$("#loader").hide();
29 changes: 19 additions & 10 deletions app/views/datagroups/show.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
= link_to edit_datagroup_path(@datagroup) do
= image_tag "edit.png"
Edit
- if @datagroup.datasets.empty?
- if @datasets.empty?
= link_to datagroup_path(@datagroup), :method => :delete, :confirm => "Are you sure to delete this datagroup ?" do
= image_tag "trash.png"
Delete
Expand All @@ -12,10 +12,10 @@
= image_tag "list-ordered.png"
List Datacolumns
- unless @datagroup.categories.empty?
= link_to datagroup_path(:id => @datagroup, :format => :csv) do
= link_to datagroup_categories_path(@datagroup, :format => :csv) do
= image_tag("save.png")
Download Categories as CSV
- if current_user.admin
- show_to :admin, :data_admin do
= link_to upload_categories_datagroup_path do
= image_tag("file-upload.png")
Upload Categories as CSV
Expand All @@ -27,30 +27,39 @@

- unless @datagroup.description.blank?
%h4 Description
%p= @datagroup.description
= simple_format @datagroup.description

- unless @datagroup.comment.blank?
%h4 Comment
%p= @datagroup.comment
= simple_format @datagroup.comment

- unless @datagroup.datasets.empty?
- unless @datasets.empty?
%h3 Datasets
%ul
- @datagroup.datasets.order(:title).uniq.each do |ds|
- @datasets.each do |ds|
%li
= render :partial => ds, :locals => {:with_cart => true, :with_comment => false}

- if @datagroup.categories.empty?
%h3 Datagroup has no categories
- else
%h3 Categories
%h3
Categories
= image_tag 'spinner.gif', :id => 'img-loader'
%noscript Please enable javascript to view the categories.
#categories
#loader.hidden Sending request...
#loader.hidden Loading ...
#categories_table
= render :partial => "categories/category_list"

:javascript
$(function(){
$.getScript("#{datagroup_categories_path(@datagroup)}")
.done(function(){
$("#img-loader").remove();
})
.fail(function(jqxhr, settings, exception) {
$("#categories_table").text("Error occurs. Please try refreshing the page");
});
function show_indicator() {
$("#categories_table tbody").fadeTo("fast",0.3);
$("#loader").show();
Expand Down
2 changes: 0 additions & 2 deletions app/views/datagroups/show.js.erb

This file was deleted.

3 changes: 2 additions & 1 deletion app/views/datasets/show.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@
- u = column.users
- unless u.blank? && column.acknowledge_unknown.blank?
%div
*
%i
= [u.map{|person| link_to person, person}.join(', '), column.acknowledge_unknown].join(', ').html_safe
= (u.map{|p| link_to(p.short_name, p)} + [column.acknowledge_unknown]).compact.join(', ').html_safe
also contributed to this column.
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
match 'cart' => 'carts#show', :as => 'current_cart'

resources :datagroups, :except => [:new, :create] do
resources :categories, :only => [:index]
member do
get :upload_categories, :datacolumns
post :update_categories
Expand Down
8 changes: 8 additions & 0 deletions test/functional/categories_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ class CategoriesControllerTest < ActionController::TestCase
assert_success_no_error
end

test "download categories csv" do
login_nadrowski

get :index, {:datagroup_id => 22, :format => :cvs}

assert_success_no_error
end

test "show sheetcells cvs upload" do
login_nadrowski

Expand Down
8 changes: 0 additions & 8 deletions test/functional/datagroups_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,6 @@ class DatagroupsControllerTest < ActionController::TestCase
assert_success_no_error
end

test "download categories cvs" do
login_nadrowski

get :show, {:id => 22, :format => :cvs}

assert_success_no_error
end

test "upload updated categories cvs" do
login_nadrowski
request.env["HTTP_REFERER"] = root_url
Expand Down

0 comments on commit 8ec22ee

Please sign in to comment.