nay-record_with_operator 0.0.7 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
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.7"
15
+ VER = "0.0.8"
16
16
  CLEAN.include ['pkg']
17
17
 
18
18
  desc 'Default: run unit tests.'
@@ -8,6 +8,24 @@ module RecordWithOperator
8
8
 
9
9
  def self.included(base)
10
10
  class << base
11
+ def reflections_with_operator
12
+ create_operator_associations
13
+ reflections_without_operator
14
+ end
15
+ alias_method_chain :reflections, :operator
16
+
17
+ def operator_associations_created?
18
+ @operator_associations_created
19
+ end
20
+
21
+ def create_operator_associations
22
+ return if operator_associations_created?
23
+ belongs_to :creator, :foreign_key => "created_by", :class_name => RecordWithOperator.config[:user_class_name] if column_names.include?('created_by')
24
+ belongs_to :updater, :foreign_key => "updated_by", :class_name => RecordWithOperator.config[:user_class_name] if column_names.include?('updated_by')
25
+ belongs_to :deleter, :foreign_key => "deleted_by", :class_name => RecordWithOperator.config[:user_class_name] if column_names.include?('deleted_by')
26
+ @operator_associations_created = true
27
+ end
28
+
11
29
  def has_many_with_operator(*args, &extension)
12
30
  options = args.extract_options!
13
31
  # add AssociationWithOprator to :extend
@@ -57,35 +75,35 @@ module RecordWithOperator
57
75
  alias_method_chain :validate_find_options, :for
58
76
 
59
77
  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
78
+ # define_method association, association= ...
79
+ def association_accessor_methods_with_operator(reflection, association_proxy_class)
80
+ association_accessor_methods_without_operator(reflection, association_proxy_class)
81
+ define_method("#{reflection.name}_with_operator") do |*params|
82
+ r = send("#{reflection.name}_without_operator", *params)
83
+ r.operator ||= self.operator if r && r.respond_to?(:operator=)
84
+ r
85
+ end
86
+ alias_method_chain "#{reflection.name}".to_sym, :operator
69
87
 
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
88
+ define_method("#{reflection.name}_with_operator=") do |new_value|
89
+ new_value.operator ||= self.operator if new_value.respond_to?(:operator=)
90
+ send("#{reflection.name}_without_operator=", new_value)
75
91
  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
92
+ alias_method_chain "#{reflection.name}=".to_sym, :operator
93
+ end
94
+ alias_method_chain :association_accessor_methods, :operator
95
+
96
+ # define_method build_association, create_association ...
97
+ def association_constructor_method_with_operator(constructor, reflection, association_proxy_class)
98
+ association_constructor_method_without_operator(constructor, reflection, association_proxy_class)
99
+ define_method("#{constructor}_#{reflection.name}_with_operator") do |*params|
100
+ options = { :operator => self.operator }
101
+ params.empty? ? params[0] = options : params.first.merge!(options)
102
+ self.send("#{constructor}_#{reflection.name}_without_operator", *params)
87
103
  end
88
- alias_method_chain :association_constructor_method, :operator
104
+ alias_method_chain "#{constructor}_#{reflection.name}".to_sym, :operator
105
+ end
106
+ alias_method_chain :association_constructor_method, :operator
89
107
  end
90
108
 
91
109
  base.before_create :set_created_by
@@ -115,14 +133,8 @@ module RecordWithOperator
115
133
  def method_missing(method, *args)
116
134
  return super unless respond_to?(method)
117
135
  case method.to_sym
118
- when :creator
119
- self.class.belongs_to :creator, :foreign_key => "created_by", :class_name => RecordWithOperator.config[:user_class_name]
120
- send(method, *args)
121
- when :updater
122
- self.class.belongs_to :updater, :foreign_key => "updated_by", :class_name => RecordWithOperator.config[:user_class_name]
123
- send(method, *args)
124
- when :deleter
125
- self.class.belongs_to :deletor, :foreign_key => "deleted_by", :class_name => RecordWithOperator.config[:user_class_name]
136
+ when :creator, :updater, :deleter
137
+ self.class.create_operator_associations
126
138
  send(method, *args)
127
139
  else
128
140
  super
@@ -0,0 +1,39 @@
1
+ require 'test_helper'
2
+
3
+ class User < ActiveRecord::Base
4
+ end
5
+
6
+ class NoteForReflectionTest < ActiveRecord::Base
7
+ set_table_name "notes"
8
+ end
9
+
10
+ class CreatorNoteForReflectionTest < ActiveRecord::Base
11
+ set_table_name "creator_notes"
12
+ end
13
+
14
+ class RecordWithOperatorReflectionTest < ActiveSupport::TestCase
15
+ def setup
16
+ RecordWithOperator.config[:user_class_name] = "User"
17
+ @user1 = User.create!(:name => "user1")
18
+ raise "@user1.id is nil" unless @user1.id
19
+ @note = NoteForReflectionTest.create!(:body => "test", :operator => @user1)
20
+
21
+ @creator_note = CreatorNoteForReflectionTest.create!(:body => "test", :operator => @user1)
22
+ end
23
+
24
+ def test_include
25
+ assert NoteForReflectionTest.find(:all, :include => [:creator, :updater])
26
+ end
27
+
28
+ def test_joins
29
+ assert NoteForReflectionTest.find(:all, :joins => [:creator, :updater])
30
+ end
31
+
32
+ def test_include_missing_association
33
+ assert_raise(ActiveRecord::ConfigurationError) { CreatorNoteForReflectionTest.find(:all, :include => [:updater]) }
34
+ end
35
+
36
+ def test_joins_missing_association
37
+ assert_raise(ActiveRecord::ConfigurationError) { CreatorNoteForReflectionTest.find(:all, :joins => [:updater]) }
38
+ end
39
+ end
@@ -62,7 +62,6 @@ class RecordWithOperatorTest < ActiveSupport::TestCase
62
62
  end
63
63
 
64
64
  # creator/updater/deleter association operation
65
-
66
65
  def test_note_should_be_respond_to_creator
67
66
  assert NoteWithUser.new.respond_to? :creator
68
67
  end
@@ -219,6 +218,4 @@ class RecordWithOperatorTest < ActiveSupport::TestCase
219
218
  note.memos.create!(:body => "memo")
220
219
  assert_equal @user2, note.memos.new_arrivals.find_by_body("memo").operator
221
220
  end
222
-
223
-
224
- end
221
+ 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.7
4
+ version: 0.0.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yasuko Ohba
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-02-23 00:00:00 -08:00
12
+ date: 2009-03-11 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -44,6 +44,7 @@ files:
44
44
  - rails/init.rb
45
45
  - test/record_with_operator_belongs_to_association_test.rb
46
46
  - test/record_with_operator_has_one_association_test.rb
47
+ - test/record_with_operator_reflection_test.rb
47
48
  - test/record_with_operator_test.rb
48
49
  - test/record_with_operator_user_class_name_test.rb
49
50
  - test/schema.rb
@@ -79,6 +80,7 @@ summary: Rails plugin to set created_by, updated_by, deleted_by to ActiveRecord
79
80
  test_files:
80
81
  - test/record_with_operator_belongs_to_association_test.rb
81
82
  - test/record_with_operator_has_one_association_test.rb
83
+ - test/record_with_operator_reflection_test.rb
82
84
  - test/record_with_operator_test.rb
83
85
  - test/record_with_operator_user_class_name_test.rb
84
86
  - test/schema.rb