Skip to content
Browse files

Moving table_for from top-level Tabletastic to Helper namespace

  • Loading branch information...
1 parent 9a6819c commit 0ad7561fae95b6795917e2563f1da12b63a485b2 @jgdavey jgdavey committed Apr 18, 2010
Showing with 40 additions and 33 deletions.
  1. +2 −0 init.rb
  2. +1 −30 lib/tabletastic.rb
  3. +36 −0 lib/tabletastic/helper.rb
  4. +0 −3 rails/init.rb
  5. +1 −0 spec/spec_helper.rb
View
2 init.rb
@@ -0,0 +1,2 @@
+# Include hook code here
+require 'tabletastic'
View
31 lib/tabletastic.rb
@@ -1,38 +1,9 @@
require 'tabletastic/table_builder'
+require 'tabletastic/helper'
module Tabletastic
@@default_table_html = {}
@@default_table_block = lambda {|table| table.data}
mattr_accessor :default_table_html, :default_table_block
-
- # returns and outputs a table for the given active record collection
- def table_for(collection, *args, &block)
- block = Tabletastic.default_table_block unless block_given?
- klass = default_class_for(collection)
- options = args.extract_options!
- initialize_html_options(options, klass)
- result = block.call(TableBuilder.new(collection, klass, self))
- content_tag(:table, result, options[:html])
- end
-
- private
- # Finds the class representing the objects within the collection
- def default_class_for(collection)
- if collection.respond_to?(:proxy_reflection)
- collection.proxy_reflection.klass
- elsif !collection.empty?
- collection.first.class
- end
- end
-
- def initialize_html_options(options, klass)
- options[:html] ||= {}
- options[:html][:id] ||= get_id_for(klass)
- options[:html].reverse_merge!(Tabletastic.default_table_html)
- end
-
- def get_id_for(klass)
- klass ? klass.model_name.collection : ""
- end
end
View
36 lib/tabletastic/helper.rb
@@ -0,0 +1,36 @@
+module Tabletastic
+ module Helper
+ # returns and outputs a table for the given active record collection
+ def table_for(collection, *args, &block)
+ block = Tabletastic.default_table_block unless block_given?
+ klass = default_class_for(collection)
+ options = args.extract_options!
+ initialize_html_options(options, klass)
+ result = block.call(TableBuilder.new(collection, klass, self))
+
+ content_tag(:table, result, options[:html])
+ end
+
+ private
+ # Finds the class representing the objects within the collection
+ def default_class_for(collection)
+ if collection.respond_to?(:proxy_reflection)
+ collection.proxy_reflection.klass
+ elsif !collection.empty?
+ collection.first.class
+ end
+ end
+
+ def initialize_html_options(options, klass)
+ options[:html] ||= {}
+ options[:html][:id] ||= get_id_for(klass)
+ options[:html].reverse_merge!(Tabletastic.default_table_html)
+ end
+
+ def get_id_for(klass)
+ klass ? klass.model_name.collection : ""
+ end
+ end
+end
+
+ActionView::Base.send :include, Tabletastic::Helper
View
3 rails/init.rb
@@ -1,3 +0,0 @@
-# Include hook code here
-require File.join(File.dirname(__FILE__), *%w[.. lib tabletastic])
-ActionView::Base.send :include, Tabletastic
View
1 spec/spec_helper.rb
@@ -113,3 +113,4 @@ def edit_admin_post_path(post); "/admin/posts/#{post.id}/edit"; end
include TabletasticSpecHelper
include Tabletastic
+include Tabletastic::Helper

0 comments on commit 0ad7561

Please sign in to comment.
Something went wrong with that request. Please try again.