class ActiveFedora::Relation
def ==(other)
def ==(other) case other when Relation other.where_values == where_values when Array to_a == other end end
def apply_finder_options(options)
def apply_finder_options(options) relation = clone return relation unless options options.assert_valid_keys(VALID_FIND_OPTIONS) finders = options.dup finders.delete_if { |key, value| value.nil? && key != :limit } ([:order,:limit,:start] & finders.keys).each do |finder| relation = relation.send(finder, finders[finder]) end relation = relation.where(finders[:conditions]) if options.has_key?(:conditions) relation end
def create(*args, &block)
users.create(name: nil) # validation on name
# #
users.create { |user| user.name = 'tenderlove' }
users.create # #
users.create(name: 'fxn')
users.create # #
users = User.where(name: 'Oscar')
==== Examples
Expects arguments in the same format as +Base.create+.
defined in the relation. Returns the initialized object if validation fails.
Tries to create a new record with the same scoped attributes
def create(*args, &block) scoping { @klass.create(*args, &block) } end
def delete_all(conditions = nil)
def delete_all(conditions = nil) if conditions where(conditions).delete_all else to_a.each {|object| object.delete }.tap { reset }.size end end
def destroy_all(conditions = nil)
Person.destroy_all(:status_s => "inactive")
==== Examples
more information.
Conditions section in the ActiveFedora::Relation#where for
to destroy. If omitted, all records are destroyed. See the
* +conditions+ - A string, array, or hash that specifies which records
==== Parameters
+delete_all+ instead.
rows quickly, without concern for their associations or callbacks, use
possibly more, to enforce your callbacks). If you want to delete many
once. It generates at least one fedora +DELETE+ query per record (or
record can be time consuming when you're removing many records at
Note: Instantiation, callback execution, and deletion of each
persisted).
reflect that no changes should be made (since they can't be
collection of objects that were destroyed; each will be frozen, to
+before_destroy+/+after_destroy+ Observer methods). Returns the
executed (including :dependent association options and
record and calling its +destroy+ method. Each object's callbacks are
Destroys the records matching +conditions+ by instantiating each
def destroy_all(conditions = nil) if conditions where(conditions).destroy_all else to_a.each {|object| object.destroy }.tap { reset }.size end end
def initialize(klass, values = {})
def initialize(klass, values = {}) @klass = klass @loaded = false @values = {} end
def initialize_copy(other)
def initialize_copy(other) # Dup the values @values = Hash[@values] reset end
def inspect
def inspect to_a.inspect # "<#{self.class} @klass=\"#{@klass}\" @values=\"#{@values.inspect}\">" end
def reset
def reset @first = @loaded = nil @records = [] self end
def scope_for_create
def scope_for_create @scope_for_create ||= where_values_hash.merge(create_with_value) end
def to_a
def to_a return @records if loaded? args = @klass == ActiveFedora::Base ? {:cast=>true} : {} args[:rows] = limit_value if limit_value args[:start] = offset_value if offset_value args[:sort] = order_values if order_values @records = to_enum(:find_each, where_values, args).to_a @loaded = true @records end
def where_values_hash
User.where(name: 'Oscar').where_values_hash
Returns a hash of where conditions.
def where_values_hash {} end