class Admins::CompetitionNavSettingService < ApplicationService attr_reader :competition, :params def initialize(competition, params) @params = params @competition = competition end def call ActiveRecord::Base.transaction do competition.competition_modules.where(module_type: 'md').destroy_all # hidden_module_type = competition.all_module_types - params[:module_type] # competition.competition_modules.where(module_type: hidden_module_type).update_all(hidden: 1) params[:navbar].each do |nav| module_type = nav["module_type"] if competition.all_module_types.include?(module_type) com_module = competition.competition_modules.find_by(module_type: module_type) else com_module = CompetitionModule.create!(competition_id: competition.id, module_type: 'md') end com_module.update_attributes!(hidden: nav["hidden"] ? 0 : 1, position: nav["position"] ? nav["position"] : com_module.position, name: nav["name"], url: nav["url"]) end competition.update_attributes!(enroll_end_time: params[:enroll_end_time]) if params[:competition_staffs].present? competition.competition_staffs.delete_all params[:competition_staffs].each_with_index do |staff_params, index| competition.competition_staffs.create!(staff_params.merge(position: index + 1)) end end competition end end end