Created
December 10, 2015 11:44
-
-
Save oleglukashev/07b25a635af891851b65 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def check_and_update_kitcheners | |
if params.has_key?(:restaurant_shop) | |
related_properties_params = Array.new | |
params[:restaurant_shop].each do |restaurant_shop_id, person_ids| | |
person_ids.each do |person_id| | |
kitchener_obj = {person_id: person_id, restaurant_shop_id: restaurant_shop_id} | |
related_properties_params.push kitchener_obj | |
end | |
end | |
existing_properties = @kitcheners | |
if related_properties_params.nil? || !related_properties_params.any? | |
existing_properties.each do |property| | |
property.delete | |
end | |
else | |
to_delete = [] | |
existing = [] | |
existing_properties.each do |property| | |
exist_kitchener = {person_id: property.person_id, restaurant_shop_id: property.restaurant_shop_id} | |
existing.push exist_kitchener | |
if !related_properties_params.include? exist_kitchener | |
to_delete.push exist_kitchener | |
end | |
end | |
to_add = related_properties_params - existing | |
to_delete.each do |property| | |
@kitcheners.find_by(person_id: property[:person_id], restaurant_shop_id: property[:restaurant_shop_id]).delete | |
end | |
to_add.each do |property| | |
kitchener = @kitcheners.find_by(person_id: property[:person_id], restaurant_shop_id: property[:restaurant_shop_id]) | |
if kitchener.nil? | |
kitchener = @kitcheners.new | |
kitchener.person_id = property[:person_id] | |
kitchener.restaurant_shop_id = property[:restaurant_shop_id] | |
kitchener.save | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment