ninsho 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -123,7 +123,10 @@ en:
123
123
 
124
124
  ### Changelog
125
125
 
126
- * Current gem version 0.0.1
126
+ * Current gem version 0.0.2
127
+ * Add more documentation on code
128
+ * Add aouth token for Facebook friends
129
+ * Released gem version 0.0.1
127
130
 
128
131
  ### Devs
129
132
 
@@ -135,8 +138,7 @@ en:
135
138
  * Add tests
136
139
  * Support for Mongoid
137
140
  * Add handy helpers
138
- * Add aouth token for Facebook friends
139
- * Add more documentation on code
141
+
140
142
 
141
143
  ## Credits
142
144
  Icalia Labs - weare@icalialabs.com
@@ -146,7 +148,17 @@ Icalia Labs - weare@icalialabs.com
146
148
 
147
149
  [Like us on Facebook](https://www.facebook.com/icalialab "Like us on Facebook")
148
150
 
151
+ ### Special thanks
152
+
153
+ Inspired by the plataformatec guys, and pieces of code based on you! Thanks!
154
+
155
+ https://github.com/plataformatec/
149
156
 
150
157
  ### License
151
158
 
152
159
  MIT License. Copyright 2012-2013 IcaliaLabs. http://icalialabs.com
160
+
161
+
162
+
163
+
164
+
@@ -4,6 +4,7 @@ class Ninsho::SessionsController < NinshoController
4
4
  @providers = Ninsho.providers
5
5
  end
6
6
 
7
+ # Handles the omniauth record creation
7
8
  def create
8
9
  resource = build_resource_from_omniauth
9
10
  if resource.authenticated?
@@ -14,6 +14,7 @@ class NinshoController < Ninsho.parent_controller.constantize
14
14
  instance_variable_get(:"@#{resource_name}")
15
15
  end
16
16
 
17
+ # Omniauth hash for creating the records
17
18
  def resource_params
18
19
  env['omniauth.auth']
19
20
  end
@@ -23,6 +24,8 @@ class NinshoController < Ninsho.parent_controller.constantize
23
24
  Ninsho.resource_name #authentication
24
25
  end
25
26
 
27
+ # Class name for the ninsho model
28
+ # commonly Authentication
26
29
  def resource_class
27
30
  Ninsho.resource_class
28
31
  end
@@ -32,13 +35,12 @@ class NinshoController < Ninsho.parent_controller.constantize
32
35
  instance_variable_set(:"@#{resource_name}", new_resource)
33
36
  end
34
37
 
35
- # Parent resource
38
+ # Parent resource, commonly user
36
39
  def parent_resource
37
40
  resource_class.reflect_on_all_associations(:belongs_to).first.name.to_s
38
41
  end
39
42
 
40
- # Build a ninsho resource.
41
- # Assignment bypasses attribute protection when :unsafe option is passed
43
+ # Build a ninsho resource, from the omniauth hash
42
44
  def build_resource_from_omniauth
43
45
  self.resource = resource_class.from_omniauth(resource_params)
44
46
  end
@@ -40,10 +40,11 @@ CONTENT
40
40
 
41
41
  def migration_data
42
42
  <<RUBY
43
- ## Database authentications
43
+ ## Ninsho model fields
44
44
  t.integer :user_id
45
45
  t.string :provider
46
46
  t.string :uid
47
+ t.string :oauth_token
47
48
  RUBY
48
49
  end
49
50
  end
@@ -2,7 +2,6 @@ module Ninsho
2
2
  module Generators
3
3
  # Include this module in your generator to generate Ninsho views.
4
4
  # `copy_views` is the main method and by default copies all views
5
- # with forms.
6
5
  module ViewPathTemplates #:nodoc:
7
6
  extend ActiveSupport::Concern
8
7
 
@@ -6,5 +6,9 @@ Hey you, thanks for installing Ninsho, here are some tips:
6
6
 
7
7
  rails g ninsho:views
8
8
 
9
+ 2. Having trouble?, Check out the documentation at:
10
+
11
+ https://github.com/IcaliaLabs/ninsho
12
+
9
13
  ===============================================================================
10
14
 
@@ -12,29 +12,46 @@ module Ninsho
12
12
  autoload :Helpers, 'ninsho/controllers/helpers'
13
13
  end
14
14
 
15
+ # The parent controller all Ninsho controllers inherits from.
16
+ # The Default is set to ApplicationController.
15
17
  mattr_accessor :parent_controller
16
18
  @@parent_controller = "ApplicationController"
17
19
 
20
+ # The name class of the resource generate by the Ninsho model generator
21
+ # Comonly Authentication
18
22
  mattr_accessor :resource_class
19
23
  @@resource_class = ""
20
24
 
25
+ # The resource name singularized and downcased
26
+ # Used as a proxy to map ninsho resource
27
+ # Used in routes on ninsho_on method
21
28
  mattr_accessor :resource_name
22
29
  @@resource_name = ""
23
30
 
31
+ # List all providers added in the initializer
32
+ # All of them are symbols
33
+ # [ :facebook, :twitter, :github ]
24
34
  mattr_reader :providers
25
35
  @@providers = []
26
36
 
37
+ # The class name for the resource relation
38
+ # Is commonly the class User
27
39
  mattr_accessor :parent_resource_name
28
40
  @@parent_resource_name = ''
29
41
 
30
-
42
+ # Hash which contains omniauth configurations
31
43
  mattr_reader :omniauth_configs
32
44
  @@omniauth_configs = ActiveSupport::OrderedHash.new
33
45
 
46
+ # Default setup for Ninsho.
47
+ # Run the rails g ninsho:install to create a fresh initializer
34
48
  def self.setup
35
49
  yield self
36
50
  end
37
51
 
52
+ # It is used toe get the resource class and map it with
53
+ # ActiveSupport to get the class name
54
+ # :user will become User
38
55
  class Getter
39
56
  def initialize name
40
57
  @name = name
@@ -54,6 +71,10 @@ module Ninsho
54
71
  end
55
72
  end
56
73
 
74
+ # Used to specify an omniauth provider
75
+ #
76
+ # config.omniauth :facebook, 'APPD_ID', 'APP_SECRET'
77
+ #
57
78
  def self.omniauth(provider, *args)
58
79
  config = Ninsho::OmniAuth::Config.new(provider, args)
59
80
  @@providers << config.strategy_name.to_sym
@@ -1,11 +1,14 @@
1
1
  module Ninsho
2
+ #
3
+ # Responsible for manage the authentication process with the
4
+ # omniauth hash
5
+ #
2
6
  class Authentication
3
- PARENT_RESOURCE_NAME = Ninsho.parent_resource_name.to_s.downcase
4
-
5
7
  def initialize(omniauth = nil)
6
8
  @omniauth = omniauth
7
9
  @provider = omniauth['provider']
8
10
  @uid = omniauth['uid']
11
+ @oauth_token = @omniauth.credentials.token
9
12
  @email = omniauth['info']['email']
10
13
  end
11
14
 
@@ -13,24 +16,27 @@ module Ninsho
13
16
  user.present?
14
17
  end
15
18
 
19
+ # Little method to check if the record is find by the provider and uid
16
20
  def from_oauth
17
- Ninsho.resource_class.find_by_provider_and_uid(@provider, @uid)
21
+ Ninsho.resource_class.where(@omniauth.slice(:provider, :uid)).first_or_initialize.tap do |resource|
22
+ resource.provider = @provider
23
+ resource.uid = @uid
24
+ resource.oauth_token = @oauth_token
25
+ resource.save if resource.respond_to?(Ninsho.parent_resource_name.to_s.downcase.to_sym) && !resource.new_record?
26
+ end
18
27
  end
19
28
 
29
+ # Method to create an authentication record when user is find,
30
+ # otherwise creates a user with the authentication
20
31
  def from_user
21
32
  user = Ninsho.parent_resource_name.send :find_by_email, @email
22
- if user
23
- user.send("#{Ninsho.resource_name.pluralize}").build(provider: @provider, uid: @uid)
24
- user.send(:save)
25
- user
26
- else
27
- user = Ninsho.parent_resource_name.send :new, { email: @email }
28
- user.send("#{Ninsho.resource_name.pluralize}").build(provider: @provider, uid: @uid)
29
- user.send(:save)
30
- user
31
- end
33
+ user = Ninsho.parent_resource_name.send :new, { email: @email } unless user
34
+ user.send("#{Ninsho.resource_name.pluralize}").build(provider: @provider, uid: @uid, oauth_token: @oauth_token)
35
+ user.send(:save)
36
+ user
32
37
  end
33
38
 
39
+ # Check if a parent record is returned
34
40
  def user
35
41
  from_oauth.try(Ninsho.parent_resource_name.to_s.downcase.to_sym) || from_user
36
42
  end
@@ -3,40 +3,49 @@ module Ninsho
3
3
  module Helpers
4
4
  extend ActiveSupport::Concern
5
5
 
6
- RESOURCE_NAME = Ninsho.resource_name.singularize
7
- PARENT_RESOURCE_NAME = Ninsho.parent_resource_name.to_s.downcase
8
-
9
- def store_location
10
- session[:return_to] = request.fullpath
11
- end
12
-
13
- def clear_return_to
14
- session[:return_to] = nil
15
- end
16
-
17
- def redirect_back_or(default)
18
- redirect_to(session[:return_to] || default )
19
- clear_return_to
20
- end
21
-
22
- def after_sign_in_path_for
23
- redirect_back_or redirect_to_root
24
- end
25
-
26
- def sign_out
27
- session["#{PARENT_RESOURCE_NAME}_id".to_sym] = nil
28
- end
29
-
30
- def sign_in_and_redirect(parent_id, path=nil)
31
- session["#{PARENT_RESOURCE_NAME}_id".to_sym] = parent_id
32
- redirect_to path
33
- end
34
-
35
- def sign_in(parent_id)
36
- session["#{PARENT_RESOURCE_NAME}_id".to_sym] = parent_id
37
- end
38
-
39
- class_eval <<-METHODS, __FILE__, __LINE__ + 1
6
+ RESOURCE_NAME = Ninsho.resource_name.singularize
7
+ PARENT_RESOURCE_NAME = Ninsho.parent_resource_name.to_s.downcase
8
+
9
+ # Stores the location for better redirection
10
+ def store_location
11
+ session[:return_to] = request.fullpath
12
+ end
13
+
14
+ # Clears the session after redirection is done
15
+ def clear_return_to
16
+ session[:return_to] = nil
17
+ end
18
+
19
+ # Method which handles the redirection
20
+ def redirect_back_or(default)
21
+ redirect_to(session[:return_to] || default )
22
+ clear_return_to
23
+ end
24
+
25
+ # Destroy the user session
26
+ def sign_out
27
+ session["#{PARENT_RESOURCE_NAME}_id".to_sym] = nil
28
+ end
29
+
30
+ def sign_in_and_redirect(parent_id, path=nil)
31
+ session["#{PARENT_RESOURCE_NAME}_id".to_sym] = parent_id
32
+ redirect_to path
33
+ end
34
+
35
+ # Set the session for the authenticated user
36
+ def sign_in(parent_id)
37
+ session["#{PARENT_RESOURCE_NAME}_id".to_sym] = parent_id
38
+ end
39
+
40
+ # There be monsters!
41
+ # Creates several methods according to the parent resource
42
+ #
43
+ # authenticate_user!
44
+ #
45
+ # user_signed_in?
46
+ #
47
+ # current_user
48
+ class_eval <<-METHODS, __FILE__, __LINE__ + 1
40
49
  def current_#{PARENT_RESOURCE_NAME}
41
50
  @current_#{PARENT_RESOURCE_NAME} ||= #{Ninsho.parent_resource_name}.find(session[:#{PARENT_RESOURCE_NAME}_id]) if session[:#{PARENT_RESOURCE_NAME}_id]
42
51
  end
@@ -49,31 +58,32 @@ module Ninsho
49
58
  deny_access unless #{PARENT_RESOURCE_NAME}_signed_in?
50
59
  end
51
60
 
52
- METHODS
53
-
54
- define_method "link_#{RESOURCE_NAME}_with" do |provider|
55
- link_to "Connect with #{provider.to_s.capitalize}", "auth/#{provider.to_s}"
56
- end
57
-
61
+ METHODS
58
62
 
59
- # Method used by sessions controller to sign out a user.
60
- # You can overwrite it in your ApplicationController
61
- #
62
- # By default it is the root_path.
63
- def redirect_on_sign_out_path
64
- redirect_to_root
65
- end
63
+ define_method "link_#{RESOURCE_NAME}_with" do |provider|
64
+ link_to "Connect with #{provider.to_s.capitalize}", "auth/#{provider.to_s}"
65
+ end
66
66
 
67
- def redirect_on_sign_in_path
68
- redirect_to_root
69
- end
70
- def redirect_to_root
71
- redirect_to respond_to?(:root_path) ? root_path : "/"
72
- end
73
67
 
74
- def deny_access
75
- redirect_to_root
76
- end
68
+ # Method used by sessions controller to sign out a user.
69
+ # You can overwrite it in your ApplicationController
70
+ #
71
+ # By default it is the root_path.
72
+ def redirect_on_sign_out_path
73
+ redirect_to_root
74
+ end
75
+
76
+ def redirect_on_sign_in_path
77
+ redirect_to_root
78
+ end
79
+ def redirect_to_root
80
+ redirect_to respond_to?(:root_path) ? root_path : "/"
81
+ end
82
+
83
+ # Redirection if user has no right to enter
84
+ def deny_access
85
+ redirect_to_root
86
+ end
77
87
  end
78
88
  end
79
89
  end
@@ -20,7 +20,9 @@ module Ninsho
20
20
  Ninsho.parent_resource_name = Ninsho.ref(associated_model.to_s.classify).get
21
21
  end
22
22
  end
23
-
23
+
24
+ # Responsible for creating or find the record with the
25
+ # omniauth hash
24
26
  def from_omniauth(omniauth = nil)
25
27
  Ninsho::Authentication.new(omniauth)
26
28
  end
@@ -1,3 +1,4 @@
1
+ # Thanks to plataformatec for providing this
1
2
  begin
2
3
  require "omniauth"
3
4
  require 'omniauth/version'
@@ -7,6 +7,8 @@ module Ninsho
7
7
  end
8
8
  end
9
9
 
10
+ # Responsible for setting upt the configuration for omniauth
11
+ # providers and mount it into ninsho
10
12
  class Config
11
13
  attr_accessor :strategy
12
14
  attr_reader :args, :options, :provider, :strategy_name
@@ -7,10 +7,13 @@ module Ninsho
7
7
  # Force routes to be loaded if we are doing any eager load.
8
8
  config.before_eager_load { |app| app.reload_routes! }
9
9
 
10
+ # Loads then Ninsho Helpers into the Ninsho Controllers
11
+ # for the ones whicn inherits from have access
10
12
  ActiveSupport.on_load(:action_controller) do
11
13
  include Ninsho::Controllers::Helpers
12
14
  end
13
15
 
16
+ # Setup for omniauth strategies when initializer is run
14
17
  initializer "ninsho.omniauth" do |app|
15
18
  Ninsho.omniauth_configs.each do |provider, config|
16
19
  app.middleware.use config.strategy_class, *config.args do |strategy|
@@ -1,6 +1,5 @@
1
-
2
1
  module ActionDispatch::Routing
3
-
2
+
4
3
 
5
4
  class Mapper
6
5
  #Includes the ninsho_on method for routes. This method is responsible
@@ -20,22 +19,21 @@ module ActionDispatch::Routing
20
19
  # authentication_session_path POST /authentication/:provider/callback { controller: 'ninsho/sessions', action: 'create' }
21
20
  # destroy_authentication_session_path DELETE /sign_out { controller: 'ninsho/sessions', action: 'destroy' }
22
21
  #
22
+ def ninsho_on(resources_name)
23
+ drawer = Ninsho::RoutesDrawer.new resources_name
24
+ Ninsho.resource_class = drawer.to
25
+ Ninsho.resource_name = drawer.resource
26
+ ninsho_session(drawer.singular_name)
27
+ end
23
28
 
24
- def ninsho_on(resources_name)
25
- drawer = Ninsho::RoutesDrawer.new resources_name
26
- Ninsho.resource_class = drawer.to
27
- Ninsho.resource_name = drawer.resource
28
- ninsho_session(drawer.singular_name)
29
- end
30
-
31
- protected
29
+ protected
32
30
 
33
- def ninsho_session(name) #:nodoc:
34
- resource :session, :only => [], :controller => 'ninsho/sessions', :path => "" do
35
- get :new, :path => 'sign_in', as: "new_#{name}"
36
- match :create, path: "auth/:provider/callback", as: "#{name}"
37
- delete :destroy, path: 'sign_out', as: "destroy_#{name}"
38
- end
39
- end
31
+ def ninsho_session(name) #:nodoc:
32
+ resource :session, :only => [], :controller => 'ninsho/sessions', :path => "" do
33
+ get :new, :path => 'sign_in', as: "new_#{name}"
34
+ match :create, path: "auth/:provider/callback", as: "#{name}"
35
+ delete :destroy, path: 'sign_out', as: "destroy_#{name}"
36
+ end
37
+ end
40
38
  end
41
39
  end
@@ -1,4 +1,7 @@
1
1
  module Ninsho
2
+ # Responsible for loading and mounting the Ninsho::Interface method on
3
+ # ActiveRecord, which allows it to add the belongs_to_ninsho method
4
+ # inside models
2
5
  class Railtie < Rails::Railtie
3
6
  ActiveSupport.on_load :active_record do
4
7
  ActiveRecord::Base.send :include, Ninsho::Interface
@@ -3,6 +3,8 @@ module Ninsho
3
3
  # The required value in ninsho_on is actually not used internally, but it's
4
4
  # inflected to find all other values.
5
5
  #
6
+ # routes_drawer.to #=> Authentication
7
+ #
6
8
  class RoutesDrawer #:nodoc:
7
9
  attr_reader :singular_name, :klass, :resource
8
10
 
@@ -1,3 +1,3 @@
1
1
  module Ninsho
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.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.0.1
4
+ version: 0.0.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-03-13 00:00:00.000000000 Z
12
+ date: 2013-03-15 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: orm_adapter