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 <<-EOV
def #{after_add_callback_symbol}(added)
self.#{name}.add_to_list_bottom(added)
end
def #{before_remove_callback_symbol}(removed)
self.#{name}.remove_from_list(removed)
end
def update_attributes_with_#{name_ids}(params)
if params[#{name_ids_symbol}].kind_of?(Array)
@list_ids ||= {}
@list_ids[#{name_ids_symbol}] = params[#{name_ids_symbol}].reject(&:blank?)
end
update_attributes_without_#{name_ids}(params)
end
alias_method_chain :update_attributes, #{name_ids_symbol}
after_save :reset_#{name}_position
def reset_#{name}_position
if @list_ids && @list_ids[#{name_ids_symbol}]
self.#{name}.reset_positions_by_ids @list_ids[#{name_ids_symbol}]
end
end
EOV
end
has_and_belongs_to_many_without_list_handling(name, options, &extension)
end
|