dm-userstamp 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt ADDED
@@ -0,0 +1,2 @@
1
+ 0.0.1 (26-08-2008)
2
+ * Initial Release
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2008 Richard Livsey (richard@livsey.org)
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.
data/Manifest.txt ADDED
@@ -0,0 +1,11 @@
1
+ History.txt
2
+ LICENSE
3
+ Manifest.txt
4
+ README.txt
5
+ Rakefile
6
+ TODO
7
+ lib/dm-userstamps.rb
8
+ lib/dm-userstamps/version.rb
9
+ spec/integration/userstamps_spec.rb
10
+ spec/spec.opts
11
+ spec/spec_helper.rb
data/README.txt ADDED
@@ -0,0 +1,33 @@
1
+ = dm-userstamp
2
+
3
+ DataMapper plugin which adds "magic" to created_by_id, updated_by_id fields
4
+
5
+ Assumes a model called User and a class attribute of current_user
6
+ Just need to add created_by_id and/or updated_by_id Integer fields to the model
7
+
8
+ Eg:
9
+
10
+ class User
11
+ include DataMapper::Resource
12
+
13
+ cattr_accessor :current_user
14
+ ...
15
+ end
16
+
17
+ class Monkey
18
+ include DataMapper::Resource
19
+
20
+ property :created_by_id, Integer
21
+ property :updated_by_id, Integer
22
+ ...
23
+ end
24
+
25
+ == Install
26
+
27
+ sudo gem install dm-userstamp
28
+
29
+ == Thanks
30
+
31
+ Code based on:
32
+ * AR userstamp plugin - http://github.com/ctran/userstamp/tree/master
33
+ * DM timestamps plugin - http://github.com/sam/dm-more/tree/master/dm-timestamps
data/Rakefile ADDED
@@ -0,0 +1,18 @@
1
+ # -*- ruby -*-
2
+
3
+ require 'rubygems'
4
+ require 'hoe'
5
+ require './lib/dm-userstamps.rb'
6
+ require './lib/dm-userstamps/version.rb'
7
+
8
+ Hoe.new('dm-userstamp', DataMapper::Userstamps::VERSION) do |p|
9
+ p.name = 'dm-userstamp'
10
+ p.remote_rdoc_dir = '' # Release to root
11
+ p.rubyforge_name = 'dm-userstamp'
12
+ p.author = "Richard Livsey"
13
+ p.description = "DataMapper plugin to add automatic updating of created_by_id and updated_by_id attributes"
14
+ p.summary = "DataMapper plugin to add automatic updating of created_by_id and updated_by_id attributes"
15
+ p.email = 'richard@livsey.org'
16
+ end
17
+
18
+ # vim: syntax=Ruby
data/TODO ADDED
@@ -0,0 +1 @@
1
+ * make more flexible so User model and field names aren't hardcoded
@@ -0,0 +1,5 @@
1
+ module DataMapper
2
+ module Userstamps
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,27 @@
1
+ require 'rubygems'
2
+
3
+ gem 'dm-core', '=0.9.4'
4
+ require 'dm-core'
5
+
6
+ module DataMapper
7
+ module Userstamp
8
+ USERSTAMP_PROPERTIES = {
9
+ :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
+ :updated_by_id => lambda { |r| r.updated_by_id = User.current_user.id if User.current_user}
11
+ }
12
+
13
+ def self.included(model)
14
+ model.before :save, :set_userstamp_properties
15
+ end
16
+
17
+ private
18
+
19
+ def set_userstamp_properties
20
+ self.class.properties.slice(*USERSTAMP_PROPERTIES.keys).compact.each do |property|
21
+ USERSTAMP_PROPERTIES[property.name][self]
22
+ end
23
+ end
24
+ end
25
+
26
+ Resource::append_inclusions Userstamp
27
+ end
@@ -0,0 +1,112 @@
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
data/spec/spec.opts ADDED
@@ -0,0 +1,2 @@
1
+ --format specdoc
2
+ --colour
@@ -0,0 +1,29 @@
1
+ require 'rubygems'
2
+ gem 'rspec', '>=1.1.3'
3
+ require 'spec'
4
+ require 'pathname'
5
+ require Pathname(__FILE__).dirname.parent.expand_path + 'lib/dm-userstamp'
6
+ require 'dm-core'
7
+
8
+ def load_driver(name, default_uri)
9
+ return false if ENV['ADAPTER'] != name.to_s
10
+
11
+ lib = "do_#{name}"
12
+
13
+ begin
14
+ gem lib, '=0.9.4'
15
+ require lib
16
+ DataMapper.setup(name, ENV["#{name.to_s.upcase}_SPEC_URI"] || default_uri)
17
+ DataMapper::Repository.adapters[:default] = DataMapper::Repository.adapters[name]
18
+ true
19
+ rescue Gem::LoadError => e
20
+ warn "Could not load #{lib}: #{e}"
21
+ false
22
+ end
23
+ end
24
+
25
+ ENV['ADAPTER'] ||= 'sqlite3'
26
+
27
+ HAS_SQLITE3 = load_driver(:sqlite3, 'sqlite3::memory:')
28
+ HAS_MYSQL = load_driver(:mysql, 'mysql://localhost/dm_core_test')
29
+ HAS_POSTGRES = load_driver(:postgres, 'postgres://postgres@localhost/dm_core_test')
metadata ADDED
@@ -0,0 +1,75 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dm-userstamp
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Richard Livsey
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-08-28 00:00:00 +01:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: hoe
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 1.7.0
24
+ version:
25
+ description: DataMapper plugin to add automatic updating of created_by_id and updated_by_id attributes
26
+ email: richard@livsey.org
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - History.txt
33
+ - Manifest.txt
34
+ - README.txt
35
+ files:
36
+ - History.txt
37
+ - LICENSE
38
+ - Manifest.txt
39
+ - README.txt
40
+ - Rakefile
41
+ - TODO
42
+ - lib/dm-userstamps.rb
43
+ - lib/dm-userstamps/version.rb
44
+ - spec/integration/userstamps_spec.rb
45
+ - spec/spec.opts
46
+ - spec/spec_helper.rb
47
+ has_rdoc: true
48
+ homepage: DataMapper plugin which adds "magic" to created_by_id, updated_by_id fields
49
+ post_install_message:
50
+ rdoc_options:
51
+ - --main
52
+ - README.txt
53
+ require_paths:
54
+ - lib
55
+ required_ruby_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: "0"
60
+ version:
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: "0"
66
+ version:
67
+ requirements: []
68
+
69
+ rubyforge_project: dm-userstamp
70
+ rubygems_version: 1.2.0
71
+ signing_key:
72
+ specification_version: 2
73
+ summary: DataMapper plugin to add automatic updating of created_by_id and updated_by_id attributes
74
+ test_files: []
75
+