app/models/effective/effective_datatable/options.rb in effective_datatables-2.1.3 vs app/models/effective/effective_datatable/options.rb in effective_datatables-2.1.4

- old
+ new

@@ -14,15 +14,17 @@ sql_table = (collection.table rescue nil) # Here we identify all belongs_to associations and build up a Hash like: # {user: {foreign_key: 'user_id', klass: User}, order: {foreign_key: 'order_id', klass: Effective::Order}} belong_tos = (collection.klass.reflect_on_all_associations(:belongs_to) rescue []).inject({}) do |retval, bt| - next if bt.options[:polymorphic] + if bt.options[:polymorphic] + retval[bt.name.to_s] = {foreign_key: bt.foreign_key, klass: bt.name, polymorphic: true} + else + klass = bt.klass || (bt.foreign_type.sub('_type', '').classify.constantize rescue nil) + retval[bt.name.to_s] = {foreign_key: bt.foreign_key, klass: klass} if bt.foreign_key.present? && klass.present? + end - klass = bt.klass || (bt.foreign_type.sub('_type', '').classify.constantize rescue nil) - retval[bt.name.to_s] = {foreign_key: bt.foreign_key, klass: klass} if bt.foreign_key.present? && klass.present? - retval end # has_manys has_manys = (collection.klass.reflect_on_all_associations(:has_many) rescue []).inject({}) do |retval, hm| @@ -53,11 +55,15 @@ # Type cols[name][:type] ||= cols[name][:as] # Use as: or type: interchangeably cols[name][:type] ||= ( if belong_tos.key?(name) - :belongs_to + if belong_tos[name][:polymorphic] + :belongs_to_polymorphic + else + :belongs_to + end elsif has_manys.key?(name) :has_many elsif sql_column.try(:type).present? sql_column.type else @@ -65,12 +71,12 @@ end ) cols[name][:class] = "col-#{cols[name][:type]} col-#{name} #{cols[name][:class]}".strip - # HasMany - if cols[name][:type] == :has_many + # We can't easily sort these fields + if cols[name][:type] == :has_many || cols[name][:type] == :belongs_to_polymorphic cols[name][:sortable] = false end # EffectiveObfuscation if name == 'id' && defined?(EffectiveObfuscation) && collection.respond_to?(:deobfuscate) @@ -116,11 +122,11 @@ # This is a fix for passing filter[:selected] == false, it needs to be 'false' filter[:selected] = filter[:selected].to_s unless filter[:selected].nil? # If you pass values, just assume it's a select - filter[:type] ||= :select if filter.key?(:values) + filter[:type] ||= :select if filter.key?(:values) && col_type != :belongs_to_polymorphic # Check if this is an aggregate column if ['SUM(', 'COUNT(', 'MAX(', 'MIN(', 'AVG('].any? { |str| sql_column.include?(str) } filter[:sql_operation] = :having end @@ -135,9 +141,11 @@ else Proc.new { belongs_to[:klass].all.map { |obj| [obj.to_s, obj.id] }.sort { |x, y| x[0] <=> y[0] } } end ) } + when :belongs_to_polymorphic + {type: :grouped_select, values: {}} when :has_many { type: :select, multiple: true, values: (