Module: Ixtlan::Models::UpdateChildren
- Defined in:
- lib/ixtlan/models/update_children.rb
Instance Method Summary collapse
- #update_children(new_children, old_children_sym) ⇒ Object
- #update_restricted_children(new_children, old_children_sym, allowed_children) ⇒ Object
Instance Method Details
#update_children(new_children, old_children_sym) ⇒ Object
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 |
# File 'lib/ixtlan/models/update_children.rb', line 5 def update_children(new_children, old_children_sym) # get the dm-core/collection and its model old_children = send(old_children_sym) model = old_children.model # make sure we have an array if new_children.is_a?(Array) # ids of new children new_ids = new_children.collect { |v| v.to_i } else new_children = if new_children.nil? [] else new_children[new_children.keys[0]] end new_children = [new_children] unless new_children.is_a? Array # ids of new children new_ids = new_children.collect { |v| v[:id].to_i } end # delete obsolete old children old_children.select do |g| !(new_ids.member? g.id) end.each do |g| old_children.delete(g) end # add missing new children old_ids = old_children.collect { |g| g.id } new_ids.each do |gid| old_children << model.get!(gid) unless old_ids.member? gid end end |
#update_restricted_children(new_children, old_children_sym, allowed_children) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/ixtlan/models/update_children.rb', line 40 def update_restricted_children(new_children, old_children_sym, allowed_children) # get the dm-core/collection and its model old_children = send(old_children_sym) model = old_children.model # make sure we have an array new_children = if new_children.nil? [] else new_children[new_children.keys[0]] end new_children = [new_children] unless new_children.is_a? Array # ids of new children new_ids = new_children.collect { |v| v[:id].to_i } # ids of allowed children allowed_ids = allowed_children.collect { |g| g.id } # delete obsolete and allowed old children set = old_children.select do |g| !(new_ids.member? g.id) && (allowed_ids.member? g.id) end set.each do |g| old_children.delete(g) end # add missing and allowed new children old_ids = old_children.collect { |g| g.id } new_ids.each do |gid| old_children << model.get!(gid) if !old_ids.member?(gid) && allowed_ids.member?(gid) end end |