You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
educoder/app/services/admins/competition_nav_setting_ser...

36 lines
1.3 KiB

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[:nav_module].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"], name: nav["name"], url: nav["url"])
end
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