trusty 0.2.2 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 59c014a94c8849d629bc952a1b2ff92501fba0ee
4
- data.tar.gz: 780f15a4e4cb6edc59d50f7da54effb81f994e72
3
+ metadata.gz: a1ea62ba4eb9e305e64fb08259662e965019c41c
4
+ data.tar.gz: 18f69b12dccb7c65835b4330617866fb3aaeeb22
5
5
  SHA512:
6
- metadata.gz: 897b8770ae13243c1be08704619fb6ba8f90e56827d3823ff153140f8b011ef1e3ca36f99bc1b8ba7ed01ee76a8a8ba7dba89ad2e64736164b25aa97d7a03538
7
- data.tar.gz: 591cf24333b58f913babbd3473d9a5fa3de3b1b3311dc5d84c7ea8468a0d7c01480ff918b6490fc84e8643992b19fd68bf39039947960c1d32d1abf69b890325
6
+ metadata.gz: 349d5f579b16c8033a9d0b17a7db317f3d9918246a487618cc3c95ea4201018837a901180fecad9cabc4bb9f09dbf23e3829e9f3b95b11ca4588058d4d3834d3
7
+ data.tar.gz: f3e7f8332d13f5e807b5704d2c8f24bf28e278bed24da1603dc22b5b22563b756faf66ba0a7d33f36161fef12a8128ec4074378fe623d967111389809912faa4
@@ -5,20 +5,18 @@ module Trusty
5
5
  class ModelMapper
6
6
  include MappingHelpers
7
7
 
8
- attr_reader :unique_identifiers, :required_criteria
8
+ attr_reader :model, :relation, :column_names, :unique_identifiers, :required_criteria
9
9
 
10
10
  def initialize(provider, options = {})
11
11
  @provider = provider
12
- @options = options
12
+ @options = options.dup
13
+
14
+ @model, @relation, @column_names = extract_orm_components(@options)
13
15
 
14
16
  @unique_identifiers = @options.fetch(:unique_identifiers, []).map(&:to_s)
15
17
  @required_criteria = stringify_keys @options.fetch(:required_criteria, {})
16
18
  end
17
19
 
18
- def model
19
- @options[:model]
20
- end
21
-
22
20
  def attribute_names
23
21
  # Remove required_criteria so that existing attributes are skipped
24
22
  @attribute_names ||= (@options[:attribute_names] || column_names).map(&:to_s) - required_criteria.keys
@@ -34,8 +32,9 @@ module Trusty
34
32
  end
35
33
  end
36
34
 
37
- def build_record(additional_attributes = {})
38
- model.new(attributes.merge(required_criteria).merge(additional_attributes), without_protection: true)
35
+ def build_record(additional_attributes = {}, options = {})
36
+ build_relation = (options[:relation] || relation)
37
+ build_relation.build(attributes.merge(required_criteria).merge(additional_attributes), without_protection: true)
39
38
  end
40
39
 
41
40
  def find_records(additional_criteria = {})
@@ -44,7 +43,7 @@ module Trusty
44
43
 
45
44
  raise "Missing unique attribute: #{empty_attributes.join(', ')}" if empty_attributes.any?
46
45
 
47
- conditions = model.where( unique_identifier_attributes )
46
+ conditions = relation.where( unique_identifier_attributes )
48
47
  conditions = conditions.where(additional_criteria) unless additional_criteria.empty?
49
48
  conditions.where(required_criteria)
50
49
  end
@@ -57,20 +56,48 @@ module Trusty
57
56
  end
58
57
  end
59
58
 
60
- def column_names
61
- @column_names ||= if model.respond_to? :column_names
62
- model.column_names.map(&:to_sym)
63
- elsif model.respond_to? :fields
64
- model.fields.map{|c| c[1].name.to_sym}
65
- else
66
- []
67
- end
59
+ protected
60
+
61
+ def extract_orm_components(options)
62
+ prepared_model = options[:model]
63
+ prepared_relation = options[:relation]
64
+
65
+ prepared_model ||= if prepared_relation.respond_to? :model
66
+ # ActiveRecord
67
+ prepared_relation.model
68
+ elsif prepared_relation.respond_to? :metadata
69
+ # Mongoid
70
+ prepared_relation.metadata.klass
71
+ end
72
+
73
+ prepared_relation ||= if prepared_model.respond_to? :default_scoped
74
+ # ActiveRecord
75
+ prepared_model.default_scoped
76
+ elsif prepared_model.respond_to? :default_scope
77
+ # Mongoid
78
+ prepared_model.default_scope
79
+ end
80
+
81
+ prepared_column_names = if prepared_model.respond_to? :column_names
82
+ # ActiveRecord
83
+ prepared_model.column_names.map(&:to_sym)
84
+ elsif prepared_model.respond_to? :attribute_names
85
+ # ActiveModel and Mongoid
86
+ prepared_model.attribute_names.map(&:to_sym)
87
+ elsif prepared_model.respond_to? :fields
88
+ # Older Mongoid
89
+ prepared_model.fields.map{|c| c[1].name.to_sym}
90
+ else
91
+ []
92
+ end
93
+
94
+ [prepared_model, prepared_relation, prepared_column_names]
95
+ end
96
+
97
+ def stringify_keys(original_hash)
98
+ original_hash.each_with_object({}){|(key, value), hash| hash[key.to_s] = value}
68
99
  end
69
100
 
70
- protected
71
- def stringify_keys(original_hash)
72
- original_hash.each_with_object({}){|(key, value), hash| hash[key.to_s] = value}
73
- end
74
101
  end
75
102
  end
76
103
  end
@@ -12,6 +12,7 @@ module Trusty
12
12
  # provider_attributes = OmniAuth data
13
13
  # options =
14
14
  # - :user_model = User model
15
+ # - :user_relation = Relation to create new User on (optional, to call #build method on)
15
16
  # - :user_attributes = Hash of attributes to merge into user_attributes
16
17
  # - :user_attributes_names = Array of attribute names to copy from Omniauth data (default: User.column_names)
17
18
  # - :user_required_criteria = Hash of criteria to use to find users, and also to merge into attributes
@@ -42,6 +43,7 @@ module Trusty
42
43
  )
43
44
  @provider_user = ModelMapper.new(self,
44
45
  :model => @options[:user_model] || ::User,
46
+ :relation => @options[:user_relation],
45
47
  :attributes => @options[:user_attributes],
46
48
  :attribute_names => @options[:user_attribute_names],
47
49
  :unique_identifiers => @options[:user_identifiers] || [:email],
@@ -99,23 +101,16 @@ module Trusty
99
101
 
100
102
  # USER
101
103
 
102
- def build_user(attributes = {})
103
- @provider_user.build_record(attributes)
104
+ # Option :relation - pass in relation to build Identity from
105
+ def build_user(attributes = {}, options = {})
106
+ @provider_user.build_record(attributes, options)
104
107
  end
105
108
 
106
109
  # IDENTITY
107
110
 
108
- def build_identity(attributes = {})
109
- @provider_identity.build_record(attributes)
110
- end
111
-
112
- def build_identity_for_user(user)
113
- # build_identity.tap do |identity|
114
- # # this assumes there is an inverse relationship automatically created
115
- # # such as user.identities would now contain this identity for the user
116
- # identity.user = user
117
- # end
118
- build_identity(user: user)
111
+ # Option :relation - pass in relation to build Identity from
112
+ def build_identity(attributes = {}, options = {})
113
+ @provider_identity.build_record(attributes, options)
119
114
  end
120
115
 
121
116
  def update_identity!(identity)
@@ -1,3 +1,3 @@
1
1
  module Trusty
2
- VERSION = "0.2.2"
2
+ VERSION = "0.2.3"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: trusty
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joel Van Horn
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-04-18 00:00:00.000000000 Z
11
+ date: 2017-04-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler