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
|
# File 'lib/backframe/activerecord/acts_as_distinct.rb', line 14
def acts_as_distinct(*args)
field = args[0]
arguments = args[1]
class_eval "\n after_initialize :set_default_\#{field}, :if => Proc.new { |m| m.new_record? && m.\#{arguments[:parent]}.present? }\n after_save :toggle_\#{field}_distinction, :if => Proc.new { |m| m.\#{arguments[:parent]}.present? && m.\#{field} && (m.new_record? || m.\#{field}_changed?) }\n\n scope :\#{field.to_s.gsub('is_', '')}, -> { where(:\#{field} => true) }\n\n def \#{field.to_s.gsub('is_', '')}?\n return self.\#{field}\n end\n\n private\n\n def set_default_\#{field}\n self.\#{field} = self.\#{arguments[:parent]}.\#{arguments[:association]}.empty?\n end\n\n def toggle_\#{field}_distinction\n self.\#{arguments[:parent]}.\#{arguments[:association]}.where('id != ?', self.id).each do |item|\n item.update_attributes(:\#{field} => false)\n end\n end\n\n EOV\n\nend\n"
|