initial changeset support
This commit is contained in:
parent
e15fa25639
commit
8c5638d696
8 changed files with 156 additions and 9 deletions
26
app/controllers/changeset_controller.rb
Normal file
26
app/controllers/changeset_controller.rb
Normal file
|
@ -0,0 +1,26 @@
|
|||
# The ChangesetController is the RESTful interface to Changeset objects
|
||||
|
||||
class ChangesetController < ApplicationController
|
||||
require 'xml/libxml'
|
||||
|
||||
before_filter :authorize, :only => [:create, :update, :delete]
|
||||
before_filter :check_write_availability, :only => [:create, :update, :delete]
|
||||
before_filter :check_read_availability, :except => [:create, :update, :delete]
|
||||
|
||||
# Create a changeset from XML.
|
||||
def create
|
||||
if request.put?
|
||||
cs = Changeset.from_xml(request.raw_post, true)
|
||||
|
||||
if cs
|
||||
cs.user_id = @user.id
|
||||
cs.save_with_tags!
|
||||
render :text => cs.id.to_s, :content_type => "text/plain"
|
||||
else
|
||||
render :nothing => true, :status => :bad_request
|
||||
end
|
||||
else
|
||||
render :nothing => true, :status => :method_not_allowed
|
||||
end
|
||||
end
|
||||
end
|
9
app/controllers/changeset_tag_controller.rb
Normal file
9
app/controllers/changeset_tag_controller.rb
Normal file
|
@ -0,0 +1,9 @@
|
|||
class ChangesetTagController < ApplicationController
|
||||
layout 'site'
|
||||
|
||||
def search
|
||||
@tags = ChangesetTag.find(:all, :limit => 11, :conditions => ["match(v) against (?)", params[:query][:query].to_s] )
|
||||
end
|
||||
|
||||
|
||||
end
|
73
app/models/changeset.rb
Normal file
73
app/models/changeset.rb
Normal file
|
@ -0,0 +1,73 @@
|
|||
class Changeset < ActiveRecord::Base
|
||||
require 'xml/libxml'
|
||||
|
||||
belongs_to :user
|
||||
|
||||
has_many :changeset_tags, :foreign_key => 'id'
|
||||
|
||||
def self.from_xml(xml, create=false)
|
||||
begin
|
||||
p = XML::Parser.new
|
||||
p.string = xml
|
||||
doc = p.parse
|
||||
|
||||
cs = Changeset.new
|
||||
|
||||
doc.find('//osm/changeset').each do |pt|
|
||||
if create
|
||||
cs.created_at = Time.now
|
||||
end
|
||||
|
||||
pt.find('tag').each do |tag|
|
||||
cs.add_tag_keyval(tag['k'], tag['v'])
|
||||
end
|
||||
end
|
||||
rescue Exception => ex
|
||||
print "noes "+ ex.to_s + "\n"
|
||||
cs = nil
|
||||
end
|
||||
|
||||
return cs
|
||||
end
|
||||
|
||||
def tags
|
||||
unless @tags
|
||||
@tags = {}
|
||||
self.changeset_tags.each do |tag|
|
||||
@tags[tag.k] = tag.v
|
||||
end
|
||||
end
|
||||
@tags
|
||||
end
|
||||
|
||||
def tags=(t)
|
||||
@tags = t
|
||||
end
|
||||
|
||||
def add_tag_keyval(k, v)
|
||||
@tags = Hash.new unless @tags
|
||||
@tags[k] = v
|
||||
end
|
||||
|
||||
def save_with_tags!
|
||||
t = Time.now
|
||||
|
||||
Changeset.transaction do
|
||||
# fixme update modified_at time?
|
||||
self.save!
|
||||
end
|
||||
|
||||
ChangesetTag.transaction do
|
||||
tags = self.tags
|
||||
ChangesetTag.delete_all(['id = ?', self.id])
|
||||
|
||||
tags.each do |k,v|
|
||||
tag = ChangesetTag.new
|
||||
tag.k = k
|
||||
tag.v = v
|
||||
tag.id = self.id
|
||||
tag.save!
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
5
app/models/changeset_tag.rb
Normal file
5
app/models/changeset_tag.rb
Normal file
|
@ -0,0 +1,5 @@
|
|||
class ChangesetTag < ActiveRecord::Base
|
||||
|
||||
belongs_to :changeset, :foreign_key => 'id'
|
||||
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue