Module RubyAMF::Rails::Model
In: lib/rubyamf/rails/model.rb

Rails-specific implementation of RubyAMF::Model APIs

Methods

Included Modules

RubyAMF::Model

Public Instance methods

[Source]

     # File lib/rubyamf/rails/model.rb, line 75
 75:     def rubyamf_hash options=nil
 76:       return super(options) unless RubyAMF.configuration.check_for_associations
 77: 
 78:       options ||= {}
 79: 
 80:       # Iterate through assocations and check to see if they are loaded
 81:       auto_include = []
 82:       self.class.reflect_on_all_associations.each do |reflection|
 83:         next if reflection.macro == :belongs_to # Skip belongs_to to prevent recursion
 84:         is_loaded = if self.respond_to?(:association)
 85:           # Rails 3.1
 86:           self.association(reflection.name).loaded?
 87:         elsif self.respond_to?("loaded_#{reflection.name}?")
 88:           # Rails 2.3 and 3.0 for some types
 89:           self.send("loaded_#{reflection.name}?")
 90:         else
 91:           # Rails 2.3 and 3.0 for some types
 92:           self.send(reflection.name).loaded?
 93:         end
 94:         auto_include << reflection.name if is_loaded
 95:       end
 96: 
 97:       # Add these assocations to the :include if they are not already there
 98:       if include_associations = options.delete(:include)
 99:         if include_associations.is_a?(Hash)
100:           auto_include.each {|assoc| include_associations[assoc] ||= {}}
101:         else
102:           include_associations = Array.wrap(include_associations) | auto_include
103:         end
104:         options[:include] = include_associations
105:       else
106:         options[:include] = auto_include if auto_include.length > 0
107:       end
108: 
109:       super(options)
110:     end

[Source]

    # File lib/rubyamf/rails/model.rb, line 10
10:     def rubyamf_init props, dynamic_props = nil
11:       # Convert props and dynamic props to hash with string keys for attributes
12:       attrs = {}
13:       props.each {|k,v| attrs[k.to_s] = v}
14:       dynamic_props.each {|k,v| attrs[k.to_s] = v} unless dynamic_props.nil?
15: 
16:       # Is it a new record or existing? - support composite primary keys just in case
17:       is_new = true
18:       pk = Array.wrap(self.class.primary_key).map &:to_s
19:       if pk.length > 1 || pk[0] != 'id'
20:         unless pk.any? {|k| attrs[k].nil?}
21:           search = pk.map {|k| attrs[k]}
22:           search = search.first if search.length == 1
23:           is_new = !self.class.exists?(search) # Look it up in the database to make sure because it may be a string PK (or composite PK)
24:         end
25:       else
26:         is_new = false unless attrs['id'] == 0 || attrs['id'] == nil
27:       end
28: 
29:       # Get base attributes hash for later use
30:       base_attrs = self.send(:attributes_from_column_definition)
31: 
32:       if is_new
33:         # Call initialize to populate everything for a new object
34:         self.send(:initialize)
35: 
36:         # Populate part of given primary key
37:         pk.each do |k|
38:           self.send("#{k}=", attrs[k]) unless attrs[k].nil?
39:         end
40:       else
41:         # Initialize with defaults so that changed properties will be marked dirty
42:         pk_attrs = pk.inject({}) {|h, k| h[k] = attrs[k]; h}
43:         base_attrs.merge!(pk_attrs)
44: 
45:         if ::ActiveRecord::VERSION::MAJOR == 2
46:           # if rails 2, use code from ActiveRecord::Base#instantiate (just copied it over)
47:           object = self
48:           object.instance_variable_set("@attributes", base_attrs)
49:           object.instance_variable_set("@attributes_cache", Hash.new)
50: 
51:           if object.respond_to_without_attributes?(:after_find)
52:             object.send(:callback, :after_find)
53:           end
54: 
55:           if object.respond_to_without_attributes?(:after_initialize)
56:             object.send(:callback, :after_initialize)
57:           end
58:         else
59:           # if rails 3, use init_with('attributes' => attributes_hash)
60:           self.init_with('attributes' => base_attrs)
61:         end
62:       end
63: 
64:       # Delete pk from attrs as they have already been set
65:       pk.each {|k| attrs.delete(k)}
66: 
67:       # Set attributtes
68:       # warhammerkid: Should we be setting associations some other way (not attributes)?
69:       rubyamf_set_non_attributes attrs, base_attrs
70:       self.send(:attributes=, attrs)
71: 
72:       self
73:     end

[Source]

     # File lib/rubyamf/rails/model.rb, line 112
112:     def rubyamf_retrieve_association association
113:       case self.class.reflect_on_association(association).macro
114:       when :has_many, :has_and_belongs_to_many
115:         send(association).to_a
116:       when :has_one, :belongs_to
117:         send(association)
118:       end
119:     end

[Validate]