Module: CompositePrimaryKeys::ActiveRecord::Base::CompositeClassMethods

Defined in:
lib/composite_primary_keys/base.rb

Instance Method Summary collapse

Instance Method Details

#columnsObject

Returns an array of column objects for the table associated with this class. Each column that matches to one of the primary keys has its primary attribute set to true



222
223
224
225
226
227
228
# File 'lib/composite_primary_keys/base.rb', line 222

def columns
  unless @columns
    @columns = connection.columns(table_name, "#{name} Columns")
    @columns.each {|column| column.primary = primary_keys.include?(column.name.to_sym)}
  end
  @columns
end

#composite?Boolean

Returns:

  • (Boolean)


158
159
160
# File 'lib/composite_primary_keys/base.rb', line 158

def composite?
  true
end

#composite_where_clause(ids) ⇒ Object

Creates WHERE condition from list of composited ids

User.update_all({:role => 'admin'}, :conditions => composite_where_clause([[1, 2], [2, 2]])) #=> UPDATE admins SET admin.role='admin' WHERE (admin.type=1 AND admin.type2=2) OR (admin.type=2 AND admin.type2=2)
User.find(:all, :conditions => composite_where_clause([[1, 2], [2, 2]])) #=> SELECT * FROM admins WHERE (admin.type=1 AND admin.type2=2) OR (admin.type=2 AND admin.type2=2)


171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/composite_primary_keys/base.rb', line 171

def composite_where_clause(ids)
  if ids.is_a?(String)
    ids = [[ids]]
  elsif not ids.first.is_a?(Array) # if single comp key passed, turn into an array of 1
    ids = [ids.to_composite_ids]
  end
  
  ids.map do |id_set|
    [primary_keys, id_set].transpose.map do |key, id|
      "#{table_name}.#{key.to_s}=#{sanitize(id)}"
    end.join(" AND ")
  end.join(") OR (")       
end

#delete(*ids) ⇒ Object

Deletes the record with the given ids without instantiating an object first, e.g. delete(1,2) If an array of ids is provided (e.g. delete(, [3,4]), all of them are deleted.



196
197
198
199
200
201
202
203
204
205
# File 'lib/composite_primary_keys/base.rb', line 196

def delete(*ids)
  unless ids.is_a?(Array); raise "*ids must be an Array"; end
  ids = [ids.to_composite_ids] if not ids.first.is_a?(Array)
  where_clause = ids.map do |id_set|
    [primary_keys, id_set].transpose.map do |key, id|
      "#{quoted_table_name}.#{connection.quote_column_name(key.to_s)}=#{sanitize(id)}"
    end.join(" AND ")
  end.join(") OR (")
  delete_all([ "(#{where_clause})" ])
end

#destroy(*ids) ⇒ Object

Destroys the record with the given ids by instantiating the object and calling #destroy (all the callbacks are the triggered). If an array of ids is provided, all of them are destroyed.



209
210
211
212
213
214
215
216
217
# File 'lib/composite_primary_keys/base.rb', line 209

def destroy(*ids)
  unless ids.is_a?(Array); raise "*ids must be an Array"; end
  if ids.first.is_a?(Array)
    ids = ids.map{|compids| compids.to_composite_ids}
  else
    ids = ids.to_composite_ids
  end
  ids.first.is_a?(CompositeIds) ? ids.each { |id_set| find(id_set).destroy } : find(ids).destroy
end

#exists?(ids) ⇒ Boolean

Returns true if the given ids represents the primary keys of a record in the database, false otherwise. Example:

Person.exists?(5,7)

Returns:

  • (Boolean)


188
189
190
191
# File 'lib/composite_primary_keys/base.rb', line 188

def exists?(ids)
  obj = find(ids) rescue false
  !obj.nil? and obj.is_a?(self)
end

#ids_to_s(many_ids, id_sep = CompositePrimaryKeys::ID_SEP, list_sep = ',', left_bracket = '(', right_bracket = ')') ⇒ Object

ids_to_s([,[7,3]]) -> “(1,2),(7,3)” ids_to_s([,[7,3]], ‘,’, ‘;’) -> “1,2;7,3”



164
165
166
# File 'lib/composite_primary_keys/base.rb', line 164

def ids_to_s(many_ids, id_sep = CompositePrimaryKeys::ID_SEP, list_sep = ',', left_bracket = '(', right_bracket = ')')
  many_ids.map {|ids| "#{left_bracket}#{ids}#{right_bracket}"}.join(list_sep)
end

#primary_keyObject



155
# File 'lib/composite_primary_keys/base.rb', line 155

def primary_key; primary_keys; end

#primary_key=(keys) ⇒ Object



156
# File 'lib/composite_primary_keys/base.rb', line 156

def primary_key=(keys); primary_keys = keys; end

#reset_sequence_nameObject

:nodoc:

Raises:



238
239
240
# File 'lib/composite_primary_keys/base.rb', line 238

def reset_sequence_name #:nodoc:
  raise CompositeKeyError, CompositePrimaryKeys::ActiveRecord::Base::INVALID_FOR_COMPOSITE_KEYS
end

#sequence_nameObject

Lazy-set the sequence name to the connection’s default. This method is only ever called once since set_sequence_name overrides it.

Raises:



234
235
236
# File 'lib/composite_primary_keys/base.rb', line 234

def sequence_name #:nodoc:
  raise CompositeKeyError, CompositePrimaryKeys::ActiveRecord::Base::INVALID_FOR_COMPOSITE_KEYS
end

#set_primary_key(value = nil, &block) ⇒ Object

Raises:



242
243
244
# File 'lib/composite_primary_keys/base.rb', line 242

def set_primary_key(value = nil, &block)
  raise CompositeKeyError, CompositePrimaryKeys::ActiveRecord::Base::INVALID_FOR_COMPOSITE_KEYS
end