Class: ActiveFedora::Associations::CollectionAssociation
- Inherits:
-
Association
- Object
- Association
- ActiveFedora::Associations::CollectionAssociation
- Defined in:
- lib/active_fedora/associations/collection_association.rb
Overview
:nodoc:
Direct Known Subclasses
Builder::DirectlyContains, Builder::IndirectlyContains, ContainsAssociation, FilterAssociation, HasAndBelongsToManyAssociation, HasManyAssociation, OrdersAssociation
Instance Attribute Summary collapse
-
#proxy ⇒ Object
readonly
Returns the value of attribute proxy.
Attributes inherited from Association
#inversed, #owner, #reflection, #target
Instance Method Summary collapse
- #add_to_target(record, skip_callbacks = false) {|record| ... } ⇒ Object
- #any? ⇒ Boolean
- #build(attributes = {}, &block) ⇒ Object
-
#concat(*records) ⇒ Object
Add
records
to this association. - #concat_records(*records) ⇒ Object
-
#count(_options = {}) ⇒ Object
Count all records using solr.
- #create(attrs = {}) ⇒ Object
- #create!(attrs = {}) ⇒ Object
-
#delete(*records) ⇒ Object
Removes
records
from this association callingbefore_remove
andafter_remove
callbacks. -
#delete_all ⇒ Object
Remove all records from this association.
-
#destroy(*records) ⇒ Object
Destroy
records
and remove them from this association callingbefore_remove
andafter_remove
callbacks. -
#destroy_all ⇒ Object
Remove all records from this association.
-
#empty? ⇒ Boolean
Returns true if the collection is empty.
- #find(*args) ⇒ Object
- #first(*args) ⇒ Object
-
#ids_reader ⇒ Object
Implements the ids reader method, e.g.
-
#ids_writer(ids) ⇒ Object
Implements the ids writer method, e.g.
- #include?(record) ⇒ Boolean
- #last(*args) ⇒ Object
- #load_from_solr(opts = {}) ⇒ Object
- #load_target ⇒ Object
- #null_scope? ⇒ Boolean
-
#reader(opts = false) ⇒ Object
Implements the reader method, e.g.
-
#replace(other_array) ⇒ Object
Replace this collection with
other_array
This will perform a diff and delete/add only records that have changed. - #reset ⇒ Object
- #scope(opts = {}) ⇒ Object
- #select(_select = nil, &block) ⇒ Object
-
#size ⇒ Object
Returns the size of the collection.
-
#target=(target) ⇒ Object
Sets the target of this proxy to
\target
, and the loaded flag totrue
. - #to_ary ⇒ Object (also: #to_a)
-
#writer(records) ⇒ Object
Implements the writer method, e.g.
Methods inherited from Association
#association_scope, #initialize, #initialize_attributes, #loaded!, #loaded?, #reload, #reset_scope, #set_inverse_instance, #stale_target?, #target_scope
Constructor Details
This class inherits a constructor from ActiveFedora::Associations::Association
Instance Attribute Details
#proxy ⇒ Object (readonly)
Returns the value of attribute proxy.
4 5 6 |
# File 'lib/active_fedora/associations/collection_association.rb', line 4 def proxy @proxy end |
Instance Method Details
#add_to_target(record, skip_callbacks = false) {|record| ... } ⇒ Object
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 |
# File 'lib/active_fedora/associations/collection_association.rb', line 257 def add_to_target(record, skip_callbacks = false) # Start transaction callback(:before_add, record) unless skip_callbacks yield(record) if block_given? if @reflection.[:uniq] && index = @target.index(record) @target[index] = record else @target << record end callback(:after_add, record) unless skip_callbacks set_inverse_instance(record) # End transaction record end |
#any? ⇒ Boolean
126 127 128 129 130 131 132 |
# File 'lib/active_fedora/associations/collection_association.rb', line 126 def any? if block_given? load_target.any? { |*block_args| yield(*block_args) } else !empty? end end |
#build(attributes = {}, &block) ⇒ Object
139 140 141 142 143 144 145 146 147 |
# File 'lib/active_fedora/associations/collection_association.rb', line 139 def build(attributes = {}, &block) if attributes.is_a?(Array) attributes.collect { |attr| build(attr, &block) } else add_to_target(build_record(attributes)) do |record| yield(record) if block_given? end end end |
#concat(*records) ⇒ Object
Add records
to this association. Returns self
so method calls may be chained. Since << flattens its argument list and inserts each record, push
and concat
behave identically.
151 152 153 154 |
# File 'lib/active_fedora/associations/collection_association.rb', line 151 def concat(*records) load_target unless owner.new_record? concat_records(records) end |
#concat_records(*records) ⇒ Object
156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/active_fedora/associations/collection_association.rb', line 156 def concat_records(*records) result = true records.flatten.each do |record| raise_on_type_mismatch!(record) add_to_target(record) do |_r| result &&= insert_record(record) unless owner.new_record? end end result && records end |
#count(_options = {}) ⇒ Object
Count all records using solr. Construct options and pass them with scope to the target class’s count
.
231 232 233 |
# File 'lib/active_fedora/associations/collection_association.rb', line 231 def count( = {}) scope.count end |
#create(attrs = {}) ⇒ Object
211 212 213 214 215 216 217 218 219 220 |
# File 'lib/active_fedora/associations/collection_association.rb', line 211 def create(attrs = {}) if attrs.is_a?(Array) attrs.collect { |attr| create(attr) } else _create_record(attrs) do |record| yield(record) if block_given? record.save end end end |
#create!(attrs = {}) ⇒ Object
222 223 224 225 226 227 |
# File 'lib/active_fedora/associations/collection_association.rb', line 222 def create!(attrs = {}) _create_record(attrs) do |record| yield(record) if block_given? record.save! end end |
#delete(*records) ⇒ Object
Removes records
from this association calling before_remove
and after_remove
callbacks.
This method is abstract in the sense that delete_records
has to be provided by descendants. Note this method does not imply the records are actually removed from the database, that depends precisely on delete_records
. They are in any case removed from the collection.
197 198 199 |
# File 'lib/active_fedora/associations/collection_association.rb', line 197 def delete(*records) delete_or_destroy(records, [:dependent]) end |
#delete_all ⇒ Object
Remove all records from this association
See delete for more info.
172 173 174 175 176 177 178 |
# File 'lib/active_fedora/associations/collection_association.rb', line 172 def delete_all # TODO: load_target causes extra loads. Can't we just send delete requests? delete(load_target).tap do reset loaded! end end |
#destroy(*records) ⇒ Object
Destroy records
and remove them from this association calling before_remove
and after_remove
callbacks.
Note that this method will always remove records from the database ignoring the :dependent
option.
206 207 208 209 |
# File 'lib/active_fedora/associations/collection_association.rb', line 206 def destroy(*records) records = find(records) if records.any? { |record| record.is_a?(Integer) || record.is_a?(String) } delete_or_destroy(records, :destroy) end |
#destroy_all ⇒ Object
Remove all records from this association
See delete for more info.
183 184 185 186 187 188 |
# File 'lib/active_fedora/associations/collection_association.rb', line 183 def destroy_all destroy(load_target).tap do reset loaded! end end |
#empty? ⇒ Boolean
Returns true if the collection is empty.
If the collection has been loaded it is equivalent to collection. size.zero?
. If the collection has not been loaded, it is equivalent to collection.count_records == 0
. If the collection has not already been loaded and you are going to fetch the records anyway it is better to check collection.length.zero?
.
93 94 95 96 97 98 99 |
# File 'lib/active_fedora/associations/collection_association.rb', line 93 def empty? if loaded? size.zero? else @target.blank? && count_records.zero? end end |
#find(*args) ⇒ Object
55 56 57 |
# File 'lib/active_fedora/associations/collection_association.rb', line 55 def find(*args) scope.find(*args) end |
#first(*args) ⇒ Object
59 60 61 |
# File 'lib/active_fedora/associations/collection_association.rb', line 59 def first(*args) first_or_last(:first, *args) end |
#ids_reader ⇒ Object
Implements the ids reader method, e.g. foo.item_ids for Foo.has_many :items it discards any ids where the record it belongs to was marked for destruction.
33 34 35 36 37 38 39 |
# File 'lib/active_fedora/associations/collection_association.rb', line 33 def ids_reader if loaded? load_target.reject(&:marked_for_destruction?).map(&:id) else load_from_solr.map(&:id) end end |
#ids_writer(ids) ⇒ Object
Implements the ids writer method, e.g. foo.item_ids= for Foo.has_many :items
42 43 44 45 46 47 48 |
# File 'lib/active_fedora/associations/collection_association.rb', line 42 def ids_writer(ids) ids = Array(ids).reject(&:blank?) replace(klass.find(ids)) # .index_by { |r| r.id }.values_at(*ids)) # TODO, like this when find() can return multiple records # send("#{reflection.name}=", reflection.klass.find(ids)) # send("#{reflection.name}=", ids.collect { |id| reflection.klass.find(id)}) end |
#include?(record) ⇒ Boolean
114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/active_fedora/associations/collection_association.rb', line 114 def include?(record) if record.is_a?(reflection.klass) if record.new_record? target.include?(record) else loaded? ? target.include?(record) : scope.exists?(record) end else false end end |
#last(*args) ⇒ Object
63 64 65 |
# File 'lib/active_fedora/associations/collection_association.rb', line 63 def last(*args) first_or_last(:last, *args) end |
#load_from_solr(opts = {}) ⇒ Object
249 250 251 252 253 254 255 |
# File 'lib/active_fedora/associations/collection_association.rb', line 249 def load_from_solr(opts = {}) finder_query = construct_query return [] if finder_query.empty? rows = opts.delete(:rows) { count } return [] if rows.zero? SolrService.query(finder_query, { rows: rows }.merge(opts)) end |
#load_target ⇒ Object
241 242 243 244 245 246 |
# File 'lib/active_fedora/associations/collection_association.rb', line 241 def load_target @target = merge_target_lists(find_target, @target) if find_target? loaded! target end |
#null_scope? ⇒ Boolean
281 282 283 |
# File 'lib/active_fedora/associations/collection_association.rb', line 281 def null_scope? owner.new_record? && !foreign_key_present? end |
#reader(opts = false) ⇒ Object
Implements the reader method, e.g. foo.items for Foo.has_many :items
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/active_fedora/associations/collection_association.rb', line 9 def reader(opts = false) if opts.is_a?(Hash) return load_from_solr(opts) if opts.delete(:response_format) == :solr raise ArgumentError, "Hash parameter must include :response_format=>:solr (#{opts.inspect})" else force_reload = opts end reload if force_reload || stale_target? if null_scope? # Cache the proxy separately before the owner has an id # or else a post-save proxy will still lack the id @null_proxy ||= CollectionProxy.new(self) else @proxy ||= CollectionProxy.new(self) end end |
#replace(other_array) ⇒ Object
Replace this collection with other_array
This will perform a diff and delete/add only records that have changed.
103 104 105 106 107 108 109 110 111 112 |
# File 'lib/active_fedora/associations/collection_association.rb', line 103 def replace(other_array) other_array.each { |val| raise_on_type_mismatch!(val) } load_target deletions = @target - other_array additions = other_array - @target delete(deletions) concat(additions) end |
#reset ⇒ Object
50 51 52 53 |
# File 'lib/active_fedora/associations/collection_association.rb', line 50 def reset super @target = [] end |
#scope(opts = {}) ⇒ Object
275 276 277 278 279 |
# File 'lib/active_fedora/associations/collection_association.rb', line 275 def scope(opts = {}) scope = super() scope.none! if opts.fetch(:nullify, true) && null_scope? scope end |
#select(_select = nil, &block) ⇒ Object
285 286 287 |
# File 'lib/active_fedora/associations/collection_association.rb', line 285 def select(_select = nil, &block) to_a.select(&block) end |
#size ⇒ Object
Returns the size of the collection
If the collection has been already loaded size
and length
are equivalent. If not and you are going to need the records anyway length
will take one less query. Otherwise size
is more efficient.
This method is abstract in the sense that it relies on count_records
, which is a method descendants have to provide.
75 76 77 78 79 80 81 82 83 84 |
# File 'lib/active_fedora/associations/collection_association.rb', line 75 def size if !find_target? || loaded? target.size elsif !loaded? && target.is_a?(Array) unsaved_records = target.select(&:new_record?) unsaved_records.size + count_records else count_records end end |
#target=(target) ⇒ Object
Sets the target of this proxy to \target
, and the loaded flag to true
.
236 237 238 239 |
# File 'lib/active_fedora/associations/collection_association.rb', line 236 def target=(target) @target = [target] loaded! end |
#to_ary ⇒ Object Also known as: to_a
134 135 136 |
# File 'lib/active_fedora/associations/collection_association.rb', line 134 def to_ary load_target.dup end |
#writer(records) ⇒ Object
Implements the writer method, e.g. foo.items= for Foo.has_many :items
27 28 29 |
# File 'lib/active_fedora/associations/collection_association.rb', line 27 def writer(records) replace(records) end |