在Rails 3中标记的好方法是什么?
答案 0 :(得分:7)
我看了两个解决方案,但更喜欢 https://github.com/mbleigh/acts-as-taggable-on 过度 https://github.com/jviney/acts_as_taggable_on_steroids
更好的文档,对我而言似乎更灵活。
答案 1 :(得分:3)
https://github.com/bradphelan/rocket_tag
是我刚刚创建的一个新库。它是用Ernie Miller的吱吱声宝石实现的 在可能的情况下,所有可怕的SQL都需要正确实现标记库 很干净。
比较acts_as_taggable_ons
def tagged_with(tags, options = {})
tag_list = ActsAsTaggableOn::TagList.from(tags)
empty_result = scoped(:conditions => "1 = 0")
return empty_result if tag_list.empty?
joins = []
conditions = []
context = options.delete(:on)
alias_base_name = undecorated_table_name.gsub('.','_')
if options.delete(:exclude)
tags_conditions = tag_list.map { |t| sanitize_sql(["#{ActsAsTaggableOn::Tag.table_name}.name #{like_operator} ?", t]) }.join(" OR ")
conditions << "#{table_name}.#{primary_key} NOT IN (SELECT #{ActsAsTaggableOn::Tagging.table_name}.taggable_id FROM #{ActsAsTaggableOn::Tagging.table_name} JOIN #{ActsAsTaggableOn::Tag.table_name} ON #{ActsAsTaggableOn::Tagging.table_name}.tag_id = #{ActsAsTaggableOn::Tag.table_name}.#{ActsAsTaggableOn::Tag.primary_key} AND (#{tags_conditions}) WHERE #{ActsAsTaggableOn::Tagging.table_name}.taggable_type = #{quote_value(base_class.name)})"
elsif options.delete(:any)
# get tags, drop out if nothing returned (we need at least one)
tags = ActsAsTaggableOn::Tag.named_any(tag_list)
return scoped(:conditions => "1 = 0") unless tags.length > 0
# setup taggings alias so we can chain, ex: items_locations_taggings_awesome_cool_123
# avoid ambiguous column name
taggings_context = context ? "_#{context}" : ''
#TODO: fix alias to be smaller
taggings_alias = "#{alias_base_name}#{taggings_context}_taggings_#{tags.map(&:safe_name).join('_')}_#{rand(1024)}"
tagging_join = "JOIN #{ActsAsTaggableOn::Tagging.table_name} #{taggings_alias}" +
" ON #{taggings_alias}.taggable_id = #{table_name}.#{primary_key}" +
" AND #{taggings_alias}.taggable_type = #{quote_value(base_class.name)}"
tagging_join << " AND " + sanitize_sql(["#{taggings_alias}.context = ?", context.to_s]) if context
# don't need to sanitize sql, map all ids and join with OR logic
conditions << tags.map { |t| "#{taggings_alias}.tag_id = #{t.id}" }.join(" OR ")
select_clause = "DISTINCT #{table_name}.*" unless context and tag_types.one?
joins << tagging_join
else
tags = ActsAsTaggableOn::Tag.named_any(tag_list)
return empty_result unless tags.length == tag_list.length
tags.each do |tag|
prefix = "#{tag.safe_name}_#{rand(1024)}"
taggings_alias = "#{alias_base_name}_taggings_#{prefix}"
tagging_join = "JOIN #{ActsAsTaggableOn::Tagging.table_name} #{taggings_alias}" +
" ON #{taggings_alias}.taggable_id = #{table_name}.#{primary_key}" +
" AND #{taggings_alias}.taggable_type = #{quote_value(base_class.name)}" +
" AND #{taggings_alias}.tag_id = #{tag.id}"
tagging_join << " AND " + sanitize_sql(["#{taggings_alias}.context = ?", context.to_s]) if context
joins << tagging_join
end
end
taggings_alias, tags_alias = "#{alias_base_name}_taggings_group", "#{alias_base_name}_tags_group"
if options.delete(:match_all)
joins << "LEFT OUTER JOIN #{ActsAsTaggableOn::Tagging.table_name} #{taggings_alias}" +
" ON #{taggings_alias}.taggable_id = #{table_name}.#{primary_key}" +
" AND #{taggings_alias}.taggable_type = #{quote_value(base_class.name)}"
group_columns = ActsAsTaggableOn::Tag.using_postgresql? ? grouped_column_names_for(self) : "#{table_name}.#{primary_key}"
group = "#{group_columns} HAVING COUNT(#{taggings_alias}.taggable_id) = #{tags.size}"
end
scoped(:select => select_clause,
:joins => joins.join(" "),
:group => group,
:conditions => conditions.join(" AND "),
:order => options[:order],
:readonly => false)
end
到rocket_tags
def with_tag_context context
if context
where{taggings.context == my{context} }
else
where{}
end
end
def tagged_with tags_list, options = {}
on = options.delete :on
all = options.delete :all
q = if all
joins{tags}.where{
id.in(
my{self}.
select{id}.
joins{tags}.
where{tags.name.in(my{tags_list})}.
group{~id}.
having{count(~id)==my{tags_list.length}}.
with_tag_context(my{on})
)
}
else
joins{tags}.where{tags.name.in(my{tags_list})}.with_tag_context(on)
end
q.select{"distinct #{my{table_name}}.*"}
end
虽然我没有声称处理了acts_as_taggable_on已经实现的所有功能,但它更清洁了。总有明天:)
因此,如果您想要一个标记库,您可以深入了解并为rocket_tag添加功能可能就是您想要的。
在加载相关标签时,还要注意性能并避免N + 1问题。值得一看,但目前非常 alpha ,我会根据项目的需要添加功能。
顺便说一句。感谢act-as-taggable-on。我不会焚烧图书馆。我从中借用了架构和想法,但是当我想修复自己的功能时,我觉得代码中的SQL风格很难理解,在使用https://github.com/ernie/squeel进行AR查询后,我觉得我可以更好地工作干净的石板。
RocketTag还有一个全面的rspec测试套件https://github.com/bradphelan/rocket_tag/blob/master/spec/rocket_tag/taggable_spec.rb
答案 2 :(得分:1)
act_as_taggable_on_steroids