lib/active_fedora/orders/builder.rb in activefedora-aggregation-0.8.1 vs lib/active_fedora/orders/builder.rb in activefedora-aggregation-0.9.0
- old
+ new
@@ -20,13 +20,38 @@
name = :"ordered_#{name.to_s.singularize}_proxies"
options = {ordered_reflection: ordered_reflection}.merge(options)
super
end
+ module FixFirstLast
+ def save(*args)
+ super.tap do |result|
+ if result
+ apply_first_and_last
+ end
+ end
+ end
+ def save!(*args)
+ super.tap do |result|
+ if result
+ apply_first_and_last
+ end
+ end
+ end
+ end
+
def build
- super.tap do
+ super.tap do |result|
model.property :head, predicate: ::RDF::Vocab::IANA['first']
model.property :tail, predicate: ::RDF::Vocab::IANA.last
+ model.send(:define_method, :apply_first_and_last) do
+ source = send(result.options[:through])
+ return if head.map(&:rdf_subject) == source.head_id && tail.map(&:rdf_subject) == source.tail_id
+ self.head = source.head_id
+ self.tail = source.tail_id
+ save! if changed?
+ end
+ model.include ActiveFedora::Orders::Builder::FixFirstLast
end
end
private