Last active
December 26, 2015 06:49
-
-
Save jcutrell/7110388 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
<!-- Classdate form --> | |
<% if (params[:classdate] and params[:classdate][:course_id]) or @classdate.id %> | |
<%= nested_form_for(@classdate) do |f| %> | |
<% if @classdate.errors.any? %> | |
<div id="error_explanation"> | |
<h2><%= pluralize(@classdate.errors.count, "error") %> prohibited this classdate from being saved:</h2> | |
<ul> | |
<% @classdate.errors.full_messages.each do |msg| %> | |
<li><%= msg %></li> | |
<% end %> | |
</ul> | |
</div> | |
<% end %> | |
<%= collection_select(:classdate, :course_id, Course.all, :id, :name, {:include_blank => "Select a Course"}, | |
{ :class => "selectpicker"} | |
) %> | |
<div class="field"> | |
<%= f.label :startdatetime %><br> | |
<%= f.text_field :startdatetime, {class:"datetime-picker classdate"} %> | |
</div> | |
<div class="field"> | |
<%= f.label :enddatetime %><br> | |
<%= f.text_field :enddatetime, {class:"datetime-picker classdate"} %> | |
</div> | |
<div class="field"> | |
<%= f.label :priceoverride %><br> | |
<%= f.number_field :priceoverride %> | |
<span>If this is not set, it will default to the course price.</span> | |
</div> | |
<!-- ADDITIONAL REQUIREMENTS --> | |
<h2>Special Requirements</h2> | |
<em>These are in addition to the regular requirements of the class.</em> | |
<div class="additional-worker-requirements"> | |
<h4>Worker requirements</h4> | |
<%= f.fields_for :additional_worker_requirements, :wrapper => false do |ff| %> | |
<%= ff.collection_select(:role, User::ROLES, :to_s, :humanize, {:include_blank => "Select a User Role"}, | |
{ :class => "selectpicker"} | |
) %><br> | |
<%= ff.label :number_required %> | |
<%= ff.text_field :number_required %><br> | |
<%= ff.link_to_remove "Remove this Requirement" %><br> | |
<% end %> | |
<p><%= f.link_to_add "Add an additional requirement", :additional_worker_requirements %></p> | |
</div> | |
<div class="additional-asset-requirements"> | |
<h4>Asset requirements</h4> | |
<%= f.fields_for :additional_asset_requirements, :wrapper => false do |ff| %> | |
<%= ff.label :asset_type %> | |
<%= ff.collection_select(:asset_type, @assettypes, :to_s, :humanize, {:include_blank => "Select an Asset Type"}, | |
{ :class => "selectpicker"} | |
) %><br> | |
<%= ff.label :number_required %> | |
<%= ff.text_field :number_required %><br> | |
<%= ff.link_to_remove "Remove this Requirement" %><br> | |
<% end %> | |
<p><%= f.link_to_add "Add a requirement", :additional_asset_requirements %></p> | |
</div> | |
<h2>Missing assets</h2> | |
<div class="missing-assets"> | |
<% @classdate.missing_assets.each_with_index do |ma,i| %> | |
<% type = "" | |
ma[1].each do |k,v| | |
if v.split("_").count > 1 | |
v = v.split("_")[0] + " (#{v.split("_")[1]})" | |
end | |
type+= "#{v}".humanize | |
end | |
%> | |
<h3> | |
<%= "#{ma[2]} #{type} <small>#{ma[0].to_s.humanize.singularize.pluralize ma[2]}</small>".html_safe %> | |
</h3> | |
<!-- Here is where I'd like to build and output associated resource fields, as defined by the missing asset object I'm currently looping through --> | |
<%= f.fields_for :assignments do |ff| %> | |
<div class="field assignable_id"> | |
<%= label "assignable_id", "Select an asset to assign:" %> | |
<%= collection_select(nil, nil, PhysicalAsset.all.where(:available => true), :id, :asset_type_and_id, {:include_blank => "Select a Physical Asset"}, | |
{ :class => "selectpicker", :"data-assignable-type" => "PhysicalAsset"} | |
) %> or | |
<%= collection_select(nil, nil, User.staff, :id, :fullname, {:include_blank => "Select a User"}, | |
{ :class => "selectpicker", :"data-assignable-type" => "User" } | |
) %> | |
<%= ff.hidden_field :assignable_type, {:class => "assignable-type"} %> | |
<%= ff.hidden_field :assignable_id, {:class => "assignable-id"} %> | |
</div> | |
<div class="field"> | |
<%= ff.label :startdatetime %><br> | |
<%= @classdate.startdatetime %> | |
<%= ff.hidden_field :startdatetime, :value => @classdate.startdatetime %> | |
</div> | |
<div class="field"> | |
<%= ff.label :enddatetime %><br> | |
<%= @classdate.enddatetime %> | |
<%= ff.hidden_field :enddatetime, :value => @classdate.enddatetime %> | |
</div> | |
<div class="field"> | |
<%= ff.hidden_field :classdate_id, :value => @classdate.id %> | |
</div> | |
<div class="field"> | |
<%= ff.label :notes %><br> | |
<%= ff.text_area :notes %> | |
</div> | |
<div class="field"> | |
<%= ff.label :approved %><br> | |
<%= ff.check_box :approved %> | |
</div> | |
<%= ff.link_to_remove "Remove this Assignment" %><br> | |
<% end %> | |
<% end %> | |
</div> | |
<div class="actions"> | |
<%= f.submit %> | |
</div> | |
<% end %> | |
<% else %> | |
<form action="./new" method="GET"> | |
<h2>Select a course to create this class date.</h2> | |
<div class="field"> | |
<%= label "Course", :course %> | |
<%= collection_select(:classdate, :course_id, Course.all, :id, :name, {:include_blank => "Please Select a Course"}, | |
{ :class => "selectpicker"} | |
) %> | |
</div> | |
<div class="submit"><input type="submit" value="Next »"></div> | |
</form> | |
<% end %> |
This file contains hidden or 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 Classdate < ActiveRecord::Base | |
validates_presence_of :course_id, :message => "You must have an associated course to create a class date." | |
belongs_to :course | |
# delegate :asset_requirements, :to => :course | |
# delegate :worker_requirements, :to => :course | |
has_many :asset_requirements, :through => :course | |
has_many :worker_requirements, :through => :course | |
has_many :additional_asset_requirements | |
has_many :additional_worker_requirements | |
accepts_nested_attributes_for :additional_worker_requirements, :allow_destroy => true | |
accepts_nested_attributes_for :additional_asset_requirements, :allow_destroy => true | |
has_and_belongs_to_many :people | |
has_many :assignments | |
accepts_nested_attributes_for :assignments, :allow_destroy => true, reject_if: proc { |attributes| attributes[:assignable_id].blank? } | |
def asset_requirements | |
# self.asset_requirement_overrides.nil? ? course.asset_requirements : self.asset_requirement_overrides | |
course.asset_requirements + self.additional_asset_requirements | |
end | |
def worker_requirements | |
# self.worker_requirement_overrides.nil? ? course.worker_requirements : self.worker_requirement_overrides | |
course.worker_requirements + self.additional_worker_requirements | |
end | |
def auto_assign_assets | |
# if current_user.role? :admin | |
missing_assets.each do |req| | |
klass = req[0].to_s.singularize.camelize.constantize | |
notavailable = Assignment.where("((assignments.startdatetime > ? AND assignments.startdatetime < ?) OR (assignments.enddatetime > ? AND assignments.enddatetime < ?)) AND assignments.assignable_type = ?", startdatetime, enddatetime, startdatetime, enddatetime, klass).pluck(:assignable_id) | |
if notavailable.empty? | |
@available_assets = klass.order("RANDOM()") | |
else | |
@available_assets = klass.order("RANDOM()").where('id NOT IN (?)', notavailable).where(req[1]).limit(req[2]) | |
end | |
end | |
# end | |
end | |
def missing_assets | |
assetcounts = { :users => {}, :physical_assets => {} } | |
as = assignments | |
missing = [] | |
as.where(:assignable_type => "User").each do |assignment| | |
assetcounts[:users][assignment.assignable.role] = assetcounts[:users][assignment.assignable.role].to_i + 1 | |
end | |
as.where(:assignable_type => "PhysicalAsset").each do |pa| | |
assetcounts[:physical_assets][pa.assignable.asset_type] = assetcounts[:physical_assets][pa.assignable.asset_type].to_i + 1 | |
end | |
puts assetcounts.inspect | |
worker_requirements.each do |req| | |
assigned = assetcounts[:users][req.role].to_i | |
needed = req.number_required | |
assigned < needed and missing << [:users, {:role => req.role}, needed - assigned] | |
end | |
asset_requirements.each do |req| | |
assigned = assetcounts[:physical_assets][req.asset_type].to_i | |
needed = req.number_required | |
assigned < needed and missing << [:physical_assets, {:asset_type => req.asset_type}, needed - assigned] | |
end | |
return missing | |
end | |
def assets_ready? | |
missing_assets.empty? | |
end | |
end |
This file contains hidden or 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 Course < ActiveRecord::Base | |
has_many :classdates | |
has_many :people, :through => :classdates | |
has_and_belongs_to_many :instructors, :class_name => "User", :join_table => "instructors_teachable_courses" | |
has_many :asset_requirements | |
has_many :worker_requirements | |
accepts_nested_attributes_for :asset_requirements, :allow_destroy => true | |
accepts_nested_attributes_for :worker_requirements, :allow_destroy => true | |
has_many :prerequisites | |
has_many :prerequisite_courses, :through => :prerequisites | |
end |
This file contains hidden or 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
<%= nested_form_for(@course) do |f| %> | |
<% if @course.errors.any? %> | |
<div id="error_explanation"> | |
<h2><%= pluralize(@course.errors.count, "error") %> prohibited this course from being saved:</h2> | |
<ul> | |
<% @course.errors.full_messages.each do |msg| %> | |
<li><%= msg %></li> | |
<% end %> | |
</ul> | |
</div> | |
<% end %> | |
<div class="field"> | |
<%= f.label :name %><br> | |
<%= f.text_field :name %> | |
</div> | |
<div class="field"> | |
<%= f.label :description %><br> | |
<%= f.text_area :description %> | |
</div> | |
<div class="field"> | |
<%= f.label :price %><br> | |
<%= f.number_field :price %> | |
</div> | |
<div class="field"> | |
<%= f.label :class_size %><br> | |
<%= f.number_field :class_size %> | |
</div> | |
<div class="field"> | |
<h3>Prerequisites</h3> | |
<%= hidden_field_tag "course[prerequisite_course_ids][]", nil %> | |
<% Course.all.where("id NOT IN (?)", @course.id || 0).each do |course| %> | |
<div> | |
<%= check_box_tag "course[prerequisite_course_ids][]", course.id, @course.prerequisite_courses.include?(course) %> | |
<%= link_to course.name, course %> | |
</div> | |
<% end %> | |
</div> | |
<div class="field"> | |
<%= f.label :agemin %><br> | |
<%= f.number_field :agemin %> | |
</div> | |
<div class="field"> | |
<%= f.label :agemax %><br> | |
<%= f.number_field :agemax, :value => 99 %> | |
</div> | |
<div class="worker-requirements"> | |
<h4>Worker requirements</h4> | |
<%= f.fields_for :worker_requirements, :wrapper => false do |ff| %> | |
<%= ff.collection_select(:role, User::ROLES, :to_s, :humanize, {:include_blank => "Select a User Role"}, | |
{ :class => "selectpicker"} | |
) %><br> | |
<%= ff.label :number_required %> | |
<%= ff.text_field :number_required %><br> | |
<%= ff.link_to_remove "Remove this Requirement" %><br> | |
<% end %> | |
<p><%= f.link_to_add "Add a requirement", :worker_requirements %></p> | |
</div> | |
<div class="asset-requirements"> | |
<h4>Asset requirements</h4> | |
<%= f.fields_for :asset_requirements, :wrapper => false do |ff| %> | |
<%= ff.label :asset_type %> | |
<%= ff.collection_select(:asset_type, @assettypes, :to_s, :humanize, {:include_blank => "Select an Asset Type"}, | |
{ :class => "selectpicker"} | |
) %><br> | |
<%= ff.label :number_required %> | |
<%= ff.text_field :number_required %><br> | |
<%= ff.link_to_remove "Remove this Requirement" %><br> | |
<% end %> | |
<p><%= f.link_to_add "Add a requirement", :asset_requirements %></p> | |
</div> | |
<div class="actions"> | |
<%= f.submit %> | |
</div> | |
<% end %> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment