Class: FluidTable::Column
- Inherits:
-
Object
- Object
- FluidTable::Column
- Includes:
- ActionView::Helpers::TagHelper, Comparable
- Defined in:
- lib/fluid_table/column.rb
Constant Summary collapse
- DefaultOptions =
{ :default => true, :cacheable => true }
Instance Attribute Summary collapse
-
#alt_name ⇒ Object
basic init.
-
#configured_position ⇒ Object
overrides.
-
#default_position ⇒ Object
overrides.
-
#forced_options ⇒ Object
basic init.
-
#html_options ⇒ Object
basic init.
-
#identity ⇒ Object
basic init.
-
#is_visible ⇒ Object
overrides.
-
#options ⇒ Object
basic init.
-
#proc ⇒ Object
basic init.
-
#table ⇒ Object
basic init.
-
#table_instance ⇒ Object
duped context.
Instance Method Summary collapse
- #<=>(other_column) ⇒ Object
- #cacheable? ⇒ Boolean
- #config_filter_key ⇒ Object
- #current ⇒ Object
- #display? ⇒ Boolean
- #filter ⇒ Object
- #filter_condition ⇒ Object
- #filter_data ⇒ Object
- #filter_data_for_current ⇒ Object
- #filter_distinct? ⇒ Boolean
- #filterable? ⇒ Boolean
- #html(scope) ⇒ Object
-
#initialize(table, identity, alt_name = nil, options = {}, &proc) ⇒ Column
constructor
A new instance of Column.
- #name ⇒ Object
- #position ⇒ Object
- #positioned? ⇒ Boolean
- #reset! ⇒ Object
- #sort ⇒ Object
- #sort_on ⇒ Object
- #visible? ⇒ Boolean
Constructor Details
#initialize(table, identity, alt_name = nil, options = {}, &proc) ⇒ Column
Returns a new instance of Column.
12 13 14 15 16 17 18 19 20 |
# File 'lib/fluid_table/column.rb', line 12 def initialize(table, identity, alt_name = nil, = {}, &proc) self.table = table self.identity = identity self.alt_name = alt_name self. = .reverse_merge(DefaultOptions) self. = .delete(:html) || {} self.proc = proc self. = {} end |
Instance Attribute Details
#alt_name ⇒ Object
basic init
6 7 8 |
# File 'lib/fluid_table/column.rb', line 6 def alt_name @alt_name end |
#configured_position ⇒ Object
overrides
7 8 9 |
# File 'lib/fluid_table/column.rb', line 7 def configured_position @configured_position end |
#default_position ⇒ Object
overrides
7 8 9 |
# File 'lib/fluid_table/column.rb', line 7 def default_position @default_position end |
#forced_options ⇒ Object
basic init
6 7 8 |
# File 'lib/fluid_table/column.rb', line 6 def @forced_options end |
#html_options ⇒ Object
basic init
6 7 8 |
# File 'lib/fluid_table/column.rb', line 6 def @html_options end |
#identity ⇒ Object
basic init
6 7 8 |
# File 'lib/fluid_table/column.rb', line 6 def identity @identity end |
#is_visible ⇒ Object
overrides
7 8 9 |
# File 'lib/fluid_table/column.rb', line 7 def is_visible @is_visible end |
#options ⇒ Object
basic init
6 7 8 |
# File 'lib/fluid_table/column.rb', line 6 def @options end |
#proc ⇒ Object
basic init
6 7 8 |
# File 'lib/fluid_table/column.rb', line 6 def proc @proc end |
#table ⇒ Object
basic init
6 7 8 |
# File 'lib/fluid_table/column.rb', line 6 def table @table end |
#table_instance ⇒ Object
duped context
8 9 10 |
# File 'lib/fluid_table/column.rb', line 8 def table_instance @table_instance end |
Instance Method Details
#<=>(other_column) ⇒ Object
22 23 24 25 26 27 28 |
# File 'lib/fluid_table/column.rb', line 22 def <=>(other_column) if position == other_column.position return -1 if positioned? && !other_column.positioned? return 1 if !positioned? && other_column.positioned? end position <=> other_column.position end |
#cacheable? ⇒ Boolean
38 39 40 |
# File 'lib/fluid_table/column.rb', line 38 def cacheable? [:cacheable] end |
#config_filter_key ⇒ Object
47 48 49 |
# File 'lib/fluid_table/column.rb', line 47 def config_filter_key :"filter_for_#{sort.field}" end |
#current ⇒ Object
30 31 32 |
# File 'lib/fluid_table/column.rb', line 30 def current table_instance.view.current end |
#display? ⇒ Boolean
131 132 133 |
# File 'lib/fluid_table/column.rb', line 131 def display? is_visible.nil? ? [:default] : is_visible end |
#filter ⇒ Object
51 52 53 |
# File 'lib/fluid_table/column.rb', line 51 def filter [:filter] end |
#filter_condition ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/fluid_table/column.rb', line 88 def filter_condition filter_values = current_filter_values.dup conditions = [] if filter_values.delete('none...') conditions << "#{sort.table}.#{sort.field} IS NULL" end unless filter_values.empty? conditions << "#{sort.table}.#{sort.field} IN (?)" end return nil if conditions.empty? condition = "(#{conditions.join(' OR ')})" unless filter_values.empty? condition = [condition, filter_values] end condition end |
#filter_data ⇒ Object
63 64 65 66 67 68 69 70 |
# File 'lib/fluid_table/column.rb', line 63 def filter_data return [] unless filterable? if filter_distinct? filter_data_for_distinct else filter.first.is_a?(Array) ? filter.map(&:first) : filter end end |
#filter_data_for_current ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/fluid_table/column.rb', line 72 def filter_data_for_current return [] unless filterable? data = filter_data values = current_filter_values data.sort.inject(ActiveSupport::OrderedHash.new) do |hsh, key| checked = values.include?(key) value = if !filter_distinct? && filter.first.is_a?(Array) filter.detect{ |arr| arr.first == key }.try(:last) else checked ? 1 : 0 end hsh[key] = [checked ? 1 : 0, key].join('|') hsh end end |
#filter_distinct? ⇒ Boolean
55 56 57 |
# File 'lib/fluid_table/column.rb', line 55 def filter_distinct? filter == :distinct end |
#filterable? ⇒ Boolean
59 60 61 |
# File 'lib/fluid_table/column.rb', line 59 def filterable? filter.present? end |
#html(scope) ⇒ Object
125 126 127 128 129 |
# File 'lib/fluid_table/column.rb', line 125 def html(scope) attributes = .is_a?(Proc) ? .call(Context.new(table_instance, scope)) : = .is_a?(Proc) ? .call(Context.new(table_instance, scope)) : content_tag(:td, interior_content(scope).to_s.html_safe, attributes.merge()) end |
#name ⇒ Object
109 110 111 |
# File 'lib/fluid_table/column.rb', line 109 def name alt_name || identity.to_s.humanize end |
#position ⇒ Object
34 35 36 |
# File 'lib/fluid_table/column.rb', line 34 def position configured_position || default_position end |
#positioned? ⇒ Boolean
105 106 107 |
# File 'lib/fluid_table/column.rb', line 105 def positioned? !!configured_position end |
#reset! ⇒ Object
135 136 137 |
# File 'lib/fluid_table/column.rb', line 135 def reset! self.is_visible = self.configured_position = nil end |
#sort ⇒ Object
117 118 119 120 121 122 123 |
# File 'lib/fluid_table/column.rb', line 117 def sort return nil unless sort_on @sort ||= begin stable, sfield = sort_on.split('.') Struct.new(:table, :field).new(stable, sfield) end end |
#sort_on ⇒ Object
113 114 115 |
# File 'lib/fluid_table/column.rb', line 113 def sort_on [:sort_on] end |
#visible? ⇒ Boolean
42 43 44 45 |
# File 'lib/fluid_table/column.rb', line 42 def visible? return @visible unless @visible.nil? @visible = [:visible] ? [:visible].bind(table_instance.view).call : true end |