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
Instance Attribute Summary collapse
-
#proxy ⇒ Object
readonly
Returns the value of attribute proxy.
Attributes inherited from Association
Instance Method Summary collapse
-
#<<(*records) ⇒ Object
(also: #push, #concat)
Add
recordsto this association. - #add_record_to_target_with_callbacks(record) {|record| ... } ⇒ Object
- #add_to_target(record) {|record| ... } ⇒ Object
- #build(attributes = {}, &block) ⇒ Object
-
#count(options = {}) ⇒ Object
Count all records using solr.
- #create(attrs = {}) ⇒ Object
- #create!(attrs = {}) ⇒ Object
-
#delete(*records) ⇒ Object
Removes
recordsfrom this association callingbefore_removeandafter_removecallbacks. -
#delete_all ⇒ Object
Remove all records from this association.
-
#destroy(*records) ⇒ Object
Destroy
recordsand remove them from this association callingbefore_removeandafter_removecallbacks. -
#destroy_all ⇒ Object
Remove all records from this association.
- #find(*args) ⇒ Object
- #find_target ⇒ 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
-
#initialize(owner, reflection) ⇒ CollectionAssociation
constructor
A new instance of CollectionAssociation.
- #last(*args) ⇒ Object
- #load_from_solr(opts = Hash.new) ⇒ Object
- #load_target ⇒ Object
- #merge_target_lists(loaded, existing) ⇒ Object
- #null_scope? ⇒ Boolean
-
#reader(opts = false) ⇒ Object
Implements the reader method, e.g.
-
#replace(other_array) ⇒ Object
Replace this collection with
other_arrayThis will perform a diff and delete/add only records that have changed. - #reset ⇒ Object
- #scope(opts = {}) ⇒ Object
-
#size ⇒ Object
Returns the size of the collection .
- #to_ary ⇒ Object (also: #to_a)
-
#writer(records) ⇒ Object
Implements the writer method, e.g.
Methods inherited from Association
#association_scope, #loaded!, #loaded?, #reload, #set_inverse_instance, #stale_target?, #target_scope
Constructor Details
#initialize(owner, reflection) ⇒ CollectionAssociation
Returns a new instance of CollectionAssociation.
6 7 8 9 10 11 |
# File 'lib/active_fedora/associations/collection_association.rb', line 6 def initialize(owner, reflection) super construct_query @proxy = CollectionProxy.new(self) end |
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
#<<(*records) ⇒ Object Also known as: push, concat
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.
137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/active_fedora/associations/collection_association.rb', line 137 def <<(*records) result = true load_target unless loaded? records.flatten.each do |record| raise_on_type_mismatch(record) add_record_to_target_with_callbacks(record) do |r| result &&= insert_record(record) end end result && self end |
#add_record_to_target_with_callbacks(record) {|record| ... } ⇒ Object
271 272 273 274 275 276 277 278 279 280 281 282 283 |
# File 'lib/active_fedora/associations/collection_association.rb', line 271 def add_record_to_target_with_callbacks(record) callback(:before_add, record) yield(record) if block_given? @target ||= [] unless loaded? if index = @target.index(record) @target[index] = record else @target << record end callback(:after_add, record) # set_inverse_instance(record, @owner) record end |
#add_to_target(record) {|record| ... } ⇒ Object
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 |
# File 'lib/active_fedora/associations/collection_association.rb', line 285 def add_to_target(record) # transaction do callback(:before_add, record) yield(record) if block_given? if @reflection.[:uniq] && index = @target.index(record) @target[index] = record else @target << record end callback(:after_add, record) set_inverse_instance(record) # end record end |
#build(attributes = {}, &block) ⇒ Object
123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/active_fedora/associations/collection_association.rb', line 123 def build(attributes = {}, &block) if attributes.is_a?(Array) attributes.collect { |attr| build(attr, &block) } else build_record(attributes) do |record| block.call(record) if block_given? add_to_target(record) set_belongs_to_association_for(record) end end end |
#count(options = {}) ⇒ Object
Count all records using solr. Construct options and pass them with scope to the target class’s count.
215 216 217 |
# File 'lib/active_fedora/associations/collection_association.rb', line 215 def count( = {}) @reflection.klass.count(:conditions => @counter_query) end |
#create(attrs = {}) ⇒ Object
195 196 197 198 199 200 201 202 203 204 |
# File 'lib/active_fedora/associations/collection_association.rb', line 195 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
206 207 208 209 210 211 |
# File 'lib/active_fedora/associations/collection_association.rb', line 206 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.
181 182 183 |
# File 'lib/active_fedora/associations/collection_association.rb', line 181 def delete(*records) delete_or_destroy(records, [:dependent]) end |
#delete_all ⇒ Object
Remove all records from this association
See delete for more info.
157 158 159 160 161 162 |
# File 'lib/active_fedora/associations/collection_association.rb', line 157 def delete_all 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.
190 191 192 193 |
# File 'lib/active_fedora/associations/collection_association.rb', line 190 def destroy(*records) records = find(records) if records.any? { |record| record.kind_of?(Fixnum) || record.kind_of?(String) } delete_or_destroy(records, :destroy) end |
#destroy_all ⇒ Object
Remove all records from this association
See delete for more info.
167 168 169 170 171 172 |
# File 'lib/active_fedora/associations/collection_association.rb', line 167 def destroy_all destroy(load_target).tap do reset loaded! end end |
#find(*args) ⇒ Object
62 63 64 |
# File 'lib/active_fedora/associations/collection_association.rb', line 62 def find(*args) scope.find(*args) end |
#find_target ⇒ Object
237 238 239 240 241 |
# File 'lib/active_fedora/associations/collection_association.rb', line 237 def find_target # TODO: don't reify, just store the solr results and lazily reify. # For now, we set a hard limit of 1000 results. return ActiveFedora::SolrService.reify_solr_results(load_from_solr(rows: 1000)) end |
#first(*args) ⇒ Object
66 67 68 |
# File 'lib/active_fedora/associations/collection_association.rb', line 66 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
36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/active_fedora/associations/collection_association.rb', line 36 def ids_reader if loaded? load_target.map do |record| record.pid end else load_from_solr.map do |solr_record| solr_record['id'] end end end |
#ids_writer(ids) ⇒ Object
Implements the ids writer method, e.g. foo.item_ids= for Foo.has_many :items
49 50 51 52 53 54 55 |
# File 'lib/active_fedora/associations/collection_association.rb', line 49 def ids_writer(ids) ids = Array(ids).reject { |id| id.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
106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/active_fedora/associations/collection_association.rb', line 106 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
70 71 72 |
# File 'lib/active_fedora/associations/collection_association.rb', line 70 def last(*args) first_or_last(:last, *args) end |
#load_from_solr(opts = Hash.new) ⇒ Object
265 266 267 268 269 |
# File 'lib/active_fedora/associations/collection_association.rb', line 265 def load_from_solr(opts = Hash.new) return [] if @finder_query.empty? solr_opts = {rows: opts.delete(:rows) || count} SolrService.query(@finder_query, solr_opts.merge(opts)) end |
#load_target ⇒ Object
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 |
# File 'lib/active_fedora/associations/collection_association.rb', line 219 def load_target if find_target? targets = [] begin targets = find_target rescue ObjectNotFoundError => e ActiveFedora::Base.logger.error "Solr and Fedora may be out of sync:\n" + e. reset end @target = merge_target_lists(targets, @target) end loaded! target end |
#merge_target_lists(loaded, existing) ⇒ Object
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 |
# File 'lib/active_fedora/associations/collection_association.rb', line 243 def merge_target_lists(loaded, existing) return loaded if existing.empty? return existing if loaded.empty? loaded.map do |f| i = existing.index(f) if i existing.delete_at(i).tap do |t| keys = ["id"] + t.changes.keys + (f.attribute_names - t.attribute_names) # FIXME: this call to attributes causes many NoMethodErrors attributes = f.attributes (attributes.keys - keys).each do |k| t.send("#{k}=", attributes[k]) end end else f end end + existing end |
#null_scope? ⇒ Boolean
309 310 311 |
# File 'lib/active_fedora/associations/collection_association.rb', line 309 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
17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/active_fedora/associations/collection_association.rb', line 17 def reader(opts = false) if opts.kind_of?(Hash) if opts.delete(:response_format) == :solr return load_from_solr(opts) end raise ArgumentError, "Hash parameter must include :response_format=>:solr (#{opts.inspect})" else force_reload = opts end reload if force_reload || stale_target? proxy end |
#replace(other_array) ⇒ Object
Replace this collection with other_array This will perform a diff and delete/add only records that have changed.
95 96 97 98 99 100 101 102 103 104 |
# File 'lib/active_fedora/associations/collection_association.rb', line 95 def replace(other_array) other_array.each { |val| raise_on_type_mismatch(val) } load_target other = other_array.size < 100 ? other_array : other_array.to_set current = @target.size < 100 ? @target : @target.to_set delete(@target.select { |v| !other.include?(v) }) concat(other_array.select { |v| !current.include?(v) }) end |
#reset ⇒ Object
57 58 59 60 |
# File 'lib/active_fedora/associations/collection_association.rb', line 57 def reset reset_target! @loaded = false end |
#scope(opts = {}) ⇒ Object
303 304 305 306 307 |
# File 'lib/active_fedora/associations/collection_association.rb', line 303 def scope(opts = {}) scope = super() scope.none! if opts.fetch(:nullify, true) && null_scope? scope 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.
82 83 84 85 86 87 88 89 90 91 |
# File 'lib/active_fedora/associations/collection_association.rb', line 82 def size if @owner.new_record? && @target @target.size elsif !loaded? && @target.is_a?(Array) unsaved_records = @target.select { |r| r.new_record? } unsaved_records.size + count_records else count_records end end |
#to_ary ⇒ Object Also known as: to_a
118 119 120 |
# File 'lib/active_fedora/associations/collection_association.rb', line 118 def to_ary load_target.dup end |
#writer(records) ⇒ Object
Implements the writer method, e.g. foo.items= for Foo.has_many :items
31 32 33 |
# File 'lib/active_fedora/associations/collection_association.rb', line 31 def writer(records) replace(records) end |