Skip to content

Commit

Permalink
Addapted tags so that we can use <r:input type='checkbox' name='tagge…
Browse files Browse the repository at this point in the history
…d[]' value='foo'/> to set tags with checkbox.
  • Loading branch information
gaspard committed Dec 9, 2010
1 parent 4869720 commit 006d582
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 19 deletions.
19 changes: 12 additions & 7 deletions bricks/tags/lib/has_tags.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,17 @@ def tagged

# Set/unset a named tag
def tagged=(hash)
# named tags
hash.each do |k, v|
if v.empty?
remove_tag(k.to_s)
else
add_tag(k.to_s)
if hash.kind_of?(Array) || hash.kind_of?(String)
# input from form
@tag_names = tags_as_list(hash)
else
# named tags
hash.each do |k, v|
if v.empty?
remove_tag(k.to_s)
else
add_tag(k.to_s)
end
end
end
end
Expand Down Expand Up @@ -132,7 +137,7 @@ def remove_link(link)
private

def tags_as_list(str)
str.split(',').map(&:strip).reject{|t| t.blank? }
(str.kind_of?(String) ? str.split(',') : str).map(&:strip).reject{|t| t.blank? }
end

# Update/create links defined in relation proxies
Expand Down
5 changes: 5 additions & 0 deletions bricks/tags/patch/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,15 @@
Link # make sure it is loaded before we reopen it
class Link
safe_method :name => {:class => String, :nil => true}
safe_method :count => {:class => Number, :nil => true}

def name
self[:comment]
end

def count
self[:count]
end
end

Zena::Use::QueryNode.add_filter_field('tag', :key => 'comment', :table => ['nodes', 'links', 'TABLE1.id = TABLE2.source_id AND TABLE2.relation_id IS NULL'])
16 changes: 16 additions & 0 deletions bricks/tags/test/unit/tags_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,20 @@ def test_set_tag_list
assert_equal 'big, brown, socks', node.tag_list
end

def test_set_tagged
login(:tiger)
node = secure!(Node) { nodes(:status) }
assert node.update_attributes(:tagged => ['','big', 'brown', 'socks'])
node = secure!(Node) { nodes(:status) }
assert_equal 'big, brown, socks', node.tag_list
end

def test_set_tagged_clear
login(:tiger)
node = secure!(Node) { nodes(:status) }
assert node.update_attributes(:tagged => [''])
node = secure!(Node) { nodes(:status) }
assert_equal '', node.tag_list
end

end
27 changes: 20 additions & 7 deletions bricks/tags/test/zafu/tags.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
# test_file: ../../../../test/helpers/zena_parser_test.rb
# test_file: ../../../../test/unit/zena/zena_tags_test.rb
default:
context:
lang: 'en'
visitor: 'lion'
node: 'status'
src: "<r:tags do='each' join=',' do='[name]'/>"
tem: "/list1 = @node.tags.*var1.name/"
res: "blue,sky"

listing_no_tags:
context:
node: 'lion'
node: lion
src: "<r:tags>Nothing</r:tags>"
tem: "/list1 = @node.tags/"
res: ""
Expand All @@ -19,7 +21,7 @@ listing_with_link:

tagged_query:
context:
cat: 'blue'
cat: blue
src: "<r:context select='nodes where tag = param:cat in site' do='each' join=', ' do='[name]'/>"
res: "bird, status"

Expand All @@ -29,12 +31,23 @@ images_tagged_blue:

tagged_keys_rubyless:
context:
node: 'status'
node: status
src: "<r:tagged do='keys'><r:each join=', ' do='show'/></r:tagged>"
res: "sky, blue"

tag_names:
context:
node: 'status'
src: "<r:tag_names><r:each join=', ' do='show'/></r:tagged>"
res: "sky, blue"
node: status
src: "<r:tag_names><r:each join=', ' do='show'/></r:tag_names>"
res: "blue, sky"

tag_cloud:
src: "<r:tag_cloud in='project'><span do='each' join=' '><r:name/>(<r:show attr='count'/>)</span></r:tag_cloud>"
res: "<span>sky(2)</span>"

input_tag:
context:
node: status
src: "<r:input type='checkbox' name='tagged[]' value='sky'/>"
tem: "<input name='node[tagged][]' type='checkbox' value='sky'<%= @node.tagged[\"sky\"] == \"sky\" ? \" checked='checked'\" : '' %>/>"
res: "<input name='node[tagged][]' type='checkbox' value='sky' checked='checked'/>"
22 changes: 22 additions & 0 deletions bricks/tags/zafu/tags.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module Bricks
module Tags
module Zafu
def r_tag_cloud
if node_kind_of?(Node)
node_name = @context[:parent_node] || node
else
node_name = @context[:previous_node]
end

if @params[:in] == 'project' || @params[:in] == 'section'
filter = " AND nodes.project_id = \#{Node.connection.quote(#{node_name}.get_#{@params[:in]}_id)}"
else
filter = ''
end

method = "Link.find_by_sql(%Q{SELECT COUNT(nodes.id) AS count, links.comment FROM links INNER JOIN nodes ON nodes.id = links.source_id WHERE \#{@node.secure_scope('nodes')} AND links.target_id IS NULL#{filter} GROUP BY links.comment ORDER BY links.comment})"
open_context(:method => method, :class => [Link])
end
end # Zafu
end # Tags
end # Bricks
7 changes: 6 additions & 1 deletion lib/zafu/support/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,12 @@ def node(klass = self.node_class)
if klass == self.node_class
(@context[:saved_template] && @context[:main_node]) ? "@#{base_class.to_s.underscore}" : (@context[:node] || '@node')
elsif klass == Node
@context[:previous_node] || '@node'
if @context[:saved_template]
# hack to prevent Comment based forms creation: should work.
'@node'
else
@context[:previous_node] || '@node'
end
else
# ?
out parser_error("could not find node_name for #{klass} (current class is #{node_class})")
Expand Down
3 changes: 3 additions & 0 deletions lib/zafu/support/forms.rb
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,9 @@ def get_input_params(params = @params)

if sub_attr
if (nattr = node_attribute(attribute)) != 'nil'
if sub_attr == ''
sub_attr = params[:value] || ''
end
nattr = "#{nattr}[#{sub_attr.inspect}]"
end
else
Expand Down
3 changes: 0 additions & 3 deletions test/unit/zena/zena_tags_test.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
require 'test_helper'



class ZenaTagsTest < Zena::Controller::TestCase


yamltest :directories => [:default, "#{Zena::ROOT}/bricks/**/test/zafu"]
Section # make sure we load Section links before trying relations

Expand Down
3 changes: 2 additions & 1 deletion zena.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Gem::Specification.new do |s|

s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Gaspard Bucher"]
s.date = %q{2010-11-17}
s.date = %q{2010-12-07}
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 @@ -264,6 +264,7 @@ Gem::Specification.new do |s|
"bricks/tags/test/sites/zena/links.yml",
"bricks/tags/test/unit/tags_test.rb",
"bricks/tags/test/zafu/tags.yml",
"bricks/tags/zafu/tags.rb",
"bricks/toto.zip",
"bricks/worker/README",
"bricks/worker/migrate/20091104191643_create_delayed_jobs_table.rb",
Expand Down

0 comments on commit 006d582

Please sign in to comment.