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
|
# File 'lib/spree_related_products.rb', line 6
def self.activate
Product.class_eval do
has_many :relations, :as => :relatable
def self.relation_types
RelationType.find_all_by_applies_to(self.to_s, :order => :name)
end
def method_missing(method, *args)
relation_type = self.class.relation_types.detect { |rt| rt.name.downcase.gsub(" ", "_").pluralize == method.to_s.downcase }
if relation_type.nil?
super
else
relations.find_all_by_relation_type_id(relation_type.id).map(&:related_to).select {|product| product.deleted_at.nil? && product.available_on <= Time.now()}
end
end
end
Admin::ProductsController.class_eval do
def related
load_object
@relation_types = Product.relation_types
end
end
end
|