Skip to content

Instantly share code, notes, and snippets.

@Antiarchitect
Created November 29, 2010 01:39
Show Gist options
  • Save Antiarchitect/719477 to your computer and use it in GitHub Desktop.
Save Antiarchitect/719477 to your computer and use it in GitHub Desktop.
jQuery.js
# Этот контроллер отвечает за управление интерактивной сеткой расписания для редактора расписания.
class Editor::ClassroomsController < Editor::BaseController
def index
flash[:error] = nil
request.xhr? ? nil : cookies[:classrooms] = YAML.dump([0])
@classroom_id = params[:classroom_id]
except = params[:except] ? params[:except].split(',').map { |e| e.to_i } : "0"
classroom = params[:classroom].to_s.gsub('%', '\%').gsub('_', '\_') + '%'
respond_to do |format|
format.html
format.json { render :json => Classroom.all(:conditions => ['classrooms.id NOT IN (?) AND classrooms.name LIKE ?', except, classroom],
:include => [:building]).to_json(:only => [:id, :name], :include => { :building => { :only => :name } } )}
end
end
def show
@days = Timetable.days
@times = Timetable.times
@weeks = Timetable.weeks
@classroom = Classroom.find_by_id(params[:id], :include => :pairs)
unless @classroom
flash[:error] = 'Нет аудитории с таким названием'
else
@pairs = @classroom.pairs
grids = YAML.load(cookies[:classrooms])
grids << @classroom.id
cookies[:classrooms] = YAML.dump(grids)
@classrooms = YAML.load(cookies[:classrooms])
respond_to do |format|
format.js
end
end
end
def edit
end
def update
end
def destroy
@id = params[:id]
grids = YAML.load(cookies[:classrooms])
grids.delete(@id.to_i)
cookies[:classrooms] = YAML.dump(grids)
@classrooms = YAML.load(cookies[:classrooms])
respond_to do |format|
format.js
end
end
end
// Здесь представлен пример использования json ответа от контроллера ClassroomsController как источника данных для autocomplete, входящего в набор jQuery UI.
$('#classroom_name').change(function () {
$(this).autocomplete('disable'); // These two lines fixes bug with simultaneously
$(this).autocomplete('enable'); // opening two same auditories at once (on fast clicking)
});
$('#classroom_name').autocomplete({
disabled: false,
source: function(request, response) {
$.getJSON('/editor/classrooms.json', {
classroom: request.term
},
function(data) {
var classrooms = new Array(0);
data.each(function(i) {
classrooms.push({ label: i.classroom.name + ' (' + i.classroom.building.name + ')', value: i.classroom.id });
});
response(classrooms);
});
},
select: function(event, ui) {
$.get('/editor/classrooms/' + ui.item.value);
return false;
}
});
# Пример модели со сложной валидацией.
class Pair < ActiveRecord::Base
belongs_to :charge_card
belongs_to :classroom
has_many :subgroups, :dependent => :destroy
accepts_nested_attributes_for :subgroups
def name
[lecturer, full_discipline, 'ауд: ' + classroom.full_name, timeslot, groups_string].select{ |e| e != ''}.join(', ')
end
def timeslot
str = [Timetable.days[day_of_the_week - 1], pair_number.to_s + '-я пара', week_string].join(', ')
str == '' ? '' : 'окно: ' + str
end
def week_string
week == 0 ? 'обе недели' : week.to_s + '-я неделя'
end
def full_lecturer
self.try(:charge_card).try(:teaching_place).try(:name)
end
def lecturer
unless full_lecturer.blank?
apart = full_lecturer.split(' ')
name = apart[0]
name += (' ' + apart[1].slice(/^./) + '.') if apart[1]
name += (apart[2].slice(/^./) + '.') if apart[2]
end
name || ''
end
def lecturer_id
self.try(:charge_card).try(:teaching_place).try(:lecturer).try(:id) || nil
end
def full_discipline
full = self.try(:charge_card).try(:discipline).try(:name)
unless (lesson = self.try(:charge_card).try(:lesson_type).try(:name)).nil?
full += ' (' + lesson + ')'
end
full || ''
end
def discipline
self.try(:charge_card).try(:discipline).try(:name)
end
def short_discipline
self.try(:charge_card).try(:discipline).try(:short_name)
end
def groups
unless charge_card.nil?
groups = charge_card.groups
end
groups || []
end
def groups_string
g = groups.map do |g|
name = g.name.to_s
if (number = g.subgroups.find_by_pair_id(id).number) == 0
subgroup = ''
else
subgroup = ' (' + number.to_s + '-я подгруппа)'
end
name + subgroup
end
g.join(', ') == '' ? '' : 'группы: ' + g.join(', ')
end
def lesson_type
self.try(:charge_card).try(:lesson_type).try(:name)
end
private
def validate_on_create
conditions = { :classroom_id => classroom_id,
:day_of_the_week => day_of_the_week,
:pair_number => pair_number,
:week => [ 0, week ] }
if (conflicts = Pair.all(:conditions => conditions)).size > 0
pairs = conflicts.map { |p| p.name }.join('<br />')
errors.add_to_base "Невозможно создать пару, так как следующая пара:<br /><br />" +
pairs +
"<br /><br />уже существует в этом временном окне этой аудитории."
end
end
def validate_on_update
week_conditions = week == 0 ? [0, 1, 2] : [0, week]
conditions = ['pairs.id <> ? AND pairs.day_of_the_week = ? AND pairs.pair_number = ? AND pairs.week IN (?)',
id, day_of_the_week, pair_number, week_conditions]
if (candidates = Pair.all(:conditions => conditions)).size > 0
# classroom busyness
if (conflicts = candidates.select { |c| c.classroom_id == classroom.id }).size > 0
pairs = conflicts.map { |p| p.name }.join('<br />')
errors.add_to_base "Невозможно сохранить пару, так как следующие пары:<br /><br />" +
pairs +
"<br /><br />уже существуют в этом временном окне этой аудитории."
candidates -= conflicts
end
# lecturer busyness
if (conflicts = candidates.select { |c| c.charge_card && charge_card && c.charge_card.teaching_place.lecturer == charge_card.teaching_place.lecturer }).size > 0
pairs = conflicts.map { |p| p.name }.join('<br />')
errors.add_to_base "Невозможно сохранить пару, так как этот преподаватель уже ведет следующие пары:<br /><br />" +
pairs +
"<br /><br />в этом временном окне."
candidates -= conflicts
end
# subgroups busyness
if (conflicts = candidates.select { |c| c.charge_card && charge_card && (c.charge_card.groups & charge_card.groups).size > 0 }).size > 0
groups_intersect = conflicts.map { |c| [c, c.charge_card.groups & charge_card.groups] }
conflicts = []
groups_intersect.each do |intersect|
intersect[1].each do |group|
pair = subgroups.select { |s| group.jets.map { |j| j.id }.include?(s.jet_id) }.first
candidate = group.subgroups.find_by_pair_id(intersect[0].id)
if pair && candidate && (pair.number == 0 || candidate.number == 0 || pair.number == candidate.number)
conflicts << intersect[0]
end
end
end
if conflicts.size > 0
pairs = conflicts.map { |p| p.name }.join('<br />')
errors.add_to_base "Невозможно сохранить пару, так как у одной или нескольких групп существуют следующие пары:<br /><br />" +
pairs +
"<br /><br />в этом временном окне."
candidates -= conflicts
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment