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(field_or_proc, &block) ⇒ Object
Groups records on the table.
- #header_for(column_id) ⇒ Object
-
#initialize(builder, collection, options = {}) ⇒ DataTable
constructor
A new instance of DataTable.
- #model ⇒ Object
- #model_columns ⇒ Object
- #model_name ⇒ 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 22 23 |
# File 'lib/html_tables/data_table.rb', line 16 def auto_generate_columns! raise 'Could not auto generate columns' unless model.respond_to? :accessible_attributes model.accessible_attributes.each do |attr| col = model_columns[attr] object_to_yield.column col.name unless col.nil? end end |
#columns ⇒ Object
48 49 50 |
# File 'lib/html_tables/data_table.rb', line 48 def columns @columns ||= ActiveSupport::OrderedHash.new end |
#group_by(field_or_proc, &block) ⇒ Object
Groups records on the table.
92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/html_tables/data_table.rb', line 92 def group_by(field_or_proc, &block) proc = case field_or_proc when String, Symbol lambda { |obj| obj.public_send(field_or_proc) } when Proc field_or_proc else raise ArgumentError, 'group_by first argument must be a String, Symbol or Proc' end [:group] = { block: block, proc: proc } self end |
#header_for(column_id) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/html_tables/data_table.rb', line 52 def header_for(column_id) return nil if column_id.nil? return @builder.content_tag(:i, nil, class: 'fa fa-check-square-o') 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_name.i18n_key], raise: true) rescue nil v ||= I18n.t(column_id, scope: [:mongoid, :attributes, model_name.i18n_key], 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
25 26 27 28 29 30 31 32 33 |
# File 'lib/html_tables/data_table.rb', line 25 def model @model ||= begin n = collection.model_name.constantize if collection.respond_to?(:model_name) n = n.name if (defined? ActiveModel) && n.is_a?(ActiveModel::Name) n end @model ||= collection.first.try(:class) @model ||= [:name].to_s.singularize.constantize end |
#model_columns ⇒ Object
40 41 42 |
# File 'lib/html_tables/data_table.rb', line 40 def model_columns @model_columns ||= ActiveSupport::HashWithIndifferentAccess[*model.columns.map { |c| [c.name, c] }.flatten] end |
#model_name ⇒ Object
35 36 37 38 |
# File 'lib/html_tables/data_table.rb', line 35 def model_name @model_name ||= collection.model_name if collection.respond_to?(:model_name) @model_name ||= model.model_name end |
#object_to_yield ⇒ Object
44 45 46 |
# File 'lib/html_tables/data_table.rb', line 44 def object_to_yield @ctl ||= YieldedObject.new(self) end |
#row_options_for(item) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/html_tables/data_table.rb', line 71 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
106 107 108 |
# File 'lib/html_tables/data_table.rb', line 106 def test(item) yield item end |
#url_for(item) ⇒ Object
65 66 67 68 69 |
# File 'lib/html_tables/data_table.rb', line 65 def url_for(item) return nil unless .fetch(:enabled, true) return [:block].call(item) if [:block] @builder.url_for(item) rescue nil end |