nay-record_with_operator 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +1 -1
- data/lib/record_with_operator.rb +32 -1
- data/test/record_with_operator_belongs_to_association_test.rb +67 -0
- data/test/record_with_operator_has_one_association_test.rb +69 -0
- data/test/schema.rb +2 -1
- metadata +5 -1
- data/init.rb +0 -1
data/Rakefile
CHANGED
@@ -12,7 +12,7 @@ DESCRIPTION = "Rails plugin to set created_by, updated_by, deleted_by to ActiveR
|
|
12
12
|
GITHUB_PROJECT = "record_with_operator"
|
13
13
|
HOMEPAGE = "http://github.com/nay/#{GITHUB_PROJECT}/tree"
|
14
14
|
BIN_FILES = %w( )
|
15
|
-
VER = "0.0.
|
15
|
+
VER = "0.0.3"
|
16
16
|
CLEAN.include ['pkg']
|
17
17
|
|
18
18
|
desc 'Default: run unit tests.'
|
data/lib/record_with_operator.rb
CHANGED
@@ -55,6 +55,37 @@ module RecordWithOperator
|
|
55
55
|
end
|
56
56
|
|
57
57
|
alias_method_chain :validate_find_options, :for
|
58
|
+
|
59
|
+
private
|
60
|
+
# define_method association, association= ...
|
61
|
+
def association_accessor_methods_with_operator(reflection, association_proxy_class)
|
62
|
+
association_accessor_methods_without_operator(reflection, association_proxy_class)
|
63
|
+
define_method("#{reflection.name}_with_operator") do |*params|
|
64
|
+
r = send("#{reflection.name}_without_operator", *params)
|
65
|
+
r.operator ||= self.operator if r && r.respond_to?(:operator=)
|
66
|
+
r
|
67
|
+
end
|
68
|
+
alias_method_chain "#{reflection.name}".to_sym, :operator
|
69
|
+
|
70
|
+
define_method("#{reflection.name}_with_operator=") do |new_value|
|
71
|
+
new_value.operator ||= self.operator if new_value.respond_to?(:operator=)
|
72
|
+
send("#{reflection.name}_without_operator=", new_value)
|
73
|
+
end
|
74
|
+
alias_method_chain "#{reflection.name}=".to_sym, :operator
|
75
|
+
end
|
76
|
+
alias_method_chain :association_accessor_methods, :operator
|
77
|
+
|
78
|
+
# define_method build_association, create_association ...
|
79
|
+
def association_constructor_method_with_operator(constructor, reflection, association_proxy_class)
|
80
|
+
association_constructor_method_without_operator(constructor, reflection, association_proxy_class)
|
81
|
+
define_method("#{constructor}_#{reflection.name}_with_operator") do |*params|
|
82
|
+
options = { :operator => self.operator }
|
83
|
+
params.empty? ? params[0] = options : params.first.merge!(options)
|
84
|
+
self.send("#{constructor}_#{reflection.name}_without_operator", *params)
|
85
|
+
end
|
86
|
+
alias_method_chain "#{constructor}_#{reflection.name}".to_sym, :operator
|
87
|
+
end
|
88
|
+
alias_method_chain :association_constructor_method, :operator
|
58
89
|
end
|
59
90
|
|
60
91
|
base.before_create :set_created_by
|
@@ -113,4 +144,4 @@ module RecordWithOperator
|
|
113
144
|
self.updated_by = operator.id
|
114
145
|
end
|
115
146
|
|
116
|
-
end
|
147
|
+
end
|
@@ -0,0 +1,67 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class User < ActiveRecord::Base
|
4
|
+
end
|
5
|
+
|
6
|
+
class NoteBelongsToMemo < ActiveRecord::Base
|
7
|
+
set_table_name "notes"
|
8
|
+
belongs_to :memo
|
9
|
+
end
|
10
|
+
|
11
|
+
class Memo < ActiveRecord::Base
|
12
|
+
set_table_name "memos"
|
13
|
+
end
|
14
|
+
|
15
|
+
class RecordWithOperatorBelongsTo < ActiveSupport::TestCase
|
16
|
+
def setup
|
17
|
+
RecordWithOperator.config[:user_class_name] = "User"
|
18
|
+
@user1 = User.create!(:name => "user1")
|
19
|
+
@user2 = User.create!(:name => "user2")
|
20
|
+
@note_created_by_user1 = NoteBelongsToMemo.create!(:body => "test", :operator => @user1)
|
21
|
+
end
|
22
|
+
|
23
|
+
# belongs_to Association Test
|
24
|
+
# build_association
|
25
|
+
def test_build_memo_should_have_operator
|
26
|
+
note = NoteBelongsToMemo.find(@note_created_by_user1.id, :for => @user2)
|
27
|
+
memo = note.build_memo(:body => "memo")
|
28
|
+
assert_equal @user2, memo.operator
|
29
|
+
end
|
30
|
+
|
31
|
+
# create_association
|
32
|
+
def test_create_memo_should_have_operator_and_created_by
|
33
|
+
note = NoteBelongsToMemo.find(@note_created_by_user1.id, :for => @user2)
|
34
|
+
memo = note.create_memo(:body => "memo")
|
35
|
+
assert_equal false, memo.new_record?
|
36
|
+
assert_equal @user2, memo.operator
|
37
|
+
assert_equal @user2.id, memo.created_by
|
38
|
+
end
|
39
|
+
|
40
|
+
# association=
|
41
|
+
def test_memo_eql_should_have_operator_and_created_by
|
42
|
+
note = NoteBelongsToMemo.find(@note_created_by_user1.id, :for => @user2)
|
43
|
+
memo = Memo.new(:body => "memo")
|
44
|
+
note.memo = memo # not save
|
45
|
+
assert_equal @user2, memo.operator
|
46
|
+
end
|
47
|
+
|
48
|
+
# association
|
49
|
+
def test_auto_found_memo_should_have_operator
|
50
|
+
note = NoteBelongsToMemo.find(@note_created_by_user1.id, :for => @user2)
|
51
|
+
note.create_memo(:body => "memo")
|
52
|
+
assert_equal @user2, note.memo(true).operator
|
53
|
+
end
|
54
|
+
|
55
|
+
# association.nil?
|
56
|
+
def test_memo_nil_should_be_false_if_note_belongs_to_memo
|
57
|
+
note = NoteBelongsToMemo.find(@note_created_by_user1.id)
|
58
|
+
note.create_memo(:body => "memo")
|
59
|
+
assert !note.memo.nil?
|
60
|
+
end
|
61
|
+
|
62
|
+
# association.nil?
|
63
|
+
def test_memo_nil_should_be_true_if_note_is_independent
|
64
|
+
note = NoteBelongsToMemo.find(@note_created_by_user1.id)
|
65
|
+
assert note.memo.nil?
|
66
|
+
end
|
67
|
+
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class User < ActiveRecord::Base
|
4
|
+
end
|
5
|
+
|
6
|
+
class NoteHasOneMemo < ActiveRecord::Base
|
7
|
+
set_table_name "notes"
|
8
|
+
has_one :memo, :class_name => "Memo", :foreign_key => "note_id"
|
9
|
+
end
|
10
|
+
|
11
|
+
class Memo < ActiveRecord::Base
|
12
|
+
set_table_name "memos"
|
13
|
+
end
|
14
|
+
|
15
|
+
class RecordWithOperatorHasOneAssociationTest < ActiveSupport::TestCase
|
16
|
+
def setup
|
17
|
+
RecordWithOperator.config[:user_class_name] = "User"
|
18
|
+
@user1 = User.create!(:name => "user1")
|
19
|
+
@user2 = User.create!(:name => "user2")
|
20
|
+
@note_created_by_user1 = NoteHasOneMemo.create!(:body => "test", :operator => @user1)
|
21
|
+
end
|
22
|
+
|
23
|
+
# has_one Association Test
|
24
|
+
# build_association
|
25
|
+
def test_build_memo_should_have_operator
|
26
|
+
note = NoteHasOneMemo.find(@note_created_by_user1.id, :for => @user2)
|
27
|
+
memo = note.build_memo(:body => "memo")
|
28
|
+
assert_equal @user2, memo.operator
|
29
|
+
end
|
30
|
+
|
31
|
+
# create_association
|
32
|
+
def test_create_memo_should_have_operator_and_created_by
|
33
|
+
note = NoteHasOneMemo.find(@note_created_by_user1.id, :for => @user2)
|
34
|
+
memo = note.create_memo(:body => "memo")
|
35
|
+
assert_equal false, memo.new_record?
|
36
|
+
assert_equal @user2, memo.operator
|
37
|
+
assert_equal @user2.id, memo.created_by
|
38
|
+
end
|
39
|
+
|
40
|
+
# association=
|
41
|
+
def test_memo_eql_should_have_operator_and_created_by
|
42
|
+
note = NoteHasOneMemo.find(@note_created_by_user1.id, :for => @user2)
|
43
|
+
memo = Memo.new(:body => "memo")
|
44
|
+
note.memo = memo
|
45
|
+
assert_equal false, memo.new_record?
|
46
|
+
assert_equal @user2, memo.operator
|
47
|
+
assert_equal @user2.id, memo.created_by
|
48
|
+
end
|
49
|
+
|
50
|
+
# association
|
51
|
+
def test_auto_found_memo_should_have_operator
|
52
|
+
note = NoteHasOneMemo.find(@note_created_by_user1.id, :for => @user2)
|
53
|
+
note.create_memo(:body => "memo")
|
54
|
+
assert_equal @user2, note.memo(true).operator
|
55
|
+
end
|
56
|
+
|
57
|
+
# association.nil?
|
58
|
+
def test_memo_nil_should_be_false_if_note_has_memo
|
59
|
+
note = NoteHasOneMemo.find(@note_created_by_user1.id)
|
60
|
+
note.create_memo(:body => "memo")
|
61
|
+
assert !note.memo.nil?
|
62
|
+
end
|
63
|
+
|
64
|
+
# association.nil?
|
65
|
+
def test_memo_nil_should_be_true_if_note_has_no_memo
|
66
|
+
note = NoteHasOneMemo.find(@note_created_by_user1.id)
|
67
|
+
assert note.memo.nil?
|
68
|
+
end
|
69
|
+
end
|
data/test/schema.rb
CHANGED
@@ -23,6 +23,7 @@ ActiveRecord::Schema.define(:version => 1) do
|
|
23
23
|
end
|
24
24
|
|
25
25
|
create_table :notes, :force => true do |t|
|
26
|
+
t.column :memo_id, :integer
|
26
27
|
t.column :body, :text
|
27
28
|
t.column :created_by, :integer
|
28
29
|
t.column :created_at, :datetime
|
@@ -46,4 +47,4 @@ ActiveRecord::Schema.define(:version => 1) do
|
|
46
47
|
create_table :users, :force => true do |t|
|
47
48
|
t.column :name, :string
|
48
49
|
end
|
49
|
-
end
|
50
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: nay-record_with_operator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Yasuko Ohba
|
@@ -42,6 +42,8 @@ files:
|
|
42
42
|
- lib/record_with_operator.rb
|
43
43
|
- tasks/record_with_operator_tasks.rake
|
44
44
|
- test/database.yml
|
45
|
+
- test/record_with_operator_belongs_to_association_test.rb
|
46
|
+
- test/record_with_operator_has_one_association_test.rb
|
45
47
|
- test/record_with_operator_test.rb
|
46
48
|
- test/record_with_operator_user_class_name_test.rb
|
47
49
|
- test/schema.rb
|
@@ -75,6 +77,8 @@ specification_version: 2
|
|
75
77
|
summary: Rails plugin to set created_by, updated_by, deleted_by to ActiveRecord objects. Supports associations.
|
76
78
|
test_files:
|
77
79
|
- test/database.yml
|
80
|
+
- test/record_with_operator_belongs_to_association_test.rb
|
81
|
+
- test/record_with_operator_has_one_association_test.rb
|
78
82
|
- test/record_with_operator_test.rb
|
79
83
|
- test/record_with_operator_user_class_name_test.rb
|
80
84
|
- test/schema.rb
|
data/init.rb
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
ActiveRecord::Base.instance_eval{include RecordWithOperator} unless ActiveRecord::Base.include?(RecordWithOperator)
|