Skip to content

Commit

Permalink
Proof of concept of combining query string with hash-prefixed tags
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike Dvorkin committed Sep 23, 2009
1 parent 9a480a8 commit 009bc46
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
22 changes: 22 additions & 0 deletions lib/crm_tags/controllers.rb
Expand Up @@ -6,6 +6,12 @@ class ControllerHooks < FatFreeCRM::Callback::Base
define_method :"get_#{klass.to_s.tableize}" do |controller, context|
params, query = controller.params, controller.send(:current_query)

#---
words, tags = parse_words_and_tags(query)
controller.logger.p "words: " + words.inspect
controller.logger.p "tags: " + tags.inspect
#---

if query.blank? # No search query...
if params[:tag].blank? # No search query, no tags.
klass.my(context[:records])
Expand Down Expand Up @@ -62,4 +68,20 @@ class ControllerHooks < FatFreeCRM::Callback::Base
end # define_method
end # each

private
# Simplistic query parsing to prove the concept of combining query string
# with hash-prefixed tags.
#----------------------------------------------------------------------------
def parse_words_and_tags(query)
words, tags = [], []
query.scan(/[\w#]+/).each do |token|
if token.starts_with?("#")
tags << token
else
words << token
end
end
[ words.join(" "), tags.join(" ") ]
end

end
11 changes: 7 additions & 4 deletions lib/crm_tags/helpers.rb
@@ -1,14 +1,17 @@
module CrmTags
module Helper
module Helpers

#----------------------------------------------------------------------------
def tag_links(model)
model.tag_list.inject([]) do |arr, tag|
# arr << link_to(tag, url_for(:action => "index", :tag => tag), :title => tag)
arr << link_to_function(tag, "$('query').value += ($('query').value.length > 0 ? ' ##{tag}' : '##{tag}'); crm.search('##{tag}', '#{model.class.to_s.tableize}')", :title => tag)
query = controller.send(:current_query) || ""
hashtag = "##{tag}"
query += (query.empty? ? hashtag : " #{hashtag}")
arr << link_to_function(hashtag, "$('query').value = '#{query}'; crm.search('#{query}', '#{model.class.to_s.tableize}')", :title => tag)
end.join(", ")
end

end
end

ActionView::Base.send(:include, CrmTags::Helper)
ActionView::Base.send(:include, CrmTags::Helpers)

0 comments on commit 009bc46

Please sign in to comment.