Skip to content

Commit

Permalink
Getting the subjects from an external xml
Browse files Browse the repository at this point in the history
  • Loading branch information
alejopelaez committed Nov 29, 2010
1 parent 0c3665a commit d52a179
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 3 deletions.
27 changes: 27 additions & 0 deletions app/controllers/materias_controller.rb
Original file line number Original file line Diff line number Diff line change
@@ -1,3 +1,6 @@
require 'rexml/document'
include REXML

class MateriasController < BaseController class MateriasController < BaseController
def index def index
end end
Expand All @@ -13,4 +16,28 @@ def show
def load_from_ws def load_from_ws
end end


def load_from_xml
begin
@materias = Materia.load_from_xml

@materias.elements.each("materias/materia") do |m|
materia = Materia.find_by_code(m.attributes["codigo"])
materia = Materia.new if(materia == nil)
materia.name = m.attributes["nombre"]
materia.code = m.attributes["codigo"]
materia.credits = m.attributes["creditos"]
materia.umes = m.attributes["umes"]
materia.professor = m.elements["profesor"].attributes["nombre"]
materia.description = m.elements["descripcion"].text
materia.homepage = m.elements["homepage"].attributes["url"]
materia.program = m.elements["programa"].attributes["url"]

materia.save
end

rescue Exception => e
puts "Error ---- #{e.message} ------"
end
redirect_to :action => :list
end
end end
10 changes: 10 additions & 0 deletions app/models/materia.rb
Original file line number Original file line Diff line number Diff line change
@@ -1,2 +1,12 @@
require 'rexml/document'
include REXML

class Materia < ActiveRecord::Base class Materia < ActiveRecord::Base

validates_uniqueness_of :code

def self.load_from_xml
Document.new(File.new("public/materias.xml"))
end

end end
3 changes: 2 additions & 1 deletion app/views/materias/list.html.erb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@
profesor: <%= m.professor %><br/> profesor: <%= m.professor %><br/>
descipción: <%= m.description %><br/> descipción: <%= m.description %><br/>
<%= link_to "ver_mas", :action => :show, :id => m.id %><br/><br/> <%= link_to "ver_mas", :action => :show, :id => m.id %><br/><br/>
<% end %> <% end %><br/>
<%= link_to "Cargar desde xml", :action => :load_from_xml %>
4 changes: 2 additions & 2 deletions app/views/materias/show.html.erb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ creditos: <%= @materia.credits %><br/>
umes: <%= @materia.umes %><br/> umes: <%= @materia.umes %><br/>
profesor: <%= @materia.professor %><br/> profesor: <%= @materia.professor %><br/>
descipción: <%= @materia.description %><br/> descipción: <%= @materia.description %><br/>
programa: <%= link_to "http://#{@materia.description}" %><br/> programa: <%= link_to @materia.program, "http://#{@materia.program}" %><br/>
página de la materia: <%= "http://#{@materia.description}" %><br/> página de la materia: <%= link_to @materia.homepage, "http://#{@materia.homepage}" %><br/><br/>
<%= link_to "Todas las materias", :action => :list %> <%= link_to "Todas las materias", :action => :list %>
42 changes: 42 additions & 0 deletions public/materias.xml
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,42 @@
<?xml version="1.0" encoding="UTF-8"?>

<!--
Document : materias.xml
Created on : November 28, 2010, 4:05 PM
Author : cfernandezg
Description:
Purpose of the document follows.
-->

<materias>
<materia nombre="proyecto" codigo="ST0042" creditos="3" umes="3">
<profesor nombre="jaime acosta"/>
<descripcion>La descripcion de la materia</descripcion>
<programa url="www.programa.com"/>
<homepage url="www.home.com"/>"
</materia>
<materia nombre="topicos" codigo="ST0052" creditos="3" umes="3">
<profesor nombre="carlos"/>
<descripcion>La descripcion de la materia</descripcion>
<programa url="www.programa.com"/>
<homepage url="www.home.com"/>"
</materia>
<materia nombre="analisis" codigo="ST0038" creditos="3" umes="3">
<profesor nombre="miguel"/>
<descripcion>La descripcion de la materia</descripcion>
<programa url="www.programa.com"/>
<homepage url="www.home.com"/>"
</materia>
<materia nombre="bases de datos" codigo="ST0040" creditos="3" umes="3">
<profesor nombre="juan"/>
<descripcion>La descripcion de la materia</descripcion>
<programa url="www.programa.com"/>
<homepage url="www.home.com"/>"
</materia>
<materia nombre="sistemas operativos" codigo="ST0036" creditos="3" umes="3">
<profesor nombre="alberto"/>
<descripcion>La descripcion de la materia</descripcion>
<programa url="www.programa.com"/>
<homepage url="www.home.com"/>"
</materia>
</materias>

0 comments on commit d52a179

Please sign in to comment.