Module: Gitter::Columns
- Extended by:
- ActiveSupport::Concern
- Includes:
- ActiveSupport::Benchmarkable
- Included in:
- Grid
- Defined in:
- lib/gitter/columns.rb
Instance Method Summary collapse
- #column(name, opts = {}, &block) ⇒ Object
- #columns ⇒ Object
- #consecutive_count(arr, what) ⇒ Object
- #header(*args) ⇒ Object
- #header_row ⇒ Object
- #header_rows ⇒ Object
- #logger ⇒ Object
- #models(scope = self.scope) ⇒ Object
- #order_column ⇒ Object
- #paginate(*args) ⇒ Object
- #rows(scope = nil) ⇒ Object
- #rows_for(model) ⇒ Object
- #scope_with_columns(&scope) ⇒ Object
Instance Method Details
#column(name, opts = {}, &block) ⇒ Object
31 32 33 |
# File 'lib/gitter/columns.rb', line 31 def column name, opts = {}, &block (@columns||= {})[name] = Column.new self, name, opts, &block end |
#columns ⇒ Object
96 97 98 |
# File 'lib/gitter/columns.rb', line 96 def columns (@columns||={}).values end |
#consecutive_count(arr, what) ⇒ Object
111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/gitter/columns.rb', line 111 def consecutive_count arr, what count = 0 arr.each do |el| if el == what count +=1 else break end end count end |
#header(*args) ⇒ Object
27 28 29 |
# File 'lib/gitter/columns.rb', line 27 def header *args @current_header_row << Header.new(self,*args) end |
#header_row ⇒ Object
21 22 23 24 25 |
# File 'lib/gitter/columns.rb', line 21 def header_row @current_header_row = [] yield (@header_rows||=[]) << @current_header_row end |
#header_rows ⇒ Object
47 48 49 50 51 52 53 54 |
# File 'lib/gitter/columns.rb', line 47 def header_rows @all_header_rows ||= begin rows = @header_rows || [] max = columns.map{|col|col.headers.size}.max columns_headers = columns.map{|col| Array.new(max){|i| col.headers[i] || Header.blank }} rows += columns_headers.transpose end end |
#logger ⇒ Object
17 18 19 |
# File 'lib/gitter/columns.rb', line 17 def logger @logger ||= Logger.new(STDOUT) end |
#models(scope = self.scope) ⇒ Object
88 89 90 91 92 93 94 |
# File 'lib/gitter/columns.rb', line 88 def models scope = self.scope if respond_to? :transform transform scope else scope end end |
#order_column ⇒ Object
100 101 102 103 104 105 106 107 108 109 |
# File 'lib/gitter/columns.rb', line 100 def order_column @order_column ||= begin if order = @params[:order] @columns[:"#{order}"] or raise ArgumentError, "invalid order column #{order}" else raise ArgumentError, ':desc given but no :order' if @params[:desc] nil end end end |
#paginate(*args) ⇒ Object
43 44 45 |
# File 'lib/gitter/columns.rb', line 43 def paginate *args scope.paginate *args end |
#rows(scope = nil) ⇒ Object
82 83 84 85 86 |
# File 'lib/gitter/columns.rb', line 82 def rows scope = nil res = [] models(scope||self.scope).each{|model| res += rows_for(model)} res end |
#rows_for(model) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/gitter/columns.rb', line 56 def rows_for model #benchmark "---------rows_for #{model}" do cols = nil cols = columns.map do |c| # benchmark "cells for #{c.name}" do [c.cells(model)].flatten # end end max = cols.map{|col|col.size}.max cols.map do |col| nil_padded_cells = Array.new(max){|i| col[i]} cells = [] nil_padded_cells.each_with_index do |c,i| unless c.nil? height = consecutive_count(nil_padded_cells.slice(i+1..-1), nil) + 1 cells << Cell.new(c, rowspan: height) else # required for transpose cells << nil end end cells end.transpose #end end |
#scope_with_columns(&scope) ⇒ Object
35 36 37 38 39 40 41 |
# File 'lib/gitter/columns.rb', line 35 def scope_with_columns &scope if scope scope_without_columns &scope else @scope_with_columns ||= order_column ? order_column.ordered.scope : scope_without_columns end end |