Module: LaserLemon::DrySerial::Serializer

Defined in:
lib/serialization.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



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

def self.included(base)
  base.alias_method_chain :add_includes, :breakfast
  base.alias_method_chain :serializable_names, :breakfast
end

Instance Method Details

#add_includes_with_breakfast(&block) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/serialization.rb', line 9

def add_includes_with_breakfast(&block)
  if include_associations = options.delete(:include)
    include_has_options = include_associations.is_a?(Hash)
    associations = include_has_options ? include_associations.keys : Array(include_associations)

    for association in associations
      records = case @record.class.reflect_on_association(association).macro
      when :has_many, :has_and_belongs_to_many
        @record.send(association).to_a
      when :has_one, :belongs_to
        @record.send(association)
      end

      unless records.nil?
        association_options = include_has_options ? include_associations[association] : {}
        association_options.reverse_merge!(:style => options[:style], :include => nil)
        opts = options.except(:only, :except, :methods).merge(association_options)
        yield(association, records, opts)
      end
    end

    options[:include] = include_associations
  end
end

#serializable_names_with_breakfastObject



34
35
36
# File 'lib/serialization.rb', line 34

def serializable_names_with_breakfast
  serializable_names_without_breakfast.sort_by(&:to_s)
end