created-and-updated-by 0.1.1 → 0.1.2
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.
- data/Rakefile +2 -0
- data/VERSION +1 -1
- data/created-and-updated-by.gemspec +14 -2
- data/lib/created-and-updated-by.rb +18 -10
- data/spec/created-and-updated-by_spec.rb +43 -4
- data/spec/db/database.yml +3 -0
- data/spec/db/models.rb +9 -0
- data/spec/db/schema.rb +13 -0
- data/spec/db/test.sqlite3 +0 -0
- data/spec/spec_helper.rb +21 -0
- metadata +28 -2
data/Rakefile
CHANGED
@@ -11,6 +11,8 @@ begin
|
|
11
11
|
gem.homepage = "http://github.com/jamesdaniels/created-and-updated-by"
|
12
12
|
gem.authors = ["James Daniels"]
|
13
13
|
gem.add_development_dependency "rspec", ">= 1.2.9"
|
14
|
+
gem.add_development_dependency "activerecord"
|
15
|
+
gem.add_development_dependency "activesupport"
|
14
16
|
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
15
17
|
end
|
16
18
|
Jeweler::GemcutterTasks.new
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.2
|
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{created-and-updated-by}
|
8
|
-
s.version = "0.1.
|
8
|
+
s.version = "0.1.2"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["James Daniels"]
|
12
|
-
s.date = %q{2010-01-
|
12
|
+
s.date = %q{2010-01-21}
|
13
13
|
s.description = %q{This gem extends ActiveRecord::Base causing created_by_id and updated_by_id attributes to be set magically with about 30 LOC}
|
14
14
|
s.email = %q{james@jamesdaniels.net}
|
15
15
|
s.extra_rdoc_files = [
|
@@ -26,6 +26,10 @@ Gem::Specification.new do |s|
|
|
26
26
|
"created-and-updated-by.gemspec",
|
27
27
|
"lib/created-and-updated-by.rb",
|
28
28
|
"spec/created-and-updated-by_spec.rb",
|
29
|
+
"spec/db/database.yml",
|
30
|
+
"spec/db/models.rb",
|
31
|
+
"spec/db/schema.rb",
|
32
|
+
"spec/db/test.sqlite3",
|
29
33
|
"spec/spec.opts",
|
30
34
|
"spec/spec_helper.rb"
|
31
35
|
]
|
@@ -36,6 +40,8 @@ Gem::Specification.new do |s|
|
|
36
40
|
s.summary = %q{Preforms magic on created_by_id and updated_by_id fields}
|
37
41
|
s.test_files = [
|
38
42
|
"spec/created-and-updated-by_spec.rb",
|
43
|
+
"spec/db/models.rb",
|
44
|
+
"spec/db/schema.rb",
|
39
45
|
"spec/spec_helper.rb"
|
40
46
|
]
|
41
47
|
|
@@ -45,11 +51,17 @@ Gem::Specification.new do |s|
|
|
45
51
|
|
46
52
|
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
47
53
|
s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
|
54
|
+
s.add_development_dependency(%q<activerecord>, [">= 0"])
|
55
|
+
s.add_development_dependency(%q<activesupport>, [">= 0"])
|
48
56
|
else
|
49
57
|
s.add_dependency(%q<rspec>, [">= 1.2.9"])
|
58
|
+
s.add_dependency(%q<activerecord>, [">= 0"])
|
59
|
+
s.add_dependency(%q<activesupport>, [">= 0"])
|
50
60
|
end
|
51
61
|
else
|
52
62
|
s.add_dependency(%q<rspec>, [">= 1.2.9"])
|
63
|
+
s.add_dependency(%q<activerecord>, [">= 0"])
|
64
|
+
s.add_dependency(%q<activesupport>, [">= 0"])
|
53
65
|
end
|
54
66
|
end
|
55
67
|
|
@@ -1,24 +1,32 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'active_support'
|
3
|
+
require 'active_support/core_ext'
|
4
|
+
|
1
5
|
module CreatedAndUpdatedBy
|
2
6
|
class Stamper
|
7
|
+
include ActiveSupport::CoreExtensions
|
3
8
|
cattr_accessor :stampable, :attribute
|
4
|
-
def self.attach(stamp_model = User, stamp_attribute = :current)
|
5
|
-
raise ArgumentError, "#{stamp_model
|
9
|
+
def self.attach(stamp_model = :User, stamp_attribute = :current)
|
10
|
+
raise ArgumentError, "#{stamp_model} does not respond to #{stamp_attribute}" unless Object.const_get(stamp_model).respond_to?(stamp_attribute)
|
6
11
|
self.stampable, self.attribute = stamp_model, stamp_attribute
|
7
12
|
ActiveRecord::Base.send(:include, CreatedAndUpdatedBy)
|
8
13
|
end
|
9
14
|
end
|
10
15
|
def self.included(base)
|
11
|
-
base.class_eval
|
12
|
-
send :include, InstanceMethods
|
13
|
-
before_validation :set_stamps
|
14
|
-
belongs_to :updated_by, :class_name => CreatedAndUpdatedBy::Stamper.stampable
|
15
|
-
belongs_to :created_by, :class_name => CreatedAndUpdatedBy::Stamper.stampable
|
16
|
-
|
16
|
+
base.class_eval [
|
17
|
+
"send :include, InstanceMethods",
|
18
|
+
"before_validation :set_stamps",
|
19
|
+
"belongs_to :updated_by, :class_name => '#{CreatedAndUpdatedBy::Stamper.stampable}'",
|
20
|
+
"belongs_to :created_by, :class_name => '#{CreatedAndUpdatedBy::Stamper.stampable}'",
|
21
|
+
].join("\n")
|
17
22
|
end
|
18
23
|
module InstanceMethods
|
19
|
-
def stamper; CreatedAndUpdatedBy::Stamper.stampable.send(CreatedAndUpdatedBy::Stamper.attribute); end
|
24
|
+
def stamper; Object.const_get(CreatedAndUpdatedBy::Stamper.stampable).send(CreatedAndUpdatedBy::Stamper.attribute); end
|
20
25
|
def set_stamps
|
21
|
-
|
26
|
+
if stamper && stamper.respond_to?(:id) && respond_to?(:created_by_id) && respond_to?(:updated_by_id)
|
27
|
+
self.created_by_id = stamper.id if new_record?
|
28
|
+
self.updated_by_id = stamper.id
|
29
|
+
end
|
22
30
|
end
|
23
31
|
end
|
24
32
|
end
|
@@ -1,7 +1,46 @@
|
|
1
1
|
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
2
|
|
3
|
-
describe
|
4
|
-
it
|
5
|
-
|
6
|
-
|
3
|
+
describe User do
|
4
|
+
it 'should have a freaking user' do
|
5
|
+
User.count > 0
|
6
|
+
end
|
7
|
+
it 'should have a curent_user set' do
|
8
|
+
User.current.should == User.first
|
9
|
+
end
|
7
10
|
end
|
11
|
+
|
12
|
+
describe WithCreated do
|
13
|
+
it 'should set the created_by_id on create' do
|
14
|
+
created = WithCreated.create!
|
15
|
+
created.created_by_id.should == User.first.id
|
16
|
+
created.updated_by_id.should == User.first.id
|
17
|
+
end
|
18
|
+
it 'can retrieve the created by' do
|
19
|
+
created = WithCreated.create!
|
20
|
+
created.created_by.should == User.first
|
21
|
+
created.updated_by.should == User.first
|
22
|
+
end
|
23
|
+
it 'sets the updated by' do
|
24
|
+
User.current = User.first
|
25
|
+
created = WithCreated.create!
|
26
|
+
User.current = User.last
|
27
|
+
created.something = 'blah blah something else'
|
28
|
+
created.save!
|
29
|
+
created.created_by_id.should == User.first.id
|
30
|
+
created.updated_by_id.should == User.last.id
|
31
|
+
User.current = User.first
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
describe WithoutCreated do
|
36
|
+
it 'should set the created_by_id on create' do
|
37
|
+
created = WithoutCreated.create!
|
38
|
+
created.respond_to?(:created_by_id).should be_false
|
39
|
+
created.respond_to?(:updated_by_id).should be_false
|
40
|
+
end
|
41
|
+
it 'can retrieve the created by' do
|
42
|
+
created = WithoutCreated.create!
|
43
|
+
created.created_by.should == nil
|
44
|
+
created.updated_by.should == nil
|
45
|
+
end
|
46
|
+
end
|
data/spec/db/models.rb
ADDED
data/spec/db/schema.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
ActiveRecord::Schema.define(:version => 1) do
|
2
|
+
create_table :with_createds, :force => true do |t|
|
3
|
+
t.column :something, :string
|
4
|
+
t.column :created_by_id, :integer
|
5
|
+
t.column :updated_by_id, :integer
|
6
|
+
end
|
7
|
+
create_table :without_createds, :force => true do |t|
|
8
|
+
t.column :something, :string
|
9
|
+
end
|
10
|
+
create_table :users, :force => true do |t|
|
11
|
+
t.column :something, :string
|
12
|
+
end
|
13
|
+
end
|
Binary file
|
data/spec/spec_helper.rb
CHANGED
@@ -1,9 +1,30 @@
|
|
1
1
|
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
2
2
|
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
3
3
|
require 'created-and-updated-by'
|
4
|
+
require 'active_record'
|
5
|
+
require 'active_record/fixtures'
|
6
|
+
require 'active_support'
|
7
|
+
require 'active_support/core_ext'
|
4
8
|
require 'spec'
|
5
9
|
require 'spec/autorun'
|
6
10
|
|
11
|
+
# establish the database connection
|
12
|
+
ActiveRecord::Base.configurations = YAML::load(IO.read(File.dirname(__FILE__) + '/db/database.yml'))
|
13
|
+
ActiveRecord::Base.establish_connection('active_record_merge_test')
|
14
|
+
|
15
|
+
# load the schema
|
16
|
+
$stdout = File.open('/dev/null', 'w')
|
17
|
+
load(File.dirname(__FILE__) + "/db/schema.rb")
|
18
|
+
$stdout = STDOUT
|
19
|
+
|
20
|
+
|
21
|
+
require File.dirname(__FILE__) + '/db/models'
|
22
|
+
CreatedAndUpdatedBy::Stamper.attach(:User, :current)
|
23
|
+
|
24
|
+
users = [User.create!(:something => 'test'), User.create!(:something => 'something else')]
|
25
|
+
|
26
|
+
User.current = users[0]
|
27
|
+
|
7
28
|
Spec::Runner.configure do |config|
|
8
29
|
|
9
30
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: created-and-updated-by
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- James Daniels
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2010-01-
|
12
|
+
date: 2010-01-21 00:00:00 -05:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -22,6 +22,26 @@ dependencies:
|
|
22
22
|
- !ruby/object:Gem::Version
|
23
23
|
version: 1.2.9
|
24
24
|
version:
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: activerecord
|
27
|
+
type: :development
|
28
|
+
version_requirement:
|
29
|
+
version_requirements: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: "0"
|
34
|
+
version:
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
name: activesupport
|
37
|
+
type: :development
|
38
|
+
version_requirement:
|
39
|
+
version_requirements: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: "0"
|
44
|
+
version:
|
25
45
|
description: This gem extends ActiveRecord::Base causing created_by_id and updated_by_id attributes to be set magically with about 30 LOC
|
26
46
|
email: james@jamesdaniels.net
|
27
47
|
executables: []
|
@@ -41,6 +61,10 @@ files:
|
|
41
61
|
- created-and-updated-by.gemspec
|
42
62
|
- lib/created-and-updated-by.rb
|
43
63
|
- spec/created-and-updated-by_spec.rb
|
64
|
+
- spec/db/database.yml
|
65
|
+
- spec/db/models.rb
|
66
|
+
- spec/db/schema.rb
|
67
|
+
- spec/db/test.sqlite3
|
44
68
|
- spec/spec.opts
|
45
69
|
- spec/spec_helper.rb
|
46
70
|
has_rdoc: true
|
@@ -73,4 +97,6 @@ specification_version: 3
|
|
73
97
|
summary: Preforms magic on created_by_id and updated_by_id fields
|
74
98
|
test_files:
|
75
99
|
- spec/created-and-updated-by_spec.rb
|
100
|
+
- spec/db/models.rb
|
101
|
+
- spec/db/schema.rb
|
76
102
|
- spec/spec_helper.rb
|