From eeb099a8abb182c205984057088b521cf567284f Mon Sep 17 00:00:00 2001 From: Novikov Andrey Date: Thu, 31 Jan 2013 16:34:49 +1000 Subject: [PATCH] One user can manage multiple departments. Rename 'dept_head' to 'department' in URLs. Store current_department.id in URL. --- app/assets/javascripts/common.js | 4 ++ app/controllers/application_controller.rb | 11 ++++- app/controllers/department/base_controller.rb | 23 ++++++++++ .../charge_cards_controller.rb | 2 +- .../classrooms_controller.rb | 2 +- .../disciplines_controller.rb | 6 +-- .../groups_controller.rb | 4 +- .../jets_controller.rb | 2 +- .../lecturers_controller.rb | 2 +- .../specialities_controller.rb | 18 ++++---- .../teaching_places_controller.rb | 4 +- app/controllers/dept_head/base_controller.rb | 5 --- .../{dept_head => department}/base_helper.rb | 2 +- .../charge_cards_helper.rb | 8 ++-- .../classrooms_helper.rb | 4 +- app/helpers/department/disciplines_helper.rb | 3 ++ app/helpers/department/groups_helper.rb | 3 ++ app/helpers/department/lecturers_helper.rb | 3 ++ app/helpers/department/specialities_helper.rb | 8 ++++ .../department/teaching_places_helper.rb | 3 ++ app/helpers/dept_head/disciplines_helper.rb | 3 -- app/helpers/dept_head/groups_helper.rb | 3 -- app/helpers/dept_head/lecturers_helper.rb | 3 -- app/helpers/dept_head/specialities_helper.rb | 8 ---- .../dept_head/teaching_places_helper.rb | 3 -- app/helpers/supervisor/charge_cards_helper.rb | 2 +- app/models/ability.rb | 23 +++++----- app/models/department.rb | 2 +- app/models/group.rb | 4 +- app/models/speciality.rb | 4 +- app/models/user.rb | 2 +- .../application/_change_department.html.haml | 6 +++ .../_properties_form_column.html.haml | 0 .../classrooms/list.html.erb | 0 .../specialities/add_charge_cards.html.haml | 0 .../specialities/list.html.erb | 0 .../specialities/show.html.haml | 0 .../teaching_plan_import.html.haml | 0 app/views/layouts/_department_menu.html.haml | 12 +++--- app/views/layouts/application.html.haml | 8 ++-- config/routes.rb | 7 +++- ...0130130014707_many_departments_for_user.rb | 42 +++++++++++++++++++ db/schema.rb | 8 +++- ..._spec.rb => department_classrooms_spec.rb} | 0 .../base_controller_test.rb | 0 .../charge_cards_controller_test.rb | 0 .../disciplines_controller_test.rb | 0 .../groups_controller_test.rb | 0 .../lecturers_controller_test.rb | 0 .../specialities_controller_test.rb | 0 .../teaching_places_controller_test.rb | 0 51 files changed, 173 insertions(+), 84 deletions(-) create mode 100644 app/controllers/department/base_controller.rb rename app/controllers/{dept_head => department}/charge_cards_controller.rb (96%) rename app/controllers/{dept_head => department}/classrooms_controller.rb (87%) rename app/controllers/{dept_head => department}/disciplines_controller.rb (82%) rename app/controllers/{dept_head => department}/groups_controller.rb (94%) rename app/controllers/{dept_head => department}/jets_controller.rb (73%) rename app/controllers/{dept_head => department}/lecturers_controller.rb (77%) rename app/controllers/{dept_head => department}/specialities_controller.rb (87%) rename app/controllers/{dept_head => department}/teaching_places_controller.rb (86%) delete mode 100644 app/controllers/dept_head/base_controller.rb rename app/helpers/{dept_head => department}/base_helper.rb (50%) rename app/helpers/{dept_head => department}/charge_cards_helper.rb (75%) rename app/helpers/{dept_head => department}/classrooms_helper.rb (65%) create mode 100644 app/helpers/department/disciplines_helper.rb create mode 100644 app/helpers/department/groups_helper.rb create mode 100644 app/helpers/department/lecturers_helper.rb create mode 100644 app/helpers/department/specialities_helper.rb create mode 100644 app/helpers/department/teaching_places_helper.rb delete mode 100644 app/helpers/dept_head/disciplines_helper.rb delete mode 100644 app/helpers/dept_head/groups_helper.rb delete mode 100644 app/helpers/dept_head/lecturers_helper.rb delete mode 100644 app/helpers/dept_head/specialities_helper.rb delete mode 100644 app/helpers/dept_head/teaching_places_helper.rb create mode 100644 app/views/application/_change_department.html.haml rename app/views/{dept_head => department}/classrooms/_properties_form_column.html.haml (100%) rename app/views/{dept_head => department}/classrooms/list.html.erb (100%) rename app/views/{dept_head => department}/specialities/add_charge_cards.html.haml (100%) rename app/views/{dept_head => department}/specialities/list.html.erb (100%) rename app/views/{dept_head => department}/specialities/show.html.haml (100%) rename app/views/{dept_head => department}/specialities/teaching_plan_import.html.haml (100%) create mode 100644 db/migrate/20130130014707_many_departments_for_user.rb rename spec/features/{dept_head_classrooms_spec.rb => department_classrooms_spec.rb} (100%) rename test/functional/{dept_head => department}/base_controller_test.rb (100%) rename test/functional/{dept_head => department}/charge_cards_controller_test.rb (100%) rename test/functional/{dept_head => department}/disciplines_controller_test.rb (100%) rename test/functional/{dept_head => department}/groups_controller_test.rb (100%) rename test/functional/{dept_head => department}/lecturers_controller_test.rb (100%) rename test/functional/{dept_head => department}/specialities_controller_test.rb (100%) rename test/functional/{dept_head => department}/teaching_places_controller_test.rb (100%) diff --git a/app/assets/javascripts/common.js b/app/assets/javascripts/common.js index 7abebd4..789afdf 100644 --- a/app/assets/javascripts/common.js +++ b/app/assets/javascripts/common.js @@ -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) { diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 87920c4..0c73e1e 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -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 diff --git a/app/controllers/department/base_controller.rb b/app/controllers/department/base_controller.rb new file mode 100644 index 0000000..32d474b --- /dev/null +++ b/app/controllers/department/base_controller.rb @@ -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 diff --git a/app/controllers/dept_head/charge_cards_controller.rb b/app/controllers/department/charge_cards_controller.rb similarity index 96% rename from app/controllers/dept_head/charge_cards_controller.rb rename to app/controllers/department/charge_cards_controller.rb index aacd982..870f772 100644 --- a/app/controllers/dept_head/charge_cards_controller.rb +++ b/app/controllers/department/charge_cards_controller.rb @@ -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] diff --git a/app/controllers/dept_head/classrooms_controller.rb b/app/controllers/department/classrooms_controller.rb similarity index 87% rename from app/controllers/dept_head/classrooms_controller.rb rename to app/controllers/department/classrooms_controller.rb index aa156c0..a81c8fd 100644 --- a/app/controllers/dept_head/classrooms_controller.rb +++ b/app/controllers/department/classrooms_controller.rb @@ -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] diff --git a/app/controllers/dept_head/disciplines_controller.rb b/app/controllers/department/disciplines_controller.rb similarity index 82% rename from app/controllers/dept_head/disciplines_controller.rb rename to app/controllers/department/disciplines_controller.rb index fef9393..9e46909 100644 --- a/app/controllers/dept_head/disciplines_controller.rb +++ b/app/controllers/department/disciplines_controller.rb @@ -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 @@ -13,7 +13,7 @@ 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 @@ -21,7 +21,7 @@ def before_create_save(record) # 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} diff --git a/app/controllers/dept_head/groups_controller.rb b/app/controllers/department/groups_controller.rb similarity index 94% rename from app/controllers/dept_head/groups_controller.rb rename to app/controllers/department/groups_controller.rb index 89877f9..eaefc78 100644 --- a/app/controllers/dept_head/groups_controller.rb +++ b/app/controllers/department/groups_controller.rb @@ -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| @@ -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 diff --git a/app/controllers/dept_head/jets_controller.rb b/app/controllers/department/jets_controller.rb similarity index 73% rename from app/controllers/dept_head/jets_controller.rb rename to app/controllers/department/jets_controller.rb index 32f6317..3299662 100644 --- a/app/controllers/dept_head/jets_controller.rb +++ b/app/controllers/department/jets_controller.rb @@ -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 diff --git a/app/controllers/dept_head/lecturers_controller.rb b/app/controllers/department/lecturers_controller.rb similarity index 77% rename from app/controllers/dept_head/lecturers_controller.rb rename to app/controllers/department/lecturers_controller.rb index c0e492e..bd2464e 100644 --- a/app/controllers/dept_head/lecturers_controller.rb +++ b/app/controllers/department/lecturers_controller.rb @@ -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 diff --git a/app/controllers/dept_head/specialities_controller.rb b/app/controllers/department/specialities_controller.rb similarity index 87% rename from app/controllers/dept_head/specialities_controller.rb rename to app/controllers/department/specialities_controller.rb index ff7f742..1143463 100644 --- a/app/controllers/dept_head/specialities_controller.rb +++ b/app/controllers/department/specialities_controller.rb @@ -1,5 +1,5 @@ # -*- encoding : utf-8 -*- -class DeptHead::SpecialitiesController < DeptHead::BaseController +class Department::SpecialitiesController < Department::BaseController include GosinspParser active_scaffold do |config| @@ -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 @@ -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 @@ -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 @@ -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 }] @@ -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 diff --git a/app/controllers/dept_head/teaching_places_controller.rb b/app/controllers/department/teaching_places_controller.rb similarity index 86% rename from app/controllers/dept_head/teaching_places_controller.rb rename to app/controllers/department/teaching_places_controller.rb index bfdf62d..b8b868c 100644 --- a/app/controllers/dept_head/teaching_places_controller.rb +++ b/app/controllers/department/teaching_places_controller.rb @@ -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] @@ -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 diff --git a/app/controllers/dept_head/base_controller.rb b/app/controllers/dept_head/base_controller.rb deleted file mode 100644 index 9e36be3..0000000 --- a/app/controllers/dept_head/base_controller.rb +++ /dev/null @@ -1,5 +0,0 @@ -# -*- encoding : utf-8 -*- -class DeptHead::BaseController < ApplicationController - before_filter :authenticate_user! - before_filter { raise CanCan::AccessDenied unless current_user.department } -end diff --git a/app/helpers/dept_head/base_helper.rb b/app/helpers/department/base_helper.rb similarity index 50% rename from app/helpers/dept_head/base_helper.rb rename to app/helpers/department/base_helper.rb index 752d127..52fa8b2 100644 --- a/app/helpers/dept_head/base_helper.rb +++ b/app/helpers/department/base_helper.rb @@ -1,3 +1,3 @@ # -*- encoding : utf-8 -*- -module DeptHead::BaseHelper +module Department::BaseHelper end diff --git a/app/helpers/dept_head/charge_cards_helper.rb b/app/helpers/department/charge_cards_helper.rb similarity index 75% rename from app/helpers/dept_head/charge_cards_helper.rb rename to app/helpers/department/charge_cards_helper.rb index 9394030..2889072 100644 --- a/app/helpers/dept_head/charge_cards_helper.rb +++ b/app/helpers/department/charge_cards_helper.rb @@ -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 diff --git a/app/helpers/dept_head/classrooms_helper.rb b/app/helpers/department/classrooms_helper.rb similarity index 65% rename from app/helpers/dept_head/classrooms_helper.rb rename to app/helpers/department/classrooms_helper.rb index 5b17bee..083f4f2 100644 --- a/app/helpers/dept_head/classrooms_helper.rb +++ b/app/helpers/department/classrooms_helper.rb @@ -1,7 +1,7 @@ -module DeptHead::ClassroomsHelper +module Department::ClassroomsHelper def properties_column(record, column) record.properties_human end -end \ No newline at end of file +end diff --git a/app/helpers/department/disciplines_helper.rb b/app/helpers/department/disciplines_helper.rb new file mode 100644 index 0000000..fd36fc0 --- /dev/null +++ b/app/helpers/department/disciplines_helper.rb @@ -0,0 +1,3 @@ +# -*- encoding : utf-8 -*- +module Department::DisciplinesHelper +end diff --git a/app/helpers/department/groups_helper.rb b/app/helpers/department/groups_helper.rb new file mode 100644 index 0000000..854006f --- /dev/null +++ b/app/helpers/department/groups_helper.rb @@ -0,0 +1,3 @@ +# -*- encoding : utf-8 -*- +module Department::GroupsHelper +end diff --git a/app/helpers/department/lecturers_helper.rb b/app/helpers/department/lecturers_helper.rb new file mode 100644 index 0000000..0082d26 --- /dev/null +++ b/app/helpers/department/lecturers_helper.rb @@ -0,0 +1,3 @@ +# -*- encoding : utf-8 -*- +module Department::LecturersHelper +end diff --git a/app/helpers/department/specialities_helper.rb b/app/helpers/department/specialities_helper.rb new file mode 100644 index 0000000..28c0c9d --- /dev/null +++ b/app/helpers/department/specialities_helper.rb @@ -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 diff --git a/app/helpers/department/teaching_places_helper.rb b/app/helpers/department/teaching_places_helper.rb new file mode 100644 index 0000000..793726f --- /dev/null +++ b/app/helpers/department/teaching_places_helper.rb @@ -0,0 +1,3 @@ +# -*- encoding : utf-8 -*- +module Department::TeachingPlacesHelper +end diff --git a/app/helpers/dept_head/disciplines_helper.rb b/app/helpers/dept_head/disciplines_helper.rb deleted file mode 100644 index f7d4917..0000000 --- a/app/helpers/dept_head/disciplines_helper.rb +++ /dev/null @@ -1,3 +0,0 @@ -# -*- encoding : utf-8 -*- -module DeptHead::DisciplinesHelper -end diff --git a/app/helpers/dept_head/groups_helper.rb b/app/helpers/dept_head/groups_helper.rb deleted file mode 100644 index 81c1114..0000000 --- a/app/helpers/dept_head/groups_helper.rb +++ /dev/null @@ -1,3 +0,0 @@ -# -*- encoding : utf-8 -*- -module DeptHead::GroupsHelper -end diff --git a/app/helpers/dept_head/lecturers_helper.rb b/app/helpers/dept_head/lecturers_helper.rb deleted file mode 100644 index 23e2270..0000000 --- a/app/helpers/dept_head/lecturers_helper.rb +++ /dev/null @@ -1,3 +0,0 @@ -# -*- encoding : utf-8 -*- -module DeptHead::LecturersHelper -end diff --git a/app/helpers/dept_head/specialities_helper.rb b/app/helpers/dept_head/specialities_helper.rb deleted file mode 100644 index ade7a03..0000000 --- a/app/helpers/dept_head/specialities_helper.rb +++ /dev/null @@ -1,8 +0,0 @@ -# -*- encoding : utf-8 -*- -module DeptHead::SpecialitiesHelper - - def list_row_class(record) - record.department_id != current_user.department_id ? 'alien-speciality' : '' - end - -end diff --git a/app/helpers/dept_head/teaching_places_helper.rb b/app/helpers/dept_head/teaching_places_helper.rb deleted file mode 100644 index c92018e..0000000 --- a/app/helpers/dept_head/teaching_places_helper.rb +++ /dev/null @@ -1,3 +0,0 @@ -# -*- encoding : utf-8 -*- -module DeptHead::TeachingPlacesHelper -end diff --git a/app/helpers/supervisor/charge_cards_helper.rb b/app/helpers/supervisor/charge_cards_helper.rb index b575363..96c35e2 100644 --- a/app/helpers/supervisor/charge_cards_helper.rb +++ b/app/helpers/supervisor/charge_cards_helper.rb @@ -1,4 +1,4 @@ # -*- encoding : utf-8 -*- module Supervisor::ChargeCardsHelper - include DeptHead::ChargeCardsHelper + include Department::ChargeCardsHelper end diff --git a/app/models/ability.rb b/app/models/ability.rb index fc6b37c..e1990ae 100644 --- a/app/models/ability.rb +++ b/app/models/ability.rb @@ -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 diff --git a/app/models/department.rb b/app/models/department.rb index 82d6656..cc15b01 100644 --- a/app/models/department.rb +++ b/app/models/department.rb @@ -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 diff --git a/app/models/group.rb b/app/models/group.rb index b374d10..967115b 100644 --- a/app/models/group.rb +++ b/app/models/group.rb @@ -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 diff --git a/app/models/speciality.rb b/app/models/speciality.rb index 9eaa4b2..ef60070 100644 --- a/app/models/speciality.rb +++ b/app/models/speciality.rb @@ -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 diff --git a/app/models/user.rb b/app/models/user.rb index 15d4f9a..ea01167 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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? diff --git a/app/views/application/_change_department.html.haml b/app/views/application/_change_department.html.haml new file mode 100644 index 0000000..c360faa --- /dev/null +++ b/app/views/application/_change_department.html.haml @@ -0,0 +1,6 @@ +- depts = Department.accessible_by(current_ability, :update) +- if depts.count > 1 + = form_tag '/department/change', :method => 'post' do |f| + = label_tag :department_id, "Текущая кафедра: " + = select_tag :department_id, options_for_select(depts.map { |d| [d.name, d.id] }, current_department.id), :class => :department_change + diff --git a/app/views/dept_head/classrooms/_properties_form_column.html.haml b/app/views/department/classrooms/_properties_form_column.html.haml similarity index 100% rename from app/views/dept_head/classrooms/_properties_form_column.html.haml rename to app/views/department/classrooms/_properties_form_column.html.haml diff --git a/app/views/dept_head/classrooms/list.html.erb b/app/views/department/classrooms/list.html.erb similarity index 100% rename from app/views/dept_head/classrooms/list.html.erb rename to app/views/department/classrooms/list.html.erb diff --git a/app/views/dept_head/specialities/add_charge_cards.html.haml b/app/views/department/specialities/add_charge_cards.html.haml similarity index 100% rename from app/views/dept_head/specialities/add_charge_cards.html.haml rename to app/views/department/specialities/add_charge_cards.html.haml diff --git a/app/views/dept_head/specialities/list.html.erb b/app/views/department/specialities/list.html.erb similarity index 100% rename from app/views/dept_head/specialities/list.html.erb rename to app/views/department/specialities/list.html.erb diff --git a/app/views/dept_head/specialities/show.html.haml b/app/views/department/specialities/show.html.haml similarity index 100% rename from app/views/dept_head/specialities/show.html.haml rename to app/views/department/specialities/show.html.haml diff --git a/app/views/dept_head/specialities/teaching_plan_import.html.haml b/app/views/department/specialities/teaching_plan_import.html.haml similarity index 100% rename from app/views/dept_head/specialities/teaching_plan_import.html.haml rename to app/views/department/specialities/teaching_plan_import.html.haml diff --git a/app/views/layouts/_department_menu.html.haml b/app/views/layouts/_department_menu.html.haml index eda37ad..6a88222 100644 --- a/app/views/layouts/_department_menu.html.haml +++ b/app/views/layouts/_department_menu.html.haml @@ -1,12 +1,14 @@ .menu %ul - %li #{ link_to 'Преподаватели кафедры', dept_head_teaching_places_path } - %li #{ link_to 'Дисциплины', dept_head_disciplines_path } - %li #{ link_to 'Специальности', dept_head_specialities_path } - %li #{ link_to 'Аудитории', dept_head_classrooms_path } + %li #{ link_to 'Преподаватели кафедры', department_teaching_places_path(current_department.id) } + %li #{ link_to 'Дисциплины', department_disciplines_path(current_department.id) } + %li #{ link_to 'Специальности', department_specialities_path(current_department.id) } + %li #{ link_to 'Аудитории', department_classrooms_path(current_department.id) } %li #{ link_to 'Выход', destroy_user_session_path, :class => "red", :method => :delete } %li.semester= render 'change_semester' - + +.department= render 'change_department' + .info %p< Пожалуйста, ознакомьтесь с инструкцией по заполнению карт нагрузок преподавателей от 20 августа 2012 г. diff --git a/app/views/layouts/application.html.haml b/app/views/layouts/application.html.haml index 42bc651..9386166 100644 --- a/app/views/layouts/application.html.haml +++ b/app/views/layouts/application.html.haml @@ -7,13 +7,13 @@ = csrf_meta_tag %body - - if user_signed_in? and current_user.admin? + - if user_signed_in? and can? :manage, User = render 'layouts/admin_menu' - - if user_signed_in? and (current_user.supervisor? or current_user.admin?) + - if user_signed_in? and can? :manage, :all = render 'layouts/supervisor_menu' - - if user_signed_in? and (current_user.editor? or current_user.supervisor? or current_user.admin?) + - if user_signed_in? and can? :manage, Pair = render 'layouts/editor_menu' - - if user_signed_in? and current_user.department + - if user_signed_in? and can? :update, Department = render 'layouts/department_menu' - if notice diff --git a/config/routes.rb b/config/routes.rb index d97a402..5a3f6a8 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -58,7 +58,10 @@ ##### Раздел заведующего кафедрой ##### - namespace :dept_head do + get 'dept_head(/*parts)' => 'department/base#change_current_department' + get 'department' => 'department/base#change_current_department' + post 'department/change' => 'department/base#change_current_department' + namespace :department, :path => '/department/:department_id', :department_id => /\d+/ do resources :teaching_places do as_routes record_select_routes @@ -95,7 +98,7 @@ end end resources :classrooms do as_routes end - root :to => redirect('/dept_head/teaching_places') + root :to => redirect {|params| "/department/#{params[:department_id]}/teaching_places" } end ##### Раздел супервайзера ##### diff --git a/db/migrate/20130130014707_many_departments_for_user.rb b/db/migrate/20130130014707_many_departments_for_user.rb new file mode 100644 index 0000000..4bd99b3 --- /dev/null +++ b/db/migrate/20130130014707_many_departments_for_user.rb @@ -0,0 +1,42 @@ +class ManyDepartmentsForUser < ActiveRecord::Migration + + def up + create_table :departments_users, :id => false do |t| + t.integer :department_id + t.integer :user_id + end + # Move relation from one-to-many to many-to-many + say_with_time "Moving department ids in the new relation table" do + User.transaction do + User.find_in_batches do |users| + users.each do |user| + if Department.where(id: user.department_id).any? + user.department_ids = [user.department_id] + end + end + end + end + end + # Drop old column + remove_column :users, :department_id + end + + def down + add_column :users, :department_id, :integer + # Move relation back + say_with_time "Moving department ids back to the users table" do + User.transaction do + User.find_in_batches do |users| + users.each do |user| + if user.department_ids.any? + user.department_id = user.department_ids.first + user.save + end + end + end + end + end + drop_table :departments_users + end + +end diff --git a/db/schema.rb b/db/schema.rb index 14579ad..967e37b 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20130128041505) do +ActiveRecord::Schema.define(:version => 20130130014707) do create_table "buildings", :force => true do |t| t.string "name" @@ -58,6 +58,11 @@ t.integer "gosinsp_code" end + create_table "departments_users", :id => false, :force => true do |t| + t.integer "department_id" + t.integer "user_id" + end + create_table "disciplines", :force => true do |t| t.integer "department_id" t.string "short_name" @@ -196,7 +201,6 @@ t.datetime "last_sign_in_at" t.string "current_sign_in_ip" t.string "last_sign_in_ip" - t.integer "department_id" t.string "name" t.string "login" t.datetime "created_at" diff --git a/spec/features/dept_head_classrooms_spec.rb b/spec/features/department_classrooms_spec.rb similarity index 100% rename from spec/features/dept_head_classrooms_spec.rb rename to spec/features/department_classrooms_spec.rb diff --git a/test/functional/dept_head/base_controller_test.rb b/test/functional/department/base_controller_test.rb similarity index 100% rename from test/functional/dept_head/base_controller_test.rb rename to test/functional/department/base_controller_test.rb diff --git a/test/functional/dept_head/charge_cards_controller_test.rb b/test/functional/department/charge_cards_controller_test.rb similarity index 100% rename from test/functional/dept_head/charge_cards_controller_test.rb rename to test/functional/department/charge_cards_controller_test.rb diff --git a/test/functional/dept_head/disciplines_controller_test.rb b/test/functional/department/disciplines_controller_test.rb similarity index 100% rename from test/functional/dept_head/disciplines_controller_test.rb rename to test/functional/department/disciplines_controller_test.rb diff --git a/test/functional/dept_head/groups_controller_test.rb b/test/functional/department/groups_controller_test.rb similarity index 100% rename from test/functional/dept_head/groups_controller_test.rb rename to test/functional/department/groups_controller_test.rb diff --git a/test/functional/dept_head/lecturers_controller_test.rb b/test/functional/department/lecturers_controller_test.rb similarity index 100% rename from test/functional/dept_head/lecturers_controller_test.rb rename to test/functional/department/lecturers_controller_test.rb diff --git a/test/functional/dept_head/specialities_controller_test.rb b/test/functional/department/specialities_controller_test.rb similarity index 100% rename from test/functional/dept_head/specialities_controller_test.rb rename to test/functional/department/specialities_controller_test.rb diff --git a/test/functional/dept_head/teaching_places_controller_test.rb b/test/functional/department/teaching_places_controller_test.rb similarity index 100% rename from test/functional/dept_head/teaching_places_controller_test.rb rename to test/functional/department/teaching_places_controller_test.rb