Skip to content

Commit

Permalink
Made controller methods work with query and tags parsed out from the …
Browse files Browse the repository at this point in the history
…search string
  • Loading branch information
Mike Dvorkin committed Sep 25, 2009
1 parent 009bc46 commit dcaf750
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 22 deletions.
3 changes: 3 additions & 0 deletions README
@@ -1,3 +1,6 @@
Tags plugin for Fat Free CRM

Requirements:
./script/plugin install git://github.com/mbleigh/acts-as-taggable-on.git

Copyright (c) 2009 by Michael Dvorkin, released under the GNU Affero General Public License
52 changes: 30 additions & 22 deletions lib/crm_tags/controllers.rb
Expand Up @@ -4,25 +4,25 @@ class ControllerHooks < FatFreeCRM::Callback::Base
#----------------------------------------------------------------------------
[ Account, Contact ].each do |klass|
define_method :"get_#{klass.to_s.tableize}" do |controller, context|
params, query = controller.params, controller.send(:current_query)
params, search_string = controller.params, controller.send(:current_query)
query, tags = parse_query_and_tags(search_string)

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

if query.blank? # No search query...
if params[:tag].blank? # No search query, no tags.
if tags.blank? # No search query, no tags.
klass.my(context[:records])
else # No search query, with tags.
klass.my(context[:records]).tagged_with(params[:tag], :on => :tags)
klass.my(context[:records]).tagged_with(tags, :on => :tags)
end
else # With search query...
if params[:tag].blank? # With search query, no tags.
if tags.blank? # With search query, no tags.
klass.my(context[:records]).search(query)
else # With search query, with tags.
klass.my(context[:records]).search(query).tagged_with(params[:tag], :on => :tags)
klass.my(context[:records]).search(query).tagged_with(tags, :on => :tags)
end
end.paginate(context[:pages])
end # define_method
Expand All @@ -32,56 +32,64 @@ class ControllerHooks < FatFreeCRM::Callback::Base
#----------------------------------------------------------------------------
[ Campaign, Lead, Opportunity ].each do |klass|
define_method :"get_#{klass.to_s.tableize}" do |controller, context|
session, params, query = controller.session, controller.params, controller.send(:current_query)
session, params, search_string = controller.session, controller.params, controller.send(:current_query)
query, tags = parse_query_and_tags(search_string)
filter = :"filter_by_#{klass.to_s.singularize}_status"

#---
controller.logger.p "query: " + query.inspect
controller.logger.p "tags: " + tags.inspect
#---

if session[filter] # With filters...
filtered = session[filter].split(",")
if params[:tag].blank? # With filters, no tags...
if tags.blank? # With filters, no tags...
if query.blank? # With filters, no tags, no search query.
klass.my(context[:records]).only(filtered)
else # With filters, no tags, with search query.
klass.my(context[:records]).only(filtered).search(query)
end
else # With filters, with tags...
if query.blank? # With filters, with tags, no search query.
klass.my(context[:records]).only(filtered).tagged_with(params[:tag], :on => :tags)
klass.my(context[:records]).only(filtered).tagged_with(tags, :on => :tags)
else # With filters, with tags, with search query.
klass.my(context[:records]).only(filtered).search(query).tagged_with(params[:tag], :on => :tags)
klass.my(context[:records]).only(filtered).search(query).tagged_with(tags, :on => :tags)
end
end
else # No filters...
if params[:tag].blank? # No filters, no tags...
if tags.blank? # No filters, no tags...
if query.blank? # No filters, no tags, no search query.
klass.my(context[:records])
else # No filters, no tags, with search query.
klass.my(context[:records]).search(query)
end
else # No filters, with tags...
if query.blank? # No filters, with tags, no search query.
klass.my(context[:records]).tagged_with(params[:tag], :on => :tags)
klass.my(context[:records]).tagged_with(tags, :on => :tags)
else # No filters, with tags, with search query.
klass.my(context[:records]).search(query).tagged_with(params[:tag], :on => :tags)
klass.my(context[:records]).search(query).tagged_with(tags, :on => :tags)
end
end
end.paginate(context[:pages])
end # define_method
end # each

private
# Simplistic query parsing to prove the concept of combining query string
# with hash-prefixed tags.
# Somewhat simplistic parser that extracts query and hash-prefixed tags from
# the search string and returns them as two element array, for example:
#
# "#real Billy Bones #pirate" => [ "Billy Bones", "real, pirate" ]
#----------------------------------------------------------------------------
def parse_words_and_tags(query)
words, tags = [], []
query.scan(/[\w#]+/).each do |token|
def parse_query_and_tags(search_string)
query, tags = [], []
search_string.scan(/[\w#]+/).each do |token|
if token.starts_with?("#")
tags << token
tags << token[1 .. -1]
else
words << token
query << token
end
end
[ words.join(" "), tags.join(" ") ]
[ query.join(" "), tags.join(", ") ]
end

end

0 comments on commit dcaf750

Please sign in to comment.