From 5c2f761f6588f119df8a4d6cf6f2ae0ba82020ec Mon Sep 17 00:00:00 2001 From: Pablo Targa Date: Tue, 6 Nov 2012 17:40:42 -0200 Subject: [PATCH] Add dom_id on table tr --- .gitignore | 2 + Gemfile.lock | 100 ----------------------------------------- lib/gridder.rb | 61 +++++++++++++------------ lib/gridder/version.rb | 2 +- 4 files changed, 36 insertions(+), 129 deletions(-) delete mode 100644 Gemfile.lock diff --git a/.gitignore b/.gitignore index 5cf939e..9a48a65 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,5 @@ test/dummy/tmp/ test/dummy/.sass-cache gridder-*.gem + +Gemfile.lock diff --git a/Gemfile.lock b/Gemfile.lock deleted file mode 100644 index bbdb611..0000000 --- a/Gemfile.lock +++ /dev/null @@ -1,100 +0,0 @@ -PATH - remote: . - specs: - gridder (0.0.2) - nokogiri (>= 1.5.3) - -GEM - remote: http://rubygems.org/ - specs: - actionmailer (3.2.5) - actionpack (= 3.2.5) - mail (~> 2.4.4) - actionpack (3.2.5) - activemodel (= 3.2.5) - activesupport (= 3.2.5) - builder (~> 3.0.0) - erubis (~> 2.7.0) - journey (~> 1.0.1) - rack (~> 1.4.0) - rack-cache (~> 1.2) - rack-test (~> 0.6.1) - sprockets (~> 2.1.3) - activemodel (3.2.5) - activesupport (= 3.2.5) - builder (~> 3.0.0) - activerecord (3.2.5) - activemodel (= 3.2.5) - activesupport (= 3.2.5) - arel (~> 3.0.2) - tzinfo (~> 0.3.29) - activeresource (3.2.5) - activemodel (= 3.2.5) - activesupport (= 3.2.5) - activesupport (3.2.5) - i18n (~> 0.6) - multi_json (~> 1.0) - arel (3.0.2) - builder (3.0.0) - erubis (2.7.0) - hike (1.2.1) - i18n (0.6.0) - journey (1.0.3) - jquery-rails (2.0.2) - railties (>= 3.2.0, < 5.0) - thor (~> 0.14) - json (1.7.3) - mail (2.4.4) - i18n (>= 0.4.0) - mime-types (~> 1.16) - treetop (~> 1.4.8) - mime-types (1.18) - multi_json (1.3.6) - nokogiri (1.5.3) - polyglot (0.3.3) - rack (1.4.1) - rack-cache (1.2) - rack (>= 0.4) - rack-ssl (1.3.2) - rack - rack-test (0.6.1) - rack (>= 1.0) - rails (3.2.5) - actionmailer (= 3.2.5) - actionpack (= 3.2.5) - activerecord (= 3.2.5) - activeresource (= 3.2.5) - activesupport (= 3.2.5) - bundler (~> 1.0) - railties (= 3.2.5) - railties (3.2.5) - actionpack (= 3.2.5) - activesupport (= 3.2.5) - rack-ssl (~> 1.3.2) - rake (>= 0.8.7) - rdoc (~> 3.4) - thor (>= 0.14.6, < 2.0) - rake (0.9.2.2) - rdoc (3.12) - json (~> 1.4) - sprockets (2.1.3) - hike (~> 1.2) - rack (~> 1.0) - tilt (~> 1.1, != 1.3.0) - sqlite3 (1.3.6) - thor (0.15.2) - tilt (1.3.3) - treetop (1.4.10) - polyglot - polyglot (>= 0.3.1) - tzinfo (0.3.33) - -PLATFORMS - ruby - -DEPENDENCIES - gridder! - jquery-rails - nokogiri - rails (~> 3.2.5) - sqlite3 diff --git a/lib/gridder.rb b/lib/gridder.rb index e4a17f8..102ca34 100644 --- a/lib/gridder.rb +++ b/lib/gridder.rb @@ -1,36 +1,34 @@ require "nokogiri" module Gridder - extend self - GRID_HEADER_SPLITTER = "::" - def for(data, *opts) + def self.for(data, *opts) config = {:empty_message => "Empty"} config = config.update(opts.extract_options!) - + titles = config[:body].map{|e| e[:title].to_s} max_level = titles.map{|e| e.split(GRID_HEADER_SPLITTER).size}.max - + header_rows = [] max_level.times{header_rows << []} - + titles.each do |title_token| if title_token.blank? header_rows[0] << {:title => title_token, :rowspan => max_level} next end - + splitted_title_token = title_token.split(GRID_HEADER_SPLITTER) - + splitted_title_token.each_with_index do |title, idx| level = idx+1 _hash = {} _hash[:title] = title - + if (splitted_title_token.size == level) && (max_level > level) _hash[:rowspan] = (max_level - idx) end - + if (header_rows[idx].last || {})[:title] != _hash[:title] header_rows[idx] << _hash else @@ -38,18 +36,18 @@ def for(data, *opts) a[:colspan] ||= 1 a[:colspan] += 1 end - + end end - + builder = Nokogiri::HTML::Builder.new do |doc| - + doc.table(config[:table]) do doc.thead(config[:thead]) do header_rows.each do |row| doc.tr do row.each do |cell| - doc.th(:rowspan => cell[:rowspan], :colspan => cell[:colspan]){ doc.cdata cell[:title] } + doc.th(:rowspan => cell[:rowspan], :colspan => cell[:colspan]){ doc.cdata cell[:title] } end end end @@ -58,18 +56,26 @@ def for(data, *opts) if data.blank? doc.tr{ doc.td(config[:empty_message], :class => :empty, :colspan => config[:body].size) } else - data.each do |d| - doc.tr do - config[:body].each do |b| - b.symbolize_keys! + data.each do |record| + tr_config = if config[:tr].blank? && record.is_a?(ActiveRecord::Base) + {:id => ActionController::RecordIdentifier.dom_id(record)} + elsif config[:tr].is_a?(Proc) + config[:tr].arity.zero? ? config[:tr].call : config[:tr].call(record) + else + record.send(config[:tr]) + end + + doc.tr(tr_config) do + config[:body].each do |cell| + cell.symbolize_keys! opts = {} - opts[:class] = b[:class] if b[:class].present? - opts[:style] = b[:style] if b[:style].present? - - r = if b[:data].is_a?(Proc) - b[:data].arity.zero? ? b[:data].call : b[:data].call(d) + opts[:class] = cell[:class] if cell[:class].present? + opts[:style] = cell[:style] if cell[:style].present? + + r = if cell[:data].is_a?(Proc) + cell[:data].arity.zero? ? cell[:data].call : cell[:data].call(record) else - d.send(b[:data]) + record.send(cell[:data]) end doc.td(opts){doc.cdata r} @@ -77,12 +83,11 @@ def for(data, *opts) end end end - + end - end + end end - + builder.to_html.html_safe end - end diff --git a/lib/gridder/version.rb b/lib/gridder/version.rb index f8adce8..153ba48 100644 --- a/lib/gridder/version.rb +++ b/lib/gridder/version.rb @@ -1,3 +1,3 @@ module Gridder - VERSION = "0.0.5" + VERSION = "0.0.6" end