Skip to content

Commit

Permalink
Moving table_for from top-level Tabletastic to Helper namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
jgdavey committed Apr 18, 2010
1 parent 9a6819c commit 0ad7561
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 33 deletions.
2 changes: 2 additions & 0 deletions init.rb
@@ -0,0 +1,2 @@
# Include hook code here
require 'tabletastic'
31 changes: 1 addition & 30 deletions lib/tabletastic.rb
@@ -1,38 +1,9 @@
require 'tabletastic/table_builder' require 'tabletastic/table_builder'
require 'tabletastic/helper'


module Tabletastic module Tabletastic
@@default_table_html = {} @@default_table_html = {}
@@default_table_block = lambda {|table| table.data} @@default_table_block = lambda {|table| table.data}


mattr_accessor :default_table_html, :default_table_block 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 end
36 changes: 36 additions & 0 deletions 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
3 changes: 0 additions & 3 deletions rails/init.rb

This file was deleted.

1 change: 1 addition & 0 deletions spec/spec_helper.rb
Expand Up @@ -113,3 +113,4 @@ def edit_admin_post_path(post); "/admin/posts/#{post.id}/edit"; end


include TabletasticSpecHelper include TabletasticSpecHelper
include Tabletastic include Tabletastic
include Tabletastic::Helper

0 comments on commit 0ad7561

Please sign in to comment.