Skip to content

Commit

Permalink
agrego modelo para las cadenas
Browse files Browse the repository at this point in the history
básicamente, representan un conjunto de relaciones entre productos y
actividades. Eventualmente, se pueden modelar como subgrafos del total de las
relaciones.
  • Loading branch information
mauriciopasquier committed Nov 2, 2012
1 parent f731324 commit 55d3c42
Show file tree
Hide file tree
Showing 17 changed files with 275 additions and 2 deletions.
83 changes: 83 additions & 0 deletions app/controllers/cadenas_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
class CadenasController < ApplicationController
# GET /cadenas
# GET /cadenas.json
def index
@cadenas = Cadena.all

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

# GET /cadenas/1
# GET /cadenas/1.json
def show
@cadena = Cadena.find(params[:id])

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

# GET /cadenas/new
# GET /cadenas/new.json
def new
@cadena = Cadena.new

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

# GET /cadenas/1/edit
def edit
@cadena = Cadena.find(params[:id])
end

# POST /cadenas
# POST /cadenas.json
def create
@cadena = Cadena.new(params[:cadena])

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

# PUT /cadenas/1
# PUT /cadenas/1.json
def update
@cadena = Cadena.find(params[:id])

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

# DELETE /cadenas/1
# DELETE /cadenas/1.json
def destroy
@cadena = Cadena.find(params[:id])
@cadena.destroy

respond_to do |format|
format.html { redirect_to cadenas_url }
format.json { head :no_content }
end
end
end
5 changes: 5 additions & 0 deletions app/models/cadena.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class Cadena < ActiveRecord::Base
attr_accessible :nombre

has_and_belongs_to_many :relaciones
end
2 changes: 2 additions & 0 deletions app/models/relacion.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ class Relacion < ActiveRecord::Base

belongs_to :entrada, polymorphic: true
belongs_to :salida, polymorphic: true

has_and_belongs_to_many :cadenas
end
13 changes: 13 additions & 0 deletions app/views/cadenas/_form.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
= form_for @cadena do |f|
- if @cadena.errors.any?
#error_explanation
%h2= "#{pluralize(@cadena.errors.count, "error")} prohibited this cadena from being saved:"
%ul
- @cadena.errors.full_messages.each do |msg|
%li= msg

.field
= f.label :nombre
= f.text_field :nombre
.actions
= f.submit 'Save'
7 changes: 7 additions & 0 deletions app/views/cadenas/edit.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
%h1 Editing cadena

= render 'form'

= link_to 'Show', @cadena
\|
= link_to 'Back', cadenas_path
19 changes: 19 additions & 0 deletions app/views/cadenas/index.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
%h1 Listing cadenas

%table
%tr
%th Nombre
%th
%th
%th

- @cadenas.each do |cadena|
%tr
%td= cadena.nombre
%td= link_to 'Show', cadena
%td= link_to 'Edit', edit_cadena_path(cadena)
%td= link_to 'Destroy', cadena, method: :delete, data: { confirm: 'Are you sure?' }

%br

= link_to 'New Cadena', new_cadena_path
5 changes: 5 additions & 0 deletions app/views/cadenas/new.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
%h1 New cadena

= render 'form'

= link_to 'Back', cadenas_path
9 changes: 9 additions & 0 deletions app/views/cadenas/show.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
%p#notice= notice

%p
%b Nombre:
= @cadena.nombre

= link_to 'Edit', edit_cadena_path(@cadena)
\|
= link_to 'Back', cadenas_path
4 changes: 4 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,8 @@

resources :productos, path_names: m

resources :cadenas, path_names: f do
resources :relaciones, path_names: f
end

end
13 changes: 13 additions & 0 deletions db/migrate/20121101025332_create_cadenas.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class CreateCadenas < ActiveRecord::Migration
def change
create_table :cadenas do |t|
t.string :nombre
t.timestamps
end

create_table :cadenas_relaciones, id: false do |t|
t.references :cadena
t.references :relacion
end
end
end
13 changes: 12 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.

ActiveRecord::Schema.define(:version => 20121031213137) do
ActiveRecord::Schema.define(:version => 20121101025332) do

create_table "actividades", :force => true do |t|
t.string "nombre"
Expand All @@ -21,6 +21,17 @@
t.datetime "updated_at", :null => false
end

create_table "cadenas", :force => true do |t|
t.string "nombre"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end

create_table "cadenas_relaciones", :id => false, :force => true do |t|
t.integer "cadena_id"
t.integer "relacion_id"
end

create_table "ciius", :force => true do |t|
t.integer "codigo"
t.string "descripcion"
Expand Down
6 changes: 6 additions & 0 deletions test/factories/cadenas.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# encoding: utf-8
FactoryGirl.define do
factory :cadena do
nombre { generate :cadena_unica }
end
end
8 changes: 8 additions & 0 deletions test/factories/hs12s.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# encoding: utf-8
FactoryGirl.define do
factory :hs12 do
codigo { generate :cadena_unica }
descripcion "sarasa"
comentario "sarasa"
end
end
2 changes: 1 addition & 1 deletion test/factories/productos.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
nombre { generate :cadena_unica }
descripcion "sarasa"

#hs12
hs12

end
end
17 changes: 17 additions & 0 deletions test/factories/relaciones.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# encoding: utf-8
FactoryGirl.define do
factory :relacion do
# por default producto -> actividad
association :entrada, factory: :producto
association :salida, factory: :actividad

factory :relacion_actividad_producto do
association :entrada, factory: :actividad
association :salida, factory: :producto
end

factory :relacion_producto_actividad do
# usa el default pero es más legible en los tests
end
end
end
50 changes: 50 additions & 0 deletions test/functional/cadenas_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# encoding: utf-8
require './test/test_helper'

class CadenasControllerTest < ActionController::TestCase

test "debería acceder a la lista de cadenas" do
get :index
assert_response :success
assert_not_nil assigns(:cadenas)
end

test "debería ir a 'nuevo'" do
get :new
assert_response :success
end

test "debería crear una cadena" do
assert_difference('Cadena.count') do
post :create, cadena: attributes_for(:cadena)
end

assert_redirected_to cadena_path(assigns(:cadena))
end

test "debería mostrar una cadena" do
get :show, id: create(:cadena).to_param
assert_response :success
end

test "debería ir a editar" do
get :edit, id: create(:cadena).to_param
assert_response :success
end

test "debería actualizar una cadena" do
put :update, id: create(:cadena).to_param, cadena: attributes_for(:cadena)

assert_redirected_to cadena_path(assigns(:cadena))
end

test "debería eliminar una cadena" do
cadena = create(:cadena)
assert_difference('Cadena.count', -1) do
delete :destroy, id: cadena.to_param
end

assert_redirected_to cadenas_path
end

end
21 changes: 21 additions & 0 deletions test/unit/relacion_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# encoding: utf-8
require './test/test_helper'

class RelacionTest < ActiveSupport::TestCase

test 'la fábrica debería devolver asociaciones correctas' do
producto_actividad = create(:relacion_producto_actividad)
actividad_producto = create(:relacion_actividad_producto)

assert_not_nil producto_actividad.entrada, "No le asignó un producto"
assert_not_nil producto_actividad.salida, "No le asignó una actividad"
assert_instance_of Producto, producto_actividad.entrada
assert_instance_of Actividad, producto_actividad.salida

assert_not_nil actividad_producto.entrada, "No le asignó una actividad"
assert_not_nil actividad_producto.salida, "No le asignó un producto"
assert_instance_of Actividad, actividad_producto.entrada
assert_instance_of Producto, actividad_producto.salida
end

end

0 comments on commit 55d3c42

Please sign in to comment.