Module: TaggingHelpers

Defined in:
lib/tagtools.rb

Overview

This module allows you to add additional generic functionality to your Tag class by simply extending the TaggingHelpers module.

Example:

class Tag < ActiveRecord::Base
  extend TaggingHelpers
end

Instance Method Summary collapse

Instance Method Details

#create_tag(raw_tag) ⇒ Object

Takes a string and converts it to a tag object, creating the tag object if it doesn’t yet exist.



1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
# File 'lib/tagtools.rb', line 1213

def create_tag(raw_tag)
  if self.class != Class
    raise "You must extend your tag class with the TaggingHelpers module."
  end
  return nil if raw_tag.nil?
  if raw_tag.kind_of? self
    return raw_tag
  end
  tag_object = self.find_by_name(raw_tag.to_s)
  if tag_object.nil?
    tag_object = self.new
    tag_object.name = raw_tag.to_s
    tag_object.save
  end
  return tag_object
end

#get_tag(raw_tag) ⇒ Object

Takes a string and converts it to a tag object, returning nil if it doesn’t yet exist.



1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
# File 'lib/tagtools.rb', line 1232

def get_tag(raw_tag)
  if self.class != Class
    raise "You must extend your tag class with the TaggingHelpers module."
  end
  return nil if raw_tag.nil?
  if raw_tag.kind_of? self
    return raw_tag
  end
  tag_object = self.find_by_name(raw_tag.to_s)
  return tag_object
end

#tag_query(options = {}) ⇒ Object

Unlike the tag_query class method on tagged classes, this will search across the entire tagged space, returning any tagged object, regardless of type, that matches the query criteria.

Options are:

  • :with_all_tags - An array of strings, returns all tagged objects that have all of the tags specified within the array.

  • :with_any_tags - An array of strings, returns all tagged objects that have any of the tags specified within the array.

  • :without_tags - An array of strings, removes all tagged objects that have any of the tags specified within the array from the list of results that would have otherwise been returned. Throws an error if used in the absence of of either of the other two options.

  • :user_id - An integer id for the user that this query is specific to. Only tags that this user has created will be taken into account with this query. Globally scoped tag associations will be not be included in the results if this option is set.



1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
# File 'lib/tagtools.rb', line 1063

def tag_query(options = {})
  if self.class != Class
    raise "You must extend your tag class with the TaggingHelpers module."
  end
  unless options.kind_of? Hash
    raise "The options parameter must be a hash."
  end
  validate_options([:with_any_tags, :with_all_tags,
    :without_tags, :user_id], options.keys)
  if options.size == 0
    raise "You must supply either the with_any_tags option, " +
      "the with_all_tags option, or both."
  end
  with_any_tags = options[:with_any_tags]
  unless with_any_tags.nil?
    with_any_tags.collect! { |tag| tag.to_s }
    with_any_tags.uniq!
  end
  with_all_tags = options[:with_all_tags]
  unless with_all_tags.nil?
    with_all_tags.collect! { |tag| tag.to_s }
    with_all_tags.uniq!
  end
  without_tags = options[:without_tags]
  unless without_tags.nil?
    without_tags.collect! { |tag| tag.to_s }
    without_tags.uniq!
  end
  if without_tags != nil && with_any_tags == nil &&
      with_all_tags == nil
    raise(ActiveRecord::ActiveRecordError,
      "Cannot run this query, nothing to search for.")
  end
  tagging_user_id = options[:user_id]
  results = []
  
  for association_hash in self.instance_variable_get("@tagged_classes")
    
    # Load variables from the association_hash
    tag_class = association_hash[:tag_class]
    tag_foreign_key = association_hash[:tag_foreign_key]
    item_class = association_hash[:item_class]
    item_foreign_key = association_hash[:item_foreign_key]
    user_class = association_hash[:user_class]
    user_foreign_key = association_hash[:user_foreign_key]
    join_table = association_hash[:join_table]
    
    # If they specified a user_id, and this association is a globally
    # scoped tag association, skip to the next association.
    if tagging_user_id != nil && user_class.nil?
      next
    end
          
    group_by_string = item_class.table_name + "." +
      item_class.column_names.join(
        ", #{item_class.table_name}.")
    tagging_user_id_string = ""
    unless tagging_user_id.nil?
      tagging_user_id_string =
        "AND #{join_table}.#{user_foreign_key} = #{tagging_user_id}"
    end

    with_all_tags_results = nil
    if with_all_tags != nil && with_all_tags.size > 0
      tag_name_condition = "#{tag_class.table_name}.name = '" +
        with_all_tags.join(
          "\' OR #{tag_class.table_name}.name=\'") + "'"
      with_all_tags_sql = <<-SQL
        SELECT #{item_class.table_name}.*
        FROM #{join_table}, #{item_class.table_name},
          #{tag_class.table_name}
        WHERE #{join_table}.#{tag_foreign_key} =
          #{tag_class.table_name}.#{tag_class.primary_key}
        AND (#{tag_name_condition})
        AND #{item_class.table_name}.#{item_class.primary_key} =
          #{join_table}.#{item_foreign_key}
        #{tagging_user_id_string}
        GROUP BY #{group_by_string}
        HAVING COUNT(
          #{item_class.table_name}.#{item_class.primary_key}) >=
          #{with_all_tags.size}
      SQL
      with_all_tags_results =
        item_class.find_by_sql(with_all_tags_sql)
      if tagging_user_id.nil?
        for result in with_all_tags_results
          result_tags = result.tags.map do |tag|
            tag.name
          end
          if (result_tags & with_all_tags).size != with_all_tags.size
            # Reject result
            with_all_tags_results.delete(result)
          end
        end
      end
    end

    with_any_tags_results = nil
    if with_any_tags != nil && with_any_tags.size > 0
      with_any_tags_sql = <<-SQL
        SELECT #{item_class.table_name}.*
        FROM #{join_table}, #{item_class.table_name},
          #{tag_class.table_name}
        WHERE #{tag_class.table_name}.name
        IN ('#{with_any_tags.join('\', \'')}')
        AND #{tag_class.table_name}.#{tag_class.primary_key} =
          #{join_table}.#{tag_foreign_key}
        AND #{item_class.table_name}.#{item_class.primary_key} =
          #{join_table}.#{item_foreign_key}
        #{tagging_user_id_string}
        GROUP BY #{group_by_string}
      SQL
      with_any_tags_results =
        item_class.find_by_sql(with_any_tags_sql)
    end

    if with_any_tags_results != nil &&
        with_any_tags_results.size > 0 &&
        with_all_tags_results != nil &&
        with_all_tags_results.size > 0
      results.concat(with_all_tags_results & with_any_tags_results)
    elsif with_any_tags_results != nil &&
        with_any_tags_results.size > 0
      results.concat(with_any_tags_results)
    elsif with_all_tags_results != nil &&
        with_all_tags_results.size > 0
      results.concat(with_all_tags_results)
    end
  end

  if without_tags != nil && without_tags.size > 0
    for result in results
      if tagging_user_id.nil?
        if ((result.tags.map { |tag| tag.name }) &
            without_tags).size > 0
          results.delete(result)
        end
      else
        if ((result.user_tags(tagging_user_id).map { |tag| tag.name }) &
            without_tags).size > 0
          results.delete(result)
        end
      end
    end
  end
  return results
end