class Trusty::Omniauth::ModelMapper

Attributes

column_names[R]
model[R]
relation[R]
required_criteria[R]
unique_identifiers[R]

Public Class Methods

new(provider, options = {}) click to toggle source
# File lib/trusty/omniauth/model_mapper.rb, line 10
def initialize(provider, options = {})
  @provider = provider
  @options  = options.dup

  @model, @relation, @column_names = extract_orm_components(@options)

  @unique_identifiers = @options.fetch(:unique_identifiers, []).map(&:to_s)
  @required_criteria  = stringify_keys @options.fetch(:required_criteria, {})
end

Public Instance Methods

attribute_names() click to toggle source
# File lib/trusty/omniauth/model_mapper.rb, line 20
def attribute_names
  # Remove required_criteria so that existing attributes are skipped
  @attribute_names ||= (@options[:attribute_names] || column_names).map(&:to_s) - required_criteria.keys
end
attributes(*filter_attribute_names) click to toggle source
# File lib/trusty/omniauth/model_mapper.rb, line 25
def attributes(*filter_attribute_names)
  @attributes ||= @provider.attributes(*attribute_names).merge(stringify_keys @options[:attributes]).merge(required_criteria)

  if filter_attribute_names.any?
    @attributes.slice(*filter_attribute_names)
  else
    @attributes.dup
  end
end
build_record(additional_attributes = {}, options = {}) click to toggle source
# File lib/trusty/omniauth/model_mapper.rb, line 35
def build_record(additional_attributes = {}, options = {})
  build_relation = (options[:relation] || relation)
  build_relation.build(attributes.merge(required_criteria).merge(additional_attributes))
end
find_records(additional_criteria = {}) click to toggle source
# File lib/trusty/omniauth/model_mapper.rb, line 40
def find_records(additional_criteria = {})
  unique_identifier_attributes = attributes(*unique_identifiers)
  empty_attributes = unique_identifiers - unique_identifier_attributes.keys

  raise "Missing unique attribute: #{empty_attributes.join(', ')}" if empty_attributes.any?

  conditions = relation.where( unique_identifier_attributes )
  conditions = conditions.where(additional_criteria) unless additional_criteria.empty?
  conditions.where(required_criteria)
end
update_record!(record) click to toggle source
# File lib/trusty/omniauth/model_mapper.rb, line 51
def update_record!(record)
  if Rails::VERSION::MAJOR >= 4
    record.update_attributes!(attributes)
  else
    record.update_attributes!(attributes, without_protection: true)
  end
end

Protected Instance Methods

extract_orm_components(options) click to toggle source
# File lib/trusty/omniauth/model_mapper.rb, line 61
def extract_orm_components(options)
  prepared_model    = options[:model]
  prepared_relation = options[:relation]

  prepared_model ||=  if prepared_relation.respond_to? :model
                        # ActiveRecord
                        prepared_relation.model
                      elsif prepared_relation.respond_to? :metadata
                        # Mongoid
                        prepared_relation.metadata.klass
                      else
                        prepared_model
                      end

  prepared_relation ||= if prepared_model.respond_to? :default_scoped
                          # ActiveRecord
                          prepared_model.default_scoped
                        elsif prepared_model.respond_to? :default_scope
                          # Mongoid
                          prepared_model.default_scope
                        else
                          prepared_relation
                        end

  prepared_column_names = if prepared_model.respond_to? :column_names
                            # ActiveRecord
                            prepared_model.column_names.map(&:to_sym)
                          elsif prepared_model.respond_to? :attribute_names
                            # ActiveModel and Mongoid
                            prepared_model.attribute_names.map(&:to_sym)
                          elsif prepared_model.respond_to? :fields
                            # Older Mongoid
                            prepared_model.fields.map{|c| c[1].name.to_sym}
                          else
                            []
                          end

  [prepared_model, prepared_relation, prepared_column_names]
end
stringify_keys(original_hash) click to toggle source
# File lib/trusty/omniauth/model_mapper.rb, line 101
def stringify_keys(original_hash)
  original_hash.each_with_object({}){|(key, value), hash| hash[key.to_s] = value}
end