Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 107 lines (93 sloc) 4.599 kb
db045db @dhh Initial
dhh authored
1 module ActiveRecord
2 module Associations
3 class HasAndBelongsToManyAssociation < AssociationCollection #:nodoc:
4 def initialize(owner, association_name, association_class_name, association_class_primary_key_name, join_table, options)
5 super(owner, association_name, association_class_name, association_class_primary_key_name, options)
6
7 @association_foreign_key = options[:association_foreign_key] || Inflector.underscore(Inflector.demodulize(association_class_name.downcase)) + "_id"
8 association_table_name = options[:table_name] || @association_class.table_name(association_class_name)
9 @join_table = join_table
10 @order = options[:order] || "t.#{@owner.class.primary_key}"
11
12 interpolate_sql_options!(options, :finder_sql, :delete_sql)
13 @finder_sql = options[:finder_sql] ||
14 "SELECT t.*, j.* FROM #{association_table_name} t, #{@join_table} j " +
15 "WHERE t.#{@owner.class.primary_key} = j.#{@association_foreign_key} AND " +
16 "j.#{association_class_primary_key_name} = '#{@owner.id}' " +
17 (options[:conditions] ? " AND " + options[:conditions] : "") + " " +
18 "ORDER BY #{@order}"
19 end
20
21 # Removes all records from this association. Returns +self+ so method calls may be chained.
22 def clear
23 return self if size == 0 # forces load_collection if hasn't happened already
24
25 if sql = @options[:delete_sql]
26 each { |record| @owner.connection.execute(sql) }
27 elsif @options[:conditions]
28 sql =
29 "DELETE FROM #{@join_table} WHERE #{@association_class_primary_key_name} = '#{@owner.id}' " +
30 "AND #{@association_foreign_key} IN (#{collect { |record| record.id }.join(", ")})"
31 @owner.connection.execute(sql)
32 else
33 sql = "DELETE FROM #{@join_table} WHERE #{@association_class_primary_key_name} = '#{@owner.id}'"
34 @owner.connection.execute(sql)
35 end
36
37 @collection = []
38 self
39 end
40
41 def find(association_id = nil, &block)
42 if block_given? || @options[:finder_sql]
43 load_collection
44 @collection.find(&block)
45 else
46 if loaded?
47 find_all { |record| record.id == association_id.to_i }.first
48 else
49 find_all_records(@finder_sql.sub(/ORDER BY/, "AND j.#{@association_foreign_key} = '#{association_id}' ORDER BY")).first
50 end
51 end
52 end
53
54 def push_with_attributes(record, join_attributes = {})
55 raise_on_type_mismatch(record)
56 insert_record_with_join_attributes(record, join_attributes)
57 join_attributes.each { |key, value| record.send(:write_attribute, key, value) }
58 @collection << record if loaded?
59 self
60 end
61
62 alias :concat_with_attributes :push_with_attributes
63
64 def size
65 @options[:uniq] ? count_records : super
66 end
67
68 protected
69 def find_all_records(sql = @finder_sql)
70 records = @association_class.find_by_sql(sql)
71 @options[:uniq] ? uniq(records) : records
72 end
73
74 def count_records
75 load_collection
76 @collection.size
77 end
78
79 def insert_record(record)
80 if @options[:insert_sql]
81 @owner.connection.execute(interpolate_sql(@options[:insert_sql], record))
82 else
83 sql = "INSERT INTO #{@join_table} (#{@association_class_primary_key_name}, #{@association_foreign_key}) VALUES ('#{@owner.id}','#{record.id}')"
84 @owner.connection.execute(sql)
85 end
86 end
87
88 def insert_record_with_join_attributes(record, join_attributes)
ac3c8a5 @dhh Silence errors occurring when reloading classes
dhh authored
89 attributes = { @association_class_primary_key_name => @owner.id, @association_foreign_key => record.id }.update(join_attributes)
db045db @dhh Initial
dhh authored
90 sql =
91 "INSERT INTO #{@join_table} (#{@owner.send(:quoted_column_names, attributes).join(', ')}) " +
92 "VALUES (#{attributes.values.collect { |value| @owner.send(:quote, value) }.join(', ')})"
93 @owner.connection.execute(sql)
94 end
ac3c8a5 @dhh Silence errors occurring when reloading classes
dhh authored
95
db045db @dhh Initial
dhh authored
96 def delete_records(records)
97 if sql = @options[:delete_sql]
98 records.each { |record| @owner.connection.execute(sql) }
99 else
100 ids = quoted_record_ids(records)
101 sql = "DELETE FROM #{@join_table} WHERE #{@association_class_primary_key_name} = '#{@owner.id}' AND #{@association_foreign_key} IN (#{ids})"
102 @owner.connection.execute(sql)
103 end
104 end
105 end
106 end
107 end
Something went wrong with that request. Please try again.