aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--app/models/public_body.rb22
-rw-r--r--app/models/public_body_tag.rb8
-rw-r--r--db/migrate/088_public_body_machine_tags.rb18
-rw-r--r--db/schema.rb4
-rw-r--r--spec/models/public_body_spec.rb26
5 files changed, 75 insertions, 3 deletions
diff --git a/app/models/public_body.rb b/app/models/public_body.rb
index 65acb407b..c89309b48 100644
--- a/app/models/public_body.rb
+++ b/app/models/public_body.rb
@@ -188,6 +188,7 @@ class PublicBody < ActiveRecord::Base
# 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
@@ -196,14 +197,23 @@ class PublicBody < ActiveRecord::Base
end
self.public_body_tags = []
for tag in tags
- public_body_tag = PublicBodyTag.new(:name => tag)
+ # see if is a machine tags (i.e. a tag which has a value)
+ sections = tag.split(/:/)
+ name = sections[0]
+ if sections[1]
+ value = sections[1,sections.size].join(":")
+ else
+ value = nil
+ end
+
+ 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 }.join(' ')
+ 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
@@ -213,6 +223,14 @@ class PublicBody < ActiveRecord::Base
end
return false
end
+ def get_tag_value(tag)
+ for public_body_tag in self.public_body_tags
+ if public_body_tag.name == tag
+ return public_body_tag.value
+ end
+ end
+ return false
+ end
def add_tag_if_not_already_present(tag)
self.tag_string = self.tag_string + " " + tag
end
diff --git a/app/models/public_body_tag.rb b/app/models/public_body_tag.rb
index 6ed698431..63e892e18 100644
--- a/app/models/public_body_tag.rb
+++ b/app/models/public_body_tag.rb
@@ -24,5 +24,13 @@ class PublicBodyTag < ActiveRecord::Base
validates_presence_of :name
belongs_to :public_body
+
+ def name_and_value
+ ret = self.name
+ if !self.value.nil?
+ ret += ":" + self.value
+ end
+ return ret
+ end
end
diff --git a/db/migrate/088_public_body_machine_tags.rb b/db/migrate/088_public_body_machine_tags.rb
new file mode 100644
index 000000000..0089607c6
--- /dev/null
+++ b/db/migrate/088_public_body_machine_tags.rb
@@ -0,0 +1,18 @@
+class PublicBodyMachineTags < ActiveRecord::Migration
+ def self.up
+ add_column :public_body_tags, :value, :text
+
+ # MySQL cannot index text blobs like this
+ # XXX perhaps should change :name/:value to be a :string
+ if ActiveRecord::Base.connection.adapter_name != "MySQL"
+ add_index :public_body_tags, :name
+ end
+ end
+
+ def self.down
+ raise "No reverse migration"
+ #remove_column :public_body_tags, :value
+ #remove_index :public_body_tags, :name
+ end
+end
+
diff --git a/db/schema.rb b/db/schema.rb
index e172a6e7e..b63c0d8cf 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -9,7 +9,7 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 87) do
+ActiveRecord::Schema.define(:version => 88) do
create_table "acts_as_xapian_jobs", :force => true do |t|
t.string "model", :null => false
@@ -173,9 +173,11 @@ ActiveRecord::Schema.define(:version => 87) do
t.integer "public_body_id", :null => false
t.text "name", :null => false
t.datetime "created_at", :null => false
+ t.text "value"
end
add_index "public_body_tags", ["name", "public_body_id"], :name => "index_public_body_tags_on_public_body_id_and_name", :unique => true
+ add_index "public_body_tags", ["name"], :name => "index_public_body_tags_on_name"
create_table "public_body_versions", :force => true do |t|
t.integer "public_body_id"
diff --git a/spec/models/public_body_spec.rb b/spec/models/public_body_spec.rb
index b03954373..8bd419005 100644
--- a/spec/models/public_body_spec.rb
+++ b/spec/models/public_body_spec.rb
@@ -50,6 +50,32 @@ describe PublicBody, " using tags" do
@public_body.has_tag?('cheddar').should be_true
end
+ it 'should ignore repeat tags' do
+ @public_body.tag_string = 'stilton stilton'
+ @public_body.tag_string.should == 'stilton'
+ end
+end
+
+describe PublicBody, " using machine tags" do
+ before do
+ @public_body = PublicBody.new(:name => 'Aardvark Monitoring Service',
+ :short_name => 'AMS',
+ :request_email => 'foo@flourish.org',
+ :last_edit_editor => 'test',
+ :last_edit_comment => '')
+ end
+
+ it 'should parse machine tags' do
+ @public_body.tag_string = 'wondrous cheese:green'
+ @public_body.tag_string.should == 'wondrous cheese:green'
+
+ @public_body.has_tag?('cheese:green').should be_false
+ @public_body.has_tag?('cheese').should be_true
+ @public_body.get_tag_value('cheese').should == 'green'
+
+ @public_body.get_tag_value('wondrous').should == nil
+ @public_body.get_tag_value('notthere').should == false
+ end
end
describe PublicBody, " when making up the URL name" do