dm-userstamp 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/Manifest.txt CHANGED
@@ -4,8 +4,8 @@ Manifest.txt
4
4
  README.txt
5
5
  Rakefile
6
6
  TODO
7
- lib/dm-userstamps.rb
8
- lib/dm-userstamps/version.rb
9
- spec/integration/userstamps_spec.rb
7
+ lib/dm-userstamp.rb
8
+ lib/dm-userstamp/version.rb
9
+ spec/integration/userstamp_spec.rb
10
10
  spec/spec.opts
11
11
  spec/spec_helper.rb
data/README.txt CHANGED
@@ -9,6 +9,7 @@ Eg:
9
9
 
10
10
  class User
11
11
  include DataMapper::Resource
12
+ include DataMapper::Userstamp::Stamper
12
13
 
13
14
  cattr_accessor :current_user
14
15
  ...
@@ -22,6 +23,11 @@ class Monkey
22
23
  ...
23
24
  end
24
25
 
26
+ == Docs
27
+
28
+ See rubyforge project:
29
+ * http://dm-userstamp.rubyforge.org/
30
+
25
31
  == Install
26
32
 
27
33
  sudo gem install dm-userstamp
data/Rakefile CHANGED
@@ -2,8 +2,8 @@
2
2
 
3
3
  require 'rubygems'
4
4
  require 'hoe'
5
- require './lib/dm-userstamps.rb'
6
- require './lib/dm-userstamps/version.rb'
5
+ require './lib/dm-userstamp.rb'
6
+ require './lib/dm-userstamp/version.rb'
7
7
 
8
8
  Hoe.new('dm-userstamp', DataMapper::Userstamps::VERSION) do |p|
9
9
  p.name = 'dm-userstamp'
@@ -1,5 +1,5 @@
1
1
  module DataMapper
2
2
  module Userstamps
3
- VERSION = "0.0.1"
3
+ VERSION = "0.0.2"
4
4
  end
5
5
  end
@@ -4,7 +4,29 @@ gem 'dm-core', '=0.9.4'
4
4
  require 'dm-core'
5
5
 
6
6
  module DataMapper
7
+
7
8
  module Userstamp
9
+
10
+ module Stamper
11
+ def self.included(base)
12
+ base.extend(ClassMethods)
13
+ end
14
+
15
+ module ClassMethods
16
+
17
+ def current_user=(user)
18
+ Thread.current["#{self.to_s.downcase}_#{self.object_id}_stamper"] = user
19
+ end
20
+
21
+ def current_user
22
+ Thread.current["#{self.to_s.downcase}_#{self.object_id}_stamper"]
23
+ end
24
+
25
+ end
26
+
27
+ end
28
+
29
+
8
30
  USERSTAMP_PROPERTIES = {
9
31
  :created_by_id => lambda { |r| r.created_by_id = User.current_user.id if User.current_user && r.new_record? && r.created_by_id.nil? },
10
32
  :updated_by_id => lambda { |r| r.updated_by_id = User.current_user.id if User.current_user}
@@ -0,0 +1,111 @@
1
+ require 'pathname'
2
+ require Pathname(__FILE__).dirname.expand_path.parent + 'spec_helper'
3
+ require 'pp'
4
+
5
+ return unless HAS_SQLITE3 || HAS_MYSQL || HAS_POSTGRES
6
+
7
+ describe 'DataMapper::Userstamp' do
8
+ before :all do
9
+ class User
10
+ include DataMapper::Resource
11
+ include DataMapper::Userstamp::Stamper
12
+
13
+ property :id, Integer, :serial => true
14
+ property :name, String
15
+
16
+ auto_migrate!(:default)
17
+ end
18
+
19
+ class Monkey
20
+ include DataMapper::Resource
21
+
22
+ property :id, Integer, :serial => true
23
+ property :name, String
24
+ property :created_by_id, Integer
25
+ property :updated_by_id, Integer
26
+
27
+ auto_migrate!(:default)
28
+ end
29
+
30
+ end
31
+
32
+ before :each do
33
+ @user = User.create(:name => 'Bob')
34
+ User.current_user = @user
35
+ end
36
+
37
+ after do
38
+ repository(:default).adapter.execute('DELETE from users');
39
+ repository(:default).adapter.execute('DELETE from monkeys');
40
+ end
41
+
42
+ it "should not set created_by_id if there is no current user" do
43
+ repository(:default) do
44
+ User.current_user = nil
45
+ monkey = Monkey.new(:name => 'Eric')
46
+ monkey.save
47
+ monkey.created_by_id.should be_nil
48
+ end
49
+ end
50
+
51
+ it "should not set updated_by_id if there is no current user" do
52
+ repository(:default) do
53
+ User.current_user = nil
54
+ monkey = Monkey.new(:name => 'Eric')
55
+ monkey.save
56
+ monkey.updated_by_id.should be_nil
57
+ end
58
+ end
59
+
60
+ it "should not set created_by_id if already set" do
61
+ repository(:default) do
62
+ monkey = Monkey.new(:name => 'Eric')
63
+ monkey.created_by_id = 5
64
+ monkey.save
65
+ monkey.created_by_id.should == 5
66
+ monkey.created_by_id.should be_a_kind_of(Integer)
67
+ end
68
+ end
69
+
70
+ it "should set created_by_id on creation" do
71
+ repository(:default) do
72
+ monkey = Monkey.new(:name => 'Clyde')
73
+ monkey.created_by_id.should be_nil
74
+ monkey.save
75
+ monkey.created_by_id.should be_a_kind_of(Integer)
76
+ monkey.created_by_id.should == User.current_user.id
77
+ end
78
+ end
79
+
80
+ it "should not alter created_by_id on model updates" do
81
+ repository(:default) do
82
+ monkey = Monkey.new(:name => 'Chump')
83
+ monkey.created_by_id.should be_nil
84
+ monkey.save
85
+ original_created_by_id = monkey.created_by_id
86
+ monkey.name = 'Wilma'
87
+ monkey.save
88
+ monkey.created_by_id.should eql(original_created_by_id)
89
+ end
90
+ end
91
+
92
+ it "should set updated_by_id on creation and on update" do
93
+ repository(:default) do
94
+ monkey = Monkey.new(:name => 'Johnny')
95
+ monkey.updated_by_id.should be_nil
96
+ monkey.save
97
+
98
+ monkey.updated_by_id.should be_a_kind_of(Integer)
99
+ monkey.updated_by_id.should == User.current_user.id
100
+
101
+ original_updated_by_id = monkey.updated_by_id
102
+
103
+ User.current_user = @user2 = User.create(:name => 'Fred')
104
+
105
+ monkey.name = 'Roger'
106
+ monkey.save
107
+ monkey.updated_by_id.should == @user2.id
108
+ end
109
+ end
110
+
111
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dm-userstamp
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Richard Livsey
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-08-28 00:00:00 +01:00
12
+ date: 2008-09-07 00:00:00 +01:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -39,9 +39,9 @@ files:
39
39
  - README.txt
40
40
  - Rakefile
41
41
  - TODO
42
- - lib/dm-userstamps.rb
43
- - lib/dm-userstamps/version.rb
44
- - spec/integration/userstamps_spec.rb
42
+ - lib/dm-userstamp.rb
43
+ - lib/dm-userstamp/version.rb
44
+ - spec/integration/userstamp_spec.rb
45
45
  - spec/spec.opts
46
46
  - spec/spec_helper.rb
47
47
  has_rdoc: true
@@ -1,112 +0,0 @@
1
- require 'pathname'
2
- require Pathname(__FILE__).dirname.expand_path.parent + 'spec_helper'
3
- require 'pp'
4
-
5
- return unless HAS_SQLITE3 || HAS_MYSQL || HAS_POSTGRES
6
-
7
- describe 'DataMapper::Userstamp' do
8
- before :all do
9
- class User
10
- include DataMapper::Resource
11
-
12
- cattr_accessor :current_user
13
-
14
- property :id, Integer, :serial => true
15
- property :name, String
16
-
17
- auto_migrate!(:default)
18
- end
19
-
20
- class Monkey
21
- include DataMapper::Resource
22
-
23
- property :id, Integer, :serial => true
24
- property :name, String
25
- property :created_by_id, Integer
26
- property :updated_by_id, Integer
27
-
28
- auto_migrate!(:default)
29
- end
30
-
31
- end
32
-
33
- before :each do
34
- @user = User.create(:name => 'Bob')
35
- User.current_user = @user
36
- end
37
-
38
- after do
39
- repository(:default).adapter.execute('DELETE from users');
40
- repository(:default).adapter.execute('DELETE from monkeys');
41
- end
42
-
43
- it "should not set created_by_id if there is no current user" do
44
- repository(:default) do
45
- User.current_user = nil
46
- monkey = Monkey.new(:name => 'Eric')
47
- monkey.save
48
- monkey.created_by_id.should be_nil
49
- end
50
- end
51
-
52
- it "should not set updated_by_id if there is no current user" do
53
- repository(:default) do
54
- User.current_user = nil
55
- monkey = Monkey.new(:name => 'Eric')
56
- monkey.save
57
- monkey.updated_by_id.should be_nil
58
- end
59
- end
60
-
61
- it "should not set created_by_id if already set" do
62
- repository(:default) do
63
- monkey = Monkey.new(:name => 'Eric')
64
- monkey.created_by_id = 5
65
- monkey.save
66
- monkey.created_by_id.should == 5
67
- monkey.created_by_id.should be_a_kind_of(Integer)
68
- end
69
- end
70
-
71
- it "should set created_by_id on creation" do
72
- repository(:default) do
73
- monkey = Monkey.new(:name => 'Clyde')
74
- monkey.created_by_id.should be_nil
75
- monkey.save
76
- monkey.created_by_id.should be_a_kind_of(Integer)
77
- monkey.created_by_id.should == User.current_user.id
78
- end
79
- end
80
-
81
- it "should not alter created_by_id on model updates" do
82
- repository(:default) do
83
- monkey = Monkey.new(:name => 'Chump')
84
- monkey.created_by_id.should be_nil
85
- monkey.save
86
- original_created_by_id = monkey.created_by_id
87
- monkey.name = 'Wilma'
88
- monkey.save
89
- monkey.created_by_id.should eql(original_created_by_id)
90
- end
91
- end
92
-
93
- it "should set updated_by_id on creation and on update" do
94
- repository(:default) do
95
- monkey = Monkey.new(:name => 'Johnny')
96
- monkey.updated_by_id.should be_nil
97
- monkey.save
98
-
99
- monkey.updated_by_id.should be_a_kind_of(Integer)
100
- monkey.updated_by_id.should == User.current_user.id
101
-
102
- original_updated_by_id = monkey.updated_by_id
103
-
104
- User.current_user = @user2 = User.create(:name => 'Fred')
105
-
106
- monkey.name = 'Roger'
107
- monkey.save
108
- monkey.updated_by_id.should == @user2.id
109
- end
110
- end
111
-
112
- end