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
17
18
# 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

Instance Attribute Details

#column_namesObject (readonly)

Returns the value of attribute column_names.



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

def column_names
  @column_names
end

#modelObject (readonly)

Returns the value of attribute model.



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

def model
  @model
end

#relationObject (readonly)

Returns the value of attribute relation.



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

def relation
  @relation
end

#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



20
21
22
23
# 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) ⇒ Object



25
26
27
28
29
30
31
32
33
# 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 = {}) ⇒ Object



35
36
37
38
# 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 = {}) ⇒ Object



40
41
42
43
44
45
46
47
48
49
# 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) ⇒ Object



51
52
53
54
55
56
57
# 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