Module: Hotwire::Response::ActiveRecordMixin

Defined in:
lib/hotwire/response/active_record_mixin.rb

Overview

This mixin adds the ability to pass an ActiveRecord model to add_columns

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



21
22
23
24
# File 'lib/hotwire/response/active_record_mixin.rb', line 21

def self.included(base)
  base.send :alias_method_chain, :add_columns, :active_record_model
  base.send :alias_method_chain, :set_data, :active_record_collection
end

Instance Method Details

#add_columns_with_active_record_model(args) ⇒ Object



6
7
8
9
10
11
12
13
14
# File 'lib/hotwire/response/active_record_mixin.rb', line 6

def add_columns_with_active_record_model(args)
  if args.respond_to?(:ancestors) and args.ancestors.include? ActiveRecord::Base
    args = args.columns.sort_by{|c| c.name }.map do |col| 
      [normalize_active_record_type(col.type), {:id => col.name, :label => col.name }]
    end
  end
  add_columns_without_active_record_model(args) 
  
end

#set_data_with_active_record_collection(data) ⇒ Object



16
17
18
19
# File 'lib/hotwire/response/active_record_mixin.rb', line 16

def set_data_with_active_record_collection(data)
  data = rows_from_active_record_collection(data) if data.respond_to?(:first) and data.first.is_a?(ActiveRecord::Base)
  set_data_without_active_record_collection(data)
end