<commit_before>class ForumThreadsController < Controller

	get '/forum_threads' do 
		if logged_in?
			sort_threads
			erb :'forum_threads/index'
		else
			redirect '/'
		end
	end

	get '/forum_threads/new' do 
		if logged_in?
			@thread = ForumThread.new
			@post = ForumPost.new
			erb :'forum_threads/create'
		else
			redirect '/'
		end
	end

	post '/forum_threads/search' do 
		if logged_in?
			@thread = ForumThread.find_by(trim_whitespace(params[:forum_thread], ["title"]))
			if @thread 
				redirect "/forum_threads/#{@thread.slug}"
			else
				redirect '/forum_threads?message=Title+not+found.'
			end
		else
			redirect '/'
		end
	end

	post '/forum_threads' do 
		if logged_in?
			@thread = ForumThread.new 
			set_attributes(@thread, trim_whitespace(params[:forum_thread], ["title"]), ["title"])
			if @thread.save
				@post = ForumPost.new 
				set_attributes(@post, trim_whitespace(params[:forum_post], ["content"]), ["content", "forum_user_id"])
				@post.forum_thread = @thread 
				if @post.save 
					@post.forum_user.update(updated_at: Time.now)
					redirect "/forum_threads/#{@thread.slug}"
				else
					@current_route = "/forum_threads/new"
					@thread.delete
					erb :'forum_threads/create'
					# redirect '/forum_threads/new'
				end
			else
				@current_route = "/forum_threads/new"
				# grabbing any errors that the post may have
				@post = ForumPost.new
				set_attributes(@post, trim_whitespace(params[:forum_post], ["content"]), ["content", "forum_user_id"])
				@post.forum_thread = @thread
				@post.delete if @post.save
				erb :'forum_threads/create'
				# redirect '/forum_threads/new'
			end
		else
			redirect '/'
		end
	end

	get '/forum_threads/:slug' do
		if logged_in?
			@thread = ForumThread.find_by_slug(params[:slug])
			redirect '/forum_threads' if @thread.nil?
			sort_thread_posts
			erb :'forum_threads/show'
		else
			redirect '/'
		end
	end

	get '/forum_threads/:slug/edit' do 
		if logged_in?
			@thread = ForumThread.find_by_slug(params[:slug])
			redirect '/forum_threads' if @thread.nil? || !moderator?
			erb :'forum_threads/edit'
		else
			redirect '/'
		end
	end

	delete '/forum_threads' do
		if moderator?
			begin
				@thread = ForumThread.find(params[:forum_thread][:id])
			rescue ActiveRecord::RecordNotFound
				redirect '/forum_threads'
			end
			if @thread.forum_posts
				@thread.forum_posts.each do |post|
					post.delete
				end
			end
			@thread.delete
			current_user.update(updated_at: Time.now)
			redirect '/forum_threads'
		else
			redirect '/'
		end
	end

	patch '/forum_threads' do
		if moderator?
			begin
				@thread = ForumThread.find(params[:forum_thread][:id])
			rescue ActiveRecord::RecordNotFound
				cached_route_or_home
			end
			set_attributes(@thread, trim_whitespace(params[:forum_thread], ["title"]), ["title"])
			if @thread.save
				current_user.update(updated_at: Time.now)
				redirect '/forum_threads'
			else
				@current_route = "/forum_threads/#{@thread.slug}/edit"
				erb :'forum_threads/edit'
				# cached_route_or_home
			end
		else
			redirect '/'
		end
	end

end<commit_msg>refactored ForumThreadsController to use Helpable#set_and_save_attributes<commit_after>class ForumThreadsController < Controller

	get '/forum_threads' do 
		if logged_in?
			sort_threads
			erb :'forum_threads/index'
		else
			redirect '/'
		end
	end

	get '/forum_threads/new' do 
		if logged_in?
			@thread = ForumThread.new
			@post = ForumPost.new
			erb :'forum_threads/create'
		else
			redirect '/'
		end
	end

	post '/forum_threads/search' do 
		if logged_in?
			@thread = ForumThread.find_by(trim_whitespace(params[:forum_thread], ["title"]))
			if @thread 
				redirect "/forum_threads/#{@thread.slug}"
			else
				redirect '/forum_threads?message=Title+not+found.'
			end
		else
			redirect '/'
		end
	end

	post '/forum_threads' do 
		if logged_in?
			@thread = ForumThread.new 
			if set_and_save_attributes(@thread, trim_whitespace(params[:forum_thread], ["title"]), ["title"])
				@post = ForumPost.new 
				set_attributes(@post, trim_whitespace(params[:forum_post], ["content"]), ["content", "forum_user_id"])
				@post.forum_thread = @thread 
				if @post.save 
					@post.forum_user.update(updated_at: Time.now)
					redirect "/forum_threads/#{@thread.slug}"
				else
					@current_route = "/forum_threads/new"
					@thread.delete
					erb :'forum_threads/create'
					# redirect '/forum_threads/new'
				end
			else
				@current_route = "/forum_threads/new"
				# grabbing any errors that the post may have
				@post = ForumPost.new
				set_attributes(@post, trim_whitespace(params[:forum_post], ["content"]), ["content", "forum_user_id"])
				@post.forum_thread = @thread
				@post.delete if @post.save
				erb :'forum_threads/create'
				# redirect '/forum_threads/new'
			end
		else
			redirect '/'
		end
	end

	get '/forum_threads/:slug' do
		if logged_in?
			@thread = ForumThread.find_by_slug(params[:slug])
			redirect '/forum_threads' if @thread.nil?
			sort_thread_posts
			erb :'forum_threads/show'
		else
			redirect '/'
		end
	end

	get '/forum_threads/:slug/edit' do 
		if logged_in?
			@thread = ForumThread.find_by_slug(params[:slug])
			redirect '/forum_threads' if @thread.nil? || !moderator?
			erb :'forum_threads/edit'
		else
			redirect '/'
		end
	end

	delete '/forum_threads' do
		if moderator?
			begin
				@thread = ForumThread.find(params[:forum_thread][:id])
			rescue ActiveRecord::RecordNotFound
				redirect '/forum_threads'
			end
			if @thread.forum_posts
				@thread.forum_posts.each do |post|
					post.delete
				end
			end
			@thread.delete
			current_user.update(updated_at: Time.now)
			redirect '/forum_threads'
		else
			redirect '/'
		end
	end

	patch '/forum_threads' do
		if moderator?
			begin
				@thread = ForumThread.find(params[:forum_thread][:id])
			rescue ActiveRecord::RecordNotFound
				cached_route_or_home
			end
			if set_and_save_attributes(@thread, trim_whitespace(params[:forum_thread], ["title"]), ["title"])
				current_user.update(updated_at: Time.now)
				redirect '/forum_threads'
			else
				@current_route = "/forum_threads/#{@thread.slug}/edit"
				erb :'forum_threads/edit'
				# cached_route_or_home
			end
		else
			redirect '/'
		end
	end

end
{'fraction_non_alphanumeric': 0.12295081967213115, 'fraction_numerical': 0.0, 'mean_word_length': 3.525818181818182, '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': '2000854', 'n_tokens_mistral': 2661, 'n_tokens_neox': 2128, 'n_words': 439}