Skip to content

Instantly share code, notes, and snippets.

@EminenceHC
Last active December 23, 2015 22:09
Show Gist options
  • Save EminenceHC/6701623 to your computer and use it in GitHub Desktop.
Save EminenceHC/6701623 to your computer and use it in GitHub Desktop.
Ransack and Simple_Form
class DrugAssessmentsController < ApplicationController
before_filter :authenticate_user!
def create
@form = DrugAssessment.create(drug_assessment_params)
redirect_to drug_assessments_path
end
def new
@year = Date.today.cwyear
@cweek = Date.today.cweek
@date = Date.commercial(@year, @cweek)
@string = @date.strftime("%Y/%m/%d")
@form = DrugAssessment.new(cweek: (params[:cweek] || @cweek))
@user = current_user
if
@user.loginable_type == 'Student'
@loginable = @user.loginable
@studentid = @loginable.id
@form.student_id = @studentid
elsif
@user.loginable_type == 'Counselor'
@form.student_id = 5
else
@form.student_id = 8
end
end
def index
@user = current_user
@loginable = @user.loginable
if @user.loginable_type == 'Student'
@studentid = @loginable.id
@l = DrugAssessment.order('cweek DESC').where(student_id: @studentid).search(params[:l])
elsif @user.loginable_type == 'Counselor'
@sid = 3
@l = DrugAssessment.order('cweek DESC').where(student_id: @sid).search(params[:l])
end
if
@form = @l.result
@test = Array.new
@form.each do |r|
unless r.cweek.nil? or r.cweek == 0
@test.push(r.cweek)
end
end
@test = @test.uniq
@year = Date.today.cwyear
@current_cweek = Date.today.cweek
@start_cweek = 34
@date = Date.commercial(@year, @current_cweek)
@datestring = @date.strftime("%Y/%m/%d")
while @current_cweek > @start_cweek do
if @test.include?(@current_cweek)
else
charles = DrugAssessment.new(cweek: @current_cweek)
@form << charles
end
@current_cweek = @current_cweek - 1
end
@form = @form.sort_by{|a| -(a.cweek)}
else
@l = DrugAssessment.search(params[:l])
@form = @l.result
end
end
def show
@student = DrugAssessment.find(params[:id])
end
end
private
def drug_assessment_params
params.require(:drug_assessment).permit(:id, :student_id, :alcohol, :marijuana, :drug_assessment, :week, :cweek)
end
<div class="span12" style="width:100%;margin:5px;">
<%= button_to 'New Drug Assessment', new_drug_assessment_path, :class => 'btn btn-success', :style => 'float:right;', :method => :get %>
<%= simple_form_for @filter do |f| %>
<%= f.text_field :filter %>
<% end %>
<%= simple_form_for @l, :url => drug_assessments_path, :html => {:method => :get, :class => 'form-horizontal', :style => 'margin:10px;'} do |f| %>
<% if @user.loginable_type != 'Student' %>
<%= f.label :student_id_eq, "Student ID" %>
<%= f.text_field :student_id_eq %>
<br><br>
<% end %>
<%= f.label :created_at_eq, "Date Created" %>
<%= f.date_field :created_at_eq, as: :date, order: [:day, :month, :year] %>
<%= f.submit "Search", :class => 'btn btn-primary' %>
<br><br>
<% end %>
<table class="table table-condensed table-bordered table-striped ">
<thead>
<tr>
<th><%= sort_link @l, :student_id, "SID" %></th>
<th><%= sort_link @l, :id, "AID" %></th>
<th><%= sort_link @l, :created_at, "Date Created" %></th>
<th><%= sort_link @l, :did_you_use, "Use" %></th>
<th><%= sort_link @l, :alcohol, "Alcohol" %></th>
<th><%= sort_link @l, :marijuana, "Marijuana" %></th>
<th><%= sort_link @l, :over_the_counter, "Counter" %></th>
<th><%= sort_link @l, :inhalent, "Inhalent" %></th>
<th><%= sort_link @l, :ecstacy, "Ecstacy" %></th>
<th><%= sort_link @l, :crack_cocaine, "Crack/Cocaine" %></th>
<th><%= sort_link @l, :heroin, "Heroin" %></th>
<th><%= sort_link @l, :methanphetamine, "Meth" %></th>
<th><%= sort_link @l, :pcp_lsd_hallucinogen, "Hallucinogen" %></th>
<th><%= sort_link @l, :prescription, "Prescription" %></th>
<th><%= sort_link @l, :methadone, "Methadone" %></th>
<th><%= sort_link @l, :cweek, "Week" %></th>
<th><%= sort_link @l, "New" %></th>
</tr>
</thead>
<% @form.each do |q| %>
<tbody>
<% if q.created_at != nil %>
<tr>
<td><%= q.student_id %></td>
<td><%= q.id %></td>
<td><%= q.created_at %></td>
<td><%= q.did_you_use %></td>
<td><%= q.alcohol %></td>
<td><%= q.marijuana %></td>
<td><%= q.over_the_counter %></td>
<td><%= q.inhalent %></td>
<td><%= q.ecstacy %></td>
<td><%= q.crack_cocaine %></td>
<td><%= q.heroin %></td>
<td><%= q.methanphetamine %></td>
<td><%= q.pcp_lsd_hallucinogen %></td>
<td><%= q.prescription %></td>
<td><%= q.methadone %></td>
<td><%= q.cweek %></td>
<td></td>
</tr>
<% else q.created_at == nil %>
<tr class="error" >
<td><%= q.student_id %></td>
<td><%= q.id %></td>
<td><%= q.created_at %></td>
<td><%= q.did_you_use %></td>
<td><%= q.alcohol %></td>
<td><%= q.marijuana %></td>
<td><%= q.over_the_counter %></td>
<td><%= q.inhalent %></td>
<td><%= q.ecstacy %></td>
<td><%= q.crack_cocaine %></td>
<td><%= q.heroin %></td>
<td><%= q.methanphetamine %></td>
<td><%= q.pcp_lsd_hallucinogen %></td>
<td><%= q.prescription %></td>
<td><%= q.methadone %></td>
<td><%= q.cweek %></td>
<td><%= link_to "Add", new_drug_assessment_path(:cweek => q.cweek) %></td>
</tr>
<% end %>
</tbody>
<% end %>
</table>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment