Module: Annotations::Acts::AnnotationValue::SingletonMethods
- Defined in:
- lib/annotations/acts_as_annotation_value.rb
Overview
Class methods added to the model that has been made acts_as_annotation_value (the mixin target class).
Instance Method Summary collapse
-
#has_duplicate_annotation?(annotation) ⇒ Boolean
This class level method is used to determine whether there is an existing annotation on an annotatable object, regardless of source.
-
#with_attribute_names(attributes) ⇒ Object
A set of all values that have been used, or seeded, with one of the provided attribute names.
Instance Method Details
#has_duplicate_annotation?(annotation) ⇒ Boolean
This class level method is used to determine whether there is an existing annotation on an annotatable object, regardless of source. So, it is used to determine whether a “duplicate” exists (but the notion of “duplicate” may vary between annotation value classes).
This method may be redefined in your model. A default implementation is provided that may suffice for most cases. But note that it makes certain assumptions that may not be valid for all kinds of annotation value models:
-
the joins for the value model use
ActiveRecord::Base::table_nameto determine the name of the table to join on. -
the field used make the comparison on the content is defined by the ‘:content_field’ option passed into acts_as_annotation_value.
Note: A precondition to this method is: this expects a valid annotation object (i.e. one that contains a valid value object, valid annotatable object, valid attribute and so on).
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/annotations/acts_as_annotation_value.rb', line 53 def has_duplicate_annotation?(annotation) return false unless annotation.value.is_a?(self) val_table_name = self.table_name existing = Annotation.find(:all, :joins => "INNER JOIN annotation_attributes ON annotation_attributes.id = annotations.attribute_id INNER JOIN #{val_table_name} ON annotations.value_type = '#{self.name}' AND #{val_table_name}.id = annotations.value_id", :conditions => [ "annotations.annotatable_type = ? AND annotations.annotatable_id = ? AND annotation_attributes.name = ? AND #{val_table_name}.#{self.ann_value_content_field} = ?", annotation.annotatable_type, annotation.annotatable_id, annotation.attribute_name, annotation.value.send(self.ann_value_content_field) ]) if existing.length == 0 || existing.first.id == annotation.id return false else return true end end |
#with_attribute_names(attributes) ⇒ Object
A set of all values that have been used, or seeded, with one of the provided attribute names
79 80 81 82 83 84 |
# File 'lib/annotations/acts_as_annotation_value.rb', line 79 def with_attribute_names attributes attributes = Array(attributes) annotations = Annotation.with_attribute_names(attributes).with_value_type(self.name).include_values.collect{|ann| ann.value} seeds = AnnotationValueSeed.with_attribute_names(attributes).with_value_type(self.name).include_values.collect{|ann| ann.value} (annotations | seeds).uniq end |