aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--app/models/public_body.rb63
-rw-r--r--config/environment.rb2
-rw-r--r--vendor/plugins/has_tag_string/README.txt1
-rw-r--r--vendor/plugins/has_tag_string/init.rb2
-rw-r--r--vendor/plugins/has_tag_string/lib/has_tag_string.rb87
5 files changed, 94 insertions, 61 deletions
diff --git a/app/models/public_body.rb b/app/models/public_body.rb
index d54c761e6..491ffe989 100644
--- a/app/models/public_body.rb
+++ b/app/models/public_body.rb
@@ -40,9 +40,11 @@ class PublicBody < ActiveRecord::Base
validates_uniqueness_of :name
has_many :info_requests, :order => 'created_at desc'
- has_many :public_body_tags
has_many :track_things, :order => 'created_at desc'
+ has_many :public_body_tags
+ has_tag_string
+
# like find_by_url_name but also search historic url_name if none found
def self.find_by_url_name_with_historic(name)
found = PublicBody.find_all_by_url_name(name)
@@ -183,65 +185,6 @@ class PublicBody < ActiveRecord::Base
end
end
- # Given an input string of tags, sets all tags to that string.
- # XXX This immediately saves the new tags.
- def tag_string=(tag_string)
- tag_string = tag_string.strip
- # split tags apart
- tags = tag_string.split(/\s+/).uniq
-
- ActiveRecord::Base.transaction do
- for public_body_tag in self.public_body_tags
- public_body_tag.destroy
- end
- self.public_body_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)
-
- public_body_tag = PublicBodyTag.new(:name => name, :value => value)
- self.public_body_tags << public_body_tag
- public_body_tag.public_body = self
- end
- end
- end
- def tag_string
- return self.public_body_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
- return true
- end
- end
- return false
- end
- class TagNotFound < StandardError
- end
- def get_tag_values(tag)
- found = false
- results = []
- for public_body_tag in self.public_body_tags
- if public_body_tag.name == tag
- found = true
- if !public_body_tag.value.nil?
- results << public_body_tag.value
- end
- end
- end
- if !found
- raise TagNotFound
- end
- return results
- end
- def add_tag_if_not_already_present(tag)
- self.tag_string = self.tag_string + " " + tag
- end
-
- # Find all public bodies with a particular tag
- def self.find_by_tag(tag)
- return PublicBodyTag.find(:all, :conditions => ['name = ?', tag] ).map { |t| t.public_body }.sort { |a,b| a.name <=> b.name }
- end
# Use tags to describe what type of thing this is
def type_of_authority(html = false)
diff --git a/config/environment.rb b/config/environment.rb
index 5a61d7e56..c4351dbd1 100644
--- a/config/environment.rb
+++ b/config/environment.rb
@@ -105,7 +105,7 @@ if (MySociety::Config.get("DOMAIN", "") != "")
}
end
-# Load monkey patches from lib/
+# Load monkey patches and other things from lib/
require 'tmail_extensions.rb'
require 'activesupport_cache_extensions.rb'
require 'public_body_categories.rb'
diff --git a/vendor/plugins/has_tag_string/README.txt b/vendor/plugins/has_tag_string/README.txt
new file mode 100644
index 000000000..0d3a38229
--- /dev/null
+++ b/vendor/plugins/has_tag_string/README.txt
@@ -0,0 +1 @@
+Plugin used only in WhatDoTheyKnow right now.
diff --git a/vendor/plugins/has_tag_string/init.rb b/vendor/plugins/has_tag_string/init.rb
new file mode 100644
index 000000000..4a07073a7
--- /dev/null
+++ b/vendor/plugins/has_tag_string/init.rb
@@ -0,0 +1,2 @@
+require 'has_tag_string'
+
diff --git a/vendor/plugins/has_tag_string/lib/has_tag_string.rb b/vendor/plugins/has_tag_string/lib/has_tag_string.rb
new file mode 100644
index 000000000..ca147ba27
--- /dev/null
+++ b/vendor/plugins/has_tag_string/lib/has_tag_string.rb
@@ -0,0 +1,87 @@
+# lib/has_tag_string.rb:
+# 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
+#
+# Copyright (c) 2010 UK Citizens Online Democracy. All rights reserved.
+# Email: francis@mysociety.org; WWW: http://www.mysociety.org/
+
+module HasTagString
+ module InstanceMethods
+ # Given an input string of tags, sets all tags to that string.
+ # XXX This immediately saves the new tags.
+ def tag_string=(tag_string)
+ tag_string = tag_string.strip
+ # split tags apart
+ tags = tag_string.split(/\s+/).uniq
+
+ ActiveRecord::Base.transaction do
+ for public_body_tag in self.public_body_tags
+ public_body_tag.destroy
+ end
+ self.public_body_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)
+
+ public_body_tag = PublicBodyTag.new(:name => name, :value => value)
+ self.public_body_tags << public_body_tag
+ public_body_tag.public_body = self
+ end
+ end
+ end
+ def tag_string
+ return self.public_body_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
+ return true
+ end
+ end
+ return false
+ end
+ class TagNotFound < StandardError
+ end
+ def get_tag_values(tag)
+ found = false
+ results = []
+ for public_body_tag in self.public_body_tags
+ if public_body_tag.name == tag
+ found = true
+ if !public_body_tag.value.nil?
+ results << public_body_tag.value
+ end
+ end
+ end
+ if !found
+ raise TagNotFound
+ end
+ return results
+ end
+ def add_tag_if_not_already_present(tag)
+ self.tag_string = self.tag_string + " " + tag
+ end
+
+ end
+
+ 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 }
+ end
+ end
+
+ ######################################################################
+ # Main entry point, add has_tag_string to your model.
+ module HasMethods
+ def has_tag_string()
+ include InstanceMethods
+ self.class.send :include, ClassMethods
+ end
+ end
+
+end
+
+ActiveRecord::Base.extend HasTagString::HasMethods
+