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
- #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 |
# 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
46 47 48 |
# File 'lib/html_tables/data_table.rb', line 46 def columns @columns ||= ActiveSupport::OrderedHash.new end |
#group_by(column_or_lambda, &block) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/html_tables/data_table.rb', line 87 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
50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/html_tables/data_table.rb', line 50 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
23 24 25 26 27 28 29 30 31 |
# File 'lib/html_tables/data_table.rb', line 23 def model @model ||= begin n = collection.model_name.constantize if collection.respond_to?(:model_name) n = n.name if n.is_a?(ActiveModel::Name) n end @model ||= collection.first.try(:class) @model ||= [:name].to_s.singularize.constantize end |
#model_columns ⇒ Object
38 39 40 |
# File 'lib/html_tables/data_table.rb', line 38 def model_columns @model_columns ||= ActiveSupport::HashWithIndifferentAccess[*model.columns.map { |c| [c.name, c] }.flatten] end |
#model_name ⇒ Object
33 34 35 36 |
# File 'lib/html_tables/data_table.rb', line 33 def model_name @model_name ||= collection.model_name if collection.respond_to?(:model_name) @model_name ||= model.model_name end |
#object_to_yield ⇒ Object
42 43 44 |
# File 'lib/html_tables/data_table.rb', line 42 def object_to_yield @ctl ||= YieldedObject.new(self) end |
#row_options_for(item) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/html_tables/data_table.rb', line 69 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
99 100 101 |
# File 'lib/html_tables/data_table.rb', line 99 def test(item) yield item end |
#url_for(item) ⇒ Object
63 64 65 66 67 |
# File 'lib/html_tables/data_table.rb', line 63 def url_for(item) return nil unless .fetch(:enabled, true) return [:block].call(item) if [:block] @builder.url_for(item) rescue nil end |