active_session 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/active_session.rb +1 -111
- data/lib/active_session/{attribute_methods.rb → base.rb} +13 -5
- data/lib/active_session/errors.rb +0 -4
- data/lib/active_session/version.rb +1 -1
- data/lib/authenticity_validator.rb +7 -0
- metadata +9 -11
- data/test/models/user.rb +0 -11
- data/test/models/user_session.rb +0 -4
- data/test/user_session_test.rb +0 -126
data/lib/active_session.rb
CHANGED
@@ -1,117 +1,7 @@
|
|
1
1
|
require "active_model"
|
2
|
-
require "
|
3
|
-
require "active_session/attribute_methods"
|
2
|
+
require "authencitity_validator"
|
4
3
|
require "active_session/errors"
|
5
4
|
|
6
5
|
module ActiveSession
|
7
|
-
# Adds a simple session mechanism for a given model.
|
8
|
-
#
|
9
|
-
# <tt>authenticates :user</tt> adds <tt>user</tt> and
|
10
|
-
# <tt>user=(new_user)</tt> methods.
|
11
|
-
#
|
12
|
-
# Options are:
|
13
|
-
# * <tt>:class_name</tt> - Specifies the class name of the
|
14
|
-
# association. Use it only if that name can't be inferred from the
|
15
|
-
# name. So <tt>authenticates :user</tt> will by default be linked
|
16
|
-
# to the User class, but if the real class name is Admin, you'll
|
17
|
-
# have to specify it using this option.
|
18
|
-
# * <tt>:foreign_key</tt> - Specifies the foreign key of the
|
19
|
-
# association. Use it only if the key can't be inferred from the
|
20
|
-
# name. So <tt>authenticates :user</tt> will by default add
|
21
|
-
# <tt>user_id</tt> and <tt>user_id=(new_user_id)</tt> methods.
|
22
|
-
# * <tt>:primary_key</tt> - Specifies the primary key of the
|
23
|
-
# association. Use it only if the key is different than <tt>id</tt>.
|
24
|
-
# * <tt>:finder_method</tt> - Specifies the finder_method for the
|
25
|
-
# association. Uses <tt>find_for_authentication</tt> method by
|
26
|
-
# default.
|
27
|
-
# * <tt>:persists</tt> - Specifies if the session model is
|
28
|
-
# persistable in the store. If set to <tt>true</tt> (default)
|
29
|
-
# <tt>foreign_key</tt> will be used to store session. So
|
30
|
-
# <tt>authenticates :user</tt> will by default store the session
|
31
|
-
# using <tt>:user_id</tt> key in the store, and use
|
32
|
-
# <tt>user_id</tt> method to fetch the stored value. You can
|
33
|
-
# change it to <tt>:email</tt> if you want to use <tt>:email</tt>
|
34
|
-
# key and <tt>email</tt> method to store the session. You can also
|
35
|
-
# turn the persistence off by passing <tt>false</tt>. Adds
|
36
|
-
# <tt>save</tt>, <tt>destroy</tt>, <tt>persisted?</tt> and a class
|
37
|
-
# method <tt>find</tt> when turned on. When turned off adds only
|
38
|
-
# <tt>persisted?</tt> method that returns false to comply with
|
39
|
-
# <tt>ActiveModel::Lint</tt>.
|
40
6
|
|
41
|
-
def authenticates(name, options = {})
|
42
|
-
options.assert_valid_keys(:class_name, :foreign_key, :primary_key, :finder_method, :persists)
|
43
|
-
|
44
|
-
klass = options.fetch(:class_name, name.to_s.classify)
|
45
|
-
foreign_key = options.fetch(:foreign_key, "#{name}_id").to_sym
|
46
|
-
primary_key = options.fetch(:primary_key, "id").to_sym
|
47
|
-
finder_method = options.fetch(:finder_method, "find_for_authentication").to_sym
|
48
|
-
persists = options.fetch(:persists, foreign_key)
|
49
|
-
|
50
|
-
extend ActiveModel::Naming
|
51
|
-
include ActiveModel::AttributeMethods
|
52
|
-
include ActiveModel::Conversion
|
53
|
-
include ActiveModel::MassAssignmentSecurity
|
54
|
-
include ActiveModel::Validations
|
55
|
-
|
56
|
-
include AttributeMethods
|
57
|
-
|
58
|
-
attribute_method_suffix "", "="
|
59
|
-
|
60
|
-
validates name, :presence => true
|
61
|
-
|
62
|
-
class_eval <<-END, __FILE__, __LINE__ + 1
|
63
|
-
def #{name}
|
64
|
-
return @#{name} if defined?(@#{name})
|
65
|
-
@#{name} = #{klass}.#{finder_method}(:#{primary_key} => #{foreign_key})
|
66
|
-
end
|
67
|
-
|
68
|
-
def #{name}=(model)
|
69
|
-
raise ActiveSession::AssociationTypeMismatch, "#{klass} expected, got \#{model.class}" unless model.nil? or model.is_a?(#{klass})
|
70
|
-
self.#{foreign_key} = model.try(:#{primary_key})
|
71
|
-
end
|
72
|
-
|
73
|
-
def #{foreign_key}
|
74
|
-
@attributes[:#{foreign_key}]
|
75
|
-
end
|
76
|
-
|
77
|
-
def #{foreign_key}=(model_id)
|
78
|
-
remove_instance_variable("@#{name}") if instance_variable_defined?("@#{name}")
|
79
|
-
@attributes[:#{foreign_key}] = model_id
|
80
|
-
end
|
81
|
-
END
|
82
|
-
|
83
|
-
persists(persists == true ? foreign_key : persists)
|
84
|
-
end
|
85
|
-
|
86
|
-
private
|
87
|
-
|
88
|
-
def persists(key)
|
89
|
-
if key
|
90
|
-
instance_eval <<-END, __FILE__, __LINE__ + 1
|
91
|
-
def find(store)
|
92
|
-
new(store, :#{key} => store[:#{key}])
|
93
|
-
end
|
94
|
-
END
|
95
|
-
|
96
|
-
class_eval <<-END, __FILE__, __LINE__ + 1
|
97
|
-
def persisted?
|
98
|
-
@store[:#{key}].present? and @store[:#{key}] == #{key}
|
99
|
-
end
|
100
|
-
|
101
|
-
def destroy
|
102
|
-
persisted? && @store.delete(:#{key}) && true
|
103
|
-
end
|
104
|
-
|
105
|
-
def save
|
106
|
-
valid? && (@store[:#{key}] = #{key}) && true
|
107
|
-
end
|
108
|
-
END
|
109
|
-
else
|
110
|
-
class_eval <<-END, __FILE__, __LINE__ + 1
|
111
|
-
def persisted?
|
112
|
-
false
|
113
|
-
end
|
114
|
-
END
|
115
|
-
end
|
116
|
-
end
|
117
7
|
end
|
@@ -1,5 +1,13 @@
|
|
1
1
|
module ActiveSession
|
2
|
-
|
2
|
+
class Base
|
3
|
+
extend ActiveModel::Naming
|
4
|
+
include ActiveModel::AttributeMethods
|
5
|
+
include ActiveModel::Conversion
|
6
|
+
include ActiveModel::MassAssignmentSecurity
|
7
|
+
include ActiveModel::Validations
|
8
|
+
|
9
|
+
attribute_method_suffix "", "="
|
10
|
+
|
3
11
|
def initialize(store, attributes = nil, options = {})
|
4
12
|
@store = store
|
5
13
|
@attributes = {}
|
@@ -7,10 +15,6 @@ module ActiveSession
|
|
7
15
|
yield self if block_given?
|
8
16
|
end
|
9
17
|
|
10
|
-
def attributes
|
11
|
-
@attributes
|
12
|
-
end
|
13
|
-
|
14
18
|
def attributes=(attributes)
|
15
19
|
assign_attributes(attributes)
|
16
20
|
end
|
@@ -33,6 +37,10 @@ module ActiveSession
|
|
33
37
|
end
|
34
38
|
end
|
35
39
|
|
40
|
+
def persisted?
|
41
|
+
false
|
42
|
+
end
|
43
|
+
|
36
44
|
private
|
37
45
|
|
38
46
|
def attribute(attribute)
|
@@ -0,0 +1,7 @@
|
|
1
|
+
class AuthenticityValidator < ActiveModel::EachValidator
|
2
|
+
def validate_each(record, attribute, value)
|
3
|
+
authenticating_record = options[:of] ? record.send(options[:of]) : record
|
4
|
+
authentic = authenticating_record ? authenticating_record.authenticate(value) : true
|
5
|
+
record.errors.add(attribute, :invalid) unless authentic
|
6
|
+
end
|
7
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_session
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -10,11 +10,11 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2011-10-
|
13
|
+
date: 2011-10-24 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: activemodel
|
17
|
-
requirement: &
|
17
|
+
requirement: &19050140 !ruby/object:Gem::Requirement
|
18
18
|
none: false
|
19
19
|
requirements:
|
20
20
|
- - ~>
|
@@ -22,10 +22,10 @@ dependencies:
|
|
22
22
|
version: 3.1.1
|
23
23
|
type: :runtime
|
24
24
|
prerelease: false
|
25
|
-
version_requirements: *
|
25
|
+
version_requirements: *19050140
|
26
26
|
- !ruby/object:Gem::Dependency
|
27
27
|
name: activesupport
|
28
|
-
requirement: &
|
28
|
+
requirement: &19049640 !ruby/object:Gem::Requirement
|
29
29
|
none: false
|
30
30
|
requirements:
|
31
31
|
- - ~>
|
@@ -33,7 +33,7 @@ dependencies:
|
|
33
33
|
version: 3.1.1
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
|
-
version_requirements: *
|
36
|
+
version_requirements: *19049640
|
37
37
|
description: ActiveModel session
|
38
38
|
email:
|
39
39
|
- qoobaa@gmail.com
|
@@ -49,13 +49,11 @@ files:
|
|
49
49
|
- Rakefile
|
50
50
|
- active_session.gemspec
|
51
51
|
- lib/active_session.rb
|
52
|
-
- lib/active_session/
|
52
|
+
- lib/active_session/base.rb
|
53
53
|
- lib/active_session/errors.rb
|
54
54
|
- lib/active_session/version.rb
|
55
|
+
- lib/authenticity_validator.rb
|
55
56
|
- test/helper.rb
|
56
|
-
- test/models/user.rb
|
57
|
-
- test/models/user_session.rb
|
58
|
-
- test/user_session_test.rb
|
59
57
|
homepage: ''
|
60
58
|
licenses: []
|
61
59
|
post_install_message:
|
@@ -76,7 +74,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
76
74
|
version: '0'
|
77
75
|
requirements: []
|
78
76
|
rubyforge_project:
|
79
|
-
rubygems_version: 1.8.
|
77
|
+
rubygems_version: 1.8.10
|
80
78
|
signing_key:
|
81
79
|
specification_version: 3
|
82
80
|
summary: ActiveModel session
|
data/test/models/user.rb
DELETED
data/test/models/user_session.rb
DELETED
data/test/user_session_test.rb
DELETED
@@ -1,126 +0,0 @@
|
|
1
|
-
require "helper"
|
2
|
-
|
3
|
-
require "models/user"
|
4
|
-
require "models/user_session"
|
5
|
-
|
6
|
-
describe UserSession do
|
7
|
-
include ActiveModel::Lint::Tests
|
8
|
-
|
9
|
-
before do
|
10
|
-
@store = {}
|
11
|
-
@model = UserSession.new(@store)
|
12
|
-
end
|
13
|
-
|
14
|
-
describe "valid?" do
|
15
|
-
|
16
|
-
it "returns false if new record" do
|
17
|
-
session = UserSession.new(@store)
|
18
|
-
assert_equal false, session.valid?
|
19
|
-
end
|
20
|
-
|
21
|
-
it "returns true with existing user_id" do
|
22
|
-
session = UserSession.new(@store, :user_id => 1)
|
23
|
-
assert_equal true, session.valid?
|
24
|
-
end
|
25
|
-
|
26
|
-
it "returns false with non-existing user_id" do
|
27
|
-
session = UserSession.new(@store, :user_id => "non-existing")
|
28
|
-
assert_equal false, session.valid?
|
29
|
-
end
|
30
|
-
|
31
|
-
it "returns true with existing user" do
|
32
|
-
session = UserSession.new(@store, :user => User.new(1))
|
33
|
-
assert_equal true, session.valid?
|
34
|
-
end
|
35
|
-
|
36
|
-
it "returns false when user is nil" do
|
37
|
-
session = UserSession.new(@store, :user => nil)
|
38
|
-
assert_equal false, session.valid?
|
39
|
-
end
|
40
|
-
|
41
|
-
end
|
42
|
-
|
43
|
-
describe "save" do
|
44
|
-
|
45
|
-
it "returns true with existing user_id" do
|
46
|
-
session = UserSession.new(@store, :user_id => 5)
|
47
|
-
assert_equal true, session.save
|
48
|
-
assert_equal 5, @store[:user_id]
|
49
|
-
end
|
50
|
-
|
51
|
-
it "returns false with non-existing user" do
|
52
|
-
@store[:user_id] = "666"
|
53
|
-
session = UserSession.new(@store, :user => nil)
|
54
|
-
assert_equal false, session.save
|
55
|
-
assert_equal "666", @store[:user_id]
|
56
|
-
end
|
57
|
-
|
58
|
-
end
|
59
|
-
|
60
|
-
describe "persisted?" do
|
61
|
-
|
62
|
-
it "returns true with existing user and user_id in store" do
|
63
|
-
@store[:user_id] = 1
|
64
|
-
session = UserSession.new(@store, :user_id => 1)
|
65
|
-
assert_equal true, session.persisted?
|
66
|
-
end
|
67
|
-
|
68
|
-
it "returns false without user_id" do
|
69
|
-
@store[:user_id] = "666"
|
70
|
-
session = UserSession.new(@store)
|
71
|
-
assert_equal false, session.persisted?
|
72
|
-
end
|
73
|
-
|
74
|
-
it "returns false with existing user and different user_id in store" do
|
75
|
-
@store[:user_id] = "666"
|
76
|
-
session = UserSession.new(@store, :user_id => 1)
|
77
|
-
assert_equal false, session.persisted?
|
78
|
-
end
|
79
|
-
|
80
|
-
end
|
81
|
-
|
82
|
-
describe "destroy" do
|
83
|
-
|
84
|
-
it "returns true when persisted with existing user" do
|
85
|
-
@store[:user_id] = 1
|
86
|
-
session = UserSession.new(@store, :user_id => 1)
|
87
|
-
assert_equal true, session.destroy
|
88
|
-
assert_nil @store[:user_id]
|
89
|
-
end
|
90
|
-
|
91
|
-
it "returns false when not persisted" do
|
92
|
-
@store[:user_id] = "666"
|
93
|
-
session = UserSession.new(@store, :user_id => 1)
|
94
|
-
assert_equal false, session.destroy
|
95
|
-
assert_equal "666", @store[:user_id]
|
96
|
-
end
|
97
|
-
|
98
|
-
end
|
99
|
-
|
100
|
-
describe "user" do
|
101
|
-
|
102
|
-
it "returns user with given user_id" do
|
103
|
-
session = UserSession.new(@store)
|
104
|
-
assert_nil session.user
|
105
|
-
session.user_id = 1
|
106
|
-
assert_equal 1, session.user.id
|
107
|
-
end
|
108
|
-
|
109
|
-
end
|
110
|
-
|
111
|
-
describe "user=" do
|
112
|
-
|
113
|
-
it "works properly with nil" do
|
114
|
-
session = UserSession.new(@store, :user => User.new(1))
|
115
|
-
session.user = nil
|
116
|
-
assert_nil session.user
|
117
|
-
assert_nil session.user_id
|
118
|
-
end
|
119
|
-
|
120
|
-
it "raises exception when non-user class given" do
|
121
|
-
session = UserSession.new(@store, :user_id => 1)
|
122
|
-
assert_raises(ActiveSession::AssociationTypeMismatch) { session.user = "zomg" }
|
123
|
-
end
|
124
|
-
|
125
|
-
end
|
126
|
-
end
|