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



236
237
238
239
240
241
242
# File 'lib/composite_primary_keys/base.rb', line 236

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)


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

def composite?
  true
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.



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

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_class = 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 (")
  delete_all([ "(#{where_class})" ])
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.



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

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)


202
203
204
205
# File 'lib/composite_primary_keys/base.rb', line 202

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”



195
196
197
# File 'lib/composite_primary_keys/base.rb', line 195

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



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

def primary_key; primary_keys; end

#primary_key=(keys) ⇒ Object



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

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

#reset_sequence_nameObject

:nodoc:

Raises:



252
253
254
# File 'lib/composite_primary_keys/base.rb', line 252

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:



248
249
250
# File 'lib/composite_primary_keys/base.rb', line 248

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

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

Raises:



256
257
258
# File 'lib/composite_primary_keys/base.rb', line 256

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