Class: Ember::Schema::ActiveModel

Inherits:
Object
  • Object
show all
Defined in:
lib/ember/schema/active_model.rb

Instance Method Summary collapse

Instance Method Details

#camelize(serializer, klass) ⇒ Object



28
29
30
# File 'lib/ember/schema/active_model.rb', line 28

def camelize(serializer, klass)
  (serializer._root || klass.name).camelize
end

#descendants(serializer) ⇒ Object



12
13
14
# File 'lib/ember/schema/active_model.rb', line 12

def descendants(serializer)
  serializers
end

#get_klass(serializer) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/ember/schema/active_model.rb', line 16

def get_klass(serializer)
  module_name = serializer.name.deconstantize
  class_name = serializer.root_name.camelize
  full_name = [module_name, class_name].reject(&:empty?).join("::")
  begin
    return full_name.constantize
  rescue => e
    p " - Unable to find model: '#{full_name}'. Skipping schema processing."
    return nil
  end
end

#get_type(association) ⇒ Object



74
75
76
77
78
79
80
81
# File 'lib/ember/schema/active_model.rb', line 74

def get_type(association)
  if association.is_a? ::ActiveModel::Serializer::Association::HasMany
    return "has_many"
  elsif association.is_a? ::ActiveModel::Serializer::Association::HasOne
    return "belongs_to"
  end
  return nil
end

#schema(serializer, klass) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/ember/schema/active_model.rb', line 32

def schema(serializer, klass)
  columns = if klass.respond_to? :columns_hash then klass.columns_hash else {} end

  attrs = {}
  serializer._attributes.each do |name|
    option = serializer._options[name] || {}
    if option[:type].present?
      attrs[name] = option[:type]
    else
      # If no type is given, attempt to get it from the Active Model class
      if column = columns[name.to_s]
        attrs[name] = column.type
      else
        # Other wise default to string
        attrs[name] = "string"
      end
    end
  end

  associations = {}
  serializer._associations.each do |attr, association|
    #association = association_class.new(attr, self)

    if model_association = get_type(association)
      # Real association.
      association_class_name = association.options[:class_name]
      key = association.key.gsub(/_id/,'')
      associations[key] = { model_association => association_class_name || key }
      associations[key][:async] = (association.options[:async] || false) if association.options.has_key? :async
      associations[key][:polymorphic] = (association.options[:polymorphic] || false) if association.options.has_key? :polymorphic
    else
      # Computed association. We could infer has_many vs. has_one from
      # the association class, but that would make it different from
      # real associations, which read has_one vs. belongs_to from the
      # model.
      associations[association.key] = nil
    end
  end

  return { :attributes => attrs, :associations => associations }
end

#serializersObject



8
9
10
# File 'lib/ember/schema/active_model.rb', line 8

def serializers
  ::ActiveModel::Serializer.descendants.sort_by(&:final_name)
end

#superclassObject



4
5
6
# File 'lib/ember/schema/active_model.rb', line 4

def superclass
  ::ActiveModel::Serializer
end