Skip to content

Commit

Permalink
Preventing relationships from being created with self
Browse files Browse the repository at this point in the history
  • Loading branch information
oeoeaio committed Apr 12, 2015
1 parent b24cad7 commit 0f9fbe6
Showing 1 changed file with 36 additions and 30 deletions.
66 changes: 36 additions & 30 deletions lib/tasks/data.rake
Original file line number Diff line number Diff line change
Expand Up @@ -6,47 +6,53 @@ namespace :openfoodnetwork do
OrderCycle.where('updated_at > ?', Date.today - 3.months).each do |order_cycle|
# Cycle through the incoming exchanges
order_cycle.exchanges.incoming.each do |exchange|
# Ensure that an enterprise relationship from the producer to the coordinator exists
relationship = EnterpriseRelationship.where(parent_id: exchange.sender_id, child_id: exchange.receiver_id).first
unless relationship.present?
puts "CREATING: #{exchange.sender.name} TO #{exchange.receiver.name}"
relationship = EnterpriseRelationship.create!(parent_id: exchange.sender_id, child_id: exchange.receiver_id)
end
# And that P-OC is granted
unless relationship.has_permission?(:add_to_order_cycle)
puts "PERMITTING: #{exchange.sender.name} TO #{exchange.receiver.name}"
relationship.permissions.create!(name: :add_to_order_cycle)
unless exchange.sender == exchange.receiver
# Ensure that an enterprise relationship from the producer to the coordinator exists
relationship = EnterpriseRelationship.where(parent_id: exchange.sender_id, child_id: exchange.receiver_id).first
unless relationship.present?
puts "CREATING: #{exchange.sender.name} TO #{exchange.receiver.name}"
relationship = EnterpriseRelationship.create!(parent_id: exchange.sender_id, child_id: exchange.receiver_id)
end
# And that P-OC is granted
unless relationship.has_permission?(:add_to_order_cycle)
puts "PERMITTING: #{exchange.sender.name} TO #{exchange.receiver.name}"
relationship.permissions.create!(name: :add_to_order_cycle)
end
end
end

# Cycle through the outgoing exchanges
order_cycle.exchanges.outgoing.each do |exchange|
# Enure that an enterprise relationship from the hub to the coordinator exists
relationship = EnterpriseRelationship.where(parent_id: exchange.receiver_id, child_id: exchange.sender_id).first
unless relationship.present?
puts "CREATING: #{exchange.receiver.name} TO #{exchange.sender.name}"
relationship = EnterpriseRelationship.create!(parent_id: exchange.receiver_id, child_id: exchange.sender_id)
end
# And that P-OC is granted
unless relationship.has_permission?(:add_to_order_cycle)
puts "PERMITTING: #{exchange.receiver.name} TO #{exchange.sender.name}"
relationship.permissions.create!(name: :add_to_order_cycle)
unless exchange.sender == exchange.receiver
# Enure that an enterprise relationship from the hub to the coordinator exists
relationship = EnterpriseRelationship.where(parent_id: exchange.receiver_id, child_id: exchange.sender_id).first
unless relationship.present?
puts "CREATING: #{exchange.receiver.name} TO #{exchange.sender.name}"
relationship = EnterpriseRelationship.create!(parent_id: exchange.receiver_id, child_id: exchange.sender_id)
end
# And that P-OC is granted
unless relationship.has_permission?(:add_to_order_cycle)
puts "PERMITTING: #{exchange.receiver.name} TO #{exchange.sender.name}"
relationship.permissions.create!(name: :add_to_order_cycle)
end
end

# For each variant in the exchange
products = Spree::Product.joins(:variants_including_master).where('spree_variants.id IN (?)', exchange.variants).pluck(:id).uniq
producers = Enterprise.joins(:supplied_products).where("spree_products.id IN (?)", products).uniq
producers.each do |producer|
# Ensure that an enterprise relationship from the producer to the hub exists
relationship = EnterpriseRelationship.where(parent_id: producer.id, child_id: exchange.receiver_id).first
unless relationship.present?
puts "CREATING: #{producer.name} TO #{exchange.receiver.name}"
relationship = EnterpriseRelationship.create!(parent_id: producer.id, child_id: exchange.receiver_id)
end
# And that P-OC is granted
unless relationship.has_permission?(:add_to_order_cycle)
puts "PERMITTING: #{producer.name} TO #{exchange.receiver.name}"
relationship.permissions.create!(name: :add_to_order_cycle)
unless producer == exchange.receiver
# Ensure that an enterprise relationship from the producer to the hub exists
relationship = EnterpriseRelationship.where(parent_id: producer.id, child_id: exchange.receiver_id).first
unless relationship.present?
puts "CREATING: #{producer.name} TO #{exchange.receiver.name}"
relationship = EnterpriseRelationship.create!(parent_id: producer.id, child_id: exchange.receiver_id)
end
# And that P-OC is granted
unless relationship.has_permission?(:add_to_order_cycle)
puts "PERMITTING: #{producer.name} TO #{exchange.receiver.name}"
relationship.permissions.create!(name: :add_to_order_cycle)
end
end
end
end
Expand Down

0 comments on commit 0f9fbe6

Please sign in to comment.