Class: ActiveFedora::Relation
- Inherits:
-
Object
- Object
- ActiveFedora::Relation
- Extended by:
- Deprecation
- Defined in:
- lib/active_fedora/relation.rb
Instance Attribute Summary collapse
-
#limit_value ⇒ Object
Returns the value of attribute limit_value.
-
#loaded ⇒ Object
(also: #loaded?)
readonly
Returns the value of attribute loaded.
-
#order_values ⇒ Object
Returns the value of attribute order_values.
-
#where_values ⇒ Object
Returns the value of attribute where_values.
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#all(*args) ⇒ Object
A convenience wrapper for
find(:all, *args)
. -
#count(*args) ⇒ Object
Get a count of the number of objects from solr Takes :conditions as an argument.
- #delete_all(conditions = nil) ⇒ Object
-
#destroy_all(conditions = nil) ⇒ Object
Destroys the records matching
conditions
by instantiating each record and calling itsdestroy
method. -
#find(*args) ⇒ Object
Returns an Array of objects of the Class that
find
is being called on. - #find_some(ids, cast) ⇒ Object
- #find_with_ids(ids, cast) ⇒ Object
-
#first ⇒ Object
Returns the first records that was found.
-
#initialize(klass) ⇒ Relation
constructor
A new instance of Relation.
- #inspect ⇒ Object
-
#last ⇒ Object
Returns the last record sorted by id.
-
#limit(value) ⇒ Object
Limits the number of returned records to the value specified.
-
#order(*args) ⇒ Object
Order the returned records by the field and direction provided.
- #reset ⇒ Object
- #to_a ⇒ Object
-
#where(opts) ⇒ Object
Limits the returned records to those that match the provided search conditions.
Constructor Details
#initialize(klass) ⇒ Relation
Returns a new instance of Relation.
11 12 13 14 15 16 |
# File 'lib/active_fedora/relation.rb', line 11 def initialize(klass) @klass = klass @loaded = false self.where_values = [] self.order_values = [] end |
Instance Attribute Details
#limit_value ⇒ Object
Returns the value of attribute limit_value.
9 10 11 |
# File 'lib/active_fedora/relation.rb', line 9 def limit_value @limit_value end |
#loaded ⇒ Object (readonly) Also known as: loaded?
Returns the value of attribute loaded.
6 7 8 |
# File 'lib/active_fedora/relation.rb', line 6 def loaded @loaded end |
#order_values ⇒ Object
Returns the value of attribute order_values.
9 10 11 |
# File 'lib/active_fedora/relation.rb', line 9 def order_values @order_values end |
#where_values ⇒ Object
Returns the value of attribute where_values.
9 10 11 |
# File 'lib/active_fedora/relation.rb', line 9 def where_values @where_values end |
Instance Method Details
#==(other) ⇒ Object
191 192 193 194 195 196 197 198 |
# File 'lib/active_fedora/relation.rb', line 191 def ==(other) case other when Relation other.where_values == where_values when Array to_a == other end end |
#all(*args) ⇒ Object
A convenience wrapper for find(:all, *args)
. You can pass in all the same arguments to this method as you can to find(:all)
.
158 159 160 |
# File 'lib/active_fedora/relation.rb', line 158 def all(*args) args.any? ? (args.first).to_a : to_a end |
#count(*args) ⇒ Object
Get a count of the number of objects from solr Takes :conditions as an argument
178 179 180 181 182 183 184 185 186 187 |
# File 'lib/active_fedora/relation.rb', line 178 def count(*args) return (args.first).count if args.any? opts = {} opts[:rows] = @limit_value if @limit_value opts[:sort] = @order_values if @order_values query = @where_values.present? ? @where_values : {} @klass.calculate :count, query, opts end |
#delete_all(conditions = nil) ⇒ Object
244 245 246 247 248 249 250 |
# File 'lib/active_fedora/relation.rb', line 244 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
230 231 232 233 234 235 236 237 238 239 240 241 242 |
# File 'lib/active_fedora/relation.rb', line 230 def destroy_all(conditions = nil) if conditions where(conditions).destroy_all else to_a.each {|object| begin object.destroy rescue ActiveFedora::ObjectNotFoundError logger.error "When trying to destroy #{object.pid}, encountered an ObjectNotFoundError. Solr may be out of sync with Fedora" end }.tap { reset }.size end end |
#find(*args) ⇒ Object
Returns an Array of objects of the Class that find
is being called on
@param args either a pid or :all or a hash of conditions
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/active_fedora/relation.rb', line 103 def find(*args) return to_a.find { |*block_args| yield(*block_args) } if block_given? = args. = .dup cast = if @klass == ActiveFedora::Base && !.has_key?(:cast) Deprecation.warn(Relation, "find's cast option will default to true in ActiveFedora 7.0.0. To preserve existing behavior send parameter: `:cast=> false`", caller) false else .delete(:cast) end if [:sort] # Deprecate sort sometime? sort = .delete(:sort) [:order] ||= sort if sort.present? end if .present? = args.first unless args.empty? = {conditions: } ().all else case args.first when :first, :last, :all Deprecation.warn Relation, "ActiveFedora::Base.find(#{args.first.inspect}) is deprecated. Use ActiveFedora::Base.#{args.first} instead. This option will be removed in ActiveFedora 7", caller send(args.first) else find_with_ids(args, cast) end end end |
#find_some(ids, cast) ⇒ Object
152 153 154 |
# File 'lib/active_fedora/relation.rb', line 152 def find_some(ids, cast) ids.map{|id| @klass.find_one(id, cast)} end |
#find_with_ids(ids, cast) ⇒ Object
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/active_fedora/relation.rb', line 135 def find_with_ids(ids, cast) expects_array = ids.first.kind_of?(Array) return ids.first if expects_array && ids.first.empty? ids = ids.flatten.compact.uniq case ids.size when 0 raise ArgumentError, "Couldn't find #{@klass.name} without an ID" when 1 result = @klass.find_one(ids.first, cast) expects_array ? [ result ] : result else find_some(ids, cast) end end |
#first ⇒ Object
Returns the first records that was found.
30 31 32 33 34 35 36 |
# File 'lib/active_fedora/relation.rb', line 30 def first if loaded? @records.first else @first ||= limit(1).to_a[0] end end |
#inspect ⇒ Object
200 201 202 |
# File 'lib/active_fedora/relation.rb', line 200 def inspect to_a.inspect end |
#last ⇒ Object
Returns the last record sorted by id. ID was chosen because this mimics how ActiveRecord would achieve the same behavior.
44 45 46 47 48 49 50 |
# File 'lib/active_fedora/relation.rb', line 44 def last if loaded? @records.last else @last ||= order('id desc').limit(1).to_a[0] end end |
#limit(value) ⇒ Object
Limits the number of returned records to the value specified
59 60 61 62 63 |
# File 'lib/active_fedora/relation.rb', line 59 def limit(value) relation = clone relation.limit_value = value relation end |
#order(*args) ⇒ Object
Order the returned records by the field and direction provided
86 87 88 89 90 91 92 |
# File 'lib/active_fedora/relation.rb', line 86 def order(*args) return self if args.blank? relation = clone relation.order_values += args.flatten relation end |
#reset ⇒ Object
18 19 20 21 22 |
# File 'lib/active_fedora/relation.rb', line 18 def reset @first = @loaded = nil @records = [] self end |
#to_a ⇒ Object
164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/active_fedora/relation.rb', line 164 def to_a return @records if loaded? args = @klass == ActiveFedora::Base ? {:cast=>true} : {} args[:rows] = @limit_value if @limit_value args[:sort] = @order_values if @order_values query = @where_values.present? ? @where_values : {} @records = @klass.to_enum(:find_each, query, args).to_a @records end |
#where(opts) ⇒ Object
Limits the returned records to those that match the provided search conditions
72 73 74 75 76 77 |
# File 'lib/active_fedora/relation.rb', line 72 def where(opts) return self if opts.blank? relation = clone relation.where_values = opts relation end |