Skip to content

Commit 1c9d9c1

Browse files
committed
some optimizations, and also made versioning optional
1 parent 0def257 commit 1c9d9c1

File tree

7 files changed

+55
-12
lines changed

7 files changed

+55
-12
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class AnnotationsMigrationV4 < ActiveRecord::Migration
2+
def self.up
3+
change_column :annotations,:version,:integer,:null=>true
4+
change_column :text_values,:version,:integer,:null=>true
5+
change_column :number_values,:version,:integer,:null=>true
6+
end
7+
8+
def self.down
9+
change_column :annotations,:version,:integer,:null=>false
10+
change_column :text_values,:version,:integer,:null=>false
11+
change_column :number_values,:version,:integer,:null=>false
12+
end
13+
end

init.rb

+7
Original file line numberDiff line numberDiff line change
@@ -1 +1,8 @@
11
require File.join(File.dirname(__FILE__), "lib", "annotations")
2+
3+
# FIX for engines model reloading issue in development mode
4+
if ENV['RAILS_ENV'] != 'production'
5+
load_paths.each do |path|
6+
ActiveSupport::Dependencies.load_once_paths.delete(path)
7+
end
8+
end

lib/annotations/acts_as_annotation_value.rb

+9-9
Original file line numberDiff line numberDiff line change
@@ -77,25 +77,25 @@ def has_duplicate_annotation?(annotation)
7777

7878
#A set of all values that have been used, or seeded, with one of the provided attribute names
7979
def with_attribute_names attributes
80-
#TODO: this would probably be better as a named_scope
8180
attributes = Array(attributes)
82-
attributes.reduce([]) { |values,attr|
83-
annotations = Annotation.with_attribute_name(attr).with_value_type(self.name).include_values.collect{|ann| ann.value}
84-
seeds = AnnotationValueSeed.with_attribute_name(attr).with_value_type(self.name).include_values.collect{|ann| ann.value}
85-
values | annotations | seeds
86-
}.uniq
81+
annotations = Annotation.with_attribute_names(attributes).with_value_type(self.name).include_values.collect{|ann| ann.value}
82+
seeds = AnnotationValueSeed.with_attribute_names(attributes).with_value_type(self.name).include_values.collect{|ann| ann.value}
83+
(annotations | seeds).uniq
8784
end
8885
end
8986

9087
# This module contains instance methods
9188
module InstanceMethods
9289

90+
#Whether this value exists with a given attribute name
91+
def has_attribute_name? attr
92+
!annotations.with_attribute_name(attr).empty? || !annotation_value_seeds.with_attribute_name(attr).empty?
93+
end
94+
9395
#The total number of annotations that match one or more attribute names.
9496
def annotation_count attributes
9597
attributes = Array(attributes)
96-
attributes.reduce(0) do |sum,attr|
97-
sum + annotations.with_attribute_name(attr).count
98-
end
98+
annotations.with_attribute_names(attributes).count
9999
end
100100

101101
# The actual content of the annotation value

lib/annotations/config.rb

+8-1
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,12 @@ module Config
9393
# allowed as value objects.
9494
# NOTE (2): The attribute name(s) specified MUST all be in lowercase.
9595
@@valid_value_types = { }
96+
97+
# This determines whether versioning is enabled.
98+
# The default behaviour is true, in which case when a new annotation is created or updated, a copy of the new version
99+
# is stored in Annotation::Version and linked to the annotation. Likewise versions of the annotation values are created.
100+
# By setting to false, no versions are recorded.
101+
@@versioning_enabled = true
96102

97103
def self.reset
98104
@@attribute_names_for_values_to_be_downcased = [ ]
@@ -122,7 +128,8 @@ def self.reset
122128
:default_attribute_identifier_template,
123129
:attribute_name_transform_for_identifier,
124130
:value_factories,
125-
:valid_value_types ].each do |sym|
131+
:valid_value_types,
132+
:versioning_enabled].each do |sym|
126133
class_eval <<-EOS, __FILE__, __LINE__
127134
def self.#{sym}
128135
if defined?(#{sym.to_s.upcase})

lib/annotations_version_fu.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def latest
3131
end
3232
end
3333

34-
before_save :check_for_new_version
34+
before_save :check_for_new_version if Annotations::Config.versioning_enabled
3535
end
3636

3737
# Versioned Model

lib/app/models/annotation.rb

+8
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,14 @@ def value_content
100100
:order => "created_at DESC" }
101101
}
102102

103+
# Finder to get all annotations with one of the given attribute_names.
104+
named_scope :with_attribute_names, lambda { |attrib_names|
105+
conditions = [attrib_names.collect{"annotation_attributes.name = ?"}.join(" or ")] | attrib_names
106+
{ :conditions => conditions,
107+
:joins => :attribute,
108+
:order => "created_at DESC" }
109+
}
110+
103111
# Finder to get all annotations for a given value_type.
104112
named_scope :with_value_type, lambda { |value_type|
105113
{ :conditions => { :value_type => value_type },

lib/app/models/annotation_value_seed.rb

+9-1
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,21 @@ class AnnotationValueSeed < ActiveRecord::Base
1616
{ :include => [ :value ] }
1717
}
1818

19-
# Finder to get all annotations with a given attribute_name.
19+
# Finder to get all annotation value seeds with a given attrib_name.
2020
named_scope :with_attribute_name, lambda { |attrib_name|
2121
{ :conditions => { :annotation_attributes => { :name => attrib_name } },
2222
:joins => :attribute,
2323
:order => "created_at DESC" }
2424
}
2525

26+
# Finder to get all annotation value seeds with one of the given attrib_names.
27+
named_scope :with_attribute_names, lambda { |attrib_names|
28+
conditions = [attrib_names.collect{"annotation_attributes.name = ?"}.join(" or ")] | attrib_names
29+
{ :conditions => conditions,
30+
:joins => :attribute,
31+
:order => "created_at DESC" }
32+
}
33+
2634
# Finder to get all annotations for a given value_type.
2735
named_scope :with_value_type, lambda { |value_type|
2836
{ :conditions => { :value_type => value_type },

0 commit comments

Comments
 (0)