<commit_before>class Tournament < ActiveRecord::Base
	belongs_to :game
	has_many :tournament_stages
	has_many :settings_raw, class_name: "TournamentSetting"
	has_and_belongs_to_many :players, class_name: "User", association_foreign_key: "player_id", join_table: "players_tournaments"
	has_and_belongs_to_many :hosts,   class_name: "User", association_foreign_key: "host_id",   join_table: "hosts_tournaments"

	def stages_ordered
		h = {}
		i = 1
		self.tournament_stages.order(:id).each do |s|
			h[i] = s
			i += 1
		end
		return h
	end

	def settings
		@settings ||= Settings.new(self)
	end
	def settings=(setting)
		setting.each do |key, value|
			value = false if value == "0"
			settings[key] = value
		end
	end

	class Settings
		def initialize(tournament)
			@tournament = tournament
		end

		def [](setting_name)
			tournament_setting = @tournament.settings_raw.find_by_name(setting_name)
			if tournament_setting.nil?
				return nil
			else
				return tournament_setting.value
			end
		end

		def []=(setting_name, val)
			tournament_setting = @tournament.settings_raw.find_by_name(setting_name)
			if tournament_setting.nil?
				game_setting = @tournament.game.settings.find_by_name(setting_name)
				@tournament.settings_raw.create(name: setting, value: val,
					vartype: game_setting.vartype,
					type_opt: game_setting.type_opt,
					description: game_setting.description,
					display_order: game_setting.display_order)
			else
				tournament_setting.value = val
			end
		end
<commit_msg>save before setting settings<commit_after>class Tournament < ActiveRecord::Base
	belongs_to :game
	has_many :tournament_stages
	has_many :settings_raw, class_name: "TournamentSetting"
	has_and_belongs_to_many :players, class_name: "User", association_foreign_key: "player_id", join_table: "players_tournaments"
	has_and_belongs_to_many :hosts,   class_name: "User", association_foreign_key: "host_id",   join_table: "hosts_tournaments"

	def stages_ordered
		h = {}
		i = 1
		self.tournament_stages.order(:id).each do |s|
			h[i] = s
			i += 1
		end
		return h
	end

	def settings
		@settings ||= Settings.new(self)
	end
	def settings=(setting)
		setting.each do |key, value|
			value = false if value == "0"
			settings[key] = value
		end
	end

	class Settings
		def initialize(tournament)
			@tournament = tournament
		end

		def [](setting_name)
			tournament_setting = @tournament.settings_raw.find_by_name(setting_name)
			if tournament_setting.nil?
				return nil
			else
				return tournament_setting.value
			end
		end

		def []=(setting_name, val)
			@tournament.save
			tournament_setting = @tournament.settings_raw.find_by_name(setting_name)
			if tournament_setting.nil?
				game_setting = @tournament.game.settings.find_by_name(setting_name)
				@tournament.settings_raw.create(name: setting, value: val,
					vartype: game_setting.vartype,
					type_opt: game_setting.type_opt,
					description: game_setting.description,
					display_order: game_setting.display_order)
			else
				tournament_setting.value = val
			end
		end

{'fraction_non_alphanumeric': 0.09129720853858785, 'fraction_numerical': 0.0019704433497536944, 'mean_word_length': 4.468581687612208, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3708754', 'n_tokens_mistral': 1224, 'n_tokens_neox': 1026, 'n_words': 222}