Module: ActiveRecord::Collections::Serialization

Included in:
ActiveRecord::Collection
Defined in:
lib/active_record/collections/serialization.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



4
5
6
# File 'lib/active_record/collections/serialization.rb', line 4

def self.included(base)
  base.send :extend, ClassMethods
end

Instance Method Details

#as_json(options = nil) ⇒ Object



68
69
70
71
72
73
# File 'lib/active_record/collections/serialization.rb', line 68

def as_json(options=nil)
  h = to_hash
  h[:collectable] = h[:collectable].try(:name)
  h[:collection] = h[:collection].name if h.has_key?(:collection)
  h.as_json(options)
end

#to_hash(include_limit = false) ⇒ Object Also known as: to_h



60
61
62
63
64
65
# File 'lib/active_record/collections/serialization.rb', line 60

def to_hash(include_limit=false)
  values = relation.values.merge({collectable: collectable})
  values.merge!({limit: nil, offset: nil}) if !include_limit && !try(:is_batch?)
  values[:collection] = self.class if self.class < ActiveRecord::Collection
  ActiveRecord::Collections::Serializer.to_hash(values)
end

#to_json(options = nil) ⇒ Object



75
76
77
# File 'lib/active_record/collections/serialization.rb', line 75

def to_json(options=nil)
  as_json.to_json(options)
end

#to_paramObject



79
80
81
# File 'lib/active_record/collections/serialization.rb', line 79

def to_param
  to_json
end

#to_sqlObject



56
57
58
# File 'lib/active_record/collections/serialization.rb', line 56

def to_sql
  relation.to_sql
end