lib/adauth/rails/model_bridge.rb in adauth-2.0.0pre2 vs lib/adauth/rails/model_bridge.rb in adauth-2.0.0
- old
+ new
@@ -26,28 +26,29 @@
module ModelBridge
# Registers the class methods when ModelBridge is included
def self.included(base)
base.extend ClassMethods
end
-
+
# Uses AdauthMappings to update the values on the model using the ones from Adauth
def update_from_adauth(adauth_model)
self.class::AdauthMappings.each do |k, v|
setter = "#{k.to_s}=".to_sym
- value = v.is_a?(Array) ? v.join(", ") : v
+ value = v.is_a?(Array) ? v.join(", ") : v
self.send(setter, adauth_model.send(value))
end
self.save
+ self
end
-
+
# Class Methods for ModelBridge
module ClassMethods
# Creates a new RailsModel from the adauth_model
def create_from_adauth(adauth_model)
rails_model = self.new
rails_model.update_from_adauth(adauth_model)
end
-
+
# Used to create the RailsModel if it doesn't exist and update it if it does
def return_and_create_from_adauth(adauth_model)
find_method = "find_by_#{self::AdauthSearchField.last}".to_sym
rails_model = (self.send(find_method, adauth_model.send(self::AdauthSearchField.first)) || create_from_adauth(adauth_model))
rails_model.update_from_adauth(adauth_model)
\ No newline at end of file