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
51
52
53
54
55
56
57
58
59
60
|
# File 'lib/webpulser-habtm_list.rb', line 15
def has_and_belongs_to_many_with_list_handling(name, options={}, &extension)
if options.delete(:list)
options[:extend] = RailsExtensions::HabtmList::AssociationListMethods
after_add_callback_symbol = "maintain_list_after_add_for_#{name}".to_sym
before_remove_callback_symbol = "maintain_list_before_remove_for_#{name}".to_sym
name_ids = "#{name.to_s.singularize}_ids"
name_ids_symbol = ":#{name_ids}"
options[:after_add] ||= []
options[:after_add] << after_add_callback_symbol
options[:before_remove] ||= []
options[:before_remove] << before_remove_callback_symbol
class_eval " def \#{after_add_callback_symbol}(added)\n self.\#{name}.add_to_list_bottom(added)\n end\n\n def \#{before_remove_callback_symbol}(removed)\n self.\#{name}.remove_from_list(removed)\n end\n\n def update_attributes_with_\#{name_ids}(params)\n if params[\#{name_ids_symbol}].kind_of?(Array)\n @list_ids ||= {}\n @list_ids[\#{name_ids_symbol}] = params[\#{name_ids_symbol}].reject(&:blank?)\n end\n\n update_attributes_without_\#{name_ids}(params)\n end\n\n alias_method_chain :update_attributes, \#{name_ids_symbol}\n after_save :reset_\#{name}_position\n\n def reset_\#{name}_position\n if @list_ids && @list_ids[\#{name_ids_symbol}]\n self.\#{name}.reset_positions_by_ids @list_ids[\#{name_ids_symbol}]\n end\n end\n EOV\n end\n\n has_and_belongs_to_many_without_list_handling(name, options, &extension)\nend\n"
|