Module: Annotations::Acts::Annotatable::InstanceMethods
- Defined in:
- lib/annotations/acts_as_annotatable.rb
Overview
This module contains instance methods
Instance Method Summary collapse
-
#all_annotations_excluding_attributes(attribs, include_values = false) ⇒ Object
Finder to get all annotations on this object excluding those that have the attribute names specified.
-
#annotatable_name ⇒ Object
Gets the name of the annotatable object.
-
#annotations_hash(style = :simple) ⇒ Object
When used with the default style (:simple), returns a Hash of the
annotations
values grouped by attribute name. -
#annotations_with_attribute(attrib, include_values = false) ⇒ Object
Finder to get annotations with a specific attribute.
-
#annotations_with_attribute_and_by_source(attrib, source, include_values = false) ⇒ Object
Finder to get annotations with a specific attribute by a specific source.
-
#annotations_with_attributes(attribs, include_values = false) ⇒ Object
Same as the obj.annotations_with_attribute method (above) but takes in an array for attribute names to look for.
-
#count_annotations_by(source_type_in) ⇒ Object
Returns the number of annotations on this annotatable object by the source type specified.
-
#create_annotations(annotations_data, source) ⇒ Object
Use this method to create many annotations from a Hash of data.
-
#latest_annotations(limit = nil, include_values = false) ⇒ Object
Helper method to get latest annotations.
Instance Method Details
#all_annotations_excluding_attributes(attribs, include_values = false) ⇒ Object
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.
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/annotations/acts_as_annotatable.rb', line 165 def all_annotations_excluding_attributes(attribs, include_values=false) return [] if attribs.blank? obj_type = ActiveRecord::Base.send(:class_name_of_active_record_descendant, self.class).to_s = { :joins => :attribute, :conditions => [ "`annotations`.`annotatable_type` = ? AND `annotations`.`annotatable_id` = ? AND `annotation_attributes`.`name` NOT IN (?)", obj_type, self.id, attribs ], :order => "`annotations`.`updated_at` DESC" } [:include] = [ :value ] if include_values Annotation.find(:all, ) end |
#annotatable_name ⇒ Object
Gets the name of the annotatable object
73 74 75 |
# File 'lib/annotations/acts_as_annotatable.rb', line 73 def annotatable_name self.send(self.class.annotatable_name_field) end |
#annotations_hash(style = :simple) ⇒ Object
When used with the default style (:simple), returns a Hash of the annotations
values grouped by attribute name.
Example output:
"Summary" => "Something interesting happens",
"length" => 345,
"Title" => "Harry Potter and the Exploding Men's Locker Room",
"Tag" => [ "amusing rhetoric", "wizadry" ],
"rating" => "4/5"
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 |
# File 'lib/annotations/acts_as_annotatable.rb', line 247 def annotations_hash(style=:simple) h = { } unless self.annotations.blank? self.annotations.each do |a| if h.has_key?(a.attribute_name) case h[a.attribute_name] when Array h[a.attribute_name] << a.value_content else h[a.attribute_name] = [ h[a.attribute_name], a.value_content ] end else h[a.attribute_name] = a.value_content end end end return h end |
#annotations_with_attribute(attrib, include_values = false) ⇒ Object
Finder to get annotations with a specific attribute. The input parameter is the attribute name (MUST be a String representing the attribute’s name).
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/annotations/acts_as_annotatable.rb', line 96 def annotations_with_attribute(attrib, include_values=false) return [] if attrib.blank? obj_type = ActiveRecord::Base.send(:class_name_of_active_record_descendant, self.class).to_s = { :joins => :attribute, :conditions => { :annotatable_type => obj_type, :annotatable_id => self.id, :annotation_attributes => { :name => attrib.strip.downcase } }, :order => "updated_at DESC" } [:include] = [ :value ] if include_values Annotation.find(:all, ) end |
#annotations_with_attribute_and_by_source(attrib, source, include_values = false) ⇒ Object
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.
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/annotations/acts_as_annotatable.rb', line 140 def annotations_with_attribute_and_by_source(attrib, source, include_values=false) return [] if attrib.blank? or source.nil? obj_type = ActiveRecord::Base.send(:class_name_of_active_record_descendant, self.class).to_s = { :joins => :attribute, :conditions => { :annotatable_type => obj_type, :annotatable_id => self.id, :source_type => source.class.name, :source_id => source.id, :annotation_attributes => { :name => attrib.strip.downcase } }, :order => "updated_at DESC" } [:include] = [ :value ] if include_values Annotation.find(:all, ) end |
#annotations_with_attributes(attribs, include_values = false) ⇒ Object
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.
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/annotations/acts_as_annotatable.rb', line 118 def annotations_with_attributes(attribs, include_values=false) return [] if attribs.blank? obj_type = ActiveRecord::Base.send(:class_name_of_active_record_descendant, self.class).to_s = { :joins => :attribute, :conditions => { :annotatable_type => obj_type, :annotatable_id => self.id, :annotation_attributes => { :name => attribs } }, :order => "updated_at DESC" } [:include] = [ :value ] if include_values Annotation.find(:all, ) end |
#count_annotations_by(source_type_in) ⇒ Object
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”)
187 188 189 190 191 192 193 |
# File 'lib/annotations/acts_as_annotatable.rb', line 187 def count_annotations_by(source_type_in) if source_type_in == nil || source_type_in.downcase == "all" return self.annotations.count else return self.annotations.count(:conditions => { :source_type => source_type_in }) end end |
#create_annotations(annotations_data, source) ⇒ Object
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)
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
# File 'lib/annotations/acts_as_annotatable.rb', line 207 def create_annotations(annotations_data, source) anns = [ ] annotations_data.each do |attrib, val| unless val.blank? val = [ val ].flatten val.each do |val_inner| unless val_inner.blank? ann = self.annotations.new(:attribute_name => attrib, :source_type => source.class.name, :source_id => source.id) ann.value = val_inner ann.save if ann && ann.valid? anns << ann end end end end end # Reload annotations collection self.annotations(true) return anns end |
#latest_annotations(limit = nil, include_values = false) ⇒ Object
Helper method to get latest annotations
78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/annotations/acts_as_annotatable.rb', line 78 def latest_annotations(limit=nil, include_values=false) obj_type = ActiveRecord::Base.send(:class_name_of_active_record_descendant, self.class).to_s = { :conditions => { :annotatable_type => obj_type, :annotatable_id => self.id }, :order => "updated_at DESC", :limit => limit } [:include] = [ :value ] if include_values Annotation.find(:all, ) end |