Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Preserving tag order #15

Merged
merged 2 commits into from

2 participants

@gerrit

A couple of small changes in order to preserve the order of tags both in the admin interface and when iterating over them with r:tags:each in the frontend

@jomz jomz merged commit 144266d into jomz:master
@jomz
Owner

hi Gerrit,

Finally got round to pull this.. thanks for the patch!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 14 additions and 8 deletions.
  1. +1 −1  app/models/radius_tags.rb
  2. +13 −7 lib/tagging_methods.rb
View
2  app/models/radius_tags.rb
@@ -175,7 +175,7 @@ class TagError < StandardError; end
Usage: <pre><code><r:tags:each [limit="4"]>...</r:tags:each></code></pre>
}
tag "tags:each" do |tag|
- selected_tags = tag.locals.page.meta_tags
+ selected_tags = tag.locals.page.ordered_meta_tags
if tag.attr['limit']
selected_tags = selected_tags.first(tag.attr['limit'].to_i)
end
View
20 lib/tagging_methods.rb
@@ -5,15 +5,15 @@ def valid_with_tags?
end
alias_method_chain :valid?, :tags
- def tag_with tags
+ def tag_with new_tags
self.save if self.new_record?
# just skip the whole method if the tags string hasn't changed
- return if tags == tag_list
- # do we need to delete any tags?
- tags_to_delete = tag_list.split(MetaTag::DELIMITER) - tags.split(MetaTag::DELIMITER)
- tags_to_delete.select{|t| meta_tags.delete(MetaTag.find_by_name(t))}
+ return if new_tags == tag_list
- tags.split(MetaTag::DELIMITER).each do |tag|
+ # tags have changed, so we delete all taggings and re-create to preserve order
+ taggings.clear
+
+ new_tags.split(MetaTag::DELIMITER).each do |tag|
begin
tag = MetaTag.find_or_initialize_by_name(tag.strip.squeeze(" "))
meta_tags << tag unless meta_tags.include?(tag)
@@ -29,8 +29,14 @@ def tag_with tags
alias :meta_tags= :tag_with
+ def ordered_meta_tags
+ # HACK: need to order by tagging to preserve creation order, otherwise
+ # list gets ordered by tag.id
+ meta_tags.find(:all, :order => 'taggings.id')
+ end
+
def tag_list
- meta_tags.map(&:name).join(MetaTag::DELIMITER)
+ ordered_meta_tags.map(&:name).join(MetaTag::DELIMITER)
end
#
Something went wrong with that request. Please try again.