nay-record_with_operator 0.0.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/MIT-LICENSE +20 -0
- data/README.rdoc +64 -0
- data/Rakefile +71 -0
- data/init.rb +1 -0
- data/install.rb +1 -0
- data/lib/association_with_operator.rb +24 -0
- data/lib/record_with_operator.rb +116 -0
- data/tasks/record_with_operator_tasks.rake +4 -0
- data/test/database.yml +18 -0
- data/test/record_with_operator_test.rb +206 -0
- data/test/record_with_operator_user_class_name_test.rb +59 -0
- data/test/schema.rb +49 -0
- data/test/test_helper.rb +11 -0
- data/uninstall.rb +1 -0
- metadata +81 -0
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 [name of plugin creator]
|
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/README.rdoc
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
= RecordWithOperator
|
2
|
+
|
3
|
+
== Introduction
|
4
|
+
|
5
|
+
RecordWithOperator is a rails plugin that makes your all active record models to be saved or logically deleted with created_by, updated_by, deleted_by automatically.
|
6
|
+
Also it makes creator, updater, deleter association (belongs_to) if the class has created_by, updated_by, deleted_by.
|
7
|
+
|
8
|
+
You need to set 'operator' (it may be an User object) to your model object at first.
|
9
|
+
Once you have set an operator, it will be copied to objects in the association collection, so it might be enough to set an operator per request.
|
10
|
+
Operators are also useful for your access control features on model's side.
|
11
|
+
To set an operator, simply use operator= or get objects by find method with :for option.
|
12
|
+
|
13
|
+
The logical deletion function itself is out of this plugin's scope.
|
14
|
+
This plugin assumes that the logical deletion is kicked by record.destory.
|
15
|
+
|
16
|
+
== Installation
|
17
|
+
|
18
|
+
gem install nay-record_with_operator --source http://gems.github.com
|
19
|
+
|
20
|
+
Or, you can install it in vender/plugins.
|
21
|
+
|
22
|
+
> ./script/plugin install git://github.com/nay/record_with_operator.git
|
23
|
+
|
24
|
+
|
25
|
+
== Rails Configration
|
26
|
+
|
27
|
+
In config/environment.rb:
|
28
|
+
|
29
|
+
Rails::Initializer.run do |config|
|
30
|
+
config.gem 'nay-record_with_operator', :lib => 'record_with_operator',
|
31
|
+
:source => 'http://gems.github.com'
|
32
|
+
end
|
33
|
+
|
34
|
+
== Example
|
35
|
+
|
36
|
+
Create a new note object with current_user:
|
37
|
+
|
38
|
+
note = Note.create(:body => "This is my first note.", :operator => current_user)
|
39
|
+
# note.operator will be current_user
|
40
|
+
# note.created_by will be current_user.id
|
41
|
+
|
42
|
+
Get note objects with current_user:
|
43
|
+
|
44
|
+
notes = Note.find(:all, :for => current_user)
|
45
|
+
# notes.first.operator will be current_user
|
46
|
+
# notes.find_by_body("This is my first note.").operator will be current_user
|
47
|
+
|
48
|
+
Create note's comments:
|
49
|
+
|
50
|
+
note = Note.find(params[:id], :for => current_user)
|
51
|
+
note.comments.create!(:body => params[:comment_body])
|
52
|
+
# comment's operator will be automatically set and used as created_by
|
53
|
+
|
54
|
+
== Configuration
|
55
|
+
|
56
|
+
You can change the operator's class name by setting RecordWithOperator.config[:user_class_name].
|
57
|
+
The default is 'User'.
|
58
|
+
Note that it is required to change the value before the model's creator/updater/deleter is firstly called.
|
59
|
+
For example, you can write the code under config/initializers.
|
60
|
+
|
61
|
+
RecordWithOperator.config[:user_class_name] = "AdminUser"
|
62
|
+
|
63
|
+
|
64
|
+
Copyright (c) 2009 Yasuko Ohba, released under the MIT license
|
data/Rakefile
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
require 'rake/clean'
|
4
|
+
require 'rake/testtask'
|
5
|
+
require 'rake/rdoctask'
|
6
|
+
require 'rake/gempackagetask'
|
7
|
+
|
8
|
+
NAME = "record_with_operator"
|
9
|
+
AUTHOR = "Yasuko Ohba"
|
10
|
+
EMAIL = "y.ohba@everyleaf.com"
|
11
|
+
DESCRIPTION = "Rails plugin to set created_by, updated_by, deleted_by to ActiveRecord objects. Supports associations."
|
12
|
+
GITHUB_PROJECT = "record_with_operator"
|
13
|
+
HOMEPAGE = "http://github.com/nay/#{GITHUB_PROJECT}/tree"
|
14
|
+
BIN_FILES = %w( )
|
15
|
+
VER = "0.0.2"
|
16
|
+
CLEAN.include ['pkg']
|
17
|
+
|
18
|
+
desc 'Default: run unit tests.'
|
19
|
+
task :default => :test
|
20
|
+
|
21
|
+
desc 'Test the record_with_operator plugin.'
|
22
|
+
Rake::TestTask.new(:test) do |t|
|
23
|
+
t.libs << 'lib'
|
24
|
+
t.libs << 'test'
|
25
|
+
t.pattern = 'test/**/*_test.rb'
|
26
|
+
t.verbose = true
|
27
|
+
end
|
28
|
+
|
29
|
+
desc 'Generate documentation for the record_with_operator plugin.'
|
30
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
31
|
+
rdoc.rdoc_dir = 'rdoc'
|
32
|
+
rdoc.title = 'RecordWithOperator'
|
33
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
34
|
+
rdoc.rdoc_files.include('README.rdoc')
|
35
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
36
|
+
end
|
37
|
+
|
38
|
+
spec = Gem::Specification.new do |s|
|
39
|
+
s.name = NAME
|
40
|
+
s.version = VER
|
41
|
+
s.platform = Gem::Platform::RUBY
|
42
|
+
s.has_rdoc = true
|
43
|
+
s.extra_rdoc_files = ["README.rdoc", "MIT-LICENSE"]
|
44
|
+
s.rdoc_options += ['--line-numbers', '--inline-source']
|
45
|
+
s.summary = DESCRIPTION
|
46
|
+
s.description = DESCRIPTION
|
47
|
+
s.author = AUTHOR
|
48
|
+
s.email = EMAIL
|
49
|
+
s.homepage = HOMEPAGE
|
50
|
+
s.executables = BIN_FILES
|
51
|
+
s.bindir = "bin"
|
52
|
+
s.require_path = "lib"
|
53
|
+
s.test_files = Dir["test/*"]
|
54
|
+
|
55
|
+
s.add_dependency('activerecord', '>=2.2.0')
|
56
|
+
|
57
|
+
s.files = %w(README.rdoc Rakefile MIT-LICENSE) +
|
58
|
+
%w(install.rb uninstall.rb init.rb) +
|
59
|
+
Dir.glob("{bin,doc,lib,tasks,test}/**/*")
|
60
|
+
|
61
|
+
end
|
62
|
+
|
63
|
+
Rake::GemPackageTask.new(spec) do |p|
|
64
|
+
p.need_tar = true
|
65
|
+
p.gem_spec = spec
|
66
|
+
end
|
67
|
+
|
68
|
+
desc 'Update gem spec'
|
69
|
+
task :gemspec do
|
70
|
+
open("#{NAME}.gemspec", "w").write spec.to_ruby
|
71
|
+
end
|
data/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
ActiveRecord::Base.instance_eval{include RecordWithOperator} unless ActiveRecord::Base.include?(RecordWithOperator)
|
data/install.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
# Install hook code here
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module AssociationWithOperator
|
2
|
+
|
3
|
+
def find(*args)
|
4
|
+
results = super
|
5
|
+
if results.kind_of? Array
|
6
|
+
results.each{|r| r.operator = proxy_owner.operator}
|
7
|
+
else
|
8
|
+
results.operator = proxy_owner.operator
|
9
|
+
end
|
10
|
+
results
|
11
|
+
end
|
12
|
+
|
13
|
+
|
14
|
+
def method_missing(method, *args)
|
15
|
+
results = super
|
16
|
+
if results.respond_to?(:operator=)
|
17
|
+
results.operator= proxy_owner.operator
|
18
|
+
elsif results.kind_of? Array
|
19
|
+
results.each{|r| r.operator = proxy_owner.operator if r.respond_to?(:operator=)}
|
20
|
+
end
|
21
|
+
results
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
@@ -0,0 +1,116 @@
|
|
1
|
+
module RecordWithOperator
|
2
|
+
def self.config
|
3
|
+
@config ||= {:user_class_name => "User"}
|
4
|
+
@config
|
5
|
+
end
|
6
|
+
|
7
|
+
attr_accessor :operator
|
8
|
+
|
9
|
+
def self.included(base)
|
10
|
+
class << base
|
11
|
+
def has_many_with_operator(*args, &extension)
|
12
|
+
options = args.extract_options!
|
13
|
+
# add AssociationWithOprator to :extend
|
14
|
+
if options[:extend]
|
15
|
+
options[:extend] = [options[:extend]] unless options[:extend].kind_of? Array
|
16
|
+
options[:extend] << AssociationWithOperator
|
17
|
+
else
|
18
|
+
options[:extend] = AssociationWithOperator
|
19
|
+
end
|
20
|
+
# add :set_operator to :before_add
|
21
|
+
if options[:before_add]
|
22
|
+
options[:before_add] = [options[:before_add]] unless options[:before_add].kind_of? Array
|
23
|
+
options[:before_add] << :set_operator
|
24
|
+
else
|
25
|
+
options[:before_add] = :set_operator
|
26
|
+
end
|
27
|
+
args << options
|
28
|
+
has_many_without_operator(*args, &extension)
|
29
|
+
end
|
30
|
+
alias_method_chain :has_many, :operator
|
31
|
+
|
32
|
+
def find_with_for(*args)
|
33
|
+
options = args.extract_options!
|
34
|
+
operator = options.delete(:for)
|
35
|
+
args << options
|
36
|
+
results = find_without_for(*args)
|
37
|
+
if operator
|
38
|
+
if results.kind_of? Array
|
39
|
+
results.each{|r| r.operator = operator}
|
40
|
+
else
|
41
|
+
results.operator = operator
|
42
|
+
end
|
43
|
+
end
|
44
|
+
results
|
45
|
+
end
|
46
|
+
|
47
|
+
alias_method_chain :find, :for
|
48
|
+
|
49
|
+
def validate_find_options_with_for(options)
|
50
|
+
if options
|
51
|
+
options = options.dup
|
52
|
+
options.delete(:for)
|
53
|
+
end
|
54
|
+
validate_find_options_without_for(options)
|
55
|
+
end
|
56
|
+
|
57
|
+
alias_method_chain :validate_find_options, :for
|
58
|
+
end
|
59
|
+
|
60
|
+
base.before_create :set_created_by
|
61
|
+
base.before_save :set_updated_by
|
62
|
+
base.before_destroy :set_deleted_by
|
63
|
+
|
64
|
+
end
|
65
|
+
|
66
|
+
def respond_to?(name, priv=false)
|
67
|
+
case name.to_sym
|
68
|
+
when :creator
|
69
|
+
respond_to? :created_by
|
70
|
+
when :updater
|
71
|
+
respond_to? :updated_by
|
72
|
+
when :deleter
|
73
|
+
respond_to? :deleted_by
|
74
|
+
else
|
75
|
+
super
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
private
|
80
|
+
def set_operator(child)
|
81
|
+
child.operator = self.operator
|
82
|
+
end
|
83
|
+
|
84
|
+
def method_missing(method, *args)
|
85
|
+
return super unless respond_to?(method)
|
86
|
+
case method.to_sym
|
87
|
+
when :creator
|
88
|
+
self.class.belongs_to :creator, :foreign_key => "created_by", :class_name => RecordWithOperator.config[:user_class_name]
|
89
|
+
send(method, *args)
|
90
|
+
when :updater
|
91
|
+
self.class.belongs_to :updater, :foreign_key => "updated_by", :class_name => RecordWithOperator.config[:user_class_name]
|
92
|
+
send(method, *args)
|
93
|
+
when :deleter
|
94
|
+
self.class.belongs_to :deletor, :foreign_key => "deleted_by", :class_name => RecordWithOperator.config[:user_class_name]
|
95
|
+
send(method, *args)
|
96
|
+
else
|
97
|
+
super
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
def set_created_by
|
102
|
+
return unless respond_to?(:created_by=) && operator
|
103
|
+
self.created_by = operator.id
|
104
|
+
end
|
105
|
+
|
106
|
+
def set_updated_by
|
107
|
+
return unless respond_to?(:updated_by=) && operator
|
108
|
+
self.updated_by = operator.id
|
109
|
+
end
|
110
|
+
|
111
|
+
def set_deleted_by
|
112
|
+
return unless respond_to?(:deleted_by=) && operator
|
113
|
+
self.updated_by = operator.id
|
114
|
+
end
|
115
|
+
|
116
|
+
end
|
data/test/database.yml
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
sqlite:
|
2
|
+
:adapter: sqlite
|
3
|
+
:dbfile: record_with_operator_plugin.sqlite.db
|
4
|
+
sqlite3:
|
5
|
+
:adapter: sqlite3
|
6
|
+
:dbfile: record_with_operator_plugin.sqlite3.db
|
7
|
+
postgresql:
|
8
|
+
:adapter: postgresql
|
9
|
+
:username: postgres
|
10
|
+
:password: postgres
|
11
|
+
:database: record_with_operator_plugin_test
|
12
|
+
:min_messages: ERROR
|
13
|
+
mysql:
|
14
|
+
:adapter: mysql
|
15
|
+
:host: localhost
|
16
|
+
:username:
|
17
|
+
:password:
|
18
|
+
:database: record_with_operator_plugin_test
|
@@ -0,0 +1,206 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class User < ActiveRecord::Base
|
4
|
+
end
|
5
|
+
|
6
|
+
class NoteWithUser < ActiveRecord::Base
|
7
|
+
set_table_name "notes"
|
8
|
+
has_many :memos, :class_name => "MemoWithUser", :foreign_key => "note_id"
|
9
|
+
|
10
|
+
named_scope :new_arrivals, {:order => "updated_at desc"}
|
11
|
+
|
12
|
+
def destroy_with_deleted_at
|
13
|
+
NoteWithUser.update_all("deleted_at = '#{Time.now.to_s(:db)}'", "id = #{self.id}")
|
14
|
+
end
|
15
|
+
alias_method_chain :destroy, :deleted_at
|
16
|
+
def destory!
|
17
|
+
destory_without_deleted_at
|
18
|
+
end
|
19
|
+
|
20
|
+
def deleted?
|
21
|
+
self.deleted_at <= Time.now
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
class SimpleNoteWithUser < ActiveRecord::Base
|
26
|
+
set_table_name "simple_notes"
|
27
|
+
end
|
28
|
+
|
29
|
+
class MemoWithUser < ActiveRecord::Base
|
30
|
+
set_table_name "memos"
|
31
|
+
|
32
|
+
named_scope :new_arrivals, {:order => "updated_at desc"}
|
33
|
+
end
|
34
|
+
|
35
|
+
class UpdaterNoteWithUser < ActiveRecord::Base
|
36
|
+
set_table_name "updater_notes"
|
37
|
+
|
38
|
+
end
|
39
|
+
|
40
|
+
class DeleterNoteWithUser < ActiveRecord::Base
|
41
|
+
set_table_name "deleter_notes"
|
42
|
+
|
43
|
+
end
|
44
|
+
|
45
|
+
class RecordWithOperatorTest < ActiveSupport::TestCase
|
46
|
+
def setup
|
47
|
+
RecordWithOperator.config[:user_class_name] = "User"
|
48
|
+
@user1 = User.create!(:name => "user1")
|
49
|
+
@user2 = User.create!(:name => "user2")
|
50
|
+
@note_created_by_user1 = NoteWithUser.create!(:body => "test", :operator => @user1)
|
51
|
+
end
|
52
|
+
|
53
|
+
# creator/updater/deleter association operation
|
54
|
+
|
55
|
+
def test_note_should_be_respond_to_creator
|
56
|
+
assert NoteWithUser.new.respond_to? :creator
|
57
|
+
end
|
58
|
+
|
59
|
+
def test_simple_note_should_not_be_respond_to_creator
|
60
|
+
assert_equal false, SimpleNoteWithUser.new.respond_to?(:creator)
|
61
|
+
end
|
62
|
+
|
63
|
+
def test_note_should_be_respond_to_updater
|
64
|
+
assert NoteWithUser.new.respond_to? :updater
|
65
|
+
end
|
66
|
+
|
67
|
+
def test_simple_note_should_not_be_respond_to_updater
|
68
|
+
assert_equal false, SimpleNoteWithUser.new.respond_to?(:updater)
|
69
|
+
end
|
70
|
+
|
71
|
+
def test_note_should_be_respond_to_deleter
|
72
|
+
assert NoteWithUser.new.respond_to? :deleter
|
73
|
+
end
|
74
|
+
|
75
|
+
def test_simple_note_should_not_be_respond_to_deleter
|
76
|
+
assert_equal false, SimpleNoteWithUser.new.respond_to?(:deleter)
|
77
|
+
end
|
78
|
+
|
79
|
+
# test updater without creater, deleter
|
80
|
+
def test_updater_note_should_not_be_respond_to_creater
|
81
|
+
assert_equal false, UpdaterNoteWithUser.new.respond_to?(:creater)
|
82
|
+
end
|
83
|
+
def test_updater_note_should_be_respond_to_updater
|
84
|
+
assert UpdaterNoteWithUser.new.respond_to? :updater
|
85
|
+
end
|
86
|
+
def test_updater_note_should_not_be_respond_to_deleter
|
87
|
+
assert_equal false, UpdaterNoteWithUser.new.respond_to?(:deleter)
|
88
|
+
end
|
89
|
+
|
90
|
+
# test deleter without create, updater
|
91
|
+
def test_deleter_note_should_not_be_respond_to_creater
|
92
|
+
assert_equal false, DeleterNoteWithUser.new.respond_to?(:creater)
|
93
|
+
end
|
94
|
+
def test_deleter_note_should_not_be_respond_to_updater
|
95
|
+
assert_equal false, DeleterNoteWithUser.new.respond_to?(:updater)
|
96
|
+
end
|
97
|
+
def test_deleter_note_should_be_respond_to_deleter
|
98
|
+
assert DeleterNoteWithUser.new.respond_to?(:deleter)
|
99
|
+
end
|
100
|
+
|
101
|
+
# find with :for
|
102
|
+
def test_note_should_be_found_with_for
|
103
|
+
note = NoteWithUser.find(@note_created_by_user1.id, :for => @user2)
|
104
|
+
assert_equal(@user2, note.operator)
|
105
|
+
end
|
106
|
+
|
107
|
+
def test_note_should_be_found_with_for_through_named_scope
|
108
|
+
note = NoteWithUser.new_arrivals.find(@note_created_by_user1.id, :for => @user2)
|
109
|
+
assert_equal(@user2, note.operator)
|
110
|
+
end
|
111
|
+
|
112
|
+
# save or destory with xxxx_by and can get as a creator/updator/deleter
|
113
|
+
|
114
|
+
def test_note_should_be_created_with_operator
|
115
|
+
assert_equal @user1, @note_created_by_user1.operator
|
116
|
+
end
|
117
|
+
|
118
|
+
def test_note_should_be_created_with_created_by_and_updated_by
|
119
|
+
assert_equal @user1.id, @note_created_by_user1.created_by
|
120
|
+
assert_equal @user1.id, @note_created_by_user1.updated_by
|
121
|
+
@note_created_by_user1.reload
|
122
|
+
assert_equal @user1.id, @note_created_by_user1.created_by
|
123
|
+
assert_equal @user1.id, @note_created_by_user1.updated_by
|
124
|
+
assert_equal @user1, @note_created_by_user1.creator
|
125
|
+
assert_equal @user1, @note_created_by_user1.updater
|
126
|
+
end
|
127
|
+
|
128
|
+
def test_note_should_be_updated_with_updated_by
|
129
|
+
note = NoteWithUser.find(@note_created_by_user1.id, :for => @user2)
|
130
|
+
note.body = "changed"
|
131
|
+
note.save!
|
132
|
+
assert_equal(@user2.id, note.updated_by)
|
133
|
+
note.reload
|
134
|
+
assert_equal(@user2.id, note.updated_by)
|
135
|
+
end
|
136
|
+
|
137
|
+
def test_note_should_be_destroyed_with_deleted_by
|
138
|
+
note = NoteWithUser.find(@note_created_by_user1.id, :for => @user2)
|
139
|
+
note.destroy # logically
|
140
|
+
note.reload
|
141
|
+
raise "not deleted" unless note.deleted?
|
142
|
+
assert @user2.id, note.deleted_by
|
143
|
+
end
|
144
|
+
|
145
|
+
# reload
|
146
|
+
def test_reload_should_not_change_operator
|
147
|
+
@note_created_by_user1.reload
|
148
|
+
assert_equal @user1, @note_created_by_user1.operator
|
149
|
+
end
|
150
|
+
|
151
|
+
|
152
|
+
# has_many Association Test
|
153
|
+
def test_builded_memo_should_have_operator
|
154
|
+
note = NoteWithUser.find(@note_created_by_user1.id, :for => @user2)
|
155
|
+
memo = note.memos.build(:body => "memo")
|
156
|
+
assert_equal @user2, memo.operator
|
157
|
+
end
|
158
|
+
|
159
|
+
def test_created_memo_should_have_operator_and_created_by
|
160
|
+
note = NoteWithUser.find(@note_created_by_user1.id, :for => @user2)
|
161
|
+
memo = note.memos.create(:body => "memo")
|
162
|
+
assert_equal false, memo.new_record?
|
163
|
+
assert_equal @user2, memo.operator
|
164
|
+
assert_equal @user2.id, memo.created_by
|
165
|
+
end
|
166
|
+
|
167
|
+
def test_auto_found_memo_should_have_operator
|
168
|
+
note = NoteWithUser.find(@note_created_by_user1.id, :for => @user2)
|
169
|
+
note.memos.create!(:body => "memo")
|
170
|
+
assert_equal @user2, note.memos(true).first.operator
|
171
|
+
end
|
172
|
+
|
173
|
+
def test_manualy_found_memo_should_have_operator
|
174
|
+
note = NoteWithUser.find(@note_created_by_user1.id, :for => @user2)
|
175
|
+
note.memos.create!(:body => "memo")
|
176
|
+
assert_equal @user2, note.memos.find(:first).operator
|
177
|
+
end
|
178
|
+
|
179
|
+
def test_dynamically_found_memo_should_have_operator
|
180
|
+
note = NoteWithUser.find(@note_created_by_user1.id, :for => @user2)
|
181
|
+
note.memos.create!(:body => "memo")
|
182
|
+
assert_equal @user2, note.memos.find_by_body("memo").operator
|
183
|
+
end
|
184
|
+
|
185
|
+
def test_dynamically_found_memos_should_have_operator
|
186
|
+
note = NoteWithUser.find(@note_created_by_user1.id, :for => @user2)
|
187
|
+
note.memos.create!(:body => "memo")
|
188
|
+
assert note.memos.find_all_by_body("memo").all?{|m| m.operator == @user2}
|
189
|
+
end
|
190
|
+
|
191
|
+
|
192
|
+
def test_found_memo_through_named_scope_should_have_operator
|
193
|
+
note = NoteWithUser.find(@note_created_by_user1.id, :for => @user2)
|
194
|
+
note.memos.create!(:body => "memo")
|
195
|
+
assert_equal @user2, note.memos.new_arrivals.first.operator
|
196
|
+
end
|
197
|
+
|
198
|
+
|
199
|
+
def test_dynamically_found_memo_through_named_scope_should_have_operator
|
200
|
+
note = NoteWithUser.find(@note_created_by_user1.id, :for => @user2)
|
201
|
+
note.memos.create!(:body => "memo")
|
202
|
+
assert_equal @user2, note.memos.new_arrivals.find_by_body("memo").operator
|
203
|
+
end
|
204
|
+
|
205
|
+
|
206
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
|
2
|
+
require 'test_helper'
|
3
|
+
|
4
|
+
class NoteWithAdminUser < ActiveRecord::Base
|
5
|
+
set_table_name "notes"
|
6
|
+
|
7
|
+
def destroy_with_deleted_at
|
8
|
+
NoteWithAdminUser.update_all("deleted_at = '#{Time.now.to_s(:db)}'", "id = #{self.id}")
|
9
|
+
end
|
10
|
+
alias_method_chain :destroy, :deleted_at
|
11
|
+
def destory!
|
12
|
+
destory_without_deleted_at
|
13
|
+
end
|
14
|
+
|
15
|
+
def deleted?
|
16
|
+
self.deleted_at <= Time.now
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
class AdminUser < ActiveRecord::Base
|
21
|
+
set_table_name "users"
|
22
|
+
end
|
23
|
+
|
24
|
+
class RecordWithOperatorUserClassNameTest < ActiveSupport::TestCase
|
25
|
+
def setup
|
26
|
+
RecordWithOperator.config[:user_class_name] = "AdminUser"
|
27
|
+
@user1 = AdminUser.create!(:name => "user1")
|
28
|
+
@user2 = AdminUser.create!(:name => "user2")
|
29
|
+
@note_created_by_user1 = NoteWithAdminUser.create!(:body => "test", :operator => @user1)
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_note_should_be_created_with_operator
|
33
|
+
assert_equal @user1, @note_created_by_user1.operator
|
34
|
+
end
|
35
|
+
|
36
|
+
def test_note_should_be_created_with_created_by_and_updated_by
|
37
|
+
assert_equal @user1.id, @note_created_by_user1.created_by
|
38
|
+
assert_equal @user1.id, @note_created_by_user1.updated_by
|
39
|
+
@note_created_by_user1.reload
|
40
|
+
assert_equal @user1.id, @note_created_by_user1.created_by
|
41
|
+
assert_equal @user1.id, @note_created_by_user1.updated_by
|
42
|
+
assert @note_created_by_user1.creator.kind_of?(AdminUser)
|
43
|
+
end
|
44
|
+
|
45
|
+
def test_note_should_be_found_with_for
|
46
|
+
note = NoteWithAdminUser.find(@note_created_by_user1.id, :for => @user2)
|
47
|
+
assert_equal(@user2, note.operator)
|
48
|
+
end
|
49
|
+
|
50
|
+
def test_note_should_be_updated_with_updated_by
|
51
|
+
note = NoteWithAdminUser.find(@note_created_by_user1.id, :for => @user2)
|
52
|
+
note.body = "changed"
|
53
|
+
note.save!
|
54
|
+
assert_equal(@user2.id, note.updated_by)
|
55
|
+
note.reload
|
56
|
+
assert_equal(@user2.id, note.updated_by)
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
data/test/schema.rb
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
ActiveRecord::Schema.define(:version => 1) do
|
2
|
+
|
3
|
+
create_table :simple_notes, :force => true do |t|
|
4
|
+
t.column :body, :text
|
5
|
+
end
|
6
|
+
|
7
|
+
create_table :creator_notes, :force => true do |t|
|
8
|
+
t.column :body, :text
|
9
|
+
t.column :created_by, :integer
|
10
|
+
t.column :created_at, :datetime
|
11
|
+
end
|
12
|
+
|
13
|
+
create_table :updater_notes, :force => true do |t|
|
14
|
+
t.column :body, :text
|
15
|
+
t.column :updated_by, :integer
|
16
|
+
t.column :updated_at, :datetime
|
17
|
+
end
|
18
|
+
|
19
|
+
create_table :deleter_notes, :force => true do |t|
|
20
|
+
t.column :body, :text
|
21
|
+
t.column :deleted_by, :integer
|
22
|
+
t.column :deleted_at, :datetime
|
23
|
+
end
|
24
|
+
|
25
|
+
create_table :notes, :force => true do |t|
|
26
|
+
t.column :body, :text
|
27
|
+
t.column :created_by, :integer
|
28
|
+
t.column :created_at, :datetime
|
29
|
+
t.column :updated_by, :integer
|
30
|
+
t.column :updated_at, :datetime
|
31
|
+
t.column :deleted_by, :integer
|
32
|
+
t.column :deleted_at, :datetime
|
33
|
+
end
|
34
|
+
|
35
|
+
create_table :memos, :force => true do |t|
|
36
|
+
t.column :note_id, :integer
|
37
|
+
t.column :body, :text
|
38
|
+
t.column :created_by, :integer
|
39
|
+
t.column :created_at, :datetime
|
40
|
+
t.column :updated_by, :integer
|
41
|
+
t.column :updated_at, :datetime
|
42
|
+
t.column :deleted_by, :integer
|
43
|
+
t.column :deleted_at, :datetime
|
44
|
+
end
|
45
|
+
|
46
|
+
create_table :users, :force => true do |t|
|
47
|
+
t.column :name, :string
|
48
|
+
end
|
49
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require File.expand_path(File.join(File.dirname(__FILE__), '../../../../config/environment.rb'))
|
3
|
+
require 'test/unit'
|
4
|
+
require 'active_support'
|
5
|
+
require 'active_support/test_case'
|
6
|
+
|
7
|
+
config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))
|
8
|
+
ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + "/debug.log")
|
9
|
+
ActiveRecord::Base.establish_connection(config['mysql'])
|
10
|
+
|
11
|
+
load(File.dirname(__FILE__) + "/schema.rb")
|
data/uninstall.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
# Uninstall hook code here
|
metadata
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: nay-record_with_operator
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Yasuko Ohba
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-02-20 00:00:00 -08:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: activerecord
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 2.2.0
|
24
|
+
version:
|
25
|
+
description: Rails plugin to set created_by, updated_by, deleted_by to ActiveRecord objects. Supports associations.
|
26
|
+
email: y.ohba@everyleaf.com
|
27
|
+
executables: []
|
28
|
+
|
29
|
+
extensions: []
|
30
|
+
|
31
|
+
extra_rdoc_files:
|
32
|
+
- README.rdoc
|
33
|
+
- MIT-LICENSE
|
34
|
+
files:
|
35
|
+
- README.rdoc
|
36
|
+
- Rakefile
|
37
|
+
- MIT-LICENSE
|
38
|
+
- install.rb
|
39
|
+
- uninstall.rb
|
40
|
+
- init.rb
|
41
|
+
- lib/association_with_operator.rb
|
42
|
+
- lib/record_with_operator.rb
|
43
|
+
- tasks/record_with_operator_tasks.rake
|
44
|
+
- test/database.yml
|
45
|
+
- test/record_with_operator_test.rb
|
46
|
+
- test/record_with_operator_user_class_name_test.rb
|
47
|
+
- test/schema.rb
|
48
|
+
- test/test_helper.rb
|
49
|
+
has_rdoc: true
|
50
|
+
homepage: http://github.com/nay/record_with_operator/tree
|
51
|
+
post_install_message:
|
52
|
+
rdoc_options:
|
53
|
+
- --line-numbers
|
54
|
+
- --inline-source
|
55
|
+
require_paths:
|
56
|
+
- lib
|
57
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: "0"
|
62
|
+
version:
|
63
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - ">="
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: "0"
|
68
|
+
version:
|
69
|
+
requirements: []
|
70
|
+
|
71
|
+
rubyforge_project:
|
72
|
+
rubygems_version: 1.2.0
|
73
|
+
signing_key:
|
74
|
+
specification_version: 2
|
75
|
+
summary: Rails plugin to set created_by, updated_by, deleted_by to ActiveRecord objects. Supports associations.
|
76
|
+
test_files:
|
77
|
+
- test/database.yml
|
78
|
+
- test/record_with_operator_test.rb
|
79
|
+
- test/record_with_operator_user_class_name_test.rb
|
80
|
+
- test/schema.rb
|
81
|
+
- test/test_helper.rb
|