Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Editing/updating one timetable row for school class. Added foreign ke…

…y for school class form timetable. Tests edited.
  • Loading branch information...
commit fe27710dbfa01ed269d95af2fd900bf554813765 1 parent 0d73b44
@Loremaster authored
View
34 app/controllers/timetables_controller.rb
@@ -1,6 +1,7 @@
# encoding: UTF-8
class TimetablesController < ApplicationController
- before_filter :authenticate_school_heads, :only => [ :index, :new, :create ]
+ before_filter :authenticate_school_heads, :only => [ :index, :new, :create, :edit,
+ :update ]
def index
@classes = SchoolClass.order( :class_code )
@@ -8,12 +9,12 @@ def index
if params.has_key?( :class_code )
school_class = SchoolClass.where( "class_code = ?", params[:class_code] ).first
- @tts = timetable_for_class( school_class ) # Here we get timetable for class only if subject has been choosed.
- @tt_monday = sorted_timetable_for_day( @tts, "Mon" ) # Timetable for monday.
- @tt_tuesday = sorted_timetable_for_day( @tts, "Tue" ) # Timetable for tuesday.
- @tt_wednesday = sorted_timetable_for_day( @tts, "Wed" ) # Timetable for wednesday.
- @tt_thursday = sorted_timetable_for_day( @tts, "Thu") # Timetable for thursday.
- @tt_friday = sorted_timetable_for_day( @tts, "Fri") # Timetable for friday.
+ tts = timetable_for_class( school_class ) # Here we get timetable for class only if subject has been choosed.
+ @tt_monday = sorted_timetable_for_day( tts, "Mon" ) # Timetable for monday.
+ @tt_tuesday = sorted_timetable_for_day( tts, "Tue" ) # Timetable for tuesday.
+ @tt_wednesday = sorted_timetable_for_day( tts, "Wed" ) # Timetable for wednesday.
+ @tt_thursday = sorted_timetable_for_day( tts, "Thu") # Timetable for thursday.
+ @tt_friday = sorted_timetable_for_day( tts, "Fri") # Timetable for friday.
end
end
@@ -51,8 +52,25 @@ def create
end
end
- private
+ def edit
+ @tt = Timetable.find( params[:id] )
+ @subjects_with_curriculums = collect_subjects_with_curriculums( @tt.school_class )
+ end
+
+ def update
+ @tt = Timetable.find( params[:id] )
+ if @tt.update_attributes( params[:timetable] )
+ flash[:success] = "Расписание успешно обновлено!"
+ redirect_to timetables_path
+ else
+ flash.now[:error] = @tt.errors.full_messages.to_sentence :last_word_connector => ", ",
+ :two_words_connector => ", "
+ render 'edit'
+ end
+ end
+
+ private
# Collecting subjects names for school class and curriculum_id for each subject.
def collect_subjects_with_curriculums( school_class )
subjects = school_class.curriculums.collect do |c|
View
1  app/models/school_class.rb
@@ -26,6 +26,7 @@ class SchoolClass < ActiveRecord::Base
has_many :qualifications, :through => :curriculums
has_many :orders
has_many :meetings
+ has_many :timetables
validates :class_code,
:presence => { :message => "не может быть пустым" },
View
4 app/models/timetable.rb
@@ -6,6 +6,7 @@
#
# id :integer not null, primary key
# curriculum_id :integer
+# school_class_id :integer
# tt_day_of_week :string(255)
# tt_number_of_lesson :integer
# tt_room :string(255)
@@ -16,6 +17,9 @@
class Timetable < ActiveRecord::Base
belongs_to :curriculum
+ belongs_to :school_class
+
+ validates :school_class_id, :presence => { :message => "должен быть указан" }
validates :tt_day_of_week,
:presence => true,
View
4 app/views/timetables/_tbody_day.html.erb
@@ -1,6 +1,8 @@
<tbody>
<% timetables_for_day.each do |t| %>
- <tr>
+ <tr class="cross" onclick = "location.href='<%= edit_timetable_path( :id => t.id,
+ :class_code => t.curriculum.school_class.class_code ) %>'">
+
<td><%= t.tt_number_of_lesson %></td>
<td><%= t.curriculum.qualification.subject.subject_name %></td>
<td><%= t.tt_room %></td>
View
3  app/views/timetables/_timetable_fields.html.erb
@@ -1,5 +1,6 @@
<%= f.hidden_field "[#{i}][tt_number_of_lesson]", :value => number_of_lesson %>
<%= f.hidden_field "[#{i}][tt_day_of_week]", :value => day_of_week %>
+<%= f.hidden_field "[#{i}][school_class_id]", :value => school_class_id %>
<td><%= number_of_lesson %></td>
<td>
@@ -8,7 +9,7 @@
{ :include_blank => true }) %>
</td>
<td>
- <%= text_field_tag "timetable[#{i}][tt_room]",'', :class => "span1", :maxlength => 3 %>
+ <%= text_field_tag "timetable[#{i}][tt_room]", '', :class => "span1", :maxlength => 3 %>
</td>
<td>
<%= select_tag( "timetable[#{i}][tt_type]",
View
63 app/views/timetables/edit.html.erb
@@ -0,0 +1,63 @@
+<legend>Редактирование <%= @tt.school_class.class_code %> класса</legend>
+
+<%= form_for @tt, :html => {:class => "form-horizontal"} do |f| %>
+ <%= field_set_tag do %>
+
+ <%= f.hidden_field :tt_number_of_lesson %>
+ <%= f.hidden_field :tt_day_of_week %>
+ <%= f.hidden_field :school_class_id %>
+
+ <div class="control-group">
+ <%= f.label :tt_day_of_week, "День недели", :class => "control-label" %>
+
+ <div class="controls">
+ <%= content_tag( :span, @tt.tt_day_of_week,
+ :class =>"input-xlarge uneditable-input span1" ) %>
+ </div>
+ </div>
+
+ <div class="control-group">
+ <%= f.label :tt_number_of_lesson, "Номер урока", :class => "control-label" %>
+
+ <div class="controls">
+ <%= content_tag( :span, @tt.tt_number_of_lesson,
+ :class =>"input-xlarge uneditable-input span1" ) %>
+ </div>
+ </div>
+
+ <hr/>
+
+ <div class="control-group">
+ <%= f.label :curriculum_id, "Тип предмета", :class => "control-label" %>
+
+ <div class="controls">
+ <%= f.select( :curriculum_id, # Here we use 'select' instead of 'select_tag' because 'select' allows us to give builder ('tl.select' here).
+ options_for_select( @subjects_with_curriculums,
+ @tt.curriculum_id ),
+ { :include_blank => true }) %>
+ </div>
+ </div>
+
+ <div class="control-group">
+ <%= f.label :tt_room, "Номер кабинета", :class => "control-label" %>
+
+ <div class="controls">
+ <%= f.text_field :tt_room, :class => "span2", :maxlength => 3 %>
+ </div>
+ </div>
+
+ <div class="control-group">
+ <%= f.label :tt_type, "Тип занятия", :class => "control-label" %>
+
+ <div class="controls">
+ <%= f.select( :tt_type, # Here we use 'select' instead of 'select_tag' because 'select' allows us to give builder ('tl.select' here).
+ options_for_select( [ ["Primary lesson", "Primary lesson"],
+ ["Extra", "Extra"] ],
+ @tt.tt_type ),
+ { :include_blank => true }) %>
+ </div>
+ </div>
+
+ <%= f.submit "Обновить", :class => "btn btn-large btn-warning" %>
+ <% end %>
+<% end %>
View
6 app/views/timetables/index.html.erb
@@ -1,5 +1,7 @@
<legend>Расписание</legend>
+<%# Button to show timetable for class. %>
+
<div style="margin-top: 18px;" class="btn-toolbar">
<div class="btn-group">
<a href="#" data-toggle="dropdown" class="btn dropdown-toggle">
@@ -17,6 +19,8 @@
</div>
</div>
+<%# Timetable for class. %>
+
<% if params.has_key?( :class_code ) %>
<table class="table table-bordered table-striped" name="timetable_for_school_class">
<%= render :partial => 'theads', :locals => { :day => 'Понедельник' } %>
@@ -36,6 +40,8 @@
</table>
<% end %>
+<%# Button to create timetable for class. %>
+
<div style="margin-top: 18px;" class="btn-toolbar">
<div class="btn-group">
<a href="#" data-toggle="dropdown" class="btn btn-primary btn-large dropdown-toggle">
View
10 app/views/timetables/new.html.erb
@@ -10,7 +10,7 @@
<tr>
<%= render :partial => 'timetable_fields',
:locals => { :f => f, :i => i, :day_of_week => 'Mon',
- :number_of_lesson => i } %> <%# number_of_lesson should be > 0 and < 10 %>
+ :number_of_lesson => i, :school_class_id => @class.id } %> <%# number_of_lesson should be > 0 and < 10 %>
</tr>
<% end %>
</tbody>
@@ -22,7 +22,7 @@
<tr>
<%= render :partial => 'timetable_fields',
:locals => { :f => f, :i => i, :day_of_week => 'Tue',
- :number_of_lesson => i - 8 } %>
+ :number_of_lesson => i - 8, :school_class_id => @class.id } %>
</tr>
<% end %>
</tbody>
@@ -34,7 +34,7 @@
<tr>
<%= render :partial => 'timetable_fields',
:locals => { :f => f, :i => i, :day_of_week => 'Wed',
- :number_of_lesson => i - 8*2 } %>
+ :number_of_lesson => i - 8*2, :school_class_id => @class.id } %>
</tr>
<% end %>
</tbody>
@@ -46,7 +46,7 @@
<tr>
<%= render :partial => 'timetable_fields',
:locals => { :f => f, :i => i, :day_of_week => 'Thu',
- :number_of_lesson => i - 8*3 } %>
+ :number_of_lesson => i - 8*3, :school_class_id => @class.id } %>
</tr>
<% end %>
</tbody>
@@ -58,7 +58,7 @@
<tr>
<%= render :partial => 'timetable_fields',
:locals => { :f => f, :i => i, :day_of_week => 'Fri',
- :number_of_lesson => i - 8*4 } %>
+ :number_of_lesson => i - 8*4, :school_class_id => @class.id } %>
</tr>
<% end %>
</tbody>
View
3  config/locales/en.yml
@@ -60,4 +60,5 @@ en:
tt_day_of_week: "День недели"
tt_number_of_lesson: "Номер урока"
tt_room: "Кабинет"
- tt_type: "Тип предмета"
+ tt_type: "Тип предмета"
+ school_class_id: "Идентификатор класса"
View
2  config/routes.rb
@@ -10,7 +10,7 @@
resources :orders, :only => [ :index, :new, :create, :edit, :update ]
resources :parents, :only => [ :index, :new, :create, :edit, :update ]
resources :meetings, :only => [ :index, :new, :create, :edit, :update ]
- resources :timetables, :only => [ :index, :new, :create ]
+ resources :timetables, :only => [ :index, :new, :create, :edit, :update ]
get "sessions/new"
View
1  db/migrate/20120502155751_create_timetables.rb
@@ -2,6 +2,7 @@ class CreateTimetables < ActiveRecord::Migration
def change
create_table :timetables do |t|
t.integer :curriculum_id
+ t.integer :school_class_id
t.string :tt_day_of_week
t.integer :tt_number_of_lesson
t.string :tt_room
View
1  db/schema.rb
@@ -147,6 +147,7 @@
create_table "timetables", :force => true do |t|
t.integer "curriculum_id"
+ t.integer "school_class_id"
t.string "tt_day_of_week"
t.integer "tt_number_of_lesson"
t.string "tt_room"
View
13 spec/controllers/timetables_controller_spec.rb
@@ -12,9 +12,18 @@
@sh = FactoryGirl.create( :user )
@sh.user_role = "school_head"
@sh.save!
-
- @timetable = FactoryGirl.create( :timetable )
+
@school_class = FactoryGirl.create( :school_class )
+ @curriculum = FactoryGirl.create( :curriculum )
+ @attr_timetable = {
+ :school_class_id => @school_class.id,
+ :curriculum_id => @curriculum.id,
+ :tt_day_of_week => "Mon",
+ :tt_number_of_lesson => 1,
+ :tt_room => '123',
+ :tt_type => 'Primary lesson'
+ }
+ @timetable = Timetable.create( @attr_timetable )
end
describe "GET 'index'" do
View
15 spec/factories.rb
@@ -109,13 +109,14 @@
c.school_class
end
- factory :timetable do |t|
- t.curriculum
- t.tt_day_of_week 'Mon'
- t.tt_number_of_lesson '1'
- t.tt_room '123'
- t.tt_type 'Primary lesson'
- end
+ # factory :timetable do |t|
+ # t.curriculum
+ # t.school_class
+ # t.tt_day_of_week 'Mon'
+ # t.tt_number_of_lesson '1'
+ # t.tt_room '123'
+ # t.tt_type 'Primary lesson'
+ # end
end
View
4 spec/models/timetable_spec.rb
@@ -6,6 +6,7 @@
#
# id :integer not null, primary key
# curriculum_id :integer
+# school_class_id :integer
# tt_day_of_week :string(255)
# tt_number_of_lesson :integer
# tt_room :string(255)
@@ -19,8 +20,10 @@
describe Timetable do
before(:each) do
@curriculum = FactoryGirl.create( :curriculum )
+ @school_class = FactoryGirl.create( :school_class )
@attr_timetable = {
+ :school_class_id => @school_class.id,
:curriculum_id => @curriculum.id,
:tt_day_of_week => "Mon",
:tt_number_of_lesson => 1,
@@ -29,6 +32,7 @@
}
@attr_invalid_timetable = {
+ :school_class_id => nil,
:curriculum_id => @curriculum.id,
:tt_day_of_week => "",
:tt_number_of_lesson => 0,

0 comments on commit fe27710

Please sign in to comment.
Something went wrong with that request. Please try again.