Permalink
Browse files

some optimizations, and also made versioning optional

  • Loading branch information...
1 parent 0def257 commit 1c9d9c14c991a630dd6c1f55ccf08733d9119b88 @stuzart stuzart committed Feb 3, 2012
View
13 generators/annotations_migration/templates/migration_v4.rb
@@ -0,0 +1,13 @@
+class AnnotationsMigrationV4 < ActiveRecord::Migration
+ def self.up
+ change_column :annotations,:version,:integer,:null=>true
+ change_column :text_values,:version,:integer,:null=>true
+ change_column :number_values,:version,:integer,:null=>true
+ end
+
+ def self.down
+ change_column :annotations,:version,:integer,:null=>false
+ change_column :text_values,:version,:integer,:null=>false
+ change_column :number_values,:version,:integer,:null=>false
+ end
+end
View
7 init.rb
@@ -1 +1,8 @@
require File.join(File.dirname(__FILE__), "lib", "annotations")
+
+# FIX for engines model reloading issue in development mode
+if ENV['RAILS_ENV'] != 'production'
+ load_paths.each do |path|
+ ActiveSupport::Dependencies.load_once_paths.delete(path)
+ end
+end
View
18 lib/annotations/acts_as_annotation_value.rb
@@ -77,25 +77,25 @@ def has_duplicate_annotation?(annotation)
#A set of all values that have been used, or seeded, with one of the provided attribute names
def with_attribute_names attributes
- #TODO: this would probably be better as a named_scope
attributes = Array(attributes)
- attributes.reduce([]) { |values,attr|
- annotations = Annotation.with_attribute_name(attr).with_value_type(self.name).include_values.collect{|ann| ann.value}
- seeds = AnnotationValueSeed.with_attribute_name(attr).with_value_type(self.name).include_values.collect{|ann| ann.value}
- values | annotations | seeds
- }.uniq
+ 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
end
# This module contains instance methods
module InstanceMethods
+ #Whether this value exists with a given attribute name
+ def has_attribute_name? attr
+ !annotations.with_attribute_name(attr).empty? || !annotation_value_seeds.with_attribute_name(attr).empty?
+ end
+
#The total number of annotations that match one or more attribute names.
def annotation_count attributes
attributes = Array(attributes)
- attributes.reduce(0) do |sum,attr|
- sum + annotations.with_attribute_name(attr).count
- end
+ annotations.with_attribute_names(attributes).count
end
# The actual content of the annotation value
View
9 lib/annotations/config.rb
@@ -93,6 +93,12 @@ module Config
# allowed as value objects.
# NOTE (2): The attribute name(s) specified MUST all be in lowercase.
@@valid_value_types = { }
+
+ # This determines whether versioning is enabled.
+ # The default behaviour is true, in which case when a new annotation is created or updated, a copy of the new version
+ # is stored in Annotation::Version and linked to the annotation. Likewise versions of the annotation values are created.
+ # By setting to false, no versions are recorded.
+ @@versioning_enabled = true
def self.reset
@@attribute_names_for_values_to_be_downcased = [ ]
@@ -122,7 +128,8 @@ def self.reset
:default_attribute_identifier_template,
:attribute_name_transform_for_identifier,
:value_factories,
- :valid_value_types ].each do |sym|
+ :valid_value_types,
+ :versioning_enabled].each do |sym|
class_eval <<-EOS, __FILE__, __LINE__
def self.#{sym}
if defined?(#{sym.to_s.upcase})
View
2 lib/annotations_version_fu.rb
@@ -31,7 +31,7 @@ def latest
end
end
- before_save :check_for_new_version
+ before_save :check_for_new_version if Annotations::Config.versioning_enabled
end
# Versioned Model
View
8 lib/app/models/annotation.rb
@@ -100,6 +100,14 @@ def value_content
:order => "created_at DESC" }
}
+ # Finder to get all annotations with one of the given attribute_names.
+ named_scope :with_attribute_names, lambda { |attrib_names|
+ conditions = [attrib_names.collect{"annotation_attributes.name = ?"}.join(" or ")] | attrib_names
+ { :conditions => conditions,
+ :joins => :attribute,
+ :order => "created_at DESC" }
+ }
+
# Finder to get all annotations for a given value_type.
named_scope :with_value_type, lambda { |value_type|
{ :conditions => { :value_type => value_type },
View
10 lib/app/models/annotation_value_seed.rb
@@ -16,13 +16,21 @@ class AnnotationValueSeed < ActiveRecord::Base
{ :include => [ :value ] }
}
- # Finder to get all annotations with a given attribute_name.
+ # Finder to get all annotation value seeds with a given attrib_name.
named_scope :with_attribute_name, lambda { |attrib_name|
{ :conditions => { :annotation_attributes => { :name => attrib_name } },
:joins => :attribute,
:order => "created_at DESC" }
}
+ # Finder to get all annotation value seeds with one of the given attrib_names.
+ named_scope :with_attribute_names, lambda { |attrib_names|
+ conditions = [attrib_names.collect{"annotation_attributes.name = ?"}.join(" or ")] | attrib_names
+ { :conditions => conditions,
+ :joins => :attribute,
+ :order => "created_at DESC" }
+ }
+
# Finder to get all annotations for a given value_type.
named_scope :with_value_type, lambda { |value_type|
{ :conditions => { :value_type => value_type },

0 comments on commit 1c9d9c1

Please sign in to comment.