Top Level Namespace
Defined Under Namespace
Modules: RadiantTagsExtension, RadiusTags Classes: MetaTag, TagSearchPage, Tagging
Constant Summary collapse
- TaggingMethods =
Proc.new do def return (self.errors.empty? && ) end alias_method_chain :valid?, :tags def self. # just skip the whole method if the tags string hasn't changed return self if @new_tags == tag_list # tags have changed, so we delete all taggings and re-create to preserve order taggings.clear @new_tags.to_s.split(MetaTag::DELIMITER).each do |tag| begin tag = MetaTag.find_or_initialize_by_name(tag.strip.squeeze(" ")) << tag unless .include?(tag) rescue ActiveRecord::RecordInvalid => e errors.add_to_base("Tags can not contain special characters") rescue ActiveRecord::StatementInvalid => e # With SQLite3 - a duplicate tagging will result in the following message: # SQLite3::SQLException: SQL logic error or missing database: INSERT INTO taggings ("meta_tag_id", "taggable_type", "taggable_id") VALUES(11, 'Page', 74) # raise unless e.to_s =~ /duplicate/i end end end alias_method_chain :save!, :tags def () @new_tags = end def # HACK: need to order by tagging to preserve creation order, otherwise # list gets ordered by tag.id .find(:all, :order => 'taggings.id') end def tag_list .map(&:name).join(MetaTag::DELIMITER) end # # Find all the objects tagged with the supplied list of tags # # Usage : Model.tagged_with("ruby") # Model.tagged_with("hello", "world") # Model.tagged_with("hello", "world", :limit => 10) # def self.tagged_with(*tag_list) = tag_list. tag_list = (tag_list) scope = scope(:find) [:select] ||= "#{table_name}.*" [:from] ||= "#{table_name}, meta_tags, taggings" sql = "SELECT #{(scope && scope[:select]) || [:select]} " sql << "FROM #{(scope && scope[:from]) || [:from]} " add_joins!(sql, [:joins], scope) sql << "WHERE #{table_name}.#{primary_key} = taggings.taggable_id " sql << "AND taggings.taggable_type = '#{ActiveRecord::Base.send(:class_name_of_active_record_descendant, self).to_s}' " sql << "AND taggings.meta_tag_id = meta_tags.id " sql << "AND pages.site_id = #{current_site.id} " if self.respond_to?(:current_site) sql << "AND (#{sanitize_sql(['meta_tags.name IN (?)', tag_list.map{|name| name.strip.squeeze(' ')}])}) " sql << "AND #{sanitize_sql([:conditions])} " if [:conditions] columns = column_names.map do |column| "#{table_name}.#{column}" end.join(", ") sql << "GROUP BY #{columns} " sql << "HAVING COUNT(taggings.meta_tag_id) = #{tag_list.size}" add_order!(sql, [:order], scope) add_limit!(sql, , scope) add_lock!(sql, , scope) find_by_sql(sql) end def self.tagged_with_any(*tag_list) = tag_list. tag_list = (tag_list) scope = scope(:find) [:select] ||= "#{table_name}.*" [:from] ||= "#{table_name}, meta_tags, taggings" sql = "SELECT #{(scope && scope[:select]) || [:select]} " sql << "FROM #{(scope && scope[:from]) || [:from]} " add_joins!(sql, [:joins], scope) sql << "WHERE #{table_name}.#{primary_key} = taggings.taggable_id " sql << "AND taggings.taggable_type = '#{ActiveRecord::Base.send(:class_name_of_active_record_descendant, self).to_s}' " sql << "AND taggings.meta_tag_id = meta_tags.id " sql << "AND pages.site_id = #{current_site.id} " if self.respond_to?(:current_site) sql << "AND (" sql << tag_list.inject([]) do |arr,name| arr << sanitize_sql(["meta_tags.name = ?", name.strip.squeeze(' ')]) end.join(" OR ") sql << ") " sql << "AND #{sanitize_sql([:conditions])} " if [:conditions] columns = column_names.map do |column| "#{table_name}.#{column}" end.join(", ") sql << "GROUP BY #{columns} " add_order!(sql, [:order], scope) add_limit!(sql, , scope) add_lock!(sql, , scope) find_by_sql(sql) end def self.() return [] if .blank? = Array().first = .respond_to?(:flatten) ? .flatten : .split(MetaTag::DELIMITER) .map { |tag| tag.strip.squeeze(" ") }.flatten.compact.uniq end end