Module: DataMapper::ExtJS::Model

Defined in:
lib/dm-extjs/model.rb

Instance Method Summary collapse

Instance Method Details

#ext_meta(relationships = nil) ⇒ Object

generates the meta description of a resource TODO: idProperty is set to the first primary key, in instances where you have multiple keys this would be wrong

pass array of relationships (as in to_json(:methods => RELATIONSHIPS)) so as to squash provide meta mapping for these related models, this will squash those properties on top of the base model



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/dm-extjs/model.rb', line 13

def ext_meta (relationships=nil)
  relationships ||= []
  ret = {
    :fields => properties.collect {|p| property_description(p)},
    :idProperty => properties.key.first.name
  }
  
  # map related properties to meta description
  # TODO: cleanup needed
  # because "relationships" are actually all declared :methods, this will
  # try to build meta descriptions for both methods on a model, and
  # relationships on a resource, here we attempt to treat the method as a
  # relationship, then assume it's a model level method with a return type
  # of string 
  relationships.each do |r|
    begin
      __send__(r.to_sym).model.properties.each do |p|
        prop = self.property_description(p)
        prop[:mapping] = "#{r}.#{prop[:name]}"
        prop[:name] = "#{r}__#{prop[:name]}"
        ret[:fields].push(prop)
      end
    rescue
      ret[:fields].push({
        :name => r,
        :type => 'string'
      })
    end
    
    
  end
  
  EXT_META.merge(ret)
end