Skip to content

Commit

Permalink
One user can manage multiple departments.
Browse files Browse the repository at this point in the history
Rename 'dept_head' to 'department' in URLs. Store current_department.id in URL.
  • Loading branch information
Envek committed Jan 31, 2013
1 parent 8c41ac9 commit eeb099a
Show file tree
Hide file tree
Showing 51 changed files with 173 additions and 84 deletions.
4 changes: 4 additions & 0 deletions app/assets/javascripts/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ jQuery(document).ready(function($) {
$('.semester_change').change(function() {
$(this).parent().submit();
});
$('.department_change+button').remove();
$('.department_change').change(function() {
$(this).parent().submit();
});
});

function formatResultedClassroomForSelect2 (result) {
Expand Down
11 changes: 10 additions & 1 deletion app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,22 @@ def change_current_semester
redirect_to (request.referer.blank?? timetable_groups_path : request.referer)
end

helper_method :current_department
def current_department
@current_department ||= (params[:department_id] and Department.find(params[:department_id]))
@current_department ||= Department.accessible_by(current_ability, :update).where(id: session[:current_department_id]).first
@current_department ||= Department.accessible_by(current_ability, :update).first
session[:current_department_id] = @current_department.id
return @current_department
end

protected

def after_sign_in_path_for(resource)
return admin_dept_heads_path if current_user.admin?
return supervisor_lecturers_path if current_user.supervisor?
return editor_groups_root_path if current_user.editor?
return dept_head_teaching_places_path if current_user.department
return department_teaching_places_path(current_user.department_ids.first) if current_user.department_ids.any?
return request.referrer
end

Expand Down
23 changes: 23 additions & 0 deletions app/controllers/department/base_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# -*- encoding : utf-8 -*-
class Department::BaseController < ApplicationController
before_filter :authenticate_user!
before_filter :check_access_rights, :except => :change_current_department

def change_current_department
url = if request.referer.present?
url_options = Rails.application.routes.recognize_path request.referer
url_options[:department_id] = current_department.id
url_for(url_options)
end
redirect_to (url.present?? url : department_teaching_places_path(current_department.id))
end

protected

def check_access_rights
unless Department.accessible_by(current_ability, :update).pluck(:id).include? params[:department_id].to_i
raise CanCan::AccessDenied
end
end

end
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- encoding : utf-8 -*-
class DeptHead::ChargeCardsController < DeptHead::BaseController
class Department::ChargeCardsController < Department::BaseController
active_scaffold do |config|
config.actions << :delete
config.columns = [:semester, :teaching_place, :assistant_teaching_place, :lesson_type, :jets, :discipline, :hours_quantity, :hours_per_week, :weeks_quantity, :groups, :preferred_classrooms]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class DeptHead::ClassroomsController < ApplicationController
class Department::ClassroomsController < Department::BaseController
active_scaffold :classroom do |conf|
conf.actions.exclude :create
conf.columns = [:building, :name, :title, :department, :department_lock, :capacity, :properties]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- encoding : utf-8 -*-
class DeptHead::DisciplinesController < DeptHead::BaseController
class Department::DisciplinesController < Department::BaseController
record_select :search_on => :name, :order_by => :name
active_scaffold do |config|
config.actions << :delete
Expand All @@ -13,15 +13,15 @@ class DeptHead::DisciplinesController < DeptHead::BaseController
protected

def before_create_save(record)
if @dept ||= current_user.department
if @dept ||= current_department
record.department_id = @dept.id
end
end

# Records are filtered according to CanCan abilities.

def record_select_conditions_from_controller
if @dept ||= current_user.department
if @dept ||= current_department
{:department_id => @dept.id}
else
{:department_id => nil}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- encoding : utf-8 -*-
class DeptHead::GroupsController < DeptHead::BaseController
class Department::GroupsController < Department::BaseController
before_filter :customize_active_scaffold
record_select :search_on => :name, :order_by => :name
active_scaffold do |config|
Expand Down Expand Up @@ -27,7 +27,7 @@ def teaching_plan
def customize_active_scaffold
actions = [:create, :update, :delete]
config = active_scaffold_config
if nested? and nested_parent_record.department_id != current_user.department_id
if nested? and nested_parent_record.department_id != current_department.id
actions.each do |action|
config.actions.exclude action
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- encoding : utf-8 -*-
class DeptHead::JetsController < DeptHead::BaseController
class Department::JetsController < Department::BaseController
active_scaffold do |config|
config.columns = [:group, :subgroups_quantity]
config.columns[:group].form_ui = :record_select
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- encoding : utf-8 -*-
class DeptHead::LecturersController < DeptHead::BaseController
class Department::LecturersController < Department::BaseController
record_select :search_on => :name, :order_by => :name
active_scaffold do |config|
config.actions << :delete
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- encoding : utf-8 -*-
class DeptHead::SpecialitiesController < DeptHead::BaseController
class Department::SpecialitiesController < Department::BaseController
include GosinspParser

active_scaffold do |config|
Expand All @@ -22,7 +22,7 @@ def teaching_plan

def teaching_plan_import
if params[:plan] and params[:plan].class == ActionDispatch::Http::UploadedFile
@specialities = current_user.department.specialities
@specialities = current_department.specialities
@speciality, @results, @errors = parse_and_fill_teaching_plan(params[:plan].read, @specialities)
render "supervisor/teaching_plans/fill"
return
Expand All @@ -31,7 +31,7 @@ def teaching_plan_import

def add_charge_cards
@speciality = Speciality.find(params[:id])
discipline_ids = current_user.department.disciplines.select("id").map{|d| d.id}
discipline_ids = current_department.disciplines.select("id").map{|d| d.id}
to_remove_ids = @speciality.groups.map{|g| g.jets}.flatten.map{|j| j.charge_card_id}.uniq
@cards_to_remove = ChargeCard.where(:id => to_remove_ids, :discipline_id => discipline_ids, :semester_id => current_semester.id).count
# List all charge cards to be created
Expand Down Expand Up @@ -76,7 +76,7 @@ def add_charge_cards

def create_charge_cards
@speciality = Speciality.find(params[:id])
discipline_ids = current_user.department.disciplines.select("id").map{|d| d.id}
discipline_ids = current_department.disciplines.select("id").map{|d| d.id}
conditions = {:speciality_id => @speciality.id, :discipline_id => discipline_ids}
if params[:remove]
ids = @speciality.groups.map{|g| g.jets}.flatten.map{|j| j.charge_card_id}.uniq
Expand All @@ -94,20 +94,20 @@ def create_charge_cards
end
end
end
redirect_to "/dept_head/specialities", :notice => "Создано карт нагрузок: #{created}#{", удалено: #{deleted}" if deleted}"
redirect_to "/department/#{params[:department_id]}/specialities", :notice => "Создано карт нагрузок: #{created}#{", удалено: #{deleted}" if deleted}"
end

protected

def before_create_save(record)
if dept = current_user.department
if dept = current_department
record.department_id = dept.id
end
end

def conditions_for_collection
if dept = current_user.department
discipline_ids = current_user.department.disciplines.pluck(:id)
if dept = current_department
discipline_ids = current_department.disciplines.pluck(:id)
conditions = {:discipline_id => discipline_ids, :semester => current_semester.number}
ids = TeachingPlan.all(:conditions => conditions, :select => "DISTINCT(speciality_id)").map{ |tp| tp.speciality_id }
["department_id = :department_id OR id IN (:id)", {:department_id => dept.id, :id => ids }]
Expand All @@ -117,7 +117,7 @@ def conditions_for_collection
end

def custom_finder_options
{:reorder => "department_id = #{current_user.department_id} DESC, code ASC"}
{:reorder => "department_id = #{current_department.id} DESC, code ASC"}
end

end
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- encoding : utf-8 -*-
class DeptHead::TeachingPlacesController < DeptHead::BaseController
class Department::TeachingPlacesController < Department::BaseController
active_scaffold do |config|
config.actions << :delete
config.columns = [:position, :lecturer]
Expand All @@ -16,7 +16,7 @@ class DeptHead::TeachingPlacesController < DeptHead::BaseController
protected

def before_create_save(record)
@dept ||= current_user.department
@dept ||= current_department
record.department_id = @dept.id
end

Expand Down
5 changes: 0 additions & 5 deletions app/controllers/dept_head/base_controller.rb

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# -*- encoding : utf-8 -*-
module DeptHead::BaseHelper
module Department::BaseHelper
end
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# -*- encoding : utf-8 -*-
module DeptHead::ChargeCardsHelper
module Department::ChargeCardsHelper
def options_for_association_conditions(association)
case association.name
when :teaching_place
{'teaching_places.department_id' => current_user.department.id}
{'teaching_places.department_id' => current_department.id}
when :assistant_teaching_place
{'teaching_places.department_id' => current_user.department.id}
{'teaching_places.department_id' => current_department.id}
when :discipline
{'disciplines.department_id' => current_user.department.id}
{'disciplines.department_id' => current_department.id}
else
super
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module DeptHead::ClassroomsHelper
module Department::ClassroomsHelper

def properties_column(record, column)
record.properties_human
end

end
end
3 changes: 3 additions & 0 deletions app/helpers/department/disciplines_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# -*- encoding : utf-8 -*-
module Department::DisciplinesHelper
end
3 changes: 3 additions & 0 deletions app/helpers/department/groups_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# -*- encoding : utf-8 -*-
module Department::GroupsHelper
end
3 changes: 3 additions & 0 deletions app/helpers/department/lecturers_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# -*- encoding : utf-8 -*-
module Department::LecturersHelper
end
8 changes: 8 additions & 0 deletions app/helpers/department/specialities_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# -*- encoding : utf-8 -*-
module Department::SpecialitiesHelper

def list_row_class(record)
record.department_id != current_department.id ? 'alien-speciality' : ''
end

end
3 changes: 3 additions & 0 deletions app/helpers/department/teaching_places_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# -*- encoding : utf-8 -*-
module Department::TeachingPlacesHelper
end
3 changes: 0 additions & 3 deletions app/helpers/dept_head/disciplines_helper.rb

This file was deleted.

3 changes: 0 additions & 3 deletions app/helpers/dept_head/groups_helper.rb

This file was deleted.

3 changes: 0 additions & 3 deletions app/helpers/dept_head/lecturers_helper.rb

This file was deleted.

8 changes: 0 additions & 8 deletions app/helpers/dept_head/specialities_helper.rb

This file was deleted.

3 changes: 0 additions & 3 deletions app/helpers/dept_head/teaching_places_helper.rb

This file was deleted.

2 changes: 1 addition & 1 deletion app/helpers/supervisor/charge_cards_helper.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# -*- encoding : utf-8 -*-
module Supervisor::ChargeCardsHelper
include DeptHead::ChargeCardsHelper
include Department::ChargeCardsHelper
end
23 changes: 12 additions & 11 deletions app/models/ability.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,25 @@ def initialize(user, params)
can :manage, Pair
can :read, :all
cannot :read, User
elsif user.department_id.present? # DeptHead
elsif user.department_ids.any? # DeptHead
can :update, Department, id: user.department_ids
can :browse, Lecturer
can :browse, Discipline
can :create, TeachingPlace
can :manage, ChargeCard, discipline: { department_id: user.department_id }
can :manage, TeachingPlace, department_id: user.department_id
can :manage, Discipline, department_id: user.department_id
can :manage, Classroom, department_id: user.department_id
can :manage, ChargeCard, discipline: { department_id: user.department_ids }
can :manage, TeachingPlace, department_id: user.department_ids
can :manage, Discipline, department_id: user.department_ids
can :manage, Classroom, department_id: user.department_ids
can :manage, Jet
# All the difficulties with teaching plans
can :read, Speciality
can :create, Speciality, department_id: user.department_id
can :update, Speciality, department_id: user.department_id
can :destroy, Speciality, department_id: user.department_id
can :create, Speciality, department_id: user.department_ids
can :update, Speciality, department_id: user.department_ids
can :destroy, Speciality, department_id: user.department_ids
can [:read, :browse], Group
can :create, Group, speciality: { department_id: user.department_id }
can :update, Group, speciality: { department_id: user.department_id }
can :destroy, Group, speciality: { department_id: user.department_id }
can :create, Group, speciality: { department_id: user.department_ids }
can :update, Group, speciality: { department_id: user.department_ids }
can :destroy, Group, speciality: { department_id: user.department_ids }
else
can :read, Pair
can :read, Group
Expand Down
2 changes: 1 addition & 1 deletion app/models/department.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class Department < ActiveRecord::Base
has_many :teaching_places, :dependent => :destroy
has_many :lecturers, :through => :teaching_places
has_many :specialities, :dependent => :destroy
has_many :dept_heads, :dependent => :destroy, :class_name => "User"
has_and_belongs_to_many :dept_heads, :class_name => "User"

validates :name, :presence => true, :uniqueness => true
validates :short_name, :presence => true, :uniqueness => true
Expand Down
4 changes: 2 additions & 2 deletions app/models/group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,13 @@ def self.escape_name(name)
def authorized_for_update?
return false unless current_user
return true if current_user.admin? or current_user.supervisor?
return self.speciality.try(:department_id) == current_user.department_id
return self.speciality.try(:department_id) == current_department.id
end

def authorized_for_delete?
return false unless current_user
return true if current_user.admin? or current_user.supervisor?
return self.speciality.try(:department_id) == current_user.department_id
return self.speciality.try(:department_id).include? current_department.id
end

def update_charge_cards_editor_titles
Expand Down
4 changes: 2 additions & 2 deletions app/models/speciality.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ def to_label
def authorized_for_update?
return false unless current_user
return true if current_user.admin? or current_user.supervisor?
return self.department_id == current_user.department_id
return self.department_id == current_department.id
end

def authorized_for_delete?
return false unless current_user
return true if current_user.admin? or current_user.supervisor?
return self.department_id == current_user.department_id
return self.department_id == current_department.id
end

end
2 changes: 1 addition & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class User < ActiveRecord::Base

attr_accessible :login, :name, :email, :password, :password_confirmation, :remember_me

belongs_to :department
has_and_belongs_to_many :departments

protected
def password_required?
Expand Down
Loading

0 comments on commit eeb099a

Please sign in to comment.