Module: CompositePrimaryKeys::ActiveRecord::Base::CompositeClassMethods
- Defined in:
- lib/composite_primary_keys/base.rb
Instance Method Summary collapse
-
#columns ⇒ Object
Returns an array of column objects for the table associated with this class.
- #composite? ⇒ Boolean
-
#delete(*ids) ⇒ Object
Deletes the record with the given
ids
without instantiating an object first, e.g. -
#destroy(*ids) ⇒ Object
Destroys the record with the given
ids
by instantiating the object and calling #destroy (all the callbacks are the triggered). -
#exists?(ids) ⇒ Boolean
Returns true if the given
ids
represents the primary keys of a record in the database, false otherwise. -
#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”.
- #primary_key ⇒ Object
- #primary_key=(keys) ⇒ Object
-
#reset_sequence_name ⇒ Object
:nodoc:.
-
#sequence_name ⇒ Object
Lazy-set the sequence name to the connection’s default.
- #set_primary_key(value = nil, &block) ⇒ Object
Instance Method Details
#columns ⇒ Object
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
221 222 223 224 225 226 227 |
# File 'lib/composite_primary_keys/base.rb', line 221 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
179 180 181 |
# File 'lib/composite_primary_keys/base.rb', line 179 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.
200 201 202 203 204 |
# File 'lib/composite_primary_keys/base.rb', line 200 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) delete_all([ "(#{primary_keys}) IN (#{ids_to_s(ids)})" ]) 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.
208 209 210 211 212 213 214 215 216 |
# File 'lib/composite_primary_keys/base.rb', line 208 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)
192 193 194 195 |
# File 'lib/composite_primary_keys/base.rb', line 192 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”
185 186 187 |
# File 'lib/composite_primary_keys/base.rb', line 185 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_key ⇒ Object
176 |
# File 'lib/composite_primary_keys/base.rb', line 176 def primary_key; primary_keys; end |
#primary_key=(keys) ⇒ Object
177 |
# File 'lib/composite_primary_keys/base.rb', line 177 def primary_key=(keys); primary_keys = keys; end |
#reset_sequence_name ⇒ Object
:nodoc:
237 238 239 |
# File 'lib/composite_primary_keys/base.rb', line 237 def reset_sequence_name #:nodoc: raise CompositeKeyError, CompositePrimaryKeys::ActiveRecord::Base::INVALID_FOR_COMPOSITE_KEYS end |
#sequence_name ⇒ Object
Lazy-set the sequence name to the connection’s default. This method is only ever called once since set_sequence_name overrides it.
233 234 235 |
# File 'lib/composite_primary_keys/base.rb', line 233 def sequence_name #:nodoc: raise CompositeKeyError, CompositePrimaryKeys::ActiveRecord::Base::INVALID_FOR_COMPOSITE_KEYS end |
#set_primary_key(value = nil, &block) ⇒ Object
241 242 243 |
# File 'lib/composite_primary_keys/base.rb', line 241 def set_primary_key(value = nil, &block) raise CompositeKeyError, CompositePrimaryKeys::ActiveRecord::Base::INVALID_FOR_COMPOSITE_KEYS end |