Module: Acts::DataTable::MultiColumnScopes
- Defined in:
- lib/acts_as_data_table/multi_column_scopes.rb
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
- .build_scope_name(*args) ⇒ Object
-
.database_cast(content) ⇒ Object
Performs a cast to text-like for various database types.
- .included(base) ⇒ Object
-
.process_column_arg(arg, includes, model = self) ⇒ Object
Processes a single argument for has_multi_column_scope.
Class Method Details
.build_scope_name(*args) ⇒ Object
111 112 113 |
# File 'lib/acts_as_data_table/multi_column_scopes.rb', line 111 def self.build_scope_name(*args) args.join('_').to_sym end |
.database_cast(content) ⇒ Object
Performs a cast to text-like for various database types
102 103 104 105 106 107 108 109 |
# File 'lib/acts_as_data_table/multi_column_scopes.rb', line 102 def self.database_cast(content) case ActiveRecord::Base.connection.adapter_name when 'MySQL' "CAST(#{content} AS CHAR(10000) CHARACTER SET utf8)" else "CAST(#{content} AS TEXT)" end end |
.included(base) ⇒ Object
4 5 6 |
# File 'lib/acts_as_data_table/multi_column_scopes.rb', line 4 def self.included(base) base.send :extend, ClassMethods end |
.process_column_arg(arg, includes, model = self) ⇒ Object
Processes a single argument for has_multi_column_scope. Handles
- simple values (e.g. :first_name),
- arrays which will be concatenated with a space character (e.g. [:first_name, :last_name])
- hashes which represent associations on the main model (e.g. :student => [:mat_num])
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/acts_as_data_table/multi_column_scopes.rb', line 76 def self.process_column_arg(arg, includes, model = self) if arg.is_a?(Hash) res = [] arg.each do |association, columns| includes << association Array(columns).each do |column| res << process_column_arg(column, includes, association.to_s.singularize.classify.constantize) end end res elsif arg.is_a?(Array) columns = arg.map {|a| process_column_arg(a, includes, model)} columns = columns.map {|c| "TRIM(#{c})"} "CONCAT(#{columns.join(", ' ', ")})" else if model.column_names.include?(arg.to_s) [model.table_name, arg.to_s].join('.') else raise ArgumentError.new "The table '#{model.table_name}' does not have a column named '#{arg}'" end end end |