Class: Trusty::Omniauth::ModelMapper

Inherits:
Object
  • Object
show all
Includes:
MappingHelpers
Defined in:
lib/trusty/omniauth/model_mapper.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from MappingHelpers

#clean

Constructor Details

#initialize(provider, options = {}) ⇒ ModelMapper

Returns a new instance of ModelMapper.



10
11
12
13
14
15
16
# File 'lib/trusty/omniauth/model_mapper.rb', line 10

def initialize(provider, options = {})
  @provider = provider
  @options  = options

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

Instance Attribute Details

#required_criteriaObject (readonly)

Returns the value of attribute required_criteria.



8
9
10
# File 'lib/trusty/omniauth/model_mapper.rb', line 8

def required_criteria
  @required_criteria
end

#unique_identifiersObject (readonly)

Returns the value of attribute unique_identifiers.



8
9
10
# File 'lib/trusty/omniauth/model_mapper.rb', line 8

def unique_identifiers
  @unique_identifiers
end

Instance Method Details

#attribute_namesObject



22
23
24
25
# File 'lib/trusty/omniauth/model_mapper.rb', line 22

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) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/trusty/omniauth/model_mapper.rb', line 27

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 = {}) ⇒ Object



37
38
39
# File 'lib/trusty/omniauth/model_mapper.rb', line 37

def build_record(additional_attributes = {})
  model.new(attributes.merge(required_criteria).merge(additional_attributes), without_protection: true)
end

#column_namesObject



55
56
57
58
59
60
61
62
63
# File 'lib/trusty/omniauth/model_mapper.rb', line 55

def column_names
  @column_names ||= if model.respond_to? :column_names
    model.column_names.map(&:to_sym)
  elsif model.respond_to? :fields
    model.fields.map{|c| c[1].name.to_sym}
  else
    []
  end
end

#find_records(additional_criteria = {}) ⇒ Object



41
42
43
44
45
# File 'lib/trusty/omniauth/model_mapper.rb', line 41

def find_records(additional_criteria = {})
  conditions = model.where( attributes(*unique_identifiers) )
  conditions = conditions.where(additional_criteria) unless additional_criteria.empty?
  conditions.where(required_criteria)
end

#modelObject



18
19
20
# File 'lib/trusty/omniauth/model_mapper.rb', line 18

def model
  @options[:model]
end

#update_record!(record) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/trusty/omniauth/model_mapper.rb', line 47

def update_record!(record)
  if Rails::VERSION::MAJOR >= 4
    record.update_attributes!(attributes)
  else
    record.update_attributes!(attributes, without_protection: true)
  end
end