Class: ActiveFedora::Relation

Inherits:
Object
  • Object
show all
Includes:
Calculations, Delegation, FinderMethods, QueryMethods, SpawnMethods, Enumerable
Defined in:
lib/active_fedora/relation.rb,
lib/active_fedora/relation/merger.rb

Defined Under Namespace

Classes: HashMerger, Merger

Constant Summary

Constants included from Delegation

Delegation::BLACKLISTED_ARRAY_METHODS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from FinderMethods

#exists?, #find, #find_each, #find_in_batches, #find_one, #find_take, #find_with_conditions, #first, #last, #take

Methods included from Calculations

#calculate, #count

Methods included from SpawnMethods

#merge, #merge!, #spawn

Methods included from QueryMethods

#build_where, #create_with_value, #extending!, #extending_values, #extending_values=, #limit, #limit!, #limit_value, #limit_value=, #none!, #offset, #offset!, #offset_value, #offset_value=, #order, #order!, #order_values, #order_values=, #select, #where, #where!, #where_values, #where_values=

Constructor Details

#initialize(klass, values = {}) ⇒ Relation

Returns a new instance of Relation.



14
15
16
17
18
# File 'lib/active_fedora/relation.rb', line 14

def initialize(klass, values = {})
  @klass = klass
  @loaded = false
  @values = {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class ActiveFedora::Delegation

Instance Attribute Details

#default_scopedObject

Returns the value of attribute default_scoped.



9
10
11
# File 'lib/active_fedora/relation.rb', line 9

def default_scoped
  @default_scoped
end

#klassObject

Returns the value of attribute klass.



12
13
14
# File 'lib/active_fedora/relation.rb', line 12

def klass
  @klass
end

#loadedObject (readonly) Also known as: loaded?

Returns the value of attribute loaded.



8
9
10
# File 'lib/active_fedora/relation.rb', line 8

def loaded
  @loaded
end

#valuesObject

Returns the value of attribute values.



12
13
14
# File 'lib/active_fedora/relation.rb', line 12

def values
  @values
end

Instance Method Details

#==(other) ⇒ Object



68
69
70
71
72
73
74
75
# File 'lib/active_fedora/relation.rb', line 68

def ==(other)
  case other
  when Relation
    other.where_values == where_values
  when Array
    to_a == other
  end
end

#create(*args, &block) ⇒ Object

Tries to create a new record with the same scoped attributes defined in the relation. Returns the initialized object if validation fails.

Expects arguments in the same format as Base.create.

Examples

users = User.where(name: 'Oscar')
users.create # #<User id: 3, name: "oscar", ...>

users.create(name: 'fxn')
users.create # #<User id: 4, name: "fxn", ...>

users.create { |user| user.name = 'tenderlove' }
# #<User id: 5, name: "tenderlove", ...>

users.create(name: nil) # validation on name
# #<User id: nil, name: nil, ...>


44
45
46
# File 'lib/active_fedora/relation.rb', line 44

def create(*args, &block)
  scoping { @klass.create(*args, &block) }
end

#delete_all(conditions = nil) ⇒ Object



116
117
118
119
120
121
122
# File 'lib/active_fedora/relation.rb', line 116

def delete_all(conditions = nil)
  if conditions
    where(conditions).delete_all
  else
    to_a.each {|object| object.delete }.tap { reset }.size
  end
end

#destroy_all(conditions = nil) ⇒ Object

Destroys the records matching conditions by instantiating each record and calling its destroy method. Each object’s callbacks are executed (including :dependent association options and before_destroy/after_destroy Observer methods). Returns the collection of objects that were destroyed; each will be frozen, to reflect that no changes should be made (since they can’t be persisted).

Note: Instantiation, callback execution, and deletion of each record can be time consuming when you’re removing many records at once. It generates at least one fedora DELETE query per record (or possibly more, to enforce your callbacks). If you want to delete many rows quickly, without concern for their associations or callbacks, use delete_all instead.

Parameters

  • conditions - A string, array, or hash that specifies which records to destroy. If omitted, all records are destroyed. See the Conditions section in the ActiveFedora::Relation#where for more information.

Examples

Person.destroy_all(:status_s => "inactive")
Person.where(:age_i => 18).destroy_all


108
109
110
111
112
113
114
# File 'lib/active_fedora/relation.rb', line 108

def destroy_all(conditions = nil)
  if conditions
    where(conditions).destroy_all
  else
    to_a.each {|object| object.destroy }.tap { reset }.size
  end
end

#initialize_copy(other) ⇒ Object

This method gets called on clone



21
22
23
24
25
# File 'lib/active_fedora/relation.rb', line 21

def initialize_copy(other)
  # Dup the values
  @values = Hash[@values]
  reset
end

#inspectObject



77
78
79
80
# File 'lib/active_fedora/relation.rb', line 77

def inspect
  to_a.inspect
  # "<#{self.class} @klass=\"#{@klass}\" @values=\"#{@values.inspect}\">"
end

#resetObject



48
49
50
51
52
# File 'lib/active_fedora/relation.rb', line 48

def reset
  @first = @loaded = nil
  @records = []
  self
end

#scope_for_createObject



132
133
134
# File 'lib/active_fedora/relation.rb', line 132

def scope_for_create
  @scope_for_create ||= where_values_hash.merge(create_with_value)
end

#to_aObject



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/active_fedora/relation.rb', line 54

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

#where_values_hashObject

Returns a hash of where conditions.

User.where(name: 'Oscar').where_values_hash
# => {name: "Oscar"}


128
129
130
# File 'lib/active_fedora/relation.rb', line 128

def where_values_hash
  {}
end