has_many_by_status 1.0.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/README +3 -0
- data/lib/has_many_by_status.rb +89 -0
- data/test/has_many_by_status_test.rb +11 -0
- data/test/test_helper.rb +1 -0
- metadata +68 -0
data/README
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
module ActiveRecord #:nodoc:
|
2
|
+
module Has #:nodoc:
|
3
|
+
|
4
|
+
module ManyByStatus
|
5
|
+
def self.included(base) # :nodoc:
|
6
|
+
base.extend ClassMethods
|
7
|
+
end
|
8
|
+
|
9
|
+
module ClassMethods
|
10
|
+
|
11
|
+
STATUS_EXCLUDES = %w{deleted captcha pilot unregistered password}
|
12
|
+
|
13
|
+
def has_many_by_status(association_id, options = {}, &extension)
|
14
|
+
begin
|
15
|
+
# don't allow multiple calls
|
16
|
+
#return if self.included_modules.include?(ActiveRecord::Has::ManyByStatus::ActMethods)
|
17
|
+
|
18
|
+
self_class_name = self.to_s.downcase
|
19
|
+
|
20
|
+
send :include, ActiveRecord::Has::ManyByStatus::ActMethods
|
21
|
+
|
22
|
+
# check to make sure if the user has overridden the class_name field in the model.
|
23
|
+
# if they haven't then get it from the association_id.
|
24
|
+
unless options.has_key? :class_name
|
25
|
+
class_name = association_id.to_s.camelize
|
26
|
+
class_name = class_name.singularize
|
27
|
+
h_options = options.merge({:class_name => class_name})
|
28
|
+
else # use the overridden class_name
|
29
|
+
class_name = options[:class_name]
|
30
|
+
h_options = options # looks dumb, but isn't.
|
31
|
+
end
|
32
|
+
status_class_name = h_options[:status_class_name] || "Status"
|
33
|
+
h_options.delete(:status_class_name)
|
34
|
+
#puts "!!!!!!!status_class_name = #{status_class_name}"
|
35
|
+
|
36
|
+
class_eval do
|
37
|
+
#puts "eval(\"#{status_class_name}.active.id\") = #{eval("#{status_class_name}.active.id")}"
|
38
|
+
# build default has_many as active
|
39
|
+
#puts "association_id = #{association_id}"
|
40
|
+
#puts "h_options = #{h_options.inspect}"
|
41
|
+
has_many "#{association_id}".to_sym, h_options.merge({:conditions => ["status_id = ?", eval("#{status_class_name}.active.id")]})
|
42
|
+
# build default has_many ALL
|
43
|
+
has_many "all_#{association_id}".to_sym, h_options
|
44
|
+
|
45
|
+
# build has_many for each status
|
46
|
+
#puts "let's get the status' from the db..."
|
47
|
+
rows = eval("#{status_class_name}.find(:all)")
|
48
|
+
#puts "rows.size = #{rows.size}"
|
49
|
+
rows.each do |status| #get all the statuses in the db.
|
50
|
+
unless STATUS_EXCLUDES.include?(status.name) # don't build a deleted method
|
51
|
+
has_many "#{status.name}_#{association_id}".to_sym, h_options.merge({:conditions => ["status_id = ?", status.id]})
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
# let's build deleted accessors if the object is paranoid
|
57
|
+
if self.respond_to? :paranoid?
|
58
|
+
class_eval do
|
59
|
+
x_options = options
|
60
|
+
x_options.delete(:dependent)
|
61
|
+
define_method("deleted_#{association_id}") do
|
62
|
+
eval("#{class_name}.find_with_deleted(:all, x_options.merge({:conditions => [\"#{self_class_name}_id = ? and status_id = ?\", self.id, Status.deleted.id]}))")
|
63
|
+
end
|
64
|
+
|
65
|
+
define_method("all_#{association_id}_with_deleted") do
|
66
|
+
eval("#{class_name}.find_with_deleted(:all, x_options.merge({:conditions => [\"#{self_class_name}_id = ?\", self.id]}))")
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
rescue => ex
|
71
|
+
puts "Error in has_many_by_status: #{ex.message}"
|
72
|
+
puts ex.backtrace
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
module ActMethods
|
78
|
+
def self.included(base) # :nodoc:
|
79
|
+
base.extend ClassMethods
|
80
|
+
end
|
81
|
+
|
82
|
+
module ClassMethods
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
ActiveRecord::Base.send :include, ActiveRecord::Has::ManyByStatus
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "test/unit"
|
metadata
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
rubygems_version: 0.9.2
|
3
|
+
specification_version: 1
|
4
|
+
name: has_many_by_status
|
5
|
+
version: !ruby/object:Gem::Version
|
6
|
+
version: 1.0.0
|
7
|
+
date: 2007-03-01 00:00:00 -05:00
|
8
|
+
summary: has_many_by_status
|
9
|
+
require_paths:
|
10
|
+
- lib
|
11
|
+
- lib
|
12
|
+
email:
|
13
|
+
homepage:
|
14
|
+
rubyforge_project:
|
15
|
+
description: "has_many_by_status was developed by: markbates"
|
16
|
+
autorequire:
|
17
|
+
- has_many_by_status
|
18
|
+
- acts_as_constant
|
19
|
+
default_executable:
|
20
|
+
bindir: bin
|
21
|
+
has_rdoc: false
|
22
|
+
required_ruby_version: !ruby/object:Gem::Version::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 0.0.0
|
27
|
+
version:
|
28
|
+
platform: ruby
|
29
|
+
signing_key:
|
30
|
+
cert_chain:
|
31
|
+
post_install_message:
|
32
|
+
authors:
|
33
|
+
- markbates
|
34
|
+
files:
|
35
|
+
- lib/has_many_by_status.rb
|
36
|
+
- README
|
37
|
+
test_files:
|
38
|
+
- test/has_many_by_status_test.rb
|
39
|
+
- test/test_helper.rb
|
40
|
+
rdoc_options: []
|
41
|
+
|
42
|
+
extra_rdoc_files: []
|
43
|
+
|
44
|
+
executables: []
|
45
|
+
|
46
|
+
extensions: []
|
47
|
+
|
48
|
+
requirements: []
|
49
|
+
|
50
|
+
dependencies:
|
51
|
+
- !ruby/object:Gem::Dependency
|
52
|
+
name: rails
|
53
|
+
version_requirement:
|
54
|
+
version_requirements: !ruby/object:Gem::Version::Requirement
|
55
|
+
requirements:
|
56
|
+
- - ">="
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
version: 1.2.1
|
59
|
+
version:
|
60
|
+
- !ruby/object:Gem::Dependency
|
61
|
+
name: acts_as_constant
|
62
|
+
version_requirement:
|
63
|
+
version_requirements: !ruby/object:Gem::Version::Requirement
|
64
|
+
requirements:
|
65
|
+
- - ">="
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: 1.0.2
|
68
|
+
version:
|