ninsho 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -158,12 +158,16 @@ en:
158
158
 
159
159
  ### Changelog
160
160
 
161
- * Current gem version 0.1.0
161
+ * Current gem version 0.1.2
162
+ * Add hold_attributes to authentication relation
163
+ * Add default User as parent resource name
162
164
  * Add more documentation on code
163
165
  * Add aouth token for Facebook friends
164
166
  * Add more flexibility to handle authentications and save to multiple fields
165
167
  * Add handy helpers
166
168
  * Add rails live example
169
+ * Released gem version 0.1.1
170
+ * Released gem version 0.1.0
167
171
  * Released gem version 0.0.3
168
172
  * Released gem version 0.0.2
169
173
  * Released gem version 0.0.1
@@ -42,6 +42,10 @@ module Ninsho
42
42
  # Hash which contains omniauth configurations
43
43
  mattr_reader :omniauth_configs
44
44
  @@omniauth_configs = ActiveSupport::OrderedHash.new
45
+
46
+ # Attributes to be updated to the parent resource class
47
+ mattr_accessor :parent_resource_holding_attributes
48
+ @@parent_resource_holding_attributes = []
45
49
 
46
50
  # Default setup for Ninsho.
47
51
  # Run the rails g ninsho:install to create a fresh initializer
@@ -18,7 +18,8 @@ module Ninsho
18
18
 
19
19
  # Little method to check if the record is find by the provider and uid
20
20
  def from_oauth
21
- Ninsho.resource_class.where(@omniauth.slice(:provider, :uid)).first_or_initialize.tap do |resource|
21
+ resource_class = Ninsho.resource_class.where(@omniauth.slice(:provider, :uid)).first_or_initialize
22
+ resource_class.tap do |resource|
22
23
  resource.provider = @provider
23
24
  resource.uid = @uid
24
25
  resource.oauth_token = @oauth_token
@@ -29,16 +30,39 @@ module Ninsho
29
30
  # Method to create an authentication record when user is find,
30
31
  # otherwise creates a user with the authentication
31
32
  def from_user
32
- user = Ninsho.parent_resource_name.send :find_by_email, @email
33
- user = Ninsho.parent_resource_name.send :new, { email: @email } unless user
33
+ attrs = { email: @email }
34
+ user = Ninsho.parent_resource_name.where(attrs).first_or_initialize
35
+ user.attributes = holding_attributes(attrs) if user.new_record?
34
36
  user.send("#{Ninsho.resource_name.pluralize}").build(provider: @provider, uid: @uid, oauth_token: @oauth_token)
35
- user.send(:save)
37
+ user.save
36
38
  user
37
39
  end
38
40
 
39
- # Check if a parent record is returned
41
+ # Check if a parent record is returned, commonly the User model
40
42
  def user
41
- from_oauth.try(Ninsho.parent_resource_name.to_s.downcase.to_sym) || from_user
43
+ from_oauth.try(Ninsho.parent_resource_name.to_s.downcase.to_sym) || from_user
44
+ end
45
+
46
+ #Holding user attributes
47
+ def holding_attributes(attrs = {})
48
+ Ninsho.parent_resource_holding_attributes.each do |attr|
49
+ attrs[attr] = nested_hash_value(@omniauth, attr)
50
+ end
51
+ attrs
52
+ end
53
+
54
+ private
55
+
56
+ # Recursive method to look for the key on the omniauth hash
57
+ # and returns the value
58
+ def nested_hash_value(obj,key)
59
+ if obj.respond_to?(:key?) && obj.key?(key)
60
+ obj[key]
61
+ elsif obj.respond_to?(:each)
62
+ r = nil
63
+ obj.find{ |*a| r=nested_hash_value(a.last,key) }
64
+ r
65
+ end
42
66
  end
43
67
  end
44
68
  end
@@ -16,15 +16,28 @@ module Ninsho
16
16
 
17
17
  def belongs_to_ninsho(*args)
18
18
  options = args.extract_options!
19
+ Ninsho.parent_resource_holding_attributes = options[:hold_attributes] || []
20
+ options.reject! { |k, v| k == :hold_attributes }
19
21
 
20
22
  associations = args.collect(&:to_s).collect(&:downcase)
21
23
  association_keys = associations.collect { |association| "#{association}_id" }
22
24
 
23
25
  #Set the belongs_to association by ActiveRecord
24
26
  associations.each do |associated_model|
25
- belongs_to associated_model.to_sym
27
+ belongs_to associated_model.to_sym, options
26
28
  Ninsho.parent_resource_name = Ninsho.ref(associated_model.to_s.classify).get
27
29
  end
30
+
31
+ ## Attributes delegation
32
+ Ninsho.parent_resource_holding_attributes.each do |attr|
33
+ delegate attr, to: Ninsho.parent_resource_name.to_s.downcase.to_sym
34
+
35
+ class_eval <<-RUBY
36
+ def #{attr}=(value)
37
+ self.#{Ninsho.parent_resource_name.to_s.downcase.to_sym}.#{attr} = value
38
+ end
39
+ RUBY
40
+ end
28
41
  end
29
42
 
30
43
  # Responsible for creating or find the record with the
@@ -1,3 +1,3 @@
1
1
  module Ninsho
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ninsho
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-05-21 00:00:00.000000000 Z
12
+ date: 2013-05-24 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: orm_adapter
@@ -117,7 +117,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
117
117
  version: '0'
118
118
  requirements: []
119
119
  rubyforge_project: ninsho
120
- rubygems_version: 1.8.24
120
+ rubygems_version: 1.8.25
121
121
  signing_key:
122
122
  specification_version: 3
123
123
  summary: Easy authentication with many providers