forked from taryneast/hyperactiveresource
/
cross_associated_model.rb
50 lines (41 loc) · 1.85 KB
/
cross_associated_model.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
module CrossAssociatedModel
def self.included(mod)
mod.extend(CrossAssociatedModelClassMethods)
end
module CrossAssociatedModelClassMethods
#The base implementation assumes a resource that references a record
def belongs_to_record( name )
#Assumes this record has a "name_id" and that a "Name" exists that is an active_record
instance_var = "@#{name}"
define_method( name ) do
association_id = send("#{name}_id")
resource_class = name.to_s.classify.constantize
if association_id == 0 or association_id.nil?
return nil
elsif instance_variable_get(instance_var).nil?
instance_variable_set( instance_var, resource_class.find(association_id) )
end
instance_variable_get(instance_var)
end
define_method( "#{name}=" ) do |new_associated_resource|
instance_variable_set( instance_var, new_associated_resource )
send("#{name}_id=", (new_associated_resource.nil? ? nil : new_associated_resource.id))
end
end
#The base implementation assumes a resource that references many record
def has_many_records( name )
#Assumes this record has a "name_ids" and that a "Name" exists that is an active_record
instance_var = "@#{name}"
define_method( name ) do
resource_class = name.to_s.classify.constantize
finder_method = "find_all_by_#{self.class.name.tableize.singularize}_id"
if instance_variable_get(instance_var).nil?
instance_variable_set( instance_var, resource_class.send( finder_method, self.id ) )
end
instance_variable_get(instance_var)
end
end
alias_method :belongs_to_resource, :belongs_to_record
alias_method :has_many_resources, :has_many_records
end
end