Skip to content

Commit

Permalink
adicionando registrations (matricula)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasmcastro committed Apr 14, 2012
1 parent 60395b9 commit 033db9d
Show file tree
Hide file tree
Showing 21 changed files with 322 additions and 45 deletions.
2 changes: 2 additions & 0 deletions Gemfile.lock
Expand Up @@ -53,6 +53,7 @@ GEM
treetop (~> 1.4.8)
mime-types (1.18)
multi_json (1.2.0)
mysql2 (0.3.11)
mysql2 (0.3.11-x86-mingw32)
polyglot (0.3.3)
rack (1.4.1)
Expand Down Expand Up @@ -100,6 +101,7 @@ GEM
multi_json (>= 1.0.2)

PLATFORMS
ruby
x86-mingw32

DEPENDENCIES
Expand Down
3 changes: 3 additions & 0 deletions app/assets/javascripts/registrations.js.coffee
@@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/
3 changes: 3 additions & 0 deletions app/assets/stylesheets/registrations.css.scss
@@ -0,0 +1,3 @@
// Place all the styles related to the Registrations controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
83 changes: 83 additions & 0 deletions app/controllers/registrations_controller.rb
@@ -0,0 +1,83 @@
class RegistrationsController < ApplicationController
# GET /registrations
# GET /registrations.json
def index
@registrations = Registration.all

respond_to do |format|
format.html # index.html.erb
format.json { render :json => @registrations }
end
end

# GET /registrations/1
# GET /registrations/1.json
def show
@registration = Registration.find(params[:id])

respond_to do |format|
format.html # show.html.erb
format.json { render :json => @registration }
end
end

# GET /registrations/new
# GET /registrations/new.json
def new
@registration = Registration.new

respond_to do |format|
format.html # new.html.erb
format.json { render :json => @registration }
end
end

# GET /registrations/1/edit
def edit
@registration = Registration.find(params[:id])
end

# POST /registrations
# POST /registrations.json
def create
@registration = Registration.new(params[:registration])

respond_to do |format|
if @registration.save
format.html { redirect_to @registration, :notice => 'Registration was successfully created.' }
format.json { render :json => @registration, :status => :created, :location => @registration }
else
format.html { render :action => "new" }
format.json { render :json => @registration.errors, :status => :unprocessable_entity }
end
end
end

# PUT /registrations/1
# PUT /registrations/1.json
def update
@registration = Registration.find(params[:id])

respond_to do |format|
if @registration.update_attributes(params[:registration])
format.html { redirect_to @registration, :notice => 'Registration was successfully updated.' }
format.json { head :no_content }
else
format.html { render :action => "edit" }
format.json { render :json => @registration.errors, :status => :unprocessable_entity }
end
end
end

# DELETE /registrations/1
# DELETE /registrations/1.json
def destroy
@registration = Registration.find(params[:id])
@registration.destroy

respond_to do |format|
format.html { redirect_to registrations_url }
format.json { head :no_content }
end
end
end
2 changes: 2 additions & 0 deletions app/helpers/registrations_helper.rb
@@ -0,0 +1,2 @@
module RegistrationsHelper
end
5 changes: 5 additions & 0 deletions app/models/course.rb
@@ -1,2 +1,7 @@
class Course < ActiveRecord::Base
has_many :registrations

def to_s
"#{self.name} (#{self.duration} horas)"
end
end
4 changes: 4 additions & 0 deletions app/models/registration.rb
@@ -0,0 +1,4 @@
class Registration < ActiveRecord::Base
belongs_to :registrable, :polymorphic => true
belongs_to :course
end
5 changes: 5 additions & 0 deletions app/models/student.rb
@@ -1,2 +1,7 @@
class Student < ActiveRecord::Base
has_many :registrations, :as => :registrable

def to_s
"#{self.name} <#{self.email}>"
end
end
1 change: 1 addition & 0 deletions app/models/teacher.rb
@@ -1,2 +1,3 @@
class Teacher < ActiveRecord::Base
has_many :registrations, :as => :registrable
end
25 changes: 25 additions & 0 deletions app/views/registrations/_form.html.erb
@@ -0,0 +1,25 @@
<%= form_for(@registration) do |f| %>
<% if @registration.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@registration.errors.count, "error") %> prohibited this registration from being saved:</h2>

<ul>
<% @registration.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>

<div class="field">
<%= f.label :registrable %><br />
<%= f.text_field :registrable %>
</div>
<div class="field">
<%= f.label :course %><br />
<%= f.text_field :course %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
6 changes: 6 additions & 0 deletions app/views/registrations/edit.html.erb
@@ -0,0 +1,6 @@
<h1>Editing registration</h1>

<%= render 'form' %>
<%= link_to 'Show', @registration %> |
<%= link_to 'Back', registrations_path %>
25 changes: 25 additions & 0 deletions app/views/registrations/index.html.erb
@@ -0,0 +1,25 @@
<h1>Listing registrations</h1>

<table>
<tr>
<th>Registrable</th>
<th>Course</th>
<th></th>
<th></th>
<th></th>
</tr>

<% @registrations.each do |registration| %>
<tr>
<td><%= registration.registrable %></td>
<td><%= registration.course %></td>
<td><%= link_to 'Show', registration %></td>
<td><%= link_to 'Edit', edit_registration_path(registration) %></td>
<td><%= link_to 'Destroy', registration, :confirm => 'Are you sure?', :method => :delete %></td>
</tr>
<% end %>
</table>

<br />

<%= link_to 'New Registration', new_registration_path %>
5 changes: 5 additions & 0 deletions app/views/registrations/new.html.erb
@@ -0,0 +1,5 @@
<h1>New registration</h1>

<%= render 'form' %>
<%= link_to 'Back', registrations_path %>
15 changes: 15 additions & 0 deletions app/views/registrations/show.html.erb
@@ -0,0 +1,15 @@
<p id="notice"><%= notice %></p>

<p>
<b>Registrable:</b>
<%= @registration.registrable %>
</p>

<p>
<b>Course:</b>
<%= @registration.course %>
</p>


<%= link_to 'Edit', edit_registration_path(@registration) %> |
<%= link_to 'Back', registrations_path %>
2 changes: 2 additions & 0 deletions config/routes.rb
@@ -1,4 +1,6 @@
Academico::Application.routes.draw do
resources :registrations

resources :courses

resources :teachers
Expand Down
12 changes: 12 additions & 0 deletions db/migrate/20120414142709_create_registrations.rb
@@ -0,0 +1,12 @@
class CreateRegistrations < ActiveRecord::Migration
def change
create_table :registrations do |t|
t.references :registrable, :polymorphic => true
t.references :course

t.timestamps
end
add_index :registrations, :registrable_id
add_index :registrations, :course_id
end
end
100 changes: 55 additions & 45 deletions db/schema.rb
@@ -1,45 +1,55 @@
# encoding: UTF-8
# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
#
# Note that this schema.rb definition is the authoritative source for your
# database schema. If you need to create the application database on another
# system, you should be using db:schema:load, not running all the migrations
# from scratch. The latter is a flawed and unsustainable approach (the more migrations
# you'll amass, the slower it'll run and the greater likelihood for issues).
#
# It's strongly recommended to check this file into your version control system.

ActiveRecord::Schema.define(:version => 20120331195615) do

create_table "courses", :force => true do |t|
t.string "name"
t.integer "duration"
t.text "description"
t.text "objectives"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end

create_table "students", :force => true do |t|
t.string "name"
t.string "email"
t.date "birth_date"
t.string "phone"
t.string "sex"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end

create_table "teachers", :force => true do |t|
t.string "name"
t.string "email"
t.string "phone"
t.date "birth_date"
t.string "sex"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end

end
# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
#
# Note that this schema.rb definition is the authoritative source for your
# database schema. If you need to create the application database on another
# system, you should be using db:schema:load, not running all the migrations
# from scratch. The latter is a flawed and unsustainable approach (the more migrations
# you'll amass, the slower it'll run and the greater likelihood for issues).
#
# It's strongly recommended to check this file into your version control system.

ActiveRecord::Schema.define(:version => 20120414142709) do

create_table "courses", :force => true do |t|
t.string "name"
t.integer "duration"
t.text "description"
t.text "objectives"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end

create_table "registrations", :force => true do |t|
t.integer "registrable_id"
t.string "registrable_type"
t.integer "course_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end

add_index "registrations", ["course_id"], :name => "index_registrations_on_course_id"
add_index "registrations", ["registrable_id"], :name => "index_registrations_on_registrable_id"

create_table "students", :force => true do |t|
t.string "name"
t.string "email"
t.date "birth_date"
t.string "phone"
t.string "sex"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end

create_table "teachers", :force => true do |t|
t.string "name"
t.string "email"
t.string "phone"
t.date "birth_date"
t.string "sex"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end

end
9 changes: 9 additions & 0 deletions test/fixtures/registrations.yml
@@ -0,0 +1,9 @@
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/Fixtures.html

one:
registrable:
course:

two:
registrable:
course:
49 changes: 49 additions & 0 deletions test/functional/registrations_controller_test.rb
@@ -0,0 +1,49 @@
require 'test_helper'

class RegistrationsControllerTest < ActionController::TestCase
setup do
@registration = registrations(:one)
end

test "should get index" do
get :index
assert_response :success
assert_not_nil assigns(:registrations)
end

test "should get new" do
get :new
assert_response :success
end

test "should create registration" do
assert_difference('Registration.count') do
post :create, :registration => @registration.attributes
end

assert_redirected_to registration_path(assigns(:registration))
end

test "should show registration" do
get :show, :id => @registration
assert_response :success
end

test "should get edit" do
get :edit, :id => @registration
assert_response :success
end

test "should update registration" do
put :update, :id => @registration, :registration => @registration.attributes
assert_redirected_to registration_path(assigns(:registration))
end

test "should destroy registration" do
assert_difference('Registration.count', -1) do
delete :destroy, :id => @registration
end

assert_redirected_to registrations_path
end
end

0 comments on commit 033db9d

Please sign in to comment.