forked from jgdavey/tabletastic
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moving table_for from top-level Tabletastic to Helper namespace
- Loading branch information
Showing
5 changed files
with
40 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,2 @@ | |||
# Include hook code here | |||
require 'tabletastic' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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 |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters