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



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

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)


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

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)


168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/composite_primary_keys/base.rb', line 168

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.



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

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.



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

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)


185
186
187
188
189
190
191
192
# File 'lib/composite_primary_keys/base.rb', line 185

def exists?(ids)
  if ids.is_a?(Array) && ids.first.is_a?(String)
    count(:conditions => ids) > 0
  else
    obj = find(ids) rescue false
    !obj.nil? and obj.is_a?(self)            
  end
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”



161
162
163
# File 'lib/composite_primary_keys/base.rb', line 161

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



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

def primary_key; primary_keys; end

#primary_key=(keys) ⇒ Object



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

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

#reset_sequence_nameObject

:nodoc:

Raises:



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

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:



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

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

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

Raises:



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

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