active_session 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/active_session/version.rb +1 -1
- data/lib/active_session.rb +25 -17
- data/test/user_session_test.rb +5 -0
- metadata +5 -5
data/lib/active_session.rb
CHANGED
@@ -26,27 +26,35 @@ module ActiveSession
|
|
26
26
|
end
|
27
27
|
|
28
28
|
def belongs_to(name, options = {})
|
29
|
-
klass = options.fetch(:class_name, name.to_s.classify
|
30
|
-
foreign_key = options.fetch(:foreign_key, "#{name}_id")
|
29
|
+
klass = options.fetch(:class_name, name.to_s.classify)
|
30
|
+
foreign_key = options.fetch(:foreign_key, "#{name}_id")
|
31
31
|
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
32
|
+
class_eval <<-END, __FILE__, __LINE__ + 1
|
33
|
+
def #{name}
|
34
|
+
return @#{name} if defined?(@#{name})
|
35
|
+
@#{name} = #{klass}.find(#{foreign_key})
|
36
|
+
end
|
37
|
+
END
|
36
38
|
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
39
|
+
class_eval <<-END, __FILE__, __LINE__ + 1
|
40
|
+
def #{name}=(model)
|
41
|
+
raise ActiveSession::AssociationTypeMismatch, "#{klass} expected, got \#\{model.class\}" unless model.nil? or model.is_a?(#{klass})
|
42
|
+
self.#{foreign_key} = model.try(:id)
|
43
|
+
end
|
44
|
+
END
|
41
45
|
|
42
|
-
|
43
|
-
|
44
|
-
|
46
|
+
class_eval <<-END, __FILE__, __LINE__ + 1
|
47
|
+
def #{foreign_key}
|
48
|
+
@attributes[:#{foreign_key}]
|
49
|
+
end
|
50
|
+
END
|
45
51
|
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
52
|
+
class_eval <<-END, __FILE__, __LINE__ + 1
|
53
|
+
def #{foreign_key}=(model_id)
|
54
|
+
remove_instance_variable("@#{name}") if instance_variable_defined?("@#{name}")
|
55
|
+
@attributes[:#{foreign_key}] = model_id
|
56
|
+
end
|
57
|
+
END
|
50
58
|
end
|
51
59
|
end
|
52
60
|
|
data/test/user_session_test.rb
CHANGED
@@ -13,6 +13,11 @@ describe UserSession do
|
|
13
13
|
|
14
14
|
describe "valid?" do
|
15
15
|
|
16
|
+
it "returns false if new record" do
|
17
|
+
session = UserSession.new(@store)
|
18
|
+
assert_equal false, session.valid?
|
19
|
+
end
|
20
|
+
|
16
21
|
it "returns true with existing user_id" do
|
17
22
|
session = UserSession.new(@store, :user_id => 1)
|
18
23
|
assert_equal true, session.valid?
|
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.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -14,7 +14,7 @@ date: 2011-10-19 00:00:00.000000000Z
|
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: activemodel
|
17
|
-
requirement: &
|
17
|
+
requirement: &19802200 !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: *19802200
|
26
26
|
- !ruby/object:Gem::Dependency
|
27
27
|
name: activesupport
|
28
|
-
requirement: &
|
28
|
+
requirement: &19801700 !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: *19801700
|
37
37
|
description: ActiveModel session
|
38
38
|
email:
|
39
39
|
- qoobaa@gmail.com
|