Module: RocketTag::Taggable::ClassMethods

Defined in:
lib/rocket_tag/taggable.rb

Instance Method Summary collapse

Instance Method Details

#attr_taggable(*contexts) ⇒ Object



316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
# File 'lib/rocket_tag/taggable.rb', line 316

def attr_taggable *contexts
  unless class_variable_defined?(:@@acts_as_rocket_tag)
    include RocketTag::Taggable::InstanceMethods
    class_variable_set(:@@acts_as_rocket_tag, true)
    alias_method_chain :reload, :tags
  end

  rocket_tag.contexts += contexts

  setup_for_rocket_tag

  contexts.each do |context|
    class_eval do

      has_many "#{context}_taggings".to_sym, 
        :source => :taggable,  
        :as => :taggable,
        :conditions => { :context => context }

      has_many "#{context}_tags".to_sym,
        :source => :tag,
        :through => :taggings,
        :conditions => [ "taggings.context = ?", context ]

      validate context do
        if not send(context).kind_of? Enumerable
          errors.add context, :invalid
        end
      end

      # This is to compensate for a rails bug that returns
      # a string for postgres
      def tags_count
        self[:tags_count].to_i
      end

      # Return an array of RocketTag::Tags for the context
      define_method "#{context}" do
        cache_tags
        tags_for_context(context)
      end

      define_method "#{context}=" do |list|
        list = Manager.parse_tags list

        # Ensure the tags are loaded
        cache_tags
        write_context(context, list)

        (@tag_dirty ||= Set.new) << context

      end
    end
  end
end

#is_valid_context?(context) ⇒ Boolean

Returns:

  • (Boolean)


128
129
130
# File 'lib/rocket_tag/taggable.rb', line 128

def is_valid_context? context
    rocket_tag.contexts.include? context
end

#normalize_contexts(context, default_if_nil = []) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/rocket_tag/taggable.rb', line 132

def normalize_contexts(context, default_if_nil = [])
  contexts = if context
    if context.class == Array
      context
    else
      [context]
    end
  else
    default_if_nil
  end

  validate_contexts(contexts)

  contexts
end

Generates a query that returns list of popular tags for given model with an extra column :tags_count.



265
266
267
# File 'lib/rocket_tag/taggable.rb', line 265

def popular_tags options={}
  tags(options)
end

#rocket_tagObject



124
125
126
# File 'lib/rocket_tag/taggable.rb', line 124

def rocket_tag
  @rocket_tag ||= RocketTag::Taggable::Manager.new(self)
end

#setup_for_rocket_tagObject



269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
# File 'lib/rocket_tag/taggable.rb', line 269

def setup_for_rocket_tag
  unless @setup_for_rocket_tag
    @setup_for_rocket_tag = true
    class_eval do
      default_scope do
        preload{taggings}.preload{tags}
      end

      before_save do
        @tag_dirty ||= Set.new

        @tag_dirty.each do |context|
          # Get the current tags for this context
          list = send(context)

          # Destroy all taggings
          destroy_tags_for_context context

          # Find existing tags
          exisiting_tags = Tag.where{name.in(list)}
          exisiting_tag_names = exisiting_tags.map &:name

          # Find missing tags
          tags_names_to_create = list - exisiting_tag_names 

          # Create missing tags
          created_tags = tags_names_to_create.map do |tag_name|
            Tag.create :name => tag_name
          end

          # Recreate taggings
          tags_to_assign = exisiting_tags + created_tags

          tags_to_assign.each do |tag|
            tagging = Tagging.new :tag => tag, 
              :taggable => self, 
              :context => context, 
              :tagger => nil
            self.taggings << tagging
          end
        end
        @tag_dirty = Set.new
      end
    end
  end
end

#tagged_with(tags_list, options = {}) ⇒ Object



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/rocket_tag/taggable.rb', line 178

def tagged_with tags_list, options = {}

  # Grab table name
  t = self.table_name

  q = joins{taggings.tag}

  case tags_list
  when Hash
    # A tag can only match it's context

    c = tags_list.each_key.map do |context|
      squeel do
        tags.name.in(tags_list[context]) & (taggings.context == context.to_s)
      end
    end.inject do |s,t|
      s | t
    end

    q = q.where(c)

  else
    # Any tag can match any context
    q = q.
      where{tags.name.in(tags_list)}.
      where(with_tag_context(options.delete(:on)))
  end

  q = q.group_by_all_columns.
    select{count(tags.id).as( tags_count)}.
    select{"#{t}.*"}.
    order("tags_count desc")

  # Isolate the aggregate uery by wrapping it as
  #
  # select * from ( ..... ) tags
  q = from(q.arel.as(self.table_name))
  
  # Restrict by minimum tag counts if required
  min = options.delete :min 
  q = q.where{tags_count>=min} if min 

  # Require all the tags if required
  all = options.delete :all
  q = q.where{tags_count==tags_list.length} if all

  # Return the relation
  q
end

#tags(options = {}) ⇒ Object

Get the tags associated with this model class This can be chained such as

User.documents.tags



232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/rocket_tag/taggable.rb', line 232

def tags(options = {})

  # Grab the current scope
  s = select{id}

  # Grab table name
  t = self.to_s

  q = RocketTag::Tag.joins{taggings}.
    where{taggings.taggable_type==t}.  # Apply taggable type
    where{taggings.taggable_id.in(s)}. # Apply current scope
    where(with_tag_context(options.delete(:on))). # Restrict by context
    group_by_all_columns.
    select{count(tags.id).as(tags_count)}.
    select('tags.*').
    order("tags_count desc")

  # Isolate the aggregate query by wrapping it as
  #
  # select * from ( ..... ) tags
  q = RocketTag::Tag.from(q.arel.as(RocketTag::Tag.table_name))
  
  # Restrict by minimum tag counts if required
  min = options.delete :min 
  q = q.where{tags_count>=min} if min 

  # Return the relation
  q

end

#validate_contexts(contexts) ⇒ Object

Verify contexts are valid for the taggable type



149
150
151
152
153
154
155
# File 'lib/rocket_tag/taggable.rb', line 149

def validate_contexts contexts
  contexts.each do |context|
    unless is_valid_context? context
      raise Exception.new("#{context} is not a valid tag context for #{self}") 
    end
  end
end

#with_tag_context(context) ⇒ Object

Filters tags according to context. context param can be either a single context id or an array of context ids



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/rocket_tag/taggable.rb', line 161

def with_tag_context context
  contexts = normalize_contexts(context)

  condition = if contexts

    contexts.map do |context|
      squeel do
        (taggings.context == context.to_s)
      end
    end.inject do |s, t|
      s | t
    end

  end

end