Class: ActiveRecord::Associations::UserTagsAssociation

Inherits:
GlobalTagsAssociation show all
Defined in:
lib/tagtools.rb

Overview

:nodoc:

Instance Method Summary collapse

Methods inherited from GlobalTagsAssociation

#delete, #include?, #size

Constructor Details

#initialize(owner, user_id, tag_class, user_class, item_class, options) ⇒ UserTagsAssociation

Returns a new instance of UserTagsAssociation.



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
315
316
317
318
319
# File 'lib/tagtools.rb', line 286

def initialize(owner, user_id, tag_class, user_class, item_class,
    options)
  @owner = owner
  @user_id = user_id
  
  @tag_class = tag_class
  @tag_class_name = tag_class.name
  @tag_foreign_key = options[:tag_foreign_key] ||
    Inflector.underscore(
      Inflector.demodulize(@tag_class.name)) + "_id"
  @user_class = user_class
  @user_class_name = user_class.name
  @user_foreign_key = options[:user_foreign_key] ||
    Inflector.underscore(
      Inflector.demodulize(@user_class.name)) + "_id"
  @item_class = item_class
  @item_class_name = item_class.name
  @item_foreign_key = options[:item_foreign_key] ||
    Inflector.underscore(
      Inflector.demodulize(@item_class.name)) + "_id"
  
  @join_table = options[:join_table]
  @options = options
  
  # For the sake of the classes we inheritted from,
  # since we're not calling super
  @association_class = @tag_class
  @association_name = Inflector.pluralize(
    Inflector.underscore(Inflector.demodulize(@tag_class.name)))
  @association_class_primary_key_name = @item_foreign_key

  reset                
  construct_sql
end

Instance Method Details

#<<(*records) ⇒ Object Also known as: push, concat



397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
# File 'lib/tagtools.rb', line 397

def <<(*records)
  result = true
  load_target
  @owner.transaction do
    flatten_deeper(records).each do |record|
      record = create_tag(record)
      next if self.include?(record)
      callback(:before_add, record)
      result &&= insert_record(record) unless @owner.new_record?
      @target << record
      @owner.instance_variable_set("@#{@options[:collection]}", nil)
      callback(:after_add, record)
    end
  end
  @target.sort!
  result and self
end

#build(attributes = {}) ⇒ Object



321
322
323
324
325
326
327
328
329
330
331
# File 'lib/tagtools.rb', line 321

def build(attributes = {})
  if @user_id.nil?
    raise "Can only build object if a tagging user has been specified."
  else
    load_target
    record = @item_class.new(attributes)
    record[@item_foreign_key] = @owner.id unless @owner.new_record?
    @target << record
    return record
  end
end

#clearObject

Removes all records from this association. Returns self so method calls may be chained.



335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
# File 'lib/tagtools.rb', line 335

def clear
  
  # forces load_target if hasn't happened already
  return self if size == 0
  
  if @user_id.nil?
    raise "Tags on an item can only be cleared for one user at a time."
  else
    @owner.connection.execute("DELETE FROM #{@join_table} " +
      "WHERE #{@join_table}.#{@item_foreign_key} = " +
      "  '#{@owner.id}' AND #{@join_table}.#{@user_foreign_key} = " +
      "  '#{@user_id}'")
  end

  @target = []
  self
end

#find(*args) ⇒ Object



357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
# File 'lib/tagtools.rb', line 357

def find(*args)
  # Return an Array if multiple ids are given.
  expects_array = args.first.kind_of?(Array)

  ids = args.flatten.compact.uniq

  # If no block is given, raise RecordNotFound.
  if ids.empty?
    raise RecordNotFound, "Couldn't find #{@tag_class.name} without an ID"
  else
    if ids.size == 1
      id = ids.first
      record = load_target.detect { |record| id == record.id }
      expects_array? ? [record] : record
    else
      load_target.select { |record| ids.include?(record.id) }
    end
  end
end

#find_firstObject



353
354
355
# File 'lib/tagtools.rb', line 353

def find_first
  load_target.first
end

#push_with_attributes(record, join_attributes = {}) ⇒ Object Also known as: concat_with_attributes



377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
# File 'lib/tagtools.rb', line 377

def push_with_attributes(record, join_attributes = {})
  if @user_id.nil?
    raise "Cannot add record without a specific user id."
  else
    unless record.kind_of? @tag_class
      record = record.to_s.create_tag
    end
    join_attributes.each { |key, value| record[key.to_s] = value }
    callback(:before_add, record)
    insert_record(record, join_attributes) unless @owner.new_record?
    @target << record
    @owner.instance_variable_set("@#{@options[:collection]}", nil)
    callback(:after_add, record)
    @target.sort!
    return self
  end
end