Class: HtmlTables::DataTable
- Inherits:
-
Object
- Object
- HtmlTables::DataTable
- Defined in:
- lib/html_tables/data_table.rb
Instance Attribute Summary collapse
-
#collection ⇒ Object
readonly
Returns the value of attribute collection.
-
#item_url_options ⇒ Object
Returns the value of attribute item_url_options.
-
#nodata_message ⇒ Object
Returns the value of attribute nodata_message.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#row_classes ⇒ Object
readonly
Returns the value of attribute row_classes.
Instance Method Summary collapse
- #auto_generate_columns! ⇒ Object
- #columns ⇒ Object
- #group_by(column_or_lambda, &block) ⇒ Object
- #header_for(column_id) ⇒ Object
-
#initialize(builder, collection, options = {}) ⇒ DataTable
constructor
A new instance of DataTable.
- #model ⇒ Object
- #model_columns ⇒ Object
- #object_to_yield ⇒ Object
- #row_options_for(item) ⇒ Object
- #test(item) {|item| ... } ⇒ Object
- #url_for(item) ⇒ Object
Constructor Details
#initialize(builder, collection, options = {}) ⇒ DataTable
Returns a new instance of DataTable.
8 9 10 11 12 13 14 |
# File 'lib/html_tables/data_table.rb', line 8 def initialize(builder, collection, = {}) @builder = builder @collection = collection @options = @item_url_options = {} @row_classes = [] end |
Instance Attribute Details
#collection ⇒ Object (readonly)
Returns the value of attribute collection.
5 6 7 |
# File 'lib/html_tables/data_table.rb', line 5 def collection @collection end |
#item_url_options ⇒ Object
Returns the value of attribute item_url_options.
6 7 8 |
# File 'lib/html_tables/data_table.rb', line 6 def @item_url_options end |
#nodata_message ⇒ Object
Returns the value of attribute nodata_message.
6 7 8 |
# File 'lib/html_tables/data_table.rb', line 6 def @nodata_message end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
5 6 7 |
# File 'lib/html_tables/data_table.rb', line 5 def @options end |
#row_classes ⇒ Object (readonly)
Returns the value of attribute row_classes.
5 6 7 |
# File 'lib/html_tables/data_table.rb', line 5 def row_classes @row_classes end |
Instance Method Details
#auto_generate_columns! ⇒ Object
16 17 18 19 20 21 |
# File 'lib/html_tables/data_table.rb', line 16 def auto_generate_columns! model.accessible_attributes.each do |attr| col = model_columns[attr] object_to_yield.column col.name unless col.nil? end end |
#columns ⇒ Object
37 38 39 |
# File 'lib/html_tables/data_table.rb', line 37 def columns @columns ||= ActiveSupport::OrderedHash.new end |
#group_by(column_or_lambda, &block) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/html_tables/data_table.rb', line 78 def group_by(column_or_lambda, &block) [:group] = { block: block, proc: case column_or_lambda when String, Symbol ->(obj) { obj.public_send(column_or_lambda) } when Proc column_or_lambda else raise ArgumentError.new "group_by first argument must be a String, Symbol or Proc" end } self end |
#header_for(column_id) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/html_tables/data_table.rb', line 41 def header_for(column_id) return nil if column_id.nil? return columns[column_id][:header] if columns[column_id][:header] return @builder.content_tag(:i, nil, class: 'icon-check') if columns[column_id][:checkbox] v ||= I18n.t(column_id, scope: [:tables, [:name] || :default], raise: true) rescue nil v ||= I18n.t(column_id, scope: [:tables, :default], raise: true) rescue nil v ||= I18n.t(column_id, scope: [:activerecord, :attributes, model.model_name.underscore], raise: true) rescue nil v ||= I18n.t(column_id, scope: [:attributes], raise: true) rescue nil v ||= model_columns[column_id].human_name rescue nil v || column_id.to_s.humanize end |
#model ⇒ Object
23 24 25 26 27 |
# File 'lib/html_tables/data_table.rb', line 23 def model @model ||= collection.model_name.constantize if collection.respond_to?(:model_name) @model ||= collection.first.try(:class) @model ||= [:name].to_s.singularize.constantize end |
#model_columns ⇒ Object
29 30 31 |
# File 'lib/html_tables/data_table.rb', line 29 def model_columns @model_columns ||= ActiveSupport::HashWithIndifferentAccess[*model.columns.map { |c| [c.name, c] }.flatten] end |
#object_to_yield ⇒ Object
33 34 35 |
# File 'lib/html_tables/data_table.rb', line 33 def object_to_yield @ctl ||= YieldedObject.new(self) end |
#row_options_for(item) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/html_tables/data_table.rb', line 60 def (item) h = {} url = self.url_for(item) h[:data] = { url: url } if url classes = [] self.row_classes.each do |cls, opts| next if opts[:if] && !test(item, &opts[:if]) next if opts[:unless] && test(item, &opts[:unless]) classes << cls end h[:class] = classes.uniq * ' ' unless classes.empty? h end |
#test(item) {|item| ... } ⇒ Object
90 91 92 |
# File 'lib/html_tables/data_table.rb', line 90 def test(item) yield item end |
#url_for(item) ⇒ Object
54 55 56 57 58 |
# File 'lib/html_tables/data_table.rb', line 54 def url_for(item) return nil unless .fetch(:enabled, true) return [:block].call(item) if [:block] @builder.url_for(item) rescue nil end |