Class: TrkDatatables::ColumnKeyOptions

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/trk_datatables/column_key_options.rb

Constant Summary collapse

TITLE_OPTION =

All options that you can use for columns:

search: if you want to enable global and column search, default is true order: ‘:asc` or `:desc` or `false`, default is `:desc`

:title
SEARCH_OPTION =
:search
ORDER_OPTION =
:order
SELECT_OPTIONS =
:select_options
PREDEFINED_RANGES =
:predefined_ranges
HIDE_OPTION =
:hide
CLASS_NAME =
:class_name
COLUMN_TYPE_IN_DB =
:column_type_in_db
COLUMN_OPTIONS =

this will load date picker SEARCH_OPTION_DATE_VALUE = :date SEARCH_OPTION_DATETIME_VALUE = :datetime

[
  SEARCH_OPTION, ORDER_OPTION, TITLE_OPTION, SELECT_OPTIONS,
  PREDEFINED_RANGES, HIDE_OPTION, CLASS_NAME,
  COLUMN_TYPE_IN_DB
].freeze
STRING_CALCULATED_IN_DB =

for columns that as calculated in db query

:string_calculated_in_db
INTEGER_CALCULATED_IN_DB =
:integer_calculated_in_db
DATE_CALCULATED_IN_DB =
:date_calculated_in_db
DATETIME_CALCULATED_IN_DB =
:datetime_calculated_in_db
BOOLEAN_CALCULATED_IN_DB =
:boolean_calculated_in_db
STRING_TYPE_CAST_POSTGRES =

for ‘columns’ that are calculated in Ruby you need to disable search and order and than it will not be used in queries

"VARCHAR".freeze
STRING_TYPE_CAST_MYSQL =
"CHAR".freeze
STRING_TYPE_CAST_SQLITE =
"TEXT".freeze
STRING_TYPE_CAST_ORACLE =
"VARCHAR2(4000)".freeze
DB_ADAPTER_STRING_TYPE_CAST =
{
  postgresql: STRING_TYPE_CAST_POSTGRES,
  mysql: STRING_TYPE_CAST_MYSQL,
  mysql2: STRING_TYPE_CAST_MYSQL,
  sqlite: STRING_TYPE_CAST_SQLITE,
  sqlite3: STRING_TYPE_CAST_SQLITE,
  oracle: STRING_TYPE_CAST_ORACLE,
  oracleenhanced: STRING_TYPE_CAST_ORACLE
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cols, global_search_cols, predefined_ranges = {}) ⇒ Object

Returns {

column_key: :'users.name',
column_options: { order: false, select_options: User.statuses },
table_class: User,
column_name: :name,
column_type_in_db: :string,
title: 'Name',
html_options: { class: 'my-class' },

}.



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/trk_datatables/column_key_options.rb', line 90

def initialize(cols, global_search_cols, predefined_ranges = {})
  @predefined_ranges = predefined_ranges
  # short notation is when we use array of keys.
  # In case first element is hash than we will use that hash
  if cols.is_a? Array
    cols = if cols.first.is_a? Hash
      cols.first
    else
      cols.each_with_object({}) do |column_key, hash|
        hash[column_key.to_sym] = {}
      end
    end
  end
  _set_data(cols)
  _set_global_search_cols(global_search_cols)
  @string_cast = _determine_string_type_cast
end

Instance Attribute Details

#string_castObject

Returns the value of attribute string_cast.



78
79
80
# File 'lib/trk_datatables/column_key_options.rb', line 78

def string_cast
  @string_cast
end

Instance Method Details

#[](index) ⇒ Object

Raises:



230
231
232
233
234
# File 'lib/trk_datatables/column_key_options.rb', line 230

def [](index)
  raise Error, "You asked for column index=#{index} but there is only #{@data.size} columns" if index >= @data.size

  @data[index]
end

#_determine_column_name(table_class, column_name) ⇒ Object



213
214
215
216
217
218
# File 'lib/trk_datatables/column_key_options.rb', line 213

def _determine_column_name(table_class, column_name)
  return column_name.titleize if table_class.blank?

  # maybe we should check if human_attribute_name exists
  table_class.human_attribute_name column_name
end

#_determine_db_type_for_column(table_class, column_name) ⇒ Object

Returns :string, :integer, :date, :datetime.

Returns:

  • :string, :integer, :date, :datetime



198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/trk_datatables/column_key_options.rb', line 198

def _determine_db_type_for_column(table_class, column_name)
  if table_class < TrkDatatables::CalculatedInDb
    table_class.determine_db_type_for_column
  elsif defined?(::ActiveRecord::Base)
    ar_column = table_class.columns_hash[column_name]
    raise Error, "Can't find column #{column_name} in #{table_class.name}" unless ar_column

    ar_column.type
  elsif defined?(::Neo4j::ActiveNode)
    (table_class.declared_properties[column_name][:type] || String).name.downcase
  else
    raise NotImplementedError, "I work only with ActiveRecord and Neo4j"
  end
end

#_determine_string_type_castObject

This is helper



183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/trk_datatables/column_key_options.rb', line 183

def _determine_string_type_cast # :nodoc:
  if defined?(::ActiveRecord::Base)
    current_adapter = if ::ActiveRecord::Base.respond_to?(:connection_db_config)
      ::ActiveRecord::Base.connection_db_config.configuration_hash[:adapter]
    else
      ::ActiveRecord::Base.connection_config[:adapter]
    end
    DB_ADAPTER_STRING_TYPE_CAST[current_adapter.to_sym]
  else
    "not_used"
  end
end

#_determine_table_class(table_name, class_name = nil) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/trk_datatables/column_key_options.rb', line 147

def _determine_table_class(table_name, class_name = nil)
  # post_users -> PostUser
  # but also check if admin_posts -> Admin::Post so user can defined
  # class_name: 'Admin::Post'
  # https://github.com/duleorlovic/rails/blob/master/activesupport/lib/active_support/inflector/methods.rb#L289
  # note that when class is not eager loaded than const_defined? returns false
  if class_name.present?
    class_name.constantize
  elsif table_name.end_with? "_calculated_in_db"
    "TrkDatatables::#{table_name.classify}".constantize
  else
    table_name.classify.constantize
  end
end

#_set_data(cols) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/trk_datatables/column_key_options.rb', line 108

def _set_data(cols)
  @data = cols.each_with_object([]) do |(column_key, column_options), arr|
    raise Error, "Column options needs to be a Hash" unless column_options.is_a? Hash

    column_options.assert_valid_keys(*COLUMN_OPTIONS)
    table_name, column_name = column_key.to_s.split "."
    if table_name.present? && table_name.end_with?("_calculated_in_db")
      # in calculated columns table_name is used only to determine type
      column_key = column_name
    elsif table_name.present? && column_name.nil?
      raise Error,
        "Unless table name ends with _calculated_in_db, column key needs to have one dot for example: posts.name"
    end

    if table_name.blank?
      column_name = column_options[TITLE_OPTION] || "actions" # some default name for a title
      column_options[SEARCH_OPTION] = false
      column_options[ORDER_OPTION] = false
    else
      table_class = _determine_table_class table_name, column_options[CLASS_NAME]

      unless column_options[SEARCH_OPTION] == false && column_options[ORDER_OPTION] == false
        column_type_in_db = column_options[COLUMN_TYPE_IN_DB] || _determine_db_type_for_column(table_class,
          column_name)
      end
    end
    arr << {
      column_key: column_key.to_sym,
      column_options: column_options,
      table_class: table_class,
      column_name: column_name,
      column_type_in_db: column_type_in_db,
      # the following are used for RenderHtml
      title: column_options[TITLE_OPTION] || _determine_column_name(table_class, column_name),
      html_options: html_options(column_options, column_type_in_db)
    }
  end
end

#_set_global_search_cols(global_search_cols) ⇒ Object



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/trk_datatables/column_key_options.rb', line 162

def _set_global_search_cols(global_search_cols)
  unless global_search_cols.is_a? Array
    raise Error,
      "global_search_cols should be array, for example %w[users.name]"
  end

  @global_search_cols = global_search_cols.each_with_object([]) do |column_key, arr|
    table_name, column_name = column_key.to_s.split "."
    table_class = _determine_table_class table_name
    column_type_in_db = _determine_db_type_for_column(table_class, column_name)
    arr << {
      column_key: column_key.to_sym,
      column_options: {},
      table_class: table_class,
      column_name: column_name,
      column_type_in_db: column_type_in_db
    }
  end
end

#each(&block) ⇒ Object



236
237
238
# File 'lib/trk_datatables/column_key_options.rb', line 236

def each(&block)
  @data.each(&block)
end

#html_options(column_options, column_type_in_db) ⇒ Object



253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# File 'lib/trk_datatables/column_key_options.rb', line 253

def html_options(column_options, column_type_in_db)
  res = {}
  res["data-searchable"] = false if column_options[SEARCH_OPTION] == false
  res["data-orderable"] = false if column_options[ORDER_OPTION] == false
  res["data-datatable-hidden-column"] = true if column_options[HIDE_OPTION] == true
  res["data-datatable-checkbox"] = true if column_type_in_db == :boolean
  if %i[date datetime].include? column_type_in_db
    res["data-datatable-range"] = (column_type_in_db == :datetime) ? :datetime : true
    if column_options[PREDEFINED_RANGES].present? ||
        (@predefined_ranges.try(:[], column_type_in_db).present? && column_options[PREDEFINED_RANGES] != false)
      res["data-datatable-predefined-ranges"] = if column_options[PREDEFINED_RANGES].is_a? Hash
        column_options[PREDEFINED_RANGES]
      else
        @predefined_ranges[column_type_in_db]
      end
      res["data-datatable-predefined-ranges"].transform_values! do |range|
        [range.first.to_s, range.last.to_s]
      end
    end
  end
  res
end

#index_by_column_key(column_key) ⇒ Object

Raises:



244
245
246
247
248
249
250
251
# File 'lib/trk_datatables/column_key_options.rb', line 244

def index_by_column_key(column_key)
  i = @data.find_index do |column_key_option|
    column_key_option[:column_key] == column_key.to_sym
  end
  raise Error, "Can't find index for #{column_key} in #{@data.map { |d| d[:column_key] }.join(", ")}" if i.nil?

  i
end

#searchableObject



220
221
222
223
224
# File 'lib/trk_datatables/column_key_options.rb', line 220

def searchable
  @data.reject do |column_key_option|
    column_key_option[:column_options][SEARCH_OPTION] == false
  end
end

#searchable_and_global_searchObject



226
227
228
# File 'lib/trk_datatables/column_key_options.rb', line 226

def searchable_and_global_search
  searchable + @global_search_cols
end

#sizeObject



240
241
242
# File 'lib/trk_datatables/column_key_options.rb', line 240

def size
  @data.size
end