Last active
December 29, 2015 11:09
-
-
Save EminenceHC/7661549 to your computer and use it in GitHub Desktop.
Refactoring Controllers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<h3>Result Sets</h3> | |
<div class="span12" style="width:100%;margin:5px;"> | |
<% if ResultSet.where(:user_id => current_user.id).count == 0 %> | |
You have not taken any surveys yet. | |
<% else %> | |
<% if current.loginable_type != 'Student' %> | |
<%= simple_form_for(result_sets_path, :method =>'get', :html => { :class => 'form-horizontal' }) do %> | |
<%= select_tag 'id', options_for_select(@quiz_id), { :prompt => 'Quiz Name', class: 'chosen-select'} %> | |
<%= select_tag 'user_id', options_for_select(@quiz_user_name), { :prompt => 'User Name', class: 'chosen-select' } %> | |
<%= submit_tag 'Submit', :class => 'btn btn-primary' %> | |
<% end %> | |
<% else %> | |
<%= simple_form_for(result_sets_path, :method =>'get', :html => { :class => 'form-horizontal' }) do %> | |
<%= select_tag 'id', options_for_select(@quiz_id), { :prompt => 'All Quizzes', class: 'chosen-select'} %> | |
<% @student_only %> | |
<%= submit_tag 'Submit', :class => 'btn btn-primary' %> | |
<% end %> | |
<% end %> | |
<table class="table table-condensed table-bordered table-striped table-responsive"> | |
<thead> | |
<tr> | |
<th>User</th> | |
<th>Quiz</th> | |
<th>Survey ID</th> | |
<th>Result ID</th> | |
<th>Possible</th> | |
<th>Correct</th> | |
<th>Percent</th> | |
<th>View</th> | |
<% if current.loginable_type != 'Student' %> | |
<th>Delete</th> | |
<% end %> | |
</tr> | |
</thead> | |
<tbody> | |
<% @return_array.each do |a| %> | |
<tr> | |
<td><%= a.user.full_name %></td> | |
<td><%= a.survey.name %></td> | |
<td><%= a.survey_id %></td> | |
<td><%= a.id %></td> | |
<td><%= a.total_count %></td> | |
<td><%= a.correct_count %></td> | |
<td><%= a.percent.ceil %>%</td> | |
<td><%= link_to " View", result_set_path(a.id), class: 'btn btn-mini glyphicon glyphicon-eye-open' %></td> | |
<% if current.loginable_type != 'Student' %><td><%= link_to "", result_set_path(a.id), method: :delete, class: 'btn btn-mini btn-danger glyphicon glyphicon-trash', confirm: 'Are you sure?', style: 'margin-left:20%' %></td><% end %> | |
<%= @sum %> | |
</tr> | |
<% end %> | |
<% @total_percentage = ((@total_correct_count.to_f / @total_count.to_f) * 100).ceil %> | |
<% if @total_percentage > 75 %> | |
<tr class="success"> | |
<% elsif @total_percentage < 75 && @total_percentage > 60 %> | |
<tr class="warning"> | |
<% else %> | |
<tr class="error"> | |
<% end %> | |
<td colspan="4"></td> | |
<td><%= @total_correct_count %></td> | |
<td><%= @total_count %></td> | |
<td><%= @total_percentage %>%</td> | |
<td></td> | |
<% if current.loginable_type != 'Student' %><td></td><% end %> | |
</tr> | |
</tbody> | |
</table> | |
</div> | |
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ResultSet < ActiveRecord::Base | |
belongs_to :user | |
belongs_to :survey | |
accepts_nested_attributes_for :survey | |
attr_accessor :score, :correct_count, :total_count, :percent | |
serialize :serial | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ResultSetsController < ApplicationController | |
before_filter :new, :authenticate_user! | |
def index | |
@survey = Survey.find(params[:survey_id]) | |
@quiz = @survey.quiz | |
@result_sets = Survey.result_sets.where(user_id: result_set_owner) | |
@quiz_id = ResultSet.all.map { |u| [u.survey.try(:name), u.survey_id]}.uniq | |
@quiz_user_name = ResultSet.all.map { |u| [u.user.full_name, u.user_id]}.uniq | |
if current.loginable_type == 'Student' | |
@student_only = params[:student_id] = current_user.loginable.id | |
end | |
runquery = ResultSet | |
runquery = runquery.where(:survey_id => params[:id]) if params[:id].present? | |
runquery = runquery.where(:user_id => params[:user_id]) if params[:user_id].present? | |
if current.loginable_type == 'Student' | |
runquery = runquery.select('result_sets.*').where(:user_id => current_user.id) | |
else | |
runquery = runquery.select('result_sets.*') | |
end | |
@query = runquery | |
@total_correct_count = 0 | |
@total_count = 0 | |
@return_array = [] | |
@query = @query.each do |a| | |
if a.try(:survey) == nil | |
next | |
end | |
@arr = [] | |
@actual = [] | |
@correct = [] | |
@score = [] | |
a.serial.to_a.each do |key,value| | |
@actual << value | |
end | |
a.survey.questions.each do |b| | |
@total_count += 1 | |
@id = b.answers | |
@id.each do |c| | |
if c.correct == true | |
@correct = @arr << c.id | |
end | |
end | |
end | |
# Virtual Attributes for View | |
@score = @correct.collect{|x| "#{x}"} & @actual | |
@total_correct_count += @score.count | |
a.correct_count = @score.count | |
a.total_count = @actual.count | |
a.percent = (a.correct_count.to_f / a.total_count.to_f) * 100 | |
@return_array << a | |
end | |
end | |
def new | |
@result_set = ResultSet.new | |
end | |
def create | |
@result_set = ResultSet.new(result_set_params) | |
@result_set.save! | |
if @result_set.save | |
redirect_to result_sets_path, notice: "Quiz Completed Successfully" | |
else | |
redirect_to result_sets_path, notice: "There was a problem, Quiz not saved." | |
end | |
end | |
def show | |
@result_set = ResultSet.find(params[:id]) | |
@actual = [] | |
@arr = [] | |
@serial = @result_set.serial | |
@result_set.serial.to_a.each do |key,value| | |
@actual << value | |
end | |
@result_set.survey.questions.each do |b| | |
@id = b.answers | |
@id.each do |c| | |
if c.correct == true | |
@correct = @arr << c.id | |
end | |
end | |
end | |
end | |
def destroy | |
@result_set = ResultSet.find(params[:id]) | |
@result_set.destroy | |
redirect_to result_sets_url, notice: "Successfully destroyed the user's quiz results." | |
end | |
private | |
def result_set_owner | |
# Not sure why the check of "student" is required, maybe you could explain a bit more. | |
# current.loginable_type == 'Student' | |
params[:user_id] || current_user.id | |
end | |
def result_set_params | |
#params.require(:result_set).permit! | |
params.require(:result_set).permit(:id, :user_id, :survey_id, :created_at, :updated_at, survey_attributes: [:id, :name, :created_at, :updated_at, :user_id]).tap do |whitelisted| | |
whitelisted[:serial] = params[:result_set][:serial] | |
end | |
end | |
end | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Student < ActiveRecord::Base | |
has_many :surveys | |
has_one :user, as: :loginable | |
has_many :drug_assessments, dependent: :destroy | |
accepts_nested_attributes_for :user, :drug_assessments | |
validates_presence_of :case_no | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class StudentsController < ApplicationController | |
before_filter :authenticate_user! | |
def create | |
@student = Student.new(student_params) | |
@user = User.new(user_params) | |
@user.loginable = @student | |
if [@student, @user].map { |rec| rec.valid? }.all? | |
[@student, @user].each { |rec| rec.save } | |
@student.save | |
@user.save | |
redirect_to students_path, :notice => "Student created." | |
else | |
render action: 'new' | |
end | |
end | |
def new | |
@student = Student.new | |
@user = @student.build_user | |
end | |
def edit | |
@student = Student.find(params[:id]) | |
@user = @student.user | |
end | |
def update | |
#authorize! :update, @student, :message => 'Not authorized as an administrator.' | |
@student = Student.find(params[:id]) | |
@user = User.find(@student.user.id) | |
@student.update(student_params) | |
@user.update(user_params) | |
if @student.update(student_params) || @user.update(user_params) | |
redirect_to students_path, :notice => "Student updated." | |
else | |
redirect_to students_path, :alert => "Unable to update student." | |
end | |
end | |
def index | |
@users_array_id = User.where(loginable_type: 'Student').includes(:loginable).all.map { |u| [u.full_name, u.loginable.id] }.uniq | |
@users_array_program = User.where(loginable_type: 'Student').includes(:loginable).all.map { |u| [u.loginable.program, u.loginable.program] }.uniq | |
@users_array_social = User.where(loginable_type: 'Student').includes(:loginable).all.map { |u| [u.social_security_no, u.social_security_no] }.uniq | |
runquery = Student.joins(:user) | |
runquery = runquery.where(:users => {:first_name => params[:first_name]}) if params[:first_name].present? | |
runquery = runquery.where(:users => {:last_name => params[:last_name]}) if params[:last_name].present? | |
runquery = runquery.where(:users => {:loginable_id => params[:id]}) if params[:id].present? | |
runquery = runquery.where(program: params[:program]) if params[:program].present? | |
runquery = runquery.where(:users => {:social_security_no => '0'}) if params[:social_security_no] == 'no_social' | |
runquery = runquery.select('students.*, students.id as sid, users.*') | |
@query = runquery | |
end | |
def search | |
@student = Student.find_by_name params[:search_name] | |
render action: 'index' | |
end | |
def show | |
@student = Student.find(params[:id]) | |
@user = @student.user | |
end | |
def toggle_active_on | |
@student = User.find(params[:id]).update_attributes(active: true) | |
redirect_to(:back) | |
end | |
def toggle_active_off | |
@student = User.find(params[:id]).update_attributes(active: false) | |
redirect_to(:back) | |
end | |
private | |
def student_params | |
params.require(:student).permit(:case_no, :program, :admission_status, :temporary_password, :admission_date, :sid) | |
end | |
def user_params | |
params.require(:user).permit(:email, :password, :password_confirmation, :name, :first_name, :middle_name, :last_name, :social_security_no, :sex, :date_of_birth, :street_address, :city, :state, :postal_code, :home_phone, :work_phone, :mobile_phone, :message_id, :password_confirmation) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Survey < ActiveRecord::Base | |
resourcify | |
has_many :questions | |
has_many :result_sets | |
accepts_nested_attributes_for :questions, :result_sets, allow_destroy: true | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SurveysController < ApplicationController | |
before_filter :authenticate_user! | |
include SurveysToBeTaken | |
def show | |
@survey = Survey.find(params[:id]) | |
@result_set = ResultSet.new | |
end | |
def new | |
@survey = Survey.new | |
@result_set = ResultSet.new | |
end | |
def create | |
@survey = Survey.new(survey_params) | |
if @survey.save | |
redirect_to @survey, notice: "Successfully created survey." | |
else | |
render :new | |
end | |
end | |
def edit | |
@survey = Survey.find(params[:id]) | |
end | |
def update | |
@survey = Survey.find(params[:id]) | |
if @survey.update_attributes(survey_params) | |
redirect_to @survey, notice: "Successfully updated survey." | |
else | |
render :edit | |
end | |
end | |
def destroy | |
@survey = Survey.find(params[:id]) | |
@survey.destroy | |
redirect_to surveys_url, notice: "Successfully destroyed survey." | |
end | |
private | |
def survey_params | |
params.require(:survey).permit(:id, :name, :created_at, :updated_at, :date1, :privilege, questions_attributes: [:id, :survey_id, :content, :created_at, :updated_at, answers_attributes: [:id, :question_id, :content, :created_at, :updated_at, :correct]]) | |
end | |
def check_if_survey_has_questions | |
@tag = Survey.find_by_name_and_user_id(self.name, self.user_id) | |
if @tag != nil | |
# | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment