Class: ActiveSync::Sync
- Inherits:
-
Object
- Object
- ActiveSync::Sync
- Defined in:
- app/models/active_sync/sync.rb
Constant Summary collapse
- @@model_descriptions =
Describes what of each model should be sent as the sync records, this is populated through calls to ‘sync’ in the model class
{}
- @@loaded =
false
Class Method Summary collapse
- .add_associations_to_description(model, association_names) ⇒ Object
- .add_attributes_to_description(model, attributes) ⇒ Object
- .associated_ids(record, model_association) ⇒ Object
- .association_record(model_association, record) ⇒ Object
- .configure_model_description(model, options) ⇒ Object
- .first_key(obj) ⇒ Object
- .get_model_association(model, association_name) ⇒ Object
- .is_sync_model?(model) ⇒ Boolean
- .model_descriptions ⇒ Object
-
.sync_record(record) ⇒ Object
Hash used in all general sync communication for a given model.
Class Method Details
.add_associations_to_description(model, association_names) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'app/models/active_sync/sync.rb', line 76 def self.add_associations_to_description model, association_names association_names.each do |association_name| association = model.reflect_on_all_associations.find{ |a| a.name == association_name } unless association.nil? begin @@model_descriptions[ model.name ][ :associations ] << { name: association.name.to_s, class: association.class_name, type: association.association_class.name } rescue NotImplementedError @@model_descriptions[ model.name ][ :associations ] << { name: association.name.to_s, class: association.class_name, type: 'ActiveRecord::Associations::HasManyThroughAssociation' } end else throw "Association #{ association_name } not found for #{ model.name }" end end end |
.add_attributes_to_description(model, attributes) ⇒ Object
70 71 72 73 74 |
# File 'app/models/active_sync/sync.rb', line 70 def self.add_attributes_to_description model, attributes attributes.each{ |attribute| @@model_descriptions[ model.name ][ :attributes ] << attribute.to_s } end |
.associated_ids(record, model_association) ⇒ Object
108 109 110 111 112 113 114 115 |
# File 'app/models/active_sync/sync.rb', line 108 def self.associated_ids record, model_association if defined? record.send( model_association[:name] ).pluck record.send( model_association[:name] ).pluck(:id) else record.send( model_association[:name] ).id end end |
.association_record(model_association, record) ⇒ Object
100 101 102 103 104 105 106 |
# File 'app/models/active_sync/sync.rb', line 100 def self.association_record model_association, record { IsReference: true, id: record.id, model_association[:name] => associated_ids( record, model_association ) } end |
.configure_model_description(model, options) ⇒ Object
31 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 |
# File 'app/models/active_sync/sync.rb', line 31 def self.configure_model_description model, @@model_descriptions[ model.name ] = { attributes: [], associations: [] } .each do | option | case when option == :all_attributes_and_associations add_attributes_to_description model, model.attribute_names add_associations_to_description model, model.reflect_on_all_associations.map( &:name ) when option == :all_attributes add_attributes_to_description model, model.attribute_names when first_key( option ) == :attributes add_attributes_to_description model, option[:attributes].map(&:to_s) when option == :all_associations add_associations_to_description model, model.reflect_on_all_associations.map( &:name ) when first_key( option ) == :associations add_associations_to_description model, option[ :associations ] else throw "Unknown sync option: #{option}" end end end |
.first_key(obj) ⇒ Object
121 122 123 |
# File 'app/models/active_sync/sync.rb', line 121 def self.first_key obj obj.respond_to?( :keys ) ? obj.keys.first : nil end |
.get_model_association(model, association_name) ⇒ Object
117 118 119 |
# File 'app/models/active_sync/sync.rb', line 117 def self.get_model_association model, association_name @@model_descriptions[ model.name ][:associations].find{ |a| a[:name] == association_name } end |
.is_sync_model?(model) ⇒ Boolean
14 15 16 17 18 19 20 |
# File 'app/models/active_sync/sync.rb', line 14 def self.is_sync_model? model model_name = model.class == String ? model : model.name model_descriptions.keys.include? model_name end |
.model_descriptions ⇒ Object
9 10 11 12 |
# File 'app/models/active_sync/sync.rb', line 9 def self.model_descriptions ( Rails.application.eager_load! && @@loaded = true ) unless Rails.application.config.cache_classes || @@loaded @@model_descriptions end |
.sync_record(record) ⇒ Object
Hash used in all general sync communication for a given model.
24 25 26 27 28 29 |
# File 'app/models/active_sync/sync.rb', line 24 def self.sync_record record @@model_descriptions[ record.class.name ][ :attributes ].reduce( {} ) do | hash, attribute | hash[ attribute ] = record.send( attribute ) hash end end |