session-rails 0.1.0 → 0.1.1

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
  SHA256:
3
- metadata.gz: 30ca743cdb599238153f9c7ae803103f0fe79be9203a5e4016c48e76f543c162
4
- data.tar.gz: 9b43723e309bb9f6fc35e6ec2d625c2377b44f2f338af664b886bf3fbde838a2
3
+ metadata.gz: b3ed67024199af94ed5df5a7419dac5de4f6922895116ef14fd9a407c84a56c2
4
+ data.tar.gz: 2ee461cb43bb57668a1311b486bb55e4582cafdb0cc02ae3e9bed998ac304932
5
5
  SHA512:
6
- metadata.gz: 83019616f3288151a9c3bfdbd5be1bbad74059c7de890236b468b7cde2d1680c4d7b8cc12f24c1f6825ecfd0756f662da229637aed28a82439eeb348f9938300
7
- data.tar.gz: c1b59412debebb4f8d5532d3e1cd37eea4c9d577d901500519aaa8d1616192d16d08d0f7c4c2a14ed85e5a21a1dd748c1480f47f2efe0668ec91df897ded786c
6
+ metadata.gz: b2df0cea9205c4581c9141aef86ba6c25a75a42b54a7a4662af9ba756d4edaf0742ff6bf42d6b2c3fec5c782da7465d20792377b7f5ffd8c58ce52b754494961
7
+ data.tar.gz: 80b2d5b79fa6d8ed0e254d3d26b86137569db6dcda0396a2ee50b12fc107049a6cdc02b8b39869dd68ca41155ba70e066a8d71d7bcd97c4f1a09bf71d0184ca3
@@ -0,0 +1,81 @@
1
+ module Session
2
+ module Attributes
3
+ extend ActiveSupport::Concern
4
+
5
+ DEFAULT_MODEL_NAME = :user
6
+ DEFAULT_ID_ATTRIBUTE = :id
7
+ DEFAULT_PUBLIC_ATTRIBUTE = :email
8
+ DEFAULT_PRIVATE_ATTRIBUTE = :password
9
+
10
+ included do
11
+ attr_accessor :public_attribute, :private_attribute
12
+
13
+ def id
14
+ return nil if model.blank?
15
+ model.send self.class.id_attribute
16
+ end
17
+
18
+ def id=(v)
19
+ return self.model = nil if v.nil?
20
+ self.model = self.class.model.find_by self.class.id_attribute => v
21
+ end
22
+
23
+ def model
24
+ return nil if public_attribute.blank?
25
+ self.class.model.find_by self.class.public_attribute => public_attribute
26
+ end
27
+
28
+ def model=(v)
29
+ self.public_attribute = v.try self.class.public_attribute
30
+ end
31
+
32
+ authenticates
33
+ end
34
+
35
+ class_methods do
36
+ attr_reader :model_name, :id_attribute, :public_attribute, :private_attribute
37
+
38
+ def model
39
+ @model ||= model_name.to_s.classify.constantize
40
+ end
41
+
42
+ def authenticates(model_name = nil, by: nil)
43
+ alias_accessor :model, :model_name do
44
+ @model_name = (model_name.to_s.underscore.presence || DEFAULT_MODEL_NAME).to_sym
45
+ @model = nil
46
+ end
47
+
48
+ attributes = by&.symbolize_keys.presence || {}
49
+
50
+ alias_accessor(:id, :id_attribute) { @id_attribute = attributes[:id].to_s.to_sym.presence || DEFAULT_ID_ATTRIBUTE }
51
+ alias_accessor(:public_attribute) { @public_attribute = attributes[:public].to_s.to_sym.presence || DEFAULT_PUBLIC_ATTRIBUTE }
52
+ alias_accessor(:private_attribute) { @private_attribute = attributes[:private].to_s.to_sym.presence || DEFAULT_PRIVATE_ATTRIBUTE }
53
+ end
54
+
55
+ def columns
56
+ [
57
+ Struct.new(:name, :type).new(public_attribute, :string),
58
+ Struct.new(:name, :type).new(private_attribute, :string)
59
+ ]
60
+ end
61
+
62
+ protected
63
+
64
+ def alias_accessor(name, name_method = nil)
65
+ method_name = send name_method || name
66
+ if method_name.present? && method_name.to_s != name.to_s
67
+ remove_method method_name if respond_to? method_name
68
+ remove_method "#{method_name}=" if respond_to? "#{method_name}="
69
+ end
70
+
71
+ yield
72
+
73
+ method_name = send name_method || name
74
+ if method_name.to_s != name.to_s
75
+ alias_method method_name, name
76
+ alias_method "#{method_name}=", "#{name}="
77
+ end
78
+ end
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,11 @@
1
+ module Session
2
+ module Comparisons
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ def ==(session)
7
+ session.is_a?(self.class) && id.present? && id == session.id
8
+ end
9
+ end
10
+ end
11
+ end
@@ -1,3 +1,42 @@
1
1
  ActionController::Base.class_eval do
2
+ before_action :initialize_sessions
3
+ before_action :check_session_authentications
2
4
 
5
+ protected
6
+
7
+ def initialize_sessions
8
+ Session::Base.descendants.each { |session| session.store = self.session }
9
+ end
10
+
11
+ def check_session_authentications
12
+ self.class.authentication_requirements.each do |session|
13
+ redirect_to controller: session.to_s.tableize, action: :new and return unless session.current.exists?
14
+ end
15
+ end
16
+
17
+ def check_session_unauthentications
18
+ self.class.unauthentication_requirements.each do |session|
19
+ redirect_to root_path if session.current.exists?
20
+ end
21
+ end
22
+
23
+ class << self
24
+ def requires_authentication(from: :session)
25
+ (authentication_requirements << from.to_s.classify.constantize).uniq!
26
+ end
27
+
28
+ def requires_unauthentication(from: :session)
29
+ (unauthentication_requirements << from.to_s.classify.constantize).uniq!
30
+ end
31
+
32
+ protected
33
+
34
+ def authentication_requirements
35
+ @authentication_requirements ||= []
36
+ end
37
+
38
+ def unauthentication_requirements
39
+ @unauthentication_requirements ||= []
40
+ end
41
+ end
3
42
  end
@@ -3,5 +3,20 @@ require "session/rails/railtie"
3
3
  module Session
4
4
  module Rails
5
5
  require __dir__ + '/engine'
6
+ require __dir__ + '/core_ext'
7
+
8
+ require __dir__ + '/validations'
9
+ require __dir__ + '/attributes'
10
+ require __dir__ + '/storage'
11
+ require __dir__ + '/comparisons'
12
+ end
13
+
14
+ class Base
15
+ include ActiveModel::Model
16
+
17
+ include Validations
18
+ include Attributes
19
+ include Storage
20
+ include Comparisons
6
21
  end
7
22
  end
@@ -1,5 +1,5 @@
1
1
  module Session
2
2
  module Rails
3
- VERSION = '0.1.0'
3
+ VERSION = '0.1.1'
4
4
  end
5
5
  end
@@ -0,0 +1,47 @@
1
+ module Session
2
+ module Storage
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ def save
7
+ if validate
8
+ self.class.current = self
9
+ return true
10
+ else
11
+ return false
12
+ end
13
+ end
14
+
15
+ def destroy
16
+ self.class.current = nil
17
+ end
18
+
19
+ def persisted?
20
+ self.class.current == self
21
+ end
22
+ alias_method :exists?, :persisted?
23
+ end
24
+
25
+ class_methods do
26
+ attr_writer :store
27
+
28
+ def store
29
+ @store ||= {}
30
+ end
31
+
32
+ def current
33
+ new id: store&.[](store_key)
34
+ end
35
+
36
+ def current=(v)
37
+ store&.[]=(store_key, v&.id)
38
+ end
39
+
40
+ protected
41
+
42
+ def store_key
43
+ :"#{self.to_s.underscore.sub '/', '_'}_#{id_attribute}"
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,39 @@
1
+ module Session
2
+ module Validations
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ validate(
7
+ :public_attribute_is_present,
8
+ :private_attribute_is_present,
9
+ :session_is_valid
10
+ )
11
+
12
+ def authenticate
13
+ model&.authenticate private_attribute
14
+ end
15
+
16
+ protected
17
+
18
+ def public_attribute_is_present
19
+ attribute_is_present self.class.public_attribute, public_attribute
20
+ end
21
+
22
+ def private_attribute_is_present
23
+ attribute_is_present self.class.private_attribute, private_attribute
24
+ end
25
+
26
+ def session_is_valid
27
+ if public_attribute.present? && private_attribute.present? && !authenticate
28
+ errors.add :base, I18n.t("activerecord.errors.models.#{self.class.to_s.underscore.sub '/', '_'}.invalid")
29
+ end
30
+ end
31
+
32
+ def attribute_is_present(name, value)
33
+ if value.blank?
34
+ errors.add name, I18n.t("activerecord.errors.models.#{self.class.to_s.underscore.sub '/', '_'}.attributes.#{name}.blank")
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: session-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Julien Dargelos
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-04-18 00:00:00.000000000 Z
11
+ date: 2018-04-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -38,7 +38,21 @@ dependencies:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
- description: Makes user session smooth.
41
+ - !ruby/object:Gem::Dependency
42
+ name: bcrypt
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: A session Active Model.
42
56
  email:
43
57
  - contact@juliendargelos.com
44
58
  executables: []
@@ -48,13 +62,16 @@ files:
48
62
  - MIT-LICENSE
49
63
  - README.md
50
64
  - Rakefile
51
- - app/models/session.rb
65
+ - lib/session/attributes.rb
66
+ - lib/session/comparisons.rb
52
67
  - lib/session/core_ext.rb
53
68
  - lib/session/core_ext/action_controller.rb
54
69
  - lib/session/engine.rb
55
70
  - lib/session/rails.rb
56
71
  - lib/session/rails/railtie.rb
57
72
  - lib/session/rails/version.rb
73
+ - lib/session/storage.rb
74
+ - lib/session/validations.rb
58
75
  - lib/tasks/session/rails_tasks.rake
59
76
  homepage: https://www.github.com/juliendargelos/session-rails
60
77
  licenses:
@@ -79,5 +96,5 @@ rubyforge_project:
79
96
  rubygems_version: 2.7.3
80
97
  signing_key:
81
98
  specification_version: 4
82
- summary: Makes user session smooth.
99
+ summary: A session Active Model.
83
100
  test_files: []
@@ -1,80 +0,0 @@
1
- class Session
2
- include ActiveModel::Model
3
-
4
- attr_accessor :email, :password
5
-
6
- validates :email, presence: true
7
- validates :password, presence: true
8
-
9
- validate do |session|
10
- errors.add :base, I18n.t('activerecord.errors.models.session.invalid') unless email.blank? || password.blank? || session.authenticate
11
- end
12
-
13
- def id
14
- user&.id
15
- end
16
-
17
- def id=(v)
18
- self.user = User.find_by id: v
19
- end
20
-
21
- def user
22
- User.find_by email: email
23
- end
24
-
25
- def user=(v)
26
- self.email = v&.email
27
- end
28
-
29
- def authenticate
30
- if user&.authenticate password
31
- self.password = nil
32
- return true
33
- else
34
- return false
35
- end
36
- end
37
-
38
- def save
39
- if validate
40
- self.class.current = self
41
- return true
42
- else
43
- return false
44
- end
45
- end
46
-
47
- def destroy
48
- self.class.current = nil
49
- end
50
-
51
- def persisted?
52
- self.class.current == self
53
- end
54
- alias_method :exists?, :persisted?
55
-
56
- def ==(session)
57
- session.is_a?(self.class) && id.present? && id == session.id
58
- end
59
-
60
- class << self
61
- KEY = :user_id
62
-
63
- attr_accessor :store
64
-
65
- def current
66
- new id: store&.[](KEY)
67
- end
68
-
69
- def current=(v)
70
- store&.[]=(KEY, v&.id)
71
- end
72
-
73
- def columns
74
- [
75
- Struct.new(:name, :type).new('email', :string),
76
- Struct.new(:name, :type).new('password', :string)
77
- ]
78
- end
79
- end
80
- end