Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added include_values optional argument (default: false) to as many fi…

…nder methods. This allows you to specify whether value records must be included in the query results or not, and *may* be used to improve performance.
  • Loading branch information...
commit 7a9fa060383db242a36a7d7136fa1878b195dd63 1 parent ba2fedb
@jits jits authored
View
1  CHANGELOG.rdoc
@@ -2,6 +2,7 @@
== 0.4.1
* Add +Annotation::include_values+ as a named scope that can be used to potentially improve performance.
+* Added +include_values+ optional argument (default: false) to as many finder methods. This allows you to specify whether value records must be included in the query results or not, and *may* be used to improve performance.
== 0.4.0
* New config option: +Annotations::Config.value_factories+ - support for value object generation factories per attribute name.
View
131 lib/annotations/acts_as_annotatable.rb
@@ -34,25 +34,35 @@ module SingletonMethods
# Helper finder to get all annotations for an object of the mixin annotatable type with the ID provided.
# This is the same as object.annotations with the added benefit that the object doesnt have to be loaded.
# E.g: Book.find_annotations_for(34) will give all annotations for the Book with ID 34.
- def find_annotations_for(id)
+ def find_annotations_for(id, include_values=false)
obj_type = ActiveRecord::Base.send(:class_name_of_active_record_descendant, self).to_s
- Annotation.find(:all,
- :conditions => { :annotatable_type => obj_type,
- :annotatable_id => id },
- :order => "updated_at DESC")
+ options = {
+ :conditions => { :annotatable_type => obj_type,
+ :annotatable_id => id },
+ :order => "updated_at DESC"
+ }
+
+ options[:include] = [ :value ] if include_values
+
+ Annotation.find(:all, options)
end
# Helper finder to get all annotations for all objects of the mixin annotatable type, by the source specified.
# E.g: Book.find_annotations_by('User', 10) will give all annotations for all Books by User with ID 10.
- def find_annotations_by(source_type, source_id)
+ def find_annotations_by(source_type, source_id, include_values=false)
obj_type = ActiveRecord::Base.send(:class_name_of_active_record_descendant, self).to_s
- Annotation.find(:all,
- :conditions => { :annotatable_type => obj_type,
- :source_type => source_type,
- :source_id => source_id },
- :order => "updated_at DESC")
+ options = {
+ :conditions => { :annotatable_type => obj_type,
+ :source_type => source_type,
+ :source_id => source_id },
+ :order => "updated_at DESC"
+ }
+
+ options[:include] = [ :value ] if include_values
+
+ Annotation.find(:all, options)
end
end
@@ -65,66 +75,86 @@ def annotatable_name
end
# Helper method to get latest annotations
- def latest_annotations(limit=nil)
+ def latest_annotations(limit=nil, include_values=false)
obj_type = ActiveRecord::Base.send(:class_name_of_active_record_descendant, self.class).to_s
- Annotation.find(:all,
- :conditions => { :annotatable_type => obj_type,
- :annotatable_id => self.id },
- :order => "updated_at DESC",
- :limit => limit)
+ options = {
+ :conditions => { :annotatable_type => obj_type,
+ :annotatable_id => self.id },
+ :order => "updated_at DESC",
+ :limit => limit
+ }
+
+ options[:include] = [ :value ] if include_values
+
+ Annotation.find(:all, options)
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).
- def annotations_with_attribute(attrib)
+ 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
- Annotation.find(:all,
- :joins => :attribute,
- :conditions => { :annotatable_type => obj_type,
- :annotatable_id => self.id,
- :annotation_attributes => { :name => attrib.strip.downcase } },
- :order => "updated_at DESC")
+ options = {
+ :joins => :attribute,
+ :conditions => { :annotatable_type => obj_type,
+ :annotatable_id => self.id,
+ :annotation_attributes => { :name => attrib.strip.downcase } },
+ :order => "updated_at DESC"
+ }
+
+ options[:include] = [ :value ] if include_values
+
+ Annotation.find(:all, options)
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.
- def annotations_with_attributes(attribs)
+ 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
- Annotation.find(:all,
- :joins => :attribute,
- :conditions => { :annotatable_type => obj_type,
- :annotatable_id => self.id,
- :annotation_attributes => { :name => attribs } },
- :order => "updated_at DESC")
+ options = {
+ :joins => :attribute,
+ :conditions => { :annotatable_type => obj_type,
+ :annotatable_id => self.id,
+ :annotation_attributes => { :name => attribs } },
+ :order => "updated_at DESC"
+ }
+
+ options[:include] = [ :value ] if include_values
+
+ Annotation.find(:all, options)
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.
- def annotations_with_attribute_and_by_source(attrib, source)
+ 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
- Annotation.find(:all,
- :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")
+ options = {
+ :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"
+ }
+
+ options[:include] = [ :value ] if include_values
+
+ Annotation.find(:all, options)
end
# Finder to get all annotations on this object excluding those that
@@ -132,18 +162,23 @@ def annotations_with_attribute_and_by_source(attrib, source)
#
# NOTE (1): the argument to this method MUST be an Array of Strings.
# NOTE (2): the returned records will be Read Only.
- def all_annotations_excluding_attributes(attribs)
+ 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
- Annotation.find(:all,
- :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")
+ options = {
+ :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"
+ }
+
+ options[:include] = [ :value ] if include_values
+
+ Annotation.find(:all, options)
end
# Returns the number of annotations on this annotatable object by the source type specified.
View
49 lib/annotations/acts_as_annotation_source.rb
@@ -24,37 +24,52 @@ module SingletonMethods
# 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.
- def annotations_by(id)
+ def annotations_by(id, include_values=false)
obj_type = ActiveRecord::Base.send(:class_name_of_active_record_descendant, self).to_s
- Annotation.find(:all,
- :conditions => { :source_type => obj_type,
- :source_id => id },
- :order => "updated_at DESC")
+ options = {
+ :conditions => { :source_type => obj_type,
+ :source_id => id },
+ :order => "updated_at DESC"
+ }
+
+ options[:include] = [ :value ] if include_values
+
+ Annotation.find(:all, options)
end
# 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.
- def annotations_for(annotatable_type, annotatable_id)
+ def annotations_for(annotatable_type, annotatable_id, include_values=false)
obj_type = ActiveRecord::Base.send(:class_name_of_active_record_descendant, self).to_s
- Annotation.find(:all,
- :conditions => { :source_type => obj_type,
- :annotatable_type => annotatable_type,
- :annotatable_id => annotatable_id },
- :order => "updated_at DESC")
+ 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
end
# This module contains instance methods
module InstanceMethods
# Helper method to get latest annotations
- def latest_annotations(limit=nil)
- Annotation.find(:all,
- :conditions => { :source_type => self.class.name,
- :source_id => id },
- :order => "updated_at DESC",
- :limit => limit)
+ def latest_annotations(limit=nil, include_values=false)
+ options = {
+ :conditions => { :source_type => self.class.name,
+ :source_id => id },
+ :order => "updated_at DESC",
+ :limit => limit
+ }
+
+ options[:include] = [ :value ] if include_values
+
+ Annotation.find(:all, options)
end
def annotation_source_name
View
29 test/acts_as_annotatable_test.rb
@@ -11,16 +11,24 @@ def test_has_many_annotations_association
def test_find_annotations_for_class_method
assert_equal 6, Book.find_annotations_for(books(:h).id).length
+ assert_equal 6, Book.find_annotations_for(books(:h).id, true).length
assert_equal 5, Book.find_annotations_for(books(:r).id).length
+ assert_equal 5, Book.find_annotations_for(books(:r).id, true).length
assert_equal 2, Chapter.find_annotations_for(chapters(:bh_c10).id).length
- assert_equal 4, Chapter.find_annotations_for(chapters(:br_c2).id).length
+ assert_equal 2, Chapter.find_annotations_for(chapters(:bh_c10).id, true).length
+ assert_equal 4, Chapter.find_annotations_for(chapters(:br_c2).id).length
+ assert_equal 4, Chapter.find_annotations_for(chapters(:br_c2).id, true).length
end
def test_find_annotations_by_class_method
assert_equal 4, Book.find_annotations_by("User", users(:jane)).length
+ assert_equal 4, Book.find_annotations_by("User", users(:jane), true).length
assert_equal 1, Book.find_annotations_by("Group", groups(:sci_fi_geeks)).length
+ assert_equal 1, Book.find_annotations_by("Group", groups(:sci_fi_geeks), true).length
assert_equal 3, Chapter.find_annotations_by("User", users(:john)).length
+ assert_equal 3, Chapter.find_annotations_by("User", users(:john), true).length
assert_equal 2, Chapter.find_annotations_by("Group", groups(:classical_fans)).length
+ assert_equal 2, Chapter.find_annotations_by("Group", groups(:classical_fans), true).length
end
def test_annotatable_name_field_class_attribute
@@ -38,37 +46,56 @@ def test_annotatable_name_instance_method
def test_latest_annotations_instance_method
assert_equal 6, books(:h).latest_annotations.length
+ assert_equal 6, books(:h).latest_annotations(nil, true).length
assert_equal 2, chapters(:bh_c10).latest_annotations.length
+ assert_equal 2, chapters(:bh_c10).latest_annotations(nil, true).length
assert_equal 2, books(:h).latest_annotations(2).length
+ assert_equal 2, books(:h).latest_annotations(2, true).length
end
def test_annotations_with_attribute_instance_method
assert_equal 2, books(:h).annotations_with_attribute("tag").length
+ assert_equal 2, books(:h).annotations_with_attribute("tag", true).length
assert_equal 0, books(:r).annotations_with_attribute("doesnt_exist").length
+ assert_equal 0, books(:r).annotations_with_attribute("doesnt_exist", true).length
assert_equal 1, chapters(:bh_c10).annotations_with_attribute("endingType").length
+ assert_equal 1, chapters(:bh_c10).annotations_with_attribute("endingType", true).length
assert_equal 1, chapters(:br_c202).annotations_with_attribute("Title").length
+ assert_equal 1, chapters(:br_c202).annotations_with_attribute("Title", true).length
end
def test_annotations_with_attributes_instance_method
assert_equal 4, books(:h).annotations_with_attributes([ "tag", "summary", "LENGTH" ]).length
+ assert_equal 4, books(:h).annotations_with_attributes([ "tag", "summary", "LENGTH" ], true).length
assert_equal 0, books(:h).annotations_with_attributes([ "doesnt_exist", "also doesn't exist" ]).length
+ assert_equal 0, books(:h).annotations_with_attributes([ "doesnt_exist", "also doesn't exist" ], true).length
assert_equal 1, chapters(:bh_c10).annotations_with_attributes([ "endingType" ]).length
+ assert_equal 1, chapters(:bh_c10).annotations_with_attributes([ "endingType" ], true).length
assert_equal 2, chapters(:br_c202).annotations_with_attributes([ "Title", "complexity", "doesn't exist but still"]).length
+ assert_equal 2, chapters(:br_c202).annotations_with_attributes([ "Title", "complexity", "doesn't exist but still"], true).length
end
def test_annotations_with_attribute_and_by_source_instance_method
assert_equal 1, books(:h).annotations_with_attribute_and_by_source("tag", users(:jane)).length
+ assert_equal 1, books(:h).annotations_with_attribute_and_by_source("tag", users(:jane), true).length
assert_equal 0, books(:r).annotations_with_attribute_and_by_source("doesnt_exist", users(:jane)).length
+ assert_equal 0, books(:r).annotations_with_attribute_and_by_source("doesnt_exist", users(:jane), true).length
assert_equal 1, chapters(:bh_c10).annotations_with_attribute_and_by_source("endingType", groups(:sci_fi_geeks)).length
+ assert_equal 1, chapters(:bh_c10).annotations_with_attribute_and_by_source("endingType", groups(:sci_fi_geeks), true).length
assert_equal 1, chapters(:br_c202).annotations_with_attribute_and_by_source("Title", users(:john)).length
+ assert_equal 1, chapters(:br_c202).annotations_with_attribute_and_by_source("Title", users(:john), true).length
end
def test_all_annotations_excluding_attributes
assert_equal 4, books(:h).all_annotations_excluding_attributes([ "TITLE", "length" ]).length
+ assert_equal 4, books(:h).all_annotations_excluding_attributes([ "TITLE", "length" ], true).length
assert_equal 5, books(:r).all_annotations_excluding_attributes([ "doesnt_exist" ]).length
+ assert_equal 5, books(:r).all_annotations_excluding_attributes([ "doesnt_exist" ], true).length
assert_equal 1, chapters(:bh_c10).all_annotations_excluding_attributes([ "endingType" ]).length
+ assert_equal 1, chapters(:bh_c10).all_annotations_excluding_attributes([ "endingType" ], true).length
assert_equal 2, chapters(:br_c202).all_annotations_excluding_attributes([ "tag", "doesn't exist but who cares" ]).length
+ assert_equal 2, chapters(:br_c202).all_annotations_excluding_attributes([ "tag", "doesn't exist but who cares" ], true).length
end
def test_count_annotations_by_instance_method
View
11 test/acts_as_annotation_source_test.rb
@@ -11,23 +11,34 @@ def test_has_many_annotations_association
def test_annotations_by_class_method
assert_equal 7, User.annotations_by(users(:john).id).length
+ assert_equal 7, User.annotations_by(users(:john).id, true).length
assert_equal 6, User.annotations_by(users(:jane).id).length
+ assert_equal 6, User.annotations_by(users(:jane).id, true).length
assert_equal 3, Group.annotations_by(groups(:sci_fi_geeks).id).length
+ assert_equal 3, Group.annotations_by(groups(:sci_fi_geeks).id, true).length
assert_equal 4, Group.annotations_by(groups(:classical_fans).id).length
+ assert_equal 4, Group.annotations_by(groups(:classical_fans).id, true).length
end
def test_annotations_for_class_method
assert_equal 4, User.annotations_for("Book", books(:h).id).length
+ assert_equal 4, User.annotations_for("Book", books(:h).id, true).length
assert_equal 1, User.annotations_for("Chapter", chapters(:bh_c10).id).length
+ assert_equal 1, User.annotations_for("Chapter", chapters(:bh_c10).id, true).length
assert_equal 1, Group.annotations_for("Book", books(:r).id).length
+ assert_equal 1, Group.annotations_for("Book", books(:r).id, true).length
assert_equal 1, Group.annotations_for("Chapter", chapters(:br_c2).id).length
+ assert_equal 1, Group.annotations_for("Chapter", chapters(:br_c2).id, true).length
end
def test_latest_annotations_instance_method
assert_equal 6, users(:jane).latest_annotations.length
+ assert_equal 6, users(:jane).latest_annotations(nil, true).length
assert_equal 3, groups(:sci_fi_geeks).latest_annotations.length
+ assert_equal 3, groups(:sci_fi_geeks).latest_annotations(nil, true).length
assert_equal 3, users(:john).latest_annotations(3).length
+ assert_equal 3, users(:john).latest_annotations(3, true).length
end
def test_annotation_source_name_instance_method
Please sign in to comment.
Something went wrong with that request. Please try again.