lib/kentouzu/has_drafts.rb in kentouzu-0.0.9 vs lib/kentouzu/has_drafts.rb in kentouzu-0.0.10
- old
+ new
@@ -45,11 +45,11 @@
define_singleton_method "new_#{drafts_association_name.to_s}".to_sym do
Draft.where(:item_type => self.name, :event => 'create')
end
- define_singleton_method "all_with_reified_#{drafts_association_name.to_s}".to_sym do |order_by = Kentouzu.timestamp_field|
+ define_singleton_method "all_with_reified_#{drafts_association_name.to_s}".to_sym do |order_by = Kentouzu.timestamp_field, &block|
existing_drafts = Draft.where("`drafts`.`item_type` = \"#{self.name}\" AND `drafts`.`item_id` IS NOT NULL").group_by { |draft| draft.item_id }.map { |k, v| v.sort_by { |draft| draft.created_at }.first }
new_drafts = Draft.where("`drafts`.`item_type` = \"#{self.name}\" AND `drafts`.`item_id` IS NULL")
existing_reified_objects = existing_drafts.map { |draft| draft.reify }
@@ -64,11 +64,11 @@
existing_objects = self.all.reject { |object| existing_reified_objects.map { |reified_object| reified_object.id }.include? object.id }
all_objects = (existing_objects + existing_reified_objects + new_reified_objects).sort_by { |object| object.send order_by }
- if block_given?
- all_objects.select! { |object| yield object }
+ if block
+ all_objects.select! { |object| block.call(object) }
end
all_objects
end