Skip to content

Commit

Permalink
add more robust tagging [Moritz Angermann]
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.techno-weenie.net/projects/mephisto/trunk@2434 567b1171-46fb-0310-a4c9-b4bef9110e78
  • Loading branch information
technoweenie committed Nov 4, 2006
1 parent b05f90d commit 9e1205a
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 13 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
* SVN *

* 0.7.1 PRE-RELEASE *

* add more robust tagging [Moritz Angermann]

* Fixed duplicate body bug in default Simpla theme when article has no excerpt

* Show excerpt by default on admin edit if the article has one
Expand Down
5 changes: 4 additions & 1 deletion app/apis/meta_weblog_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def article_dto_from(article)
:categories => article.sections.collect { |c| c.name },
:mt_text_more => article.body.to_s,
:mt_excerpt => article.excerpt.to_s,
# :mt_keywords => article.keywords.to_s,
:mt_keywords => article.tag,
# :mt_allow_comments => article.allow_comments? ? 1 : 0,
# :mt_allow_pings => article.allow_pings? ? 1 : 0,
# :mt_convert_breaks => (article.text_filter.name.to_s rescue ''),
Expand All @@ -72,6 +72,9 @@ def post_it(article, user, password, struct, publish)
# if no categories are supplied do not attempt to set any.
article.section_ids = Section.find(:all, :conditions => ['name IN (?)', struct['categories']]).collect(&:id) if struct['categories']
article.attributes = {:updater => @user, :body => struct['description'].to_s, :title => struct['title'].to_s, :excerpt => struct['mt_excerpt'].to_s}
# Keywords/Tags support
Tagging.set_on article, struct['mt_keywords'] if struct['mt_keywords'] # set/modify keywords _only_ if they are supplied. mt_keywords _overwrite_ not alter the ``tags''

utc_date = Time.utc(struct['dateCreated'].year, struct['dateCreated'].month, struct['dateCreated'].day, struct['dateCreated'].hour, struct['dateCreated'].sec, struct['dateCreated'].min) rescue article.published_at || Time.now.utc
article.published_at = publish == true ? utc_date : nil
article.save!
Expand Down
20 changes: 14 additions & 6 deletions app/models/tag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,27 @@ def [](tag)
# # => ['a', 'b', 'c']
def parse(list)
return list if list.is_a?(Array)
list.split(',').collect! { |s| s.gsub(/[^\w\ ]+/, '').downcase.strip }.delete_if { |s| s.blank? }
# more robust does handle all kinds of different tags. (comma seperated, space seperated (in quotation marks))
# should handle most the common keyword formats.
# e.g.: b'log, emacs fun, rails, ruby => "b'log", "emacs fun", "rails", "ruby"
# "b'log" "emacs fun" "rails" "ruby" => "b'log", "emacs fun", "rails", "ruby"
# 'b\'log' 'emacs fun' 'rails' 'ruby' => "b'log", "emacs fun", "rails", "ruby"
#
list.scan(/((?: |)['"]{0,1})['"]?(.*?)(?:[,'"]|$)(?:\1(?: |$))/).collect{ |tag| tag.last }.uniq.delete_if{ |tag| tag == "" }
# the old version for legacy comparison.
#list.split(',').collect! { |s| s.gsub(/[^\w\ ]+/, '').downcase.strip }.delete_if { |s| s.blank? }
end

# Parses comma separated tag list and returns tags for them.
#
# Tag.parse_to_tags('a, b, c')
# # => [Tag, Tag, Tag]
def parse_to_tags(list)
find_or_create(parse(list))
end

# Returns Tags from an array of tag names
#
#
# Tag.find_or_create(['a', 'b', 'c'])
# # => [Tag, Tag, Tag]
def find_or_create(tag_names)
Expand All @@ -38,8 +46,8 @@ def find_or_create(tag_names)
def ==(comparison_object)
super || name == comparison_object.to_s
end

def to_s() name end
alias to_param to_s
alias to_liquid to_s
end
end
50 changes: 48 additions & 2 deletions test/functional/backend_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,54 @@ def test_meta_weblog_new_post
end
end

def test_meta_weblog_new_post_tags
# test weather we can create a new post, set tags and get away with it...
tags = 'blog, emacs, fun, rails, ruby'
article = MetaWeblogStructs::Article.new(:title => 'This is a title', :description => 'This is a post', :mt_keywords => tags)

args = [ 1, 'quentin', 'test', article, 1 ]

result = invoke_layered :metaWeblog, :newPost, *args
# assert result
new_post = Article.find(result)
assert_equal Tag.parse_to_tags(tags).collect(&:name).sort, new_post.tags.collect(&:name).sort
end

def test_meta_weblog_edit_post_tags
# changing tags
tags = 'blog, emacs, fun, rails, ruby'
article = MetaWeblogStructs::Article.new(:title => 'This is a title', :description => 'This is a post', :mt_keywords => tags)
args = [ 1, 'quentin', 'test', article, 1 ]
post_id = invoke_layered :metaWeblog, :newPost, *args

new_post = Article.find(post_id)
assert_equal Tag.parse_to_tags(tags).collect(&:name).sort, new_post.tags.collect(&:name).sort

tags = 'bar, baz, foo'
article = MetaWeblogStructs::Article.new(:title => 'This is a title', :description => 'This is a post', :mt_keywords => tags)
args = [ post_id, 'quentin', 'test', article, 1]
edit_result = invoke_layered :metaWeblog, :editPost, *args
assert_equal true, edit_result

new_post = Article.find(post_id)
assert_equal Tag.parse_to_tags(tags).collect(&:name).sort, new_post.tags.collect(&:name).sort
end

def test_meta_weblog_get_post_with_tags
# set tags and see if we recive them!
tags = 'blog, emacs, fun, rails, ruby'
article = MetaWeblogStructs::Article.new(:title => 'This is a title', :description => 'This is a post', :mt_keywords => tags)

args = [ 1, 'quentin', 'test', article, 1 ]
post_id = invoke_layered :metaWeblog, :newPost, *args

args = [ post_id, 'quentin', 'test' ]
result = invoke_layered :metaWeblog, :getPost, *args

assert_equal tags, result['mt_keywords']
end


def test_meta_weblog_new_post_min
# This is going to test weather a post is correctly submited or not without the published_at field!
# See http://www.xmlrpc.com/metaWeblogApi#theStruct or
Expand Down Expand Up @@ -195,7 +243,6 @@ def test_meta_weblog_new_post_publish
assert_equal 'This is a title', new_post.title
assert_equal c[:expect], new_post.status
end

end
end

Expand Down Expand Up @@ -253,5 +300,4 @@ def new_media_object(options = {})
'bits' => Base64.encode64(File.open(File.expand_path(RAILS_ROOT) + '/public/images/mephisto/shadow.png', 'rb') { |f| f.read })
))
end

end
21 changes: 19 additions & 2 deletions test/unit/tag_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,23 @@ class TagTest < Test::Unit::TestCase

def test_should_parse_comma_separated_tags
assert_equal %w(a b c), Tag.parse('a, b, c')
assert_equal %w(a b\ c), Tag.parse('a, b c')
end

def test_should_parse_simple_tags
assert_equal %w(a b c), Tag.parse("'a' 'b' 'c'")
assert_equal %w(a b c), Tag.parse('"a" "b" "c"')
end

def test_should_parse_more_complicated_tags
# with quotation marks _in_ the string.
assert_equal %w(tagging it's weirdness), Tag.parse('tagging, it\'s, weirdness')
assert_equal %w(tagging it"s weirdness), Tag.parse('tagging, it"s, weirdness')
assert_equal %w(tagging it's weirdness), Tag.parse('"tagging" "it\'s" "weirdness"')
assert_equal %w(tagging it's weirdness), Tag.parse("'tagging' 'it's' 'weirdness'")
# with spaces...
assert_equal %w(tagging it's\ weirdness), Tag.parse("'tagging' 'it's weirdness'")
assert_equal %w(tagging it's\ weirdness), Tag.parse('"tagging" "it\'s weirdness"')
end

def test_should_return_tag_array
Expand All @@ -22,12 +39,12 @@ def test_should_create_tags_from_comma_separated_list
Tag.parse_to_tags 'ruby, a, b, rails, c'
end
end

def test_tag_equality
assert_equal tags(:ruby), 'ruby'
assert_equal Tag.find_by_name('ruby'), tags(:ruby)
end

def test_should_select_tags_by_name
assert_equal tags(:ruby), Tag[:ruby]
end
Expand Down
4 changes: 2 additions & 2 deletions test/unit/tagging_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
end
assert_equal [], assets(:gif).reload.tags
end

specify "should change tags" do
assert_difference Tagging, :count, 2 do
assert_difference Tag, :count do
Expand Down Expand Up @@ -63,7 +63,7 @@
end
assert_equal [], contents(:another).reload.tags
end

specify "should change tags" do
assert_difference Tagging, :count, 2 do
assert_difference Tag, :count do
Expand Down

0 comments on commit 9e1205a

Please sign in to comment.