From c242bfc291a7dab733c424968509709813e6e331 Mon Sep 17 00:00:00 2001 From: mr Date: Tue, 30 Aug 2011 17:07:56 +0200 Subject: add model sorting --- application.rb | 15 ++++++++++- helper.rb | 5 ++++ views/models.haml | 8 +++--- views/models_navigation.haml | 61 ++++++++++++++++++++++++++++++++++++++++++++ views/style.sass | 23 +++++++++++++++++ 5 files changed, 108 insertions(+), 4 deletions(-) create mode 100644 views/models_navigation.haml diff --git a/application.rb b/application.rb index 9ef50f8..c03915c 100644 --- a/application.rb +++ b/application.rb @@ -90,7 +90,20 @@ get '/login' do end get '/models/?' do - @models = ToxCreateModel.all.sort(:order => "DESC") + @page = params[:page] ? params[:page].to_i : 0 + order = params["order"] == "ASC" ? "ASC" : "DESC" + params["order"] = order + sort_by = params["sort_by"] + if sort_by + case sort_by + when "name", "created_at", "type", "id" + @models = ToxCreateModel.all.sort_by(sort_by.to_sym, :order => "#{order} ALPHA") + end + else + params["sort_by"] = "id" + end + + @models = ToxCreateModel.all.sort(:order => "DESC") unless @models @models.each{|m| raise "internal redis error: model is nil" unless m} haml :models, :locals=>{:models=>@models} end diff --git a/helper.rb b/helper.rb index 80707c3..799135b 100644 --- a/helper.rb +++ b/helper.rb @@ -91,5 +91,10 @@ helpers do haml :neighbors_navigation, :layout => false end + def models_navigation + @page = 0 unless @page + haml :models_navigation, :layout => false + end + end diff --git a/views/models.haml b/views/models.haml index a1ef8ba..d3ee8c0 100644 --- a/views/models.haml +++ b/views/models.haml @@ -17,9 +17,11 @@ = haml :regression, :layout => false = haml :similarity, :layout => false = haml :significant_fragments, :layout => false - -- if @models - - @models.each do |model| +- first = 5*@page +- last = first+4 += models_navigation +- if @models[first..last] + - @models[first..last].each do |model| = haml :model, :locals=>{:model=>model}, :layout => false -if @models.size == 0 .notice There are currently no models. You have to create a model first. diff --git a/views/models_navigation.haml b/views/models_navigation.haml new file mode 100644 index 0000000..fa5bd31 --- /dev/null +++ b/views/models_navigation.haml @@ -0,0 +1,61 @@ +.models_navigation + + %form{:name => "nav", :action => url_for('/models'), :method => "get", :id => "nav"} + %input{:type => :hidden, :name => :sort_by, :id => "sort_by", :value => params[:sort_by]} + %input{:type => :hidden, :name => :order, :id => "order", :value => params[:order]} + %input{:type => :hidden, :name => :subjectid, :value => session[:subjectid]} + %input{:type => :hidden, :name => :page, :id => "page", :value => params[:page]} + + Models are sorted by: + - ["id","created_at","name","type"].each do |s| + - idname = s == "created_at" ? "date" : s + - if params[:sort_by] == s + %div{:id => idname, :class => "active"} + %span="#{idname}" + - if params[:order] == "ASC" + #up{:class => "link"}= "⇓" + - else + #down{:class => "link"}= "⇑" + - else + %div{:id => idname, :class => "link"}="#{idname}" + | + models: + + #prev= "prev" unless @page.to_i == 0 + + - if @models.size < 5*@page+5 + - last = @models.size + - else + - last = 5*@page+5 + + = "(#{5*@page+1}-#{last}/#{@models.size})" + + #next= "next" unless 5*@page.to_i+5 >= @models.size + + - js = "" + - ["id","created_at","name","type"].each do |s| + - idname = s == "created_at" ? "date" : s + - js += "$('##{idname}').click(function() \{ \n " + - js += " $('#sort_by').val('#{s}');\n " + - js += " $('#nav').submit();\n " + - js += "});\n " + + :javascript + $("#prev").click(function() { + $("#page").val(#{@page-1}); + $("#nav").submit(); + }); + $("#next").click(function() { + $("#page").val(#{@page+1}); + $("#nav").submit(); + }); + #{js} + $("#down").click(function() { + $("#order").val("ASC"); + $("#nav").submit(); + }); + $("#up").click(function() { + $("#order").val("DESC"); + $("#nav").submit(); + }); + \ No newline at end of file diff --git a/views/style.sass b/views/style.sass index 15ae256..9f055e4 100644 --- a/views/style.sass +++ b/views/style.sass @@ -267,3 +267,26 @@ dl margin-left: 16px margin-right: 16px float: left + +.models_navigation + #prev + @extend a + display: inline + cursor: pointer + #next + @extend a + display: inline + cursor: pointer + .link + @extend a + display: inline + cursor: pointer + .active + display: inline + color: #000 + font-weight: bold + padding: 0 1px + .thin + font-weight: 100 + span + text-decoration: underline -- cgit v1.2.3