session-rails 0.1.0

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 30ca743cdb599238153f9c7ae803103f0fe79be9203a5e4016c48e76f543c162
4
+ data.tar.gz: 9b43723e309bb9f6fc35e6ec2d625c2377b44f2f338af664b886bf3fbde838a2
5
+ SHA512:
6
+ metadata.gz: 83019616f3288151a9c3bfdbd5be1bbad74059c7de890236b468b7cde2d1680c4d7b8cc12f24c1f6825ecfd0756f662da229637aed28a82439eeb348f9938300
7
+ data.tar.gz: c1b59412debebb4f8d5532d3e1cd37eea4c9d577d901500519aaa8d1616192d16d08d0f7c4c2a14ed85e5a21a1dd748c1480f47f2efe0668ec91df897ded786c
@@ -0,0 +1,20 @@
1
+ Copyright 2018 Julien Dargelos
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1 @@
1
+ # Session Rails
@@ -0,0 +1,27 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'Session::Rails'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.md')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ require 'bundler/gem_tasks'
18
+
19
+ require 'rake/testtask'
20
+
21
+ Rake::TestTask.new(:test) do |t|
22
+ t.libs << 'test'
23
+ t.pattern = 'test/**/*_test.rb'
24
+ t.verbose = false
25
+ end
26
+
27
+ task default: :test
@@ -0,0 +1,80 @@
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
@@ -0,0 +1 @@
1
+ require __dir__ + '/core_ext/action_controller'
@@ -0,0 +1,3 @@
1
+ ActionController::Base.class_eval do
2
+
3
+ end
@@ -0,0 +1,5 @@
1
+ module Session
2
+ class Engine < ::Rails::Engine
3
+
4
+ end
5
+ end
@@ -0,0 +1,7 @@
1
+ require "session/rails/railtie"
2
+
3
+ module Session
4
+ module Rails
5
+ require __dir__ + '/engine'
6
+ end
7
+ end
@@ -0,0 +1,6 @@
1
+ module Session
2
+ module Rails
3
+ class Railtie < ::Rails::Railtie
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,5 @@
1
+ module Session
2
+ module Rails
3
+ VERSION = '0.1.0'
4
+ end
5
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :session_rails do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,83 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: session-rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Julien Dargelos
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-04-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 5.2.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 5.2.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: sqlite3
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: Makes user session smooth.
42
+ email:
43
+ - contact@juliendargelos.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - MIT-LICENSE
49
+ - README.md
50
+ - Rakefile
51
+ - app/models/session.rb
52
+ - lib/session/core_ext.rb
53
+ - lib/session/core_ext/action_controller.rb
54
+ - lib/session/engine.rb
55
+ - lib/session/rails.rb
56
+ - lib/session/rails/railtie.rb
57
+ - lib/session/rails/version.rb
58
+ - lib/tasks/session/rails_tasks.rake
59
+ homepage: https://www.github.com/juliendargelos/session-rails
60
+ licenses:
61
+ - MIT
62
+ metadata: {}
63
+ post_install_message:
64
+ rdoc_options: []
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ requirements: []
78
+ rubyforge_project:
79
+ rubygems_version: 2.7.3
80
+ signing_key:
81
+ specification_version: 4
82
+ summary: Makes user session smooth.
83
+ test_files: []