smart_search 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/init.rb ADDED
@@ -0,0 +1,5 @@
1
+ # -*- encoding : utf-8 -*-
2
+ require 'smart_search'
3
+ ActiveRecord::Base.send(:include, SmartSearch)
4
+
5
+
@@ -0,0 +1,8 @@
1
+ # -*- encoding : utf-8 -*-
2
+ class AddSearchTags < ActiveRecord::Migration
3
+
4
+ def self.add_to_table(table_name)
5
+ add_column table_name, :search_tags, :text
6
+ end
7
+
8
+ end
@@ -0,0 +1,148 @@
1
+ # -*- encoding : utf-8 -*-
2
+ module SmartSearch
3
+
4
+ def self.included(base)
5
+ base.extend ClassMethods
6
+ end
7
+
8
+ module ClassMethods
9
+ def smart_search(options = {:on => [], :conditions => nil, :group => nil, :order => "created_at"})
10
+ # Check if search_tags exists
11
+ unless is_smart_search?
12
+ cattr_accessor :condition_default, :group_default, :tags, :order
13
+ send :include, InstanceMethods
14
+ if self.column_names.index("search_tags").nil?
15
+ ::AddSearchTags.add_to_table(self.table_name)
16
+ end
17
+ self.send(:before_save, :create_search_tags)
18
+
19
+ # options zuweisen
20
+ if options[:conditions].is_a?(String) && !options[:conditions].blank?
21
+ self.condition_default = options[:conditions]
22
+ elsif !options[:conditions].nil?
23
+ raise ArgumentError, ":conditions must be a valid SQL Query"
24
+ end
25
+
26
+ if self.column_names.include?("created_at")
27
+ self.order = options[:order] || "created_at"
28
+ else
29
+ self.order = options[:order] || "id"
30
+ end
31
+
32
+ if options[:group].is_a?(String) && !options[:group].blank?
33
+ self.group_default = options[:group]
34
+ elsif !options[:group].nil?
35
+ raise ArgumentError, ":group must be a valid SQL Query"
36
+ end
37
+
38
+ self.tags = options[:on] || []
39
+ end
40
+ end
41
+
42
+ def is_smart_search?
43
+ self.included_modules.include?(InstanceMethods)
44
+ end
45
+
46
+ def result_template_path
47
+ "/smart/search/results/#{self.name.split("::").last.underscore}"
48
+ end
49
+
50
+ def find_by_tags(tags = "", conditions = {}, group_by = nil)
51
+ if self.is_smart_search?
52
+ tags = tags.split(" ")
53
+
54
+ # Fallback for Empty String
55
+ tags << "#" if tags.empty?
56
+
57
+ tags.map! {|t| "search_tags LIKE '%#{t.downcase}%'"}
58
+
59
+ if self.condition_default
60
+ default_cond = "#{self.condition_default} AND"
61
+ else
62
+ default_cond = ""
63
+ end
64
+
65
+ sql = "SELECT * FROM #{self.table_name} WHERE #{default_cond} (#{tags.join(' AND ')})"
66
+ if conditions.nil?
67
+ sql << ""
68
+ elsif conditions.is_a?(String)
69
+ sql << " AND #{conditions} "
70
+ else
71
+ condi = ""
72
+ conditions.each do |field, value|
73
+ case value
74
+ when true
75
+ value = 1
76
+ when false
77
+ value = 0
78
+ end
79
+ if value.is_a?(String) || value.is_a?(Numeric)
80
+ value = "'#{value}'"
81
+ condi << " AND #{field.to_s} = #{value}"
82
+ elsif value.is_a?(Array)
83
+ condi << " AND #{field.to_s} IN (#{value.join(",")})"
84
+ end
85
+
86
+ end
87
+ sql << " #{condi}"
88
+ end
89
+ if !group_by.nil?
90
+ sql << " GROUP BY #{group_by}"
91
+ elsif self.group_default
92
+ sql << " GROUP BY #{self.group_default}"
93
+ end
94
+
95
+ if !self.order.blank?
96
+ sql << " ORDER BY #{self.order}"
97
+ end
98
+
99
+ puts sql
100
+ self.find_by_sql(sql)
101
+ else
102
+ raise "#{self.inspect} is not a SmartSearch"
103
+ end
104
+ end
105
+
106
+ def set_search_index
107
+ s = self.all.size.to_f
108
+ self.all.each_with_index do |a, i|
109
+ a.create_search_tags
110
+ a.send(:update_without_callbacks)
111
+ done = ((i+1).to_f/s)*100
112
+ printf "Set search index for #{self.name}: #{done}%% \r"
113
+ end
114
+ end
115
+
116
+ end
117
+
118
+ module InstanceMethods
119
+
120
+ def result_template_path
121
+ self.class.result_template_path
122
+ end
123
+
124
+ def create_search_tags
125
+ tags = []
126
+ self.class.tags.each do |tag|
127
+ if tag.is_a?(Symbol)
128
+ tags << self.send(tag) rescue ""
129
+ elsif tag.is_a?(String)
130
+ tag_methods = tag.split(".")
131
+ tagx = self.send(tag_methods[0])
132
+ tag_methods[1..-1].each do |x|
133
+ tagx = tagx.send(x) rescue ""
134
+ end
135
+ tags << tagx
136
+ end
137
+ end
138
+ searchtags = tags.join(" ").split(" ")
139
+ searchtags = searchtags.uniq.join(" ")
140
+ search_tags_min = searchtags.gsub(" ", "").downcase
141
+
142
+ self.search_tags = "#{searchtags.downcase}"
143
+ end
144
+
145
+ end
146
+
147
+
148
+ end
metadata ADDED
@@ -0,0 +1,48 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: smart_search
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Florian Eck
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-03-11 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Adds easy to use full-text search to ActiveRecord models, based the attributes
15
+ you want to search.
16
+ email: it-support@friends-systems.de
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - init.rb
22
+ - lib/smart_search.rb
23
+ - lib/add_search_tags.rb
24
+ homepage:
25
+ licenses: []
26
+ post_install_message:
27
+ rdoc_options: []
28
+ require_paths:
29
+ - lib
30
+ required_ruby_version: !ruby/object:Gem::Requirement
31
+ none: false
32
+ requirements:
33
+ - - ! '>='
34
+ - !ruby/object:Gem::Version
35
+ version: '0'
36
+ required_rubygems_version: !ruby/object:Gem::Requirement
37
+ none: false
38
+ requirements:
39
+ - - ! '>='
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ requirements: []
43
+ rubyforge_project:
44
+ rubygems_version: 1.8.25
45
+ signing_key:
46
+ specification_version: 3
47
+ summary: Simple, easy to use search.
48
+ test_files: []