Class: Trusty::Omniauth::ModelMapper
- Inherits:
-
Object
- Object
- Trusty::Omniauth::ModelMapper
- Includes:
- MappingHelpers
- Defined in:
- lib/trusty/omniauth/model_mapper.rb
Instance Attribute Summary collapse
-
#required_criteria ⇒ Object
readonly
Returns the value of attribute required_criteria.
-
#unique_identifiers ⇒ Object
readonly
Returns the value of attribute unique_identifiers.
Instance Method Summary collapse
- #attribute_names ⇒ Object
- #attributes(*filter_attribute_names) ⇒ Object
- #build_record(additional_attributes = {}) ⇒ Object
- #column_names ⇒ Object
- #find_records(additional_criteria = {}) ⇒ Object
-
#initialize(provider, options = {}) ⇒ ModelMapper
constructor
A new instance of ModelMapper.
- #model ⇒ Object
- #update_record!(record) ⇒ Object
Methods included from MappingHelpers
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, = {}) @provider = provider @options = @unique_identifiers = @options.fetch(:unique_identifiers, []).map(&:to_s) @required_criteria = stringify_keys @options.fetch(:required_criteria, {}) end |
Instance Attribute Details
#required_criteria ⇒ Object (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_identifiers ⇒ Object (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_names ⇒ Object
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_names ⇒ Object
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 |
#model ⇒ Object
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 |