record_with_operator 0.0.22 → 0.1.0
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/.gitignore +1 -0
- data/README.rdoc +6 -1
- data/Rakefile +0 -44
- data/init.rb +1 -1
- data/lib/record_with_operator/associations/extension.rb +24 -0
- data/lib/record_with_operator/extension.rb +146 -0
- data/lib/record_with_operator/relation_methods.rb +66 -0
- data/lib/record_with_operator/utility.rb +19 -0
- data/lib/record_with_operator/version.rb +2 -2
- data/lib/record_with_operator.rb +5 -166
- data/record_with_operator.gemspec +11 -3
- data/test/database.yml +16 -16
- data/test/record_with_operator_has_many_dependent_test.rb +8 -4
- data/test/record_with_operator_test.rb +10 -16
- data/test/test_helper.rb +5 -5
- metadata +100 -24
- data/.idea/inspectionProfiles/Project_Default.xml +0 -11
- data/.idea/inspectionProfiles/profiles_settings.xml +0 -7
- data/.idea/workspace.xml +0 -455
- data/lib/association_with_operator.rb +0 -34
- data/record_with_operator/.gitignore +0 -4
- data/record_with_operator/Gemfile +0 -4
- data/record_with_operator/Rakefile +0 -2
- data/record_with_operator/lib/record_with_operator/version.rb +0 -3
- data/record_with_operator/lib/record_with_operator.rb +0 -3
- data/record_with_operator/record_with_operator.gemspec +0 -21
data/.gitignore
CHANGED
data/README.rdoc
CHANGED
@@ -16,7 +16,12 @@ This plugin assumes that the logical deletion is kicked by record.destory.
|
|
16
16
|
|
17
17
|
== Installation
|
18
18
|
|
19
|
-
> ./script/plugin install git://github.com/
|
19
|
+
> ./script/plugin install git://github.com/nay/record_with_operator.git
|
20
|
+
|
21
|
+
Or
|
22
|
+
|
23
|
+
## Gemfile for Rails3
|
24
|
+
gem 'record_with_operator'
|
20
25
|
|
21
26
|
== Example
|
22
27
|
|
data/Rakefile
CHANGED
@@ -5,15 +5,6 @@ require 'rake/testtask'
|
|
5
5
|
require 'rake/rdoctask'
|
6
6
|
require 'rake/gempackagetask'
|
7
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
|
-
CLEAN.include ['pkg']
|
16
|
-
|
17
8
|
desc 'Default: run unit tests.'
|
18
9
|
task :default => :test
|
19
10
|
|
@@ -33,38 +24,3 @@ Rake::RDocTask.new(:rdoc) do |rdoc|
|
|
33
24
|
rdoc.rdoc_files.include('README.rdoc')
|
34
25
|
rdoc.rdoc_files.include('lib/**/*.rb')
|
35
26
|
end
|
36
|
-
|
37
|
-
spec = Gem::Specification.new do |s|
|
38
|
-
s.name = NAME
|
39
|
-
s.version = VER
|
40
|
-
s.platform = Gem::Platform::RUBY
|
41
|
-
s.has_rdoc = true
|
42
|
-
s.extra_rdoc_files = ["README.rdoc", "MIT-LICENSE"]
|
43
|
-
s.rdoc_options += ['--line-numbers', '--inline-source']
|
44
|
-
s.summary = DESCRIPTION
|
45
|
-
s.description = DESCRIPTION
|
46
|
-
s.author = AUTHOR
|
47
|
-
s.email = EMAIL
|
48
|
-
s.homepage = HOMEPAGE
|
49
|
-
s.executables = BIN_FILES
|
50
|
-
s.bindir = "bin"
|
51
|
-
s.require_path = "lib"
|
52
|
-
s.test_files = Dir["test/*.{rb,yml}"]
|
53
|
-
|
54
|
-
s.add_dependency('activerecord', '>=2.2.0')
|
55
|
-
|
56
|
-
s.files = %w(README.rdoc Rakefile MIT-LICENSE) +
|
57
|
-
%w(install.rb uninstall.rb) +
|
58
|
-
Dir.glob("{bin,doc,lib,tasks,rails}/**/*")
|
59
|
-
|
60
|
-
end
|
61
|
-
|
62
|
-
Rake::GemPackageTask.new(spec) do |p|
|
63
|
-
p.need_tar = true
|
64
|
-
p.gem_spec = spec
|
65
|
-
end
|
66
|
-
|
67
|
-
desc 'Update gem spec'
|
68
|
-
task :gemspec do
|
69
|
-
open("#{NAME}.gemspec", "w").write spec.to_ruby
|
70
|
-
end
|
data/init.rb
CHANGED
@@ -1 +1 @@
|
|
1
|
-
|
1
|
+
require 'record_with_operator'
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'record_with_operator/utility'
|
2
|
+
|
3
|
+
module RecordWithOperator
|
4
|
+
module Associations
|
5
|
+
module Extension
|
6
|
+
include RecordWithOperator::Utility
|
7
|
+
|
8
|
+
def find(*args)
|
9
|
+
set_operator_to_records(super, @owner.operator)
|
10
|
+
end
|
11
|
+
|
12
|
+
def method_missing(method, *args)
|
13
|
+
set_operator_to_records(super, @owner.operator)
|
14
|
+
end
|
15
|
+
|
16
|
+
protected
|
17
|
+
def construct_scope
|
18
|
+
scoping = super
|
19
|
+
scoping[:find][:for] = @owner.operator
|
20
|
+
scoping
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,146 @@
|
|
1
|
+
require 'helpers/migration_helper.rb'
|
2
|
+
|
3
|
+
module RecordWithOperator
|
4
|
+
def self.config
|
5
|
+
@config ||= {:user_class_name => "User", :operator_association_options => {}}
|
6
|
+
@config
|
7
|
+
end
|
8
|
+
|
9
|
+
module Extension
|
10
|
+
extend ActiveSupport::Concern
|
11
|
+
|
12
|
+
included do
|
13
|
+
attr_accessor :operator
|
14
|
+
|
15
|
+
before_create :set_created_by
|
16
|
+
before_save :set_updated_by
|
17
|
+
before_destroy :set_deleted_by
|
18
|
+
|
19
|
+
class << self
|
20
|
+
alias_method_chain :reflections, :operator
|
21
|
+
alias_method_chain :has_many, :operator
|
22
|
+
alias_method_chain :association_accessor_methods, :operator
|
23
|
+
alias_method_chain :association_constructor_method, :operator
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
module ClassMethods
|
28
|
+
def reflections_with_operator
|
29
|
+
create_operator_associations
|
30
|
+
reflections_without_operator
|
31
|
+
end
|
32
|
+
|
33
|
+
def operator_associations_created?
|
34
|
+
@operator_associations_created
|
35
|
+
end
|
36
|
+
|
37
|
+
def create_operator_associations
|
38
|
+
return if operator_associations_created?
|
39
|
+
@operator_associations_created = true
|
40
|
+
|
41
|
+
if self.table_exists?
|
42
|
+
belongs_to :creator, {:foreign_key => "created_by", :class_name => RecordWithOperator.config[:user_class_name]}.merge(RecordWithOperator.config[:operator_association_options]) if column_names.include?('created_by')
|
43
|
+
belongs_to :updater, {:foreign_key => "updated_by", :class_name => RecordWithOperator.config[:user_class_name]}.merge(RecordWithOperator.config[:operator_association_options]) if column_names.include?('updated_by')
|
44
|
+
belongs_to :deleter, {:foreign_key => "deleted_by", :class_name => RecordWithOperator.config[:user_class_name]}.merge(RecordWithOperator.config[:operator_association_options]) if column_names.include?('deleted_by')
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def has_many_with_operator(*args, &extension)
|
49
|
+
options = args.extract_options!
|
50
|
+
# add AssociationWithOprator to :extend
|
51
|
+
if options[:extend]
|
52
|
+
options[:extend] = [options[:extend]] unless options[:extend].kind_of? Array
|
53
|
+
options[:extend] << RecordWithOperator::Associations::Extension
|
54
|
+
else
|
55
|
+
options[:extend] = RecordWithOperator::Associations::Extension
|
56
|
+
end
|
57
|
+
# add :set_operator to :before_add
|
58
|
+
if options[:before_add]
|
59
|
+
options[:before_add] = [options[:before_add]] unless options[:before_add].kind_of? Array
|
60
|
+
options[:before_add] << :set_operator
|
61
|
+
else
|
62
|
+
options[:before_add] = :set_operator
|
63
|
+
end
|
64
|
+
args << options
|
65
|
+
has_many_without_operator(*args, &extension)
|
66
|
+
end
|
67
|
+
|
68
|
+
private
|
69
|
+
# define_method association, association= ...
|
70
|
+
def association_accessor_methods_with_operator(reflection, association_proxy_class)
|
71
|
+
association_accessor_methods_without_operator(reflection, association_proxy_class)
|
72
|
+
define_method("#{reflection.name}_with_operator") do |*params|
|
73
|
+
r = send("#{reflection.name}_without_operator", *params)
|
74
|
+
r.operator ||= self.operator if r && r.respond_to?(:operator=)
|
75
|
+
r
|
76
|
+
end
|
77
|
+
alias_method_chain "#{reflection.name}".to_sym, :operator
|
78
|
+
|
79
|
+
define_method("#{reflection.name}_with_operator=") do |new_value|
|
80
|
+
new_value.operator ||= self.operator if new_value.respond_to?(:operator=)
|
81
|
+
send("#{reflection.name}_without_operator=", new_value)
|
82
|
+
end
|
83
|
+
alias_method_chain "#{reflection.name}=".to_sym, :operator
|
84
|
+
end
|
85
|
+
|
86
|
+
# define_method build_association, create_association ...
|
87
|
+
def association_constructor_method_with_operator(constructor, reflection, association_proxy_class)
|
88
|
+
association_constructor_method_without_operator(constructor, reflection, association_proxy_class)
|
89
|
+
define_method("#{constructor}_#{reflection.name}_with_operator") do |*params|
|
90
|
+
options = { :operator => self.operator }
|
91
|
+
params.empty? ? params[0] = options : params.first.merge!(options)
|
92
|
+
self.send("#{constructor}_#{reflection.name}_without_operator", *params)
|
93
|
+
end
|
94
|
+
alias_method_chain "#{constructor}_#{reflection.name}".to_sym, :operator
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
module InstanceMethods
|
99
|
+
def respond_to?(name, priv=false)
|
100
|
+
case name.to_sym
|
101
|
+
when :creator
|
102
|
+
respond_to? :created_by
|
103
|
+
when :updater
|
104
|
+
respond_to? :updated_by
|
105
|
+
when :deleter
|
106
|
+
respond_to? :deleted_by
|
107
|
+
else
|
108
|
+
super
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
private
|
113
|
+
def set_operator(child)
|
114
|
+
child.operator ||= self.operator
|
115
|
+
end
|
116
|
+
|
117
|
+
def method_missing(method, *args)
|
118
|
+
return super unless respond_to?(method)
|
119
|
+
case method.to_sym
|
120
|
+
when :creator, :updater, :deleter
|
121
|
+
self.class.create_operator_associations
|
122
|
+
send(method, *args)
|
123
|
+
else
|
124
|
+
super
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
def set_created_by
|
129
|
+
return unless respond_to?(:created_by=) && operator
|
130
|
+
self.created_by = operator.id
|
131
|
+
end
|
132
|
+
|
133
|
+
def set_updated_by
|
134
|
+
return unless respond_to?(:updated_by=) && operator && changed? # no use setting updating_by when it's not changed
|
135
|
+
self.updated_by = operator.id
|
136
|
+
end
|
137
|
+
|
138
|
+
def set_deleted_by
|
139
|
+
return unless self.class.column_names.include?("deleted_by") && operator
|
140
|
+
return if frozen?
|
141
|
+
self.class.update_all("deleted_by = #{operator.id}", ["#{self.class.primary_key} = ?", id])
|
142
|
+
self.deleted_by = operator.id
|
143
|
+
end
|
144
|
+
end
|
145
|
+
end
|
146
|
+
end
|
@@ -0,0 +1,66 @@
|
|
1
|
+
require 'record_with_operator/utility'
|
2
|
+
|
3
|
+
module RecordWithOperator
|
4
|
+
module RelationMethods
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
included do
|
8
|
+
attr_accessor :operator
|
9
|
+
end
|
10
|
+
|
11
|
+
module InstanceMethods
|
12
|
+
include RecordWithOperator::Utility
|
13
|
+
|
14
|
+
def merge(r)
|
15
|
+
merged_relation = super
|
16
|
+
merged_relation.operator ||= r.operator
|
17
|
+
merged_relation
|
18
|
+
end
|
19
|
+
|
20
|
+
def except(*skips)
|
21
|
+
result = super
|
22
|
+
result.operator ||= operator
|
23
|
+
result
|
24
|
+
end
|
25
|
+
|
26
|
+
def only(*onlies)
|
27
|
+
result = super
|
28
|
+
result.operator ||= operator
|
29
|
+
result
|
30
|
+
end
|
31
|
+
|
32
|
+
def for(operator)
|
33
|
+
relation = clone
|
34
|
+
relation.operator ||= operator
|
35
|
+
relation
|
36
|
+
end
|
37
|
+
|
38
|
+
def apply_finder_options(options)
|
39
|
+
finders = options.dup
|
40
|
+
operator = finders.delete(:for)
|
41
|
+
relation = super(finders)
|
42
|
+
relation.for(operator)
|
43
|
+
end
|
44
|
+
|
45
|
+
def find(*args)
|
46
|
+
set_operator_to_records(super, operator)
|
47
|
+
end
|
48
|
+
|
49
|
+
def first(*args)
|
50
|
+
set_operator_to_records(super, operator)
|
51
|
+
end
|
52
|
+
|
53
|
+
def last(*args)
|
54
|
+
set_operator_to_records(super, operator)
|
55
|
+
end
|
56
|
+
|
57
|
+
def all(*args)
|
58
|
+
set_operator_to_records(super, operator)
|
59
|
+
end
|
60
|
+
|
61
|
+
def to_a
|
62
|
+
set_operator_to_records(super, operator)
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module RecordWithOperator
|
2
|
+
module Utility
|
3
|
+
private
|
4
|
+
def set_operator_to_records(records, operator)
|
5
|
+
return records unless operator
|
6
|
+
|
7
|
+
unless ActiveRecord::Relation === records
|
8
|
+
if records.respond_to?(:operator=)
|
9
|
+
records.operator = operator
|
10
|
+
elsif Array === records
|
11
|
+
records.each do |record|
|
12
|
+
record.operator = operator if record.respond_to?(:operator=)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
records
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -1,3 +1,3 @@
|
|
1
1
|
module RecordWithOperator
|
2
|
-
VERSION = "0.0
|
3
|
-
end
|
2
|
+
VERSION = "0.1.0"
|
3
|
+
end
|
data/lib/record_with_operator.rb
CHANGED
@@ -1,167 +1,6 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
def self.config
|
5
|
-
@config ||= {:user_class_name => "User", :operator_association_options => {}}
|
6
|
-
@config
|
7
|
-
end
|
1
|
+
require 'record_with_operator/relation_methods'
|
2
|
+
require 'record_with_operator/associations/extension'
|
3
|
+
require 'record_with_operator/extension'
|
8
4
|
|
9
|
-
|
10
|
-
|
11
|
-
def self.included(base)
|
12
|
-
class << base
|
13
|
-
def reflections_with_operator
|
14
|
-
create_operator_associations
|
15
|
-
reflections_without_operator
|
16
|
-
end
|
17
|
-
alias_method_chain :reflections, :operator
|
18
|
-
|
19
|
-
def operator_associations_created?
|
20
|
-
@operator_associations_created
|
21
|
-
end
|
22
|
-
|
23
|
-
def create_operator_associations
|
24
|
-
return if operator_associations_created?
|
25
|
-
@operator_associations_created = true
|
26
|
-
|
27
|
-
if self.table_exists?
|
28
|
-
belongs_to :creator, {:foreign_key => "created_by", :class_name => RecordWithOperator.config[:user_class_name]}.merge(RecordWithOperator.config[:operator_association_options]) if column_names.include?('created_by')
|
29
|
-
belongs_to :updater, {:foreign_key => "updated_by", :class_name => RecordWithOperator.config[:user_class_name]}.merge(RecordWithOperator.config[:operator_association_options]) if column_names.include?('updated_by')
|
30
|
-
belongs_to :deleter, {:foreign_key => "deleted_by", :class_name => RecordWithOperator.config[:user_class_name]}.merge(RecordWithOperator.config[:operator_association_options]) if column_names.include?('deleted_by')
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
34
|
-
def has_many_with_operator(*args, &extension)
|
35
|
-
options = args.extract_options!
|
36
|
-
# add AssociationWithOprator to :extend
|
37
|
-
if options[:extend]
|
38
|
-
options[:extend] = [options[:extend]] unless options[:extend].kind_of? Array
|
39
|
-
options[:extend] << AssociationWithOperator
|
40
|
-
else
|
41
|
-
options[:extend] = AssociationWithOperator
|
42
|
-
end
|
43
|
-
# add :set_operator to :before_add
|
44
|
-
if options[:before_add]
|
45
|
-
options[:before_add] = [options[:before_add]] unless options[:before_add].kind_of? Array
|
46
|
-
options[:before_add] << :set_operator
|
47
|
-
else
|
48
|
-
options[:before_add] = :set_operator
|
49
|
-
end
|
50
|
-
args << options
|
51
|
-
has_many_without_operator(*args, &extension)
|
52
|
-
end
|
53
|
-
alias_method_chain :has_many, :operator
|
54
|
-
|
55
|
-
def find_with_for(*args)
|
56
|
-
options = args.extract_options!
|
57
|
-
operator = options.delete(:for)
|
58
|
-
args << options
|
59
|
-
results = find_without_for(*args)
|
60
|
-
if operator
|
61
|
-
if results.kind_of? Array
|
62
|
-
results.each{|r| r.operator = operator}
|
63
|
-
elsif results
|
64
|
-
results.operator = operator
|
65
|
-
end
|
66
|
-
end
|
67
|
-
results
|
68
|
-
end
|
69
|
-
|
70
|
-
alias_method_chain :find, :for
|
71
|
-
|
72
|
-
# No longer valid in Rails 3
|
73
|
-
# def validate_find_options_with_for(options)
|
74
|
-
# if options
|
75
|
-
# options = options.dup
|
76
|
-
# options.delete(:for)
|
77
|
-
# end
|
78
|
-
# validate_find_options_without_for(options)
|
79
|
-
# end
|
80
|
-
#
|
81
|
-
# alias_method :validate_find_options, :validate_find_options_with_for
|
82
|
-
|
83
|
-
private
|
84
|
-
# define_method association, association= ...
|
85
|
-
def association_accessor_methods_with_operator(reflection, association_proxy_class)
|
86
|
-
association_accessor_methods_without_operator(reflection, association_proxy_class)
|
87
|
-
define_method("#{reflection.name}_with_operator") do |*params|
|
88
|
-
r = send("#{reflection.name}_without_operator", *params)
|
89
|
-
r.operator ||= self.operator if r && r.respond_to?(:operator=)
|
90
|
-
r
|
91
|
-
end
|
92
|
-
alias_method_chain "#{reflection.name}".to_sym, :operator
|
93
|
-
|
94
|
-
define_method("#{reflection.name}_with_operator=") do |new_value|
|
95
|
-
new_value.operator ||= self.operator if new_value.respond_to?(:operator=)
|
96
|
-
send("#{reflection.name}_without_operator=", new_value)
|
97
|
-
end
|
98
|
-
alias_method_chain "#{reflection.name}=".to_sym, :operator
|
99
|
-
end
|
100
|
-
alias_method_chain :association_accessor_methods, :operator
|
101
|
-
|
102
|
-
# define_method build_association, create_association ...
|
103
|
-
def association_constructor_method_with_operator(constructor, reflection, association_proxy_class)
|
104
|
-
association_constructor_method_without_operator(constructor, reflection, association_proxy_class)
|
105
|
-
define_method("#{constructor}_#{reflection.name}_with_operator") do |*params|
|
106
|
-
options = { :operator => self.operator }
|
107
|
-
params.empty? ? params[0] = options : params.first.merge!(options)
|
108
|
-
self.send("#{constructor}_#{reflection.name}_without_operator", *params)
|
109
|
-
end
|
110
|
-
alias_method_chain "#{constructor}_#{reflection.name}".to_sym, :operator
|
111
|
-
end
|
112
|
-
alias_method_chain :association_constructor_method, :operator
|
113
|
-
end
|
114
|
-
|
115
|
-
base.before_create :set_created_by
|
116
|
-
base.before_save :set_updated_by
|
117
|
-
base.before_destroy :set_deleted_by
|
118
|
-
|
119
|
-
end
|
120
|
-
|
121
|
-
def respond_to?(name, priv=false)
|
122
|
-
case name.to_sym
|
123
|
-
when :creator
|
124
|
-
respond_to? :created_by
|
125
|
-
when :updater
|
126
|
-
respond_to? :updated_by
|
127
|
-
when :deleter
|
128
|
-
respond_to? :deleted_by
|
129
|
-
else
|
130
|
-
super
|
131
|
-
end
|
132
|
-
end
|
133
|
-
|
134
|
-
private
|
135
|
-
def set_operator(child)
|
136
|
-
child.operator ||= self.operator
|
137
|
-
end
|
138
|
-
|
139
|
-
def method_missing(method, *args)
|
140
|
-
return super unless respond_to?(method)
|
141
|
-
case method.to_sym
|
142
|
-
when :creator, :updater, :deleter
|
143
|
-
self.class.create_operator_associations
|
144
|
-
send(method, *args)
|
145
|
-
else
|
146
|
-
super
|
147
|
-
end
|
148
|
-
end
|
149
|
-
|
150
|
-
def set_created_by
|
151
|
-
return unless respond_to?(:created_by=) && operator
|
152
|
-
self.created_by = operator.id
|
153
|
-
end
|
154
|
-
|
155
|
-
def set_updated_by
|
156
|
-
return unless respond_to?(:updated_by=) && operator && changed? # no use setting updating_by when it's not changed
|
157
|
-
self.updated_by = operator.id
|
158
|
-
end
|
159
|
-
|
160
|
-
def set_deleted_by
|
161
|
-
return unless self.class.column_names.include?("deleted_by") && operator
|
162
|
-
return if frozen?
|
163
|
-
self.class.update_all("deleted_by = #{operator.id}", ["#{self.class.primary_key} = ?", id])
|
164
|
-
self.deleted_by = operator.id
|
165
|
-
end
|
166
|
-
|
167
|
-
end
|
5
|
+
ActiveRecord::Base.send :include, RecordWithOperator::Extension unless ActiveRecord::Base.include?(RecordWithOperator::Extension)
|
6
|
+
ActiveRecord::Relation.send :include, RecordWithOperator::RelationMethods unless ActiveRecord::Relation.include?(RecordWithOperator::RelationMethods)
|
@@ -1,4 +1,4 @@
|
|
1
|
-
# -*-
|
1
|
+
# -*- mode: ruby; coding: utf-8 -*-
|
2
2
|
$:.push File.expand_path("../lib", __FILE__)
|
3
3
|
require "record_with_operator/version"
|
4
4
|
|
@@ -8,14 +8,22 @@ Gem::Specification.new do |s|
|
|
8
8
|
s.platform = Gem::Platform::RUBY
|
9
9
|
s.authors = ["Yasuko Ohba"]
|
10
10
|
s.email = ["y.ohba@everyleaf.com"]
|
11
|
-
s.homepage = ""
|
11
|
+
s.homepage = "https://github.com/nay/record_with_operator"
|
12
12
|
s.summary = %q{Rails plugin to set created_by, updated_by, deleted_by to ActiveRecord objects. Supports associations.}
|
13
|
-
s.description = %q{
|
13
|
+
s.description = %q{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. Also it makes creator, updater, deleter association (belongs_to) if the class has created_by, updated_by, deleted_by.}
|
14
14
|
|
15
15
|
s.rubyforge_project = "record_with_operator"
|
16
16
|
|
17
17
|
s.files = `git ls-files`.split("\n")
|
18
18
|
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
19
19
|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
20
|
+
s.extra_rdoc_files = ['README.rdoc']
|
20
21
|
s.require_paths = ["lib"]
|
22
|
+
|
23
|
+
s.licenses = ["MIT"]
|
24
|
+
|
25
|
+
s.add_dependency 'activerecord', ['~> 3.0.0']
|
26
|
+
s.add_development_dependency 'bundler', ['>= 1.0.0']
|
27
|
+
s.add_development_dependency 'rake', ['>= 0.8.7']
|
28
|
+
s.add_development_dependency 'sqlite3', ['>= 0']
|
21
29
|
end
|
data/test/database.yml
CHANGED
@@ -1,18 +1,18 @@
|
|
1
|
-
sqlite:
|
2
|
-
:adapter: sqlite
|
3
|
-
:dbfile: record_with_operator_plugin.sqlite.db
|
1
|
+
#sqlite:
|
2
|
+
# :adapter: sqlite
|
3
|
+
# :dbfile: record_with_operator_plugin.sqlite.db
|
4
4
|
sqlite3:
|
5
5
|
:adapter: sqlite3
|
6
|
-
:
|
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
|
6
|
+
:database: ':memory:'
|
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
|
@@ -7,8 +7,10 @@ class NoteWithUserWithDependency < ActiveRecord::Base
|
|
7
7
|
set_table_name "notes"
|
8
8
|
has_many :memos, :class_name => "MemoWithUser", :foreign_key => "note_id", :dependent => :destroy
|
9
9
|
|
10
|
-
|
11
|
-
|
10
|
+
before_destroy :check_operator
|
11
|
+
|
12
|
+
private
|
13
|
+
def check_operator
|
12
14
|
raise "can't destroy without operator" unless operator
|
13
15
|
end
|
14
16
|
end
|
@@ -16,8 +18,10 @@ end
|
|
16
18
|
class MemoWithUserWithDependency < ActiveRecord::Base
|
17
19
|
set_table_name "memos"
|
18
20
|
|
19
|
-
|
20
|
-
|
21
|
+
before_destroy :check_operator
|
22
|
+
|
23
|
+
private
|
24
|
+
def check_operator
|
21
25
|
raise "can't destroy without operator" unless operator
|
22
26
|
end
|
23
27
|
end
|
@@ -7,27 +7,21 @@ class NoteWithUser < ActiveRecord::Base
|
|
7
7
|
set_table_name "notes"
|
8
8
|
has_many :memos, :class_name => "MemoWithUser", :foreign_key => "note_id"
|
9
9
|
|
10
|
-
|
10
|
+
scope :new_arrivals, {:order => "updated_at desc"}
|
11
11
|
|
12
|
-
|
13
|
-
|
14
|
-
|
12
|
+
alias_method :destroy!, :destroy
|
13
|
+
|
14
|
+
def destroy
|
15
|
+
with_transaction_returning_status do
|
16
|
+
_run_destroy_callbacks do
|
17
|
+
self.class.update_all({:deleted_at => Time.now}, {self.class.primary_key => self.id})
|
18
|
+
end
|
15
19
|
end
|
16
20
|
freeze
|
17
21
|
end
|
18
22
|
|
19
|
-
def destroy_with_callbacks!
|
20
|
-
return false if callback(:before_destroy) == false
|
21
|
-
result = destroy_without_callbacks!
|
22
|
-
callback(:after_destroy)
|
23
|
-
result
|
24
|
-
end
|
25
|
-
def destroy!
|
26
|
-
transaction { destroy_with_callbacks! }
|
27
|
-
end
|
28
|
-
|
29
23
|
def deleted?
|
30
|
-
self.deleted_at.
|
24
|
+
!self.deleted_at.nil?
|
31
25
|
end
|
32
26
|
end
|
33
27
|
|
@@ -38,7 +32,7 @@ end
|
|
38
32
|
class MemoWithUser < ActiveRecord::Base
|
39
33
|
set_table_name "memos"
|
40
34
|
|
41
|
-
|
35
|
+
scope :new_arrivals, {:order => "updated_at desc"}
|
42
36
|
end
|
43
37
|
|
44
38
|
class UpdaterNoteWithUser < ActiveRecord::Base
|
data/test/test_helper.rb
CHANGED
@@ -1,15 +1,15 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
$:.push File.expand_path('../../', __FILE__)
|
2
|
+
|
3
|
+
require 'logger'
|
3
4
|
require 'test/unit'
|
5
|
+
require 'active_record'
|
4
6
|
require 'active_support'
|
5
7
|
require 'active_support/test_case'
|
6
8
|
|
7
|
-
require 'lib/association_with_operator'
|
8
9
|
require 'lib/record_with_operator'
|
9
|
-
require 'init'
|
10
10
|
|
11
11
|
config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))
|
12
12
|
ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + "/debug.log")
|
13
|
-
ActiveRecord::Base.establish_connection(config['
|
13
|
+
ActiveRecord::Base.establish_connection(config['sqlite3'])
|
14
14
|
|
15
15
|
load(File.dirname(__FILE__) + "/schema.rb")
|