Module: Annotations::Acts::AnnotationSource::SingletonMethods

Defined in:
lib/annotations/acts_as_annotation_source.rb

Overview

Class methods added to the model that has been made acts_as_annotation_source (the mixin target class).

Instance Method Summary collapse

Instance Method Details

#annotations_by(id, include_values = false) ⇒ Object

Helper finder to get all annotations for an object of the mixin source type with the ID provided. This is the same as #annotations on the object, with the added benefit that the object doesnt have to be loaded. E.g: User.find_annotations_by(10) will give all annotations by User with ID 34.



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/annotations/acts_as_annotation_source.rb', line 27

def annotations_by(id, include_values=false)
  obj_type = ActiveRecord::Base.send(:class_name_of_active_record_descendant, self).to_s
  
  options = {
    :conditions => { :source_type =>  obj_type, 
                     :source_id => id },
    :order => "updated_at DESC"
  }
  
  options[:include] = [ :value ] if include_values
  
  Annotation.find(:all, options)
end

#annotations_for(annotatable_type, annotatable_id, include_values = false) ⇒ Object

Helper finder to get all annotations for all objects of the mixin source type, for the annotatable object provided. E.g: User.find_annotations_for(‘Book’, 28) will give all annotations made by all Users for Book with ID 28.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/annotations/acts_as_annotation_source.rb', line 43

def annotations_for(annotatable_type, annotatable_id, include_values=false)
  obj_type = ActiveRecord::Base.send(:class_name_of_active_record_descendant, self).to_s
  
  options = {
    :conditions => { :source_type => obj_type,
                     :annotatable_type =>  annotatable_type, 
                     :annotatable_id => annotatable_id },
    :order => "updated_at DESC"
  }
  
  options[:include] = [ :value ] if include_values
  
  Annotation.find(:all, options)
end