Class: RestaurantGroupService::UpdateMembers
- Inherits:
-
Object
- Object
- RestaurantGroupService::UpdateMembers
- Defined in:
- app/services/restaurant_group_service/update_members.rb
Overview
Service class for managing and updating members of a restaurant group and staff.
Instance Attribute Summary collapse
- #errors ⇒ RestaurantGroup, ... readonly
- #restaurant_group ⇒ RestaurantGroup, ... readonly
- #staff_account ⇒ RestaurantGroup, ... readonly
Instance Method Summary collapse
-
#call(restaurant_params, id = nil) ⇒ Boolean
Updates the restaurant group members based on the provided parameters.
-
#initialize(restaurant_group) ⇒ UpdateMembers
constructor
Initializes the service with a restaurant group.
Constructor Details
#initialize(restaurant_group) ⇒ UpdateMembers
Initializes the service with a restaurant group.
11 12 13 14 15 |
# File 'app/services/restaurant_group_service/update_members.rb', line 11 def initialize(restaurant_group) @restaurant_group = restaurant_group @staff_account = restaurant_group.staff_account @errors = [] end |
Instance Attribute Details
#errors ⇒ RestaurantGroup, ... (readonly)
7 8 9 |
# File 'app/services/restaurant_group_service/update_members.rb', line 7 def errors @errors end |
#restaurant_group ⇒ RestaurantGroup, ... (readonly)
7 8 9 |
# File 'app/services/restaurant_group_service/update_members.rb', line 7 def restaurant_group @restaurant_group end |
#staff_account ⇒ RestaurantGroup, ... (readonly)
7 8 9 |
# File 'app/services/restaurant_group_service/update_members.rb', line 7 def staff_account @staff_account end |
Instance Method Details
#call(restaurant_params, id = nil) ⇒ Boolean
Updates the restaurant group members based on the provided parameters.
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 61 62 63 64 65 66 67 |
# File 'app/services/restaurant_group_service/update_members.rb', line 21 def call(restaurant_params, id = nil) ActiveRecord::Base.transaction do get_or_create_staff # if @restaurant.group.merge_allotment is true and there is params restaurants with no nickname then raise error if restaurant_group.merge_allotment? && restaurant_params.any? do |restaurant_param| restaurant_param['nick_name'].blank? end errors << 'Nick name is required for all restaurants when merge allotment is enabled.' raise ActiveRecord::Rollback end new_restaurant_ids = [] restaurant_params.each do |restaurant_param| restaurant_id = restaurant_param['id'].to_i if restaurant_param[:_destroy] == 'false' handle_add_or_update(restaurant_param) new_restaurant_ids << restaurant_id elsif id handle_remove(restaurant_id) new_restaurant_ids.delete restaurant_id end end # Remove restaurants not in the updated list. if new_restaurant_ids.present? removed_ids = restaurant_group.restaurants.ids - new_restaurant_ids removed_ids.each { |id| handle_remove(id) } end # sync staff restaurants with group restaurants all_staffs = restaurant_group.staffs all_staffs.each do |staff| sync_staff_restaurants(restaurant_group, staff) end restaurant_group.touch rescue ActiveRecord::Rollback => e Rails.logger.error("Update Members Error: #{e.}") return false end true end |