Module: Transit::Definition::Base

Extended by:
ActiveSupport::Concern
Included in:
ContentBlock
Defined in:
lib/transit/definitions/base.rb

Overview

Base module/functionality for any type of deliverable. Creates class attributes and relationships that all deliverable type classes should implement.

Instance Method Summary collapse

Instance Method Details

#build_context_attributes=(hash) ⇒ Object

Because mongoid requires the class type be the last value in a build method, we override contexts_attributes= to ensure newly created contexts are of the proper class.



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/transit/definitions/base.rb', line 44

def build_context_attributes=(hash)
  hash.each_pair do |position, attrs|
    attrs.stringify_keys!
    next if attrs.empty?
    field = self.contexts.detect do |context| 
      context.id.to_s === attrs['id'].to_s
    end
    
    field ||= self.contexts.create(attrs, (attrs.delete("_type") || 'context').classify.constantize)
    
    # We have to use update attributes because mongoid sometimes won't persist
    # translations on embededdd docs without it
    if field.respond_to?(:translations)
      field.update_attributes(attrs)
    else
      field.attributes = attrs
    end
  end
end