Module: ActiveRecordExtension::ClassMethods

Defined in:
lib/active_record_extension.rb

Overview

add your static(class) methods here

Instance Method Summary collapse

Instance Method Details

#belongs_to_field(field) ⇒ Object



68
69
70
71
72
73
# File 'lib/active_record_extension.rb', line 68

def belongs_to_field(field)
  @belongs_to_fields ||= belongs_to_fields
  matching_fields = @belongs_to_fields.select{|f| f.foreign_key == field}
  raise "Multiple matching foreign_keys. Only unique foreign keys allowed [#{field}]" if matching_fields.count > 1
  matching_fields.first
end

#belongs_to_field?(field) ⇒ Boolean

Returns:

  • (Boolean)


63
64
65
66
# File 'lib/active_record_extension.rb', line 63

def belongs_to_field?(field)
  @belongs_to_fields ||= belongs_to_fields
  @belongs_to_fields.map(&:foreign_key).include?(field)
end

#belongs_to_field_by_name(name) ⇒ Object



75
76
77
78
# File 'lib/active_record_extension.rb', line 75

def belongs_to_field_by_name(name)
  @belongs_to_fields ||= belongs_to_fields
  @belongs_to_fields.select{|f| f.name.to_s == name}.first
end

#belongs_to_fieldsObject



80
81
82
# File 'lib/active_record_extension.rb', line 80

def belongs_to_fields
  reflect_on_all_associations(:belongs_to)
end

#many_to_many_associated_tablesObject

OData does not allow creating an entry for a table that is used in a many to many joining table. You must associate tables together. If a table uses this field, it indicates its a joining many to many table. An array of the 2 associated tables, eg [Table1, Table2]



96
97
98
# File 'lib/active_record_extension.rb', line 96

def many_to_many_associated_tables
  @many_to_many_associated_tables
end

#many_to_many_associated_tables=(value) ⇒ Object



100
101
102
# File 'lib/active_record_extension.rb', line 100

def many_to_many_associated_tables=(value)
  @many_to_many_associated_tables = value
end

#many_to_many_binding_nameObject

Binding name is found in the metadata xml file. It will look something like (system users - opportunities): <EntitySet Name=“systemusers” EntityType=“Microsoft.Dynamics.CRM.systemuser”>

<NavigationPropertyBinding Path="new_systemuser_opportunity" Target="opportunities"/>

Its different for both entities, so this is a hash of tablename to binding name



108
109
110
# File 'lib/active_record_extension.rb', line 108

def many_to_many_binding_name
  @many_to_many_binding_name
end

#many_to_many_binding_name=(value) ⇒ Object



112
113
114
# File 'lib/active_record_extension.rb', line 112

def many_to_many_binding_name=(value)
  @many_to_many_binding_name = value
end

#many_to_many_use_old_apiObject

For some associations the new api does not work, notably the systemuser. But the old api does work. Hopefully in future releases Microsoft will fix the new apis.



118
119
120
# File 'lib/active_record_extension.rb', line 118

def many_to_many_use_old_api
  @many_to_many_use_old_api ||= false
end

#many_to_many_use_old_api=(value) ⇒ Object



122
123
124
# File 'lib/active_record_extension.rb', line 122

def many_to_many_use_old_api=(value)
  @many_to_many_use_old_api = value
end

#odata_field(field, options) ⇒ Object

In some cases the odata field name is different than the database field name. This method is used for this mapping



127
128
129
130
# File 'lib/active_record_extension.rb', line 127

def odata_field(field, options)
  @odata_property_key ||= {}
  @odata_property_key[field] = options[:crm_key]
end

#odata_field_value(crm_key) ⇒ Object



132
133
134
135
# File 'lib/active_record_extension.rb', line 132

def odata_field_value(crm_key)
  @odata_property_key ||= {}
  @odata_property_key[crm_key]
end

#odata_table_referenceObject

If odata refers to table differently than table name when using associations, you can use this method



85
86
87
# File 'lib/active_record_extension.rb', line 85

def odata_table_reference
  @odata_table_reference
end

#odata_table_reference=(value) ⇒ Object



89
90
91
# File 'lib/active_record_extension.rb', line 89

def odata_table_reference=(value)
  @odata_table_reference = value
end