<commit_before>class BcRequestsController < ApplicationController

	before_filter :may_create_study_subjects_required
	before_filter :valid_patid_required, :only => :create
#	before_filter :case_study_subject_required, :only => :create
	before_filter :no_existing_incomplete_bc_request_required, :only => :create
	before_filter :valid_id_required, :only => [:edit,:update,:destroy,:update_status]
	before_filter :valid_status_required, :only => [:update_status]

	def new
		@bc_request           = BcRequest.new #	sole purpose is to make testing happy
		@active_bc_requests   = BcRequest.find(:all, :conditions => { :status => 'active' })
		@waitlist_bc_requests = BcRequest.find(:all, :conditions => { :status => 'waitlist' })
	end

	def create
		@study_subject.bc_requests.create(:status => 'active')
		redirect_to new_bc_request_path
	end

	def edit
	end

	#		transform update to more generic update for status and comments/notes
	def update
#	TODO add validation in bc_request model that request status in statuses (don't forget factory)
		@bc_request.update_attributes(params[:bc_request])
		redirect_to new_bc_request_path
# TODO add rescues for failed update
#
#
	end

	#		for updating only status
	def update_status
		@bc_request.update_attribute(:status,params[:status])
# TODO add rescues for failed update
		redirect_to new_bc_request_path
	end

	def destroy
		@bc_request.destroy
		redirect_to new_bc_request_path
	end

	def index
		conditions = {}
		conditions[:status] = params[:status] if params[:status]
		@bc_requests  = BcRequest.find(:all, :conditions => conditions )
		respond_to do |format|
			format.html
			format.csv { 
				headers["Content-disposition"] = "attachment; " <<
					"filename=bc_requests_#{params[:status]}_#{Time.now.to_s(:filename)}.csv"
			}
		end
	end

	def confirm
		BcRequest.transaction do
			active_bc_requests  = BcRequest.find(:all, :conditions => { :status => 'active' })
			active_bc_requests.each do |bc_request|
				study_subject = bc_request.study_subject
				enrollment = study_subject.enrollments.find_or_create_by_project_id(
					Project['ccls'].id )
				enrollment.operational_events << OperationalEvent.create!(
					:operational_event_type => OperationalEventType['bc_request_sent'],
					:occurred_on => Date.today
				)
			end
#	I don't think that this can raise an error, but if the above do it will be skipped
			BcRequest.update_all(
				{ :status => 'pending', :sent_on => Date.today }, #	updates
				{ :status => 'active' })  #	conditions
		end	#	BcRequest.transaction do
	rescue ActiveRecord::RecordNotSaved, ActiveRecord::RecordInvalid =>  e
		flash[:error] = "Confirmation failed:#{e.inspect}:"
	ensure	#	same redirect no matter what happens above 
		redirect_to new_bc_request_path
	end

protected
<commit_msg>Modified OperationalEvent creation to pass enrollment validation.<commit_after>class BcRequestsController < ApplicationController

	before_filter :may_create_study_subjects_required
	before_filter :valid_patid_required, :only => :create
#	before_filter :case_study_subject_required, :only => :create
	before_filter :no_existing_incomplete_bc_request_required, :only => :create
	before_filter :valid_id_required, :only => [:edit,:update,:destroy,:update_status]
	before_filter :valid_status_required, :only => [:update_status]

	def new
		@bc_request           = BcRequest.new #	sole purpose is to make testing happy
		@active_bc_requests   = BcRequest.find(:all, :conditions => { :status => 'active' })
		@waitlist_bc_requests = BcRequest.find(:all, :conditions => { :status => 'waitlist' })
	end

	def create
		@study_subject.bc_requests.create(:status => 'active')
		redirect_to new_bc_request_path
	end

	def edit
	end

	#		transform update to more generic update for status and comments/notes
	def update
#	TODO add validation in bc_request model that request status in statuses (don't forget factory)
		@bc_request.update_attributes(params[:bc_request])
		redirect_to new_bc_request_path
# TODO add rescues for failed update
#
#
	end

	#		for updating only status
	def update_status
		@bc_request.update_attribute(:status,params[:status])
# TODO add rescues for failed update
		redirect_to new_bc_request_path
	end

	def destroy
		@bc_request.destroy
		redirect_to new_bc_request_path
	end

	def index
		conditions = {}
		conditions[:status] = params[:status] if params[:status]
		@bc_requests  = BcRequest.find(:all, :conditions => conditions )
		respond_to do |format|
			format.html
			format.csv { 
				headers["Content-disposition"] = "attachment; " <<
					"filename=bc_requests_#{params[:status]}_#{Time.now.to_s(:filename)}.csv"
			}
		end
	end

	def confirm
		BcRequest.transaction do
			active_bc_requests  = BcRequest.find(:all, :conditions => { :status => 'active' })
			active_bc_requests.each do |bc_request|
				study_subject = bc_request.study_subject
				enrollment = study_subject.enrollments.find_or_create_by_project_id(
					Project['ccls'].id )
#				enrollment.operational_events << OperationalEvent.create!(
				OperationalEvent.create!(
					:enrollment => enrollment,
					:operational_event_type => OperationalEventType['bc_request_sent'],
					:occurred_on => Date.today
				)
			end
#	I don't think that this can raise an error, but if the above do it will be skipped
			BcRequest.update_all(
				{ :status => 'pending', :sent_on => Date.today }, #	updates
				{ :status => 'active' })  #	conditions
		end	#	BcRequest.transaction do
	rescue ActiveRecord::RecordNotSaved, ActiveRecord::RecordInvalid =>  e
		flash[:error] = "Confirmation failed:#{e.inspect}:"
	ensure	#	same redirect no matter what happens above 
		redirect_to new_bc_request_path
	end

protected

{'fraction_non_alphanumeric': 0.10093359168574952, 'fraction_numerical': 0.0, 'mean_word_length': 4.939330543933054, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 42, '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': '6258738', 'n_tokens_mistral': 1975, 'n_tokens_neox': 1793, 'n_words': 427}