aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--app/models/info_request.rb7
-rw-r--r--app/models/info_request_event.rb7
-rw-r--r--spec/models/xapian_spec.rb19
-rw-r--r--todo.txt11
-rw-r--r--vendor/plugins/acts_as_xapian/lib/acts_as_xapian.rb16
-rw-r--r--vendor/plugins/has_tag_string/lib/has_tag_string.rb18
6 files changed, 74 insertions, 4 deletions
diff --git a/app/models/info_request.rb b/app/models/info_request.rb
index 3f5224907..7ba370838 100644
--- a/app/models/info_request.rb
+++ b/app/models/info_request.rb
@@ -146,6 +146,13 @@ class InfoRequest < ActiveRecord::Base
info_request_event.xapian_mark_needs_index
end
end
+ # Force reindex when tag string changes
+ alias_method :orig_tag_string=, :tag_string=
+ def tag_string=(tag_string)
+ ret = self.orig_tag_string=(tag_string)
+ reindex_request_events
+ return ret
+ end
# Removes anything cached about the object in the database, and saves
def clear_in_database_caches!
diff --git a/app/models/info_request_event.rb b/app/models/info_request_event.rb
index 4a659dc65..8ad2d5964 100644
--- a/app/models/info_request_event.rb
+++ b/app/models/info_request_event.rb
@@ -109,7 +109,8 @@ class InfoRequestEvent < ActiveRecord::Base
[ :commented_by, 'C', "commented_by" ],
[ :request, 'R', "request" ],
[ :variety, 'V', "variety" ],
- [ :filetype, 'T', "filetype" ]
+ [ :filetype, 'T', "filetype" ],
+ [ :tags, 'U', "tag" ]
],
:if => :indexed_by_search?,
:eager_load => [ :incoming_message, :outgoing_message, :comment, { :info_request => [ :user, :public_body, :censor_rules ] } ]
@@ -188,6 +189,10 @@ class InfoRequestEvent < ActiveRecord::Base
end
return ''
end
+ def tags
+ # this returns an array of strings, each gets indexed as separate term by acts_as_xapian
+ return self.info_request.tag_array_for_search
+ end
def indexed_by_search?
if ['sent', 'followup_sent', 'response', 'comment'].include?(self.event_type)
if !self.info_request.indexed_by_search?
diff --git a/spec/models/xapian_spec.rb b/spec/models/xapian_spec.rb
index ba1044033..2ed8e9277 100644
--- a/spec/models/xapian_spec.rb
+++ b/spec/models/xapian_spec.rb
@@ -270,6 +270,25 @@ describe InfoRequest, " when indexing requests by their title" do
end
end
+describe InfoRequest, " when indexing requests by tag" do
+ fixtures :info_request_events, :info_requests, :incoming_messages, :raw_emails, :comments
+
+ it "should find request by tag, even when changes" do
+ rebuild_xapian_index
+ ir = info_requests(:naughty_chicken_request)
+ ir.tag_string = 'bunnyrabbit'
+ ir.save!
+ update_xapian_index
+
+ xapian_object = InfoRequest.full_search([InfoRequestEvent], "tag:bunnyrabbit", 'created_at', true, nil, 100, 1)
+ xapian_object.results.size.should == 1
+ xapian_object.results[0][:model] == info_request_events(:silly_outgoing_message_event)
+
+ xapian_object = InfoRequest.full_search([InfoRequestEvent], "tag:orangeaardvark", 'created_at', true, nil, 100, 1)
+ xapian_object.results.size.should == 0
+ end
+end
+
diff --git a/todo.txt b/todo.txt
index c25c554ca..4f11f148a 100644
--- a/todo.txt
+++ b/todo.txt
@@ -1,8 +1,19 @@
Tag search
+- works with multiple tags
+- reindex upon change
+- find by first part of machine tag
+- tag search public bodies
+- document
+
+Rebuild index for tag search?
+
+Get all the tags for a given request
Displayable status as parameter on info requests
Put clips of text in RSS feeds JSONs
+Reduce the number of bogus post redirects that aren't people
+
Next (things that will reduce admin time mainly)
====
diff --git a/vendor/plugins/acts_as_xapian/lib/acts_as_xapian.rb b/vendor/plugins/acts_as_xapian/lib/acts_as_xapian.rb
index c63ea5757..047321562 100644
--- a/vendor/plugins/acts_as_xapian/lib/acts_as_xapian.rb
+++ b/vendor/plugins/acts_as_xapian/lib/acts_as_xapian.rb
@@ -682,7 +682,12 @@ module ActsAsXapian
elsif type == :boolean
value ? true : false
else
- value.to_s
+ # Arrays are for terms which require multiple of them, e.g. tags
+ if value.kind_of?(Array)
+ value.map {|v| v.to_s}
+ else
+ value.to_s
+ end
end
end
@@ -707,7 +712,14 @@ module ActsAsXapian
doc.add_term("I" + doc.data)
if self.xapian_options[:terms]
for term in self.xapian_options[:terms]
- doc.add_term(term[1] + xapian_value(term[0]))
+ value = xapian_value(term[0])
+ if value.kind_of?(Array)
+ for v in value
+ doc.add_term(term[1] + v)
+ end
+ else
+ doc.add_term(term[1] + value)
+ end
end
end
if self.xapian_options[:values]
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 5382067fb..49b82ca0d 100644
--- a/vendor/plugins/has_tag_string/lib/has_tag_string.rb
+++ b/vendor/plugins/has_tag_string/lib/has_tag_string.rb
@@ -80,11 +80,27 @@ module HasTagString
return self.tags.map { |t| t.name_and_value }.join(' ')
end
- # Returns the tags the model has, as an array of strings
+ # Returns the tags the model has, as an array of pairs of key/value
+ # (this can't be a dictionary as you can have multiple instances of a
+ # key with different values)
def tag_array
return self.tags.map { |t| [t.name, t.value] }
end
+ # Returns a list of all the strings someone might want to search for.
+ # So that is the key by itself, or the key and value.
+ # e.g. if a request was tagged openlylocal_id:12345, they might
+ # want to search for "openlylocal_id" or for "openlylocal_id:12345" to find it.
+ def tag_array_for_search
+ ret = {}
+ for tag in self.tags
+ ret[tag.name] = 1
+ ret[tag.name_and_value] = 1
+ end
+
+ return ret.keys
+ end
+
# Test to see if class is tagged with the given tag
def has_tag?(tag_as_string)
for tag in self.tags