Skip to content

Commit

Permalink
Added tags serialization to xml (along with easy hooks to add custom …
Browse files Browse the repository at this point in the history
…serialization fields).
  • Loading branch information
gaspard committed Feb 21, 2011
1 parent 7c47a0b commit 0917b8a
Show file tree
Hide file tree
Showing 10 changed files with 92 additions and 7 deletions.
5 changes: 5 additions & 0 deletions History.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
== 1.0.0.rc3

* major changes
* Included tag in xml serialization (tag_names)

== 1.0.0.rc2 2011-02-14

* major changes
Expand Down
17 changes: 16 additions & 1 deletion bricks/tags/lib/bricks/tags.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,18 @@ module ClassMethods
end

module InstanceMethods
def self.included(base)
# TODO: when we use to_xml instead of XmlSerializer, we can
# use 'tag_names' => SerializableArray.new('tag_names', 'tag', tag_names)
base.export_xml(Proc.new do |opts|
builder, rec = opts[:builder], opts[:record]
builder.tag!('tag_names', :type => :array) do
rec.tag_names.each do |tag|
builder.tag!('tag', tag, :type => :string)
end
end
end)
end

def l_tag
l_comment
Expand Down Expand Up @@ -77,6 +89,10 @@ def tag_names
@tag_names ||= (tags || []).map {|t| t[:comment]}
end

def tag_names=(tag_array)
@tag_names = tag_array
end

# List of Links that are tags for the current node.
def tags
return @tags if defined?(@tags)
Expand Down Expand Up @@ -118,7 +134,6 @@ def remove_link(link)
end
end


private

def tags_as_list(str)
Expand Down
15 changes: 15 additions & 0 deletions bricks/tags/zena/test/unit/tags_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,19 @@ def test_set_tag_list
assert_equal 'big, brown, socks', node.tag_list
end

context 'A node with tags' do
setup do
login(:tiger)
end

subject do
secure(Node) { nodes(:status) }
end

should 'serialize tag values in xml' do
assert_equal %w{blue sky}, Hash.from_xml(subject.to_xml)['node']['tag_names']
end
end # A node with tags


end
14 changes: 13 additions & 1 deletion lib/zena/acts/serializable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ def initialize(name, record, value)
end

protected

def raw_value
@raw_value ||= @record.prop[name]
end
Expand Down Expand Up @@ -76,6 +75,17 @@ def serializable_id_attributes
end

module ModelMethods
def self.included(base)
class << base
cattr_accessor :export_procs

def export_xml(proc)
self.export_procs << proc
end
end
base.export_procs = []
end

def to_xml(options = {}, &block)
options = default_serialization_options.merge(options)
serializer = XmlNodeSerializer.new(self, options)
Expand All @@ -85,6 +95,8 @@ def to_xml(options = {}, &block)
def default_serialization_options
{ :only => %w{created_at updated_at log_at event_at kpath ref_lang fullpath position},
:methods => %w{v_status klass},
:procs => self.class.export_procs,
:record => self, # needed by procs
:properties => export_properties,
:ids => export_ids,
:dasherize => false,
Expand Down
2 changes: 1 addition & 1 deletion lib/zena/info.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module Zena
VERSION = '1.0.0.rc2'
VERSION = '1.0.0.rc3'
ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..', '..'))
end
1 change: 1 addition & 0 deletions lib/zena/remote.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require 'active_support'
require 'zena/remote/interface'
require 'zena/remote/klass'
require 'zena/remote/serializable_array'
require 'zena/remote/node'
require 'zena/remote/connection'

Expand Down
13 changes: 12 additions & 1 deletion lib/zena/remote/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,17 @@ def initialize(connection, hash)
self.attributes = hash
end

def to_xml(*args)
deb args
end

def attributes=(new_attributes)
raise Exception.new("Invalid attributes. Expecting a hash, found #{new_attributes.inspect}") unless new_attributes.kind_of?(Hash)
new_attributes.stringify_keys.each do |key, value|
if value.kind_of?(Array)
writer = "#{key}=".to_sym
if self.respond_to?(writer)
self.send(writer, value)
elsif value.kind_of?(Array)
# setting multiple ids
key = "#{key}_ids" unless key =~ /_ids$/
@attributes[key] = value.map do |elem|
Expand All @@ -34,6 +41,10 @@ def attributes=(new_attributes)
end
end

def tag_names=(list)
@attributes['tag_names'] = SerializableArray.new('tag_names', 'tag', list)
end

def id
@attributes['id']
end
Expand Down
19 changes: 19 additions & 0 deletions lib/zena/remote/serializable_array.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module Zena
module Remote
class SerializableArray < Array
def initialize(name, elem_name, elements)
@name, @elem_name = name, elem_name
replace(elements)
end

def to_xml(opts)
builder = opts[:builder]
builder.tag!(@name, :type => :array) do
each do |elem|
builder.tag!(@elem_name, elem.to_s, :type => :string)
end
end
end
end
end # Remote
end # Zena
8 changes: 7 additions & 1 deletion test/integration/xml_api_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ class NodeResource < ActiveResource::Base
include Zena::Integration::MockResource
self.site = 'http://test.host'
self.element_name = 'node'
def initialize(*args)
super
hash = self.attributes
hash['tag_names'] = Zena::Remote::SerializableArray.new('tag_names', 'tag', hash['tag_names'] || [])
end
end

class BadTokenResource < ActiveResource::Base
Expand Down Expand Up @@ -61,7 +66,7 @@ class BadTokenResource < ActiveResource::Base

assert_equal [nodes_zip(:status)], result
end

should 'find the list of nodes with qb' do
subject # create index entry for art
# create index entry for status
Expand Down Expand Up @@ -108,6 +113,7 @@ class BadTokenResource < ActiveResource::Base
end

should 'save content to remote db' do
deb subject.attributes
subject.attributes.merge!('title' => 'cloud')
assert subject.save
assert_equal 'cloud', nodes(:status).title
Expand Down
5 changes: 3 additions & 2 deletions zena.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@

Gem::Specification.new do |s|
s.name = %q{zena}
s.version = "1.0.0.rc2"
s.version = "1.0.0.rc3"

s.required_rubygems_version = Gem::Requirement.new("> 1.3.1") if s.respond_to? :required_rubygems_version=
s.authors = ["Gaspard Bucher"]
s.date = %q{2011-02-14}
s.date = %q{2011-02-21}
s.default_executable = %q{zena}
s.description = %q{zena is a Ruby on Rails CMS (content managment system) with a focus on usability, ease of customization and web 2.0 goodness (application like behaviour).}
s.email = %q{gaspard@teti.ch}
Expand Down Expand Up @@ -537,6 +537,7 @@ Gem::Specification.new do |s|
"lib/zena/remote/klass.rb",
"lib/zena/remote/mock.rb",
"lib/zena/remote/node.rb",
"lib/zena/remote/serializable_array.rb",
"lib/zena/routes.rb",
"lib/zena/site_worker.rb",
"lib/zena/test_controller.rb",
Expand Down

0 comments on commit 0917b8a

Please sign in to comment.