From d2354664630fc181fae6c9c1c7bb020fff9ec865 Mon Sep 17 00:00:00 2001 From: Alinson Santos Date: Sun, 3 Aug 2008 10:55:44 -0300 Subject: [PATCH] Multiplos semestres --- app/controllers/attachments_controller.rb | 2 +- app/controllers/courses_controller.rb | 13 +++++--- app/controllers/events_controller.rb | 2 +- app/controllers/log_controller.rb | 2 +- app/controllers/news_controller.rb | 2 +- app/controllers/wiki_controller.rb | 2 +- app/models/course.rb | 8 +++-- app/models/user.rb | 2 +- app/views/courses/_form.html.haml | 6 +++- app/views/courses/index.html.haml | 36 +++++++++++++--------- app/views/widgets/menu_disciplina.html.erb | 11 +++++++ config/application.rb | 2 ++ config/initializers/nasty_hacks.rb | 16 ++++++++++ db/migrate/036_semestres.rb | 19 ++++++++++++ db/schema.rb | 8 +++-- 15 files changed, 100 insertions(+), 31 deletions(-) create mode 100644 db/migrate/036_semestres.rb diff --git a/app/controllers/attachments_controller.rb b/app/controllers/attachments_controller.rb index a5beb06..367620d 100644 --- a/app/controllers/attachments_controller.rb +++ b/app/controllers/attachments_controller.rb @@ -97,7 +97,7 @@ class AttachmentsController < ApplicationController protected def find_attachment - params[:course_id] = Course.find_by_short_name(params[:course_id]).id if !params[:course_id].is_numeric? and !Course.find_by_short_name(params[:course_id]).nil? + params[:course_id] = Course.find(:first, :conditions => ['short_name = ?', params[:course_id]], :order => 'period desc').id if !params[:course_id].is_numeric? and !Course.find_by_short_name(params[:course_id]).nil? @course = Course.find(params[:course_id]) @attachment = params[:id] ? @course.attachments.find(params[:id]) : Attachment.new end diff --git a/app/controllers/courses_controller.rb b/app/controllers/courses_controller.rb index bea1dd6..1af0e0f 100644 --- a/app/controllers/courses_controller.rb +++ b/app/controllers/courses_controller.rb @@ -22,9 +22,14 @@ class CoursesController < ApplicationController #after_filter :cache_sweep, :only => [ :create, :update, :destroy ] def index - @courses = Course.find(:all, - :order => 'period asc, full_name asc', - :conditions => (logged_in? and !@current_user.courses.empty? ? [ 'id not in (?)', @current_user.courses] : '')) + params[:period] = nil if params[:period] == App.current_period + @period = params[:period] || App.current_period + + conditions = [] + conditions.add_condition!(['period = ?', @period]) + conditions.add_condition!(['id not in (?)', @current_user.courses]) if logged_in? and !@current_user.courses.empty? + @courses = Course.find(:all, :order => 'grade asc, full_name asc', :conditions => conditions) + respond_to do |format| format.html @@ -98,7 +103,7 @@ class CoursesController < ApplicationController protected def find_course - params[:id] = Course.find_by_short_name(params[:id]).id if params[:id] and !params[:id].is_numeric? and !Course.find_by_short_name(params[:id]).nil? + params[:id] = Course.find(:first, :conditions => ['short_name = ?', params[:id]], :order => 'period desc').id if !params[:id].is_numeric? and !Course.find_by_short_name(params[:id]).nil? @course = params[:id] ? Course.find(params[:id]) : Course.new(params[:course]) end diff --git a/app/controllers/events_controller.rb b/app/controllers/events_controller.rb index 8af32c1..d9dbc1a 100644 --- a/app/controllers/events_controller.rb +++ b/app/controllers/events_controller.rb @@ -113,7 +113,7 @@ class EventsController < ApplicationController protected def find_event - params[:course_id] = Course.find_by_short_name(params[:course_id]).id if !params[:course_id].is_numeric? and !Course.find_by_short_name(params[:course_id]).nil? + params[:course_id] = Course.find(:first, :conditions => ['short_name = ?', params[:course_id]], :order => 'period desc').id if !params[:course_id].is_numeric? and !Course.find_by_short_name(params[:course_id]).nil? @course = Course.find(params[:course_id]) @event = params[:id] ? @course.events.find(params[:id]) : Event.new(params[:event]) end diff --git a/app/controllers/log_controller.rb b/app/controllers/log_controller.rb index 8317a72..b6dfd68 100644 --- a/app/controllers/log_controller.rb +++ b/app/controllers/log_controller.rb @@ -44,7 +44,7 @@ class LogController < ApplicationController protected def find_course unless params[:course_id].nil? - params[:course_id] = Course.find_by_short_name(params[:course_id]).id if !params[:course_id].is_numeric? and !Course.find_by_short_name(params[:course_id]).nil? + params[:course_id] = Course.find(:first, :conditions => ['short_name = ?', params[:course_id]], :order => 'period desc').id if !params[:course_id].is_numeric? and !Course.find_by_short_name(params[:course_id]).nil? @course = Course.find(params[:course_id]) end end diff --git a/app/controllers/news_controller.rb b/app/controllers/news_controller.rb index 71e3a98..e381aa0 100644 --- a/app/controllers/news_controller.rb +++ b/app/controllers/news_controller.rb @@ -105,7 +105,7 @@ class NewsController < ApplicationController protected def find_new - params[:course_id] = Course.find_by_short_name(params[:course_id]).id if !params[:course_id].is_numeric? and !Course.find_by_short_name(params[:course_id]).nil? + params[:course_id] = Course.find(:first, :conditions => ['short_name = ?', params[:course_id]], :order => 'period desc').id if !params[:course_id].is_numeric? and !Course.find_by_short_name(params[:course_id]).nil? @course = Course.find(params[:course_id]) @news = params[:id] ? @course.news.find(params[:id]) : News.new(params[:news]) diff --git a/app/controllers/wiki_controller.rb b/app/controllers/wiki_controller.rb index be2f605..88e8ab7 100644 --- a/app/controllers/wiki_controller.rb +++ b/app/controllers/wiki_controller.rb @@ -159,7 +159,7 @@ class WikiController < ApplicationController protected def find_wiki - params[:course_id] = Course.find_by_short_name(params[:course_id]).id if !params[:course_id].is_numeric? and !Course.find_by_short_name(params[:course_id]).nil? + params[:course_id] = Course.find(:first, :conditions => ['short_name = ?', params[:course_id]], :order => 'period desc').id if !params[:course_id].is_numeric? and !Course.find_by_short_name(params[:course_id]).nil? @course = Course.find(params[:course_id]) params[:id] = @course.wiki_pages.find_by_title(params[:id]).id if params[:id] and !params[:id].is_numeric? and !@course.wiki_pages.find_by_title(params[:id]).nil? diff --git a/app/models/course.rb b/app/models/course.rb index 06e1393..b2e68e3 100644 --- a/app/models/course.rb +++ b/app/models/course.rb @@ -43,8 +43,11 @@ class Course < ActiveRecord::Base # Validacao generate_validations - validates_uniqueness_of :short_name validates_format_of :short_name, :with => /^[^0-9]/ + + def related_courses + Course.find(:all, :conditions => [ 'short_name = ?', self.short_name], :limit => 4, :order => 'period desc') + end def after_create App.inital_wiki_pages.each do |page_title| @@ -54,6 +57,7 @@ class Course < ActiveRecord::Base end def to_param - self.short_name + return self.short_name if self.period == App.current_period + return self.id end end diff --git a/app/models/user.rb b/app/models/user.rb index 45b27b9..ea561d7 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -22,7 +22,7 @@ class User < ActiveRecord::Base acts_as_paranoid # Associacoes - has_and_belongs_to_many :courses, :order => 'full_name' + has_and_belongs_to_many :courses, :order => 'full_name', :conditions => "period = #{App.current_period}" # Validacao validates_length_of :login, :within => 3..40 diff --git a/app/views/courses/_form.html.haml b/app/views/courses/_form.html.haml index 783f445..3209d8a 100644 --- a/app/views/courses/_form.html.haml +++ b/app/views/courses/_form.html.haml @@ -14,7 +14,11 @@ %dd= text_field 'course', 'code' %dt - %label{:for => "course_period"} Semestre + %label{:for => "course_grade"} Semestre + %dd= text_field 'course', 'grade' + + %dt + %label{:for => "course_period"} Data %dd= text_field 'course', 'period' %dt diff --git a/app/views/courses/index.html.haml b/app/views/courses/index.html.haml index ba04bf3..f69a106 100644 --- a/app/views/courses/index.html.haml +++ b/app/views/courses/index.html.haml @@ -2,29 +2,35 @@ = action_icon('add', 'Cadastrar nova disciplina', new_course_url) if admin? %h4.title= App.title -%h1.title Disciplinas +%h1.title= "Disciplinas #{@period}" .box %ul - if logged_in? - %h3 Disciplinas Matriculadas - - if @current_user.courses.empty? - %li.no_itens Nenhuma disciplina matriculada - - for course in @current_user.courses - %li{highlight(course.id)} - .right - = action_icon('subtract', 'Desmatricular-se', unenroll_course_url(course)) - = link_to h(course.full_name), course_url(course) + - if params[:period].nil? + %h3 Disciplinas Matriculadas + - if @current_user.courses.empty? + %li.no_itens Nenhuma disciplina matriculada + - for course in @current_user.courses + %li{highlight(course.id)} + .right + = action_icon('subtract', 'Desmatricular-se', unenroll_course_url(course)) + = link_to h(course.full_name), course_url(course) -# cache(courses_path) do - - old_period = 0 + - old_grade = 0 - for course in @courses - - if course.period != old_period - %h3= (course.period == 99 ? "Optativas" : "Semestre #{course.period}") - - old_period = course.period + - if course.grade != old_grade + %h3= (course.grade == 99 ? "Optativas" : "Semestre #{course.grade}") + - old_grade = course.grade %li{highlight(course.id)} - .right - = action_icon('add', 'Matricular-se', enroll_course_url(course)) + - if course.period == App.current_period + .right + = action_icon('add', 'Matricular-se', enroll_course_url(course)) = link_to h(course.full_name), course_url(course) + %h3 + Outros Semestres + %li= link_to "2008.2", :period => '2008.2' + %li= link_to "2008.1", :period => '2008.1' diff --git a/app/views/widgets/menu_disciplina.html.erb b/app/views/widgets/menu_disciplina.html.erb index b424fb1..2d34c5d 100644 --- a/app/views/widgets/menu_disciplina.html.erb +++ b/app/views/widgets/menu_disciplina.html.erb @@ -8,3 +8,14 @@
  • <%= link_to "Mudanças recentes", course_log_url(@course) %>
  • + + <% if @course.related_courses.length > 1 %> + + <% end %> diff --git a/config/application.rb b/config/application.rb index 3fc234b..255d299 100644 --- a/config/application.rb +++ b/config/application.rb @@ -6,6 +6,8 @@ App.webmaster_email = "webmaster@wikiufc.gelsol.org" App.default_host = "wikiufc.gelsol.org" App.base_path = "" +App.current_period = "2008.1" + # Limites App.max_upload_file_size = 5.megabytes diff --git a/config/initializers/nasty_hacks.rb b/config/initializers/nasty_hacks.rb index b02a000..aefff15 100644 --- a/config/initializers/nasty_hacks.rb +++ b/config/initializers/nasty_hacks.rb @@ -8,3 +8,19 @@ class String Float self rescue false end end + +class Array + def add_condition! (condition, conjunction = 'AND') + if String === condition + add_condition!([condition]) + elsif Hash === condition + add_condition!([condition.keys.map { |attr| "#{attr}=?" }.join(' AND ')] + condition.values) + elsif Array === condition + self[0] = "(#{self[0]}) #{conjunction} (#{condition.shift})" unless empty? + (self << condition).flatten! + else + raise "don't know how to handle this condition type" + end + self + end +end diff --git a/db/migrate/036_semestres.rb b/db/migrate/036_semestres.rb new file mode 100644 index 0000000..8d9b6a2 --- /dev/null +++ b/db/migrate/036_semestres.rb @@ -0,0 +1,19 @@ +class Semestres < ActiveRecord::Migration + def self.up + rename_column :courses, :period, :grade + add_column :courses, :period, :string + Course.find(:all).each do |c| + c.update_attribute(:period, "2008.1") + end + + remove_index :courses, :short_name + add_index :courses, :short_name, :unique => false + end + + def self.down + remove_column :courses, :period + rename_column :courses, :grade, :period + remove_index :courses, :short_name + add_index :courses, :short_name, :unique => true + end +end diff --git a/db/schema.rb b/db/schema.rb index c57d461..404a5f2 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -9,7 +9,7 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 35) do +ActiveRecord::Schema.define(:version => 36) do create_table "attachments", :force => true do |t| t.string "file_name", :null => false @@ -19,6 +19,7 @@ ActiveRecord::Schema.define(:version => 35) do t.integer "size" t.integer "course_id" t.datetime "deleted_at" + t.string "path" end create_table "courses", :force => true do |t| @@ -26,11 +27,12 @@ ActiveRecord::Schema.define(:version => 35) do t.string "full_name", :null => false t.text "description" t.string "code", :default => "CK000", :null => false - t.integer "period", :default => 1, :null => false t.datetime "deleted_at" + t.integer "grade", :default => 1 + t.string "period" end - add_index "courses", ["short_name"], :name => "index_courses_on_short_name", :unique => true + add_index "courses", ["short_name"], :name => "index_courses_on_short_name" create_table "courses_users", :id => false, :force => true do |t| t.integer "user_id"