class AjaxDatatablesRails::Datatable::Column
Constants
- DB_ADAPTER_TYPE_CAST
- TYPE_CAST_DEFAULT
- TYPE_CAST_MYSQL
- TYPE_CAST_ORACLE
- TYPE_CAST_SQLITE
- TYPE_CAST_SQLSERVER
- VALID_SEARCH_CONDITIONS
Attributes
datatable[R]
index[R]
options[R]
search[W]
Public Class Methods
new(datatable, index, options)
click to toggle source
# File lib/ajax-datatables-rails/datatable/column.rb, line 14 def initialize(datatable, index, options) @datatable = datatable @index = index @options = options @view_column = datatable.view_columns[column_name] validate_settings! end
Public Instance Methods
column_name()
click to toggle source
# File lib/ajax-datatables-rails/datatable/column.rb, line 22 def column_name @column_name ||= options[:data]&.to_sym end
custom_field?()
click to toggle source
# File lib/ajax-datatables-rails/datatable/column.rb, line 46 def custom_field? !source.include?('.') end
data()
click to toggle source
# File lib/ajax-datatables-rails/datatable/column.rb, line 26 def data options[:data].presence || options[:name] end
field()
click to toggle source
# File lib/ajax-datatables-rails/datatable/column.rb, line 42 def field @field ||= source.split('.').last.to_sym end
formatted_value()
click to toggle source
# File lib/ajax-datatables-rails/datatable/column.rb, line 56 def formatted_value formatter ? formatter.call(search.value) : search.value end
formatter()
click to toggle source
Add formatter option to allow modification of the value before passing it to the database
# File lib/ajax-datatables-rails/datatable/column.rb, line 52 def formatter @view_column[:formatter] end
model()
click to toggle source
# File lib/ajax-datatables-rails/datatable/column.rb, line 38 def model @model ||= source.split('.').first.constantize end
source()
click to toggle source
# File lib/ajax-datatables-rails/datatable/column.rb, line 30 def source @view_column[:source] end
table()
click to toggle source
# File lib/ajax-datatables-rails/datatable/column.rb, line 34 def table model.respond_to?(:arel_table) ? model.arel_table : model end
Private Instance Methods
casted_column()
click to toggle source
# File lib/ajax-datatables-rails/datatable/column.rb, line 89 def casted_column @casted_column ||= ::Arel::Nodes::NamedFunction.new('CAST', [table[field].as(type_cast)]) end
type_cast()
click to toggle source
# File lib/ajax-datatables-rails/datatable/column.rb, line 85 def type_cast @type_cast ||= DB_ADAPTER_TYPE_CAST.fetch(datatable.db_adapter, TYPE_CAST_DEFAULT) end
valid_search_column?(column_name)
click to toggle source
# File lib/ajax-datatables-rails/datatable/column.rb, line 99 def valid_search_column?(column_name) !datatable.view_columns[column_name].nil? end
valid_search_condition?(cond)
click to toggle source
# File lib/ajax-datatables-rails/datatable/column.rb, line 114 def valid_search_condition?(cond) return true if cond.is_a?(Proc) VALID_SEARCH_CONDITIONS.include?(cond) end
validate_settings!()
click to toggle source
# File lib/ajax-datatables-rails/datatable/column.rb, line 93 def validate_settings! raise AjaxDatatablesRails::Error::InvalidSearchColumn, "Unknown column. Check that `data` field is filled on JS side with the column name" if column_name.empty? raise AjaxDatatablesRails::Error::InvalidSearchColumn, "Check that column '#{column_name}' exists in view_columns" unless valid_search_column?(column_name) raise AjaxDatatablesRails::Error::InvalidSearchCondition, cond unless valid_search_condition?(cond) end