This module contains instance methods
Finder to get all annotations on this object excluding those that have the attribute names specified.
NOTE (1): the argument to this method MUST be an Array of Strings. NOTE (2): the returned records will be Read Only.
# File lib/annotations/acts_as_annotatable.rb, line 146 146: def all_annotations_excluding_attributes(attribs) 147: return [] if attribs.blank? 148: 149: obj_type = ActiveRecord::Base.send(:class_name_of_active_record_descendant, self.class).to_s 150: 151: Annotation.find(:all, 152: :joins => :attribute, 153: :conditions => [ "`annotations`.`annotatable_type` = ? AND `annotations`.`annotatable_id` = ? AND `annotation_attributes`.`name` NOT IN (?)", 154: obj_type, 155: self.id, 156: attribs ], 157: :order => "`annotations`.`created_at` DESC") 158: end
Provides a default implementation to get the display name for an annotatable object, that can be overrided.
# File lib/annotations/acts_as_annotatable.rb, line 71 71: def annotatable_name 72: %w{ display_name title name }.each do |w| 73: return eval("self.#{w}") if self.respond_to?(w) 74: end 75: return "#{self.class.name}_#{id}" 76: end
Finder to get annotations with a specific attribute. The input parameter is the attribute name (MUST be a String representing the attribute’s name).
# File lib/annotations/acts_as_annotatable.rb, line 92 92: def annotations_with_attribute(attrib) 93: return [] if attrib.blank? 94: 95: obj_type = ActiveRecord::Base.send(:class_name_of_active_record_descendant, self.class).to_s 96: 97: Annotation.find(:all, 98: :joins => :attribute, 99: :conditions => { :annotatable_type => obj_type, 100: :annotatable_id => self.id, 101: :annotation_attributes => { :name => attrib.strip.downcase } }, 102: :order => "created_at DESC") 103: end
Finder to get annotations with a specific attribute by a specific source.
The first input parameter is the attribute name (MUST be a String representing the attribute’s name). The second input is the source object.
# File lib/annotations/acts_as_annotatable.rb, line 126 126: def annotations_with_attribute_and_by_source(attrib, source) 127: return [] if attrib.blank? or source.nil? 128: 129: obj_type = ActiveRecord::Base.send(:class_name_of_active_record_descendant, self.class).to_s 130: 131: Annotation.find(:all, 132: :joins => :attribute, 133: :conditions => { :annotatable_type => obj_type, 134: :annotatable_id => self.id, 135: :source_type => source.class.name, 136: :source_id => source.id, 137: :annotation_attributes => { :name => attrib.strip.downcase } }, 138: :order => "created_at DESC") 139: end
Same as the {obj}.annotations_with_attribute method (above) but takes in an array for attribute names to look for.
NOTE (1): the argument to this method MUST be an Array of Strings.
# File lib/annotations/acts_as_annotatable.rb, line 109 109: def annotations_with_attributes(attribs) 110: return [] if attribs.blank? 111: 112: obj_type = ActiveRecord::Base.send(:class_name_of_active_record_descendant, self.class).to_s 113: 114: Annotation.find(:all, 115: :joins => :attribute, 116: :conditions => { :annotatable_type => obj_type, 117: :annotatable_id => self.id, 118: :annotation_attributes => { :name => attribs } }, 119: :order => "created_at DESC") 120: end
Returns the number of annotations on this annotatable object by the source type specified. “all” (case insensitive) can be provided to get all annotations regardless of source type. E.g.: book.count_annotations_by(“User”) or book.count_annotations_by(“All”)
# File lib/annotations/acts_as_annotatable.rb, line 163 163: def count_annotations_by(source_type_in) 164: if source_type_in == nil || source_type_in.downcase == "all" 165: return self.annotations.count 166: else 167: return self.annotations.count(:conditions => { :source_type => source_type_in }) 168: end 169: end
Use this method to create many annotations from a Hash of data. Arrays for Hash values will be converted to multiple annotations. Blank values (nil or empty string) will be ignored and thus annotations will not be created for them.
Returns an array of Annotation objects of the annotations that were successfully created.
Code example:
data = { “tag” => [ “tag1”, “tag2”, “tag3” ], “description” => “This is a book” } book.create_annotations(data, current_user)
# File lib/annotations/acts_as_annotatable.rb, line 183 183: def create_annotations(annotations_data, source) 184: anns = [ ] 185: 186: annotations_data.each do |attrib, val| 187: unless val.blank? 188: if val.is_a? Array 189: val.each do |val_inner| 190: unless val_inner.blank? 191: ann = self.annotations << Annotation.new(:attribute_name => attrib, 192: :value => val_inner, 193: :source_type => source.class.name, 194: :source_id => source.id) 195: 196: unless ann.nil? || ann == false 197: anns << ann 198: end 199: end 200: end 201: else 202: ann = self.annotations << Annotation.new(:attribute_name => attrib, 203: :value => val, 204: :source_type => source.class.name, 205: :source_id => source.id) 206: 207: unless ann.nil? || ann == false 208: anns << ann 209: end 210: end 211: end 212: end 213: 214: return anns 215: end
Helper method to get latest annotations
# File lib/annotations/acts_as_annotatable.rb, line 79 79: def latest_annotations(limit=nil) 80: obj_type = ActiveRecord::Base.send(:class_name_of_active_record_descendant, self.class).to_s 81: 82: Annotation.find(:all, 83: :conditions => { :annotatable_type => obj_type, 84: :annotatable_id => self.id }, 85: :order => "created_at DESC", 86: :limit => limit) 87: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.