diff options
author | Francis Irving <francis@mysociety.org> | 2010-09-29 01:15:18 +0100 |
---|---|---|
committer | Francis Irving <francis@mysociety.org> | 2010-09-29 01:15:18 +0100 |
commit | c59e23e43053b64f5f7c9fdc3cc7d6c465c41f21 (patch) | |
tree | b16894588197bdfeccb086b6a9f8213bb44aea4c /vendor/plugins | |
parent | 27998683cf09b5f717ed60b58ad9a59abe794f08 (diff) |
Factor out model of public body tags into generic tag string model
Diffstat (limited to 'vendor/plugins')
-rw-r--r-- | vendor/plugins/has_tag_string/lib/has_tag_string.rb | 93 |
1 files changed, 71 insertions, 22 deletions
diff --git a/vendor/plugins/has_tag_string/lib/has_tag_string.rb b/vendor/plugins/has_tag_string/lib/has_tag_string.rb index ca147ba27..b13803d23 100644 --- a/vendor/plugins/has_tag_string/lib/has_tag_string.rb +++ b/vendor/plugins/has_tag_string/lib/has_tag_string.rb @@ -1,5 +1,5 @@ # lib/has_tag_string.rb: -# Lets a model have tags, represented as space separate strings in a public # +# Lets a model have tags, represented as space separate strings in a public # interface, but stored in the database as keys. Each tag can have a value # followed by a colon - e.g. url:http://www.flourish.org # @@ -7,6 +7,39 @@ # Email: francis@mysociety.org; WWW: http://www.mysociety.org/ module HasTagString + # Represents one tag of one model. + # The migration to make this is currently only in WDTK code. + class HasTagStringTag < ActiveRecord::Base + # XXX strip_attributes! + + validates_presence_of :name + + # Return instance of the model that this tag tags + def tagged_model + return self.model.constantize.find(self.model_id) + end + + def name_and_value + ret = self.name + if !self.value.nil? + ret += ":" + self.value + end + return ret + end + + def HasTagStringTag.split_tag_into_name_value(tag) + sections = tag.split(/:/) + name = sections[0] + if sections[1] + value = sections[1,sections.size].join(":") + else + value = nil + end + return name, value + end + end + + # Methods which are added to the model instances being tagged module InstanceMethods # Given an input string of tags, sets all tags to that string. # XXX This immediately saves the new tags. @@ -16,41 +49,51 @@ module HasTagString tags = tag_string.split(/\s+/).uniq ActiveRecord::Base.transaction do - for public_body_tag in self.public_body_tags - public_body_tag.destroy + for tag in self.tags + tag.destroy end - self.public_body_tags = [] + self.tags = [] for tag in tags # see if is a machine tags (i.e. a tag which has a value) - name, value = PublicBodyTag.split_tag_into_name_value(tag) + name, value = HasTagStringTag.split_tag_into_name_value(tag) - public_body_tag = PublicBodyTag.new(:name => name, :value => value) - self.public_body_tags << public_body_tag - public_body_tag.public_body = self + tag = HasTagStringTag.new( + :model => self.class.base_class.to_s, + :model_id => self.id, + :name => name, :value => value + ) + self.tags << tag end end end + + # Returns the tags the model has, as a space separated string def tag_string - return self.public_body_tags.map { |t| t.name_and_value }.join(' ') + return self.tags.map { |t| t.name_and_value }.join(' ') end - def has_tag?(tag) - for public_body_tag in self.public_body_tags - if public_body_tag.name == tag + + # Test to see if class is tagged with the given tag + def has_tag?(tag_as_string) + for tag in self.tags + if tag.name == tag_as_string return true end end return false end + class TagNotFound < StandardError end - def get_tag_values(tag) + + # If the tag is a machine tag, returns array of its values + def get_tag_values(tag_as_string) found = false results = [] - for public_body_tag in self.public_body_tags - if public_body_tag.name == tag + for tag in self.tags + if tag.name == tag_as_string found = true - if !public_body_tag.value.nil? - results << public_body_tag.value + if !tag.value.nil? + results << tag.value end end end @@ -59,16 +102,20 @@ module HasTagString end return results end - def add_tag_if_not_already_present(tag) - self.tag_string = self.tag_string + " " + tag - end + # Adds a new tag to the model, if it isn't already there + def add_tag_if_not_already_present(tag_as_string) + self.tag_string = self.tag_string + " " + tag_as_string + end end + # Methods which are added to the model class being tagged module ClassMethods # Find all public bodies with a particular tag - def find_by_tag(tag) - return PublicBodyTag.find(:all, :conditions => ['name = ?', tag] ).map { |t| t.public_body }.sort { |a,b| a.name <=> b.name } + def find_by_tag(tag_as_string) + return HasTagStringTag.find(:all, :conditions => + ['name = ? and model = ?', tag_as_string, self.to_s ] + ).map { |t| t.tagged_model }.sort { |a,b| a.name <=> b.name } end end @@ -76,6 +123,8 @@ module HasTagString # Main entry point, add has_tag_string to your model. module HasMethods def has_tag_string() + has_many :tags, :conditions => "model = '" + self.to_s + "'", :foreign_key => "model_id", :class_name => 'HasTagString::HasTagStringTag' + include InstanceMethods self.class.send :include, ClassMethods end |