Class: ActiveRecord::Associations::GlobalTagsAssociation

Inherits:
AssociationCollection
  • Object
show all
Defined in:
lib/tagtools.rb

Overview

:nodoc:

Direct Known Subclasses

UserTagsAssociation

Instance Method Summary collapse

Constructor Details

#initialize(owner, tag_class, item_class, options) ⇒ GlobalTagsAssociation

Returns a new instance of GlobalTagsAssociation.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/tagtools.rb', line 35

def initialize(owner, tag_class, item_class, options)
  @owner = owner

  @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"
  @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



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/tagtools.rb', line 135

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
      callback(:after_add, record)
    end
  end
  @target.sort!
  result and self
end

#build(attributes = {}) ⇒ Object



63
64
65
66
67
68
69
# File 'lib/tagtools.rb', line 63

def build(attributes = {})
  load_target
  record = @item_class.new(attributes)
  record[@item_foreign_key] = @owner.id unless @owner.new_record?
  @target << record
  return record
end

#clearObject

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



73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/tagtools.rb', line 73

def clear

  # forces load_target if hasn't happened already
  return self if size == 0

  @owner.connection.execute("DELETE FROM #{@join_table} " +
    "WHERE #{@join_table}.#{@item_foreign_key} = " +
    "  '#{@owner.id}'")

  @target = []
  self
end

#delete(*records) ⇒ Object

Remove records from this association. Does not destroy records.



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/tagtools.rb', line 156

def delete(*records)
  records = flatten_deeper(records)
  for index in 0..records.size
    records[index] = create_tag(records[index])
  end
  records.reject! do |record|
    if record.nil?
      true
    elsif record.new_record?
      @target.delete(record)
    else
      false
    end
  end
  return if records.empty?

  @owner.transaction do
    records.each { |record| callback(:before_remove, record) }
    delete_records(records)
    records.each do |record|
      @target.delete(record)
      callback(:after_remove, record)
    end
  end
end

#find(*args) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/tagtools.rb', line 90

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



86
87
88
# File 'lib/tagtools.rb', line 86

def find_first
  load_target.first
end

#include?(raw_tag) ⇒ Boolean

Returns:

  • (Boolean)


125
126
127
128
129
# File 'lib/tagtools.rb', line 125

def include?(raw_tag)
  actual_tag = get_tag(raw_tag)
  return false if actual_tag.nil?
  return @target.include?(actual_tag)
end

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



110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/tagtools.rb', line 110

def push_with_attributes(record, join_attributes = {})
  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
  callback(:after_add, record)
  @target.sort!
  return self
end

#sizeObject



131
132
133
# File 'lib/tagtools.rb', line 131

def size
  count_records
end