lib/adauth/rails/model_bridge.rb in adauth-2.0.4 vs lib/adauth/rails/model_bridge.rb in adauth-2.0.5
- old
+ new
@@ -18,11 +18,11 @@
#
# This is an array which contains 2 values and is used to find the objects record e.g.
#
# AdauthSearchField = [:login, :name]
#
- # This will cause RailsModel.find_by_name(AdauthObject.login)
+ # This will cause RailsModel.where(:name => AdauthObject.login).first_or_initialize
#
# The Order is [adauth_field, rails_field]
module ModelBridge
# Registers the class methods when ModelBridge is included
def self.included(base)
@@ -48,13 +48,16 @@
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))
+ adauth_field = self::AdauthSearchField.first
+ adauth_search_value = adauth_model.send(adauth_field)
+ rails_search_field = self::AdauthSearchField.second
+ # Model#where({}).first_or_initialize is also compatible with Mongoid (3.1.0+)
+ rails_model = self.send(:where, { rails_search_field => adauth_search_value }).first_or_initialize
rails_model.update_from_adauth(adauth_model)
- return rails_model
+ rails_model
end
end
end
end
end
\ No newline at end of file