locomotive_search_ext_plugin 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ MzIwODQyYTI2MDI0MjdkNzBhMTM2YzFkMzMzNDYyOTUyYjcxNWQyMg==
5
+ data.tar.gz: !binary |-
6
+ MjI4NGZkMTVhZTM4NmQzZjlhNTlkMTQwYjAwZDI2Y2VjOTQ0NjBkNA==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ ZmMwYmQ5Y2RkYTg5ZjNhNWI4ZWJmMzYyMzIwZjIyNjkwOWM5ZTg2M2E4ZjU2
10
+ ZWFkOGU3ZDc2ZGM0M2RmYWEwNjhkNjI0YzllNzc3NzhmMGUzNzlkNmM2NmFm
11
+ MTlhNGJiZTljZTljYTMwYWRlNjEzZDlkNDViNTNhYzNmMzM5YjY=
12
+ data.tar.gz: !binary |-
13
+ NGZhMWRiNDExMGVlZTJjODQyYzYxMmI1YTNlODY1MDdkZjk3YTM1YzMzYzBj
14
+ OTY1MzU2YzkwNThkZGE2MDExZDI0ZDBlMTA5MzQ3ZDMzMjViZWM0MTlhNzkw
15
+ OTFiM2YwYzk5NjlmYjZkNDg0NzEwY2Y4MDc2MTFlMWE5MTJjZTY=
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in search_hb_plugin.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Charlie Greene
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.textile ADDED
@@ -0,0 +1,73 @@
1
+ h1. Search Plugin
2
+
3
+ This is a plugin for LocomotiveCMS. It was created to allow LocomotiveCMS designers to add some functionallity to the LocomotiveCMS search gem.
4
+
5
+ It provides a few liwuid tags that can be used to improve the search functionality.
6
+
7
+ h2. Installation
8
+
9
+ To use this plugin you must be using a version of LocomotiveCMS that has the plugins feature designed by "Colibri Software":https://www.colibri-software.com. You can do this by making the following changes to the Gemfile in you app:
10
+
11
+ * Remove or comment out the following line:
12
+ @gem 'locomotive_cms', '~> 2.X.X', require: 'locomotive/engine'@
13
+ * Add the following line:
14
+ @gem 'locomotive_cms', require: 'locomotive/engine', git: 'https://github.com/colibri-software/locomotive_engine.git', branch: 'plugins'@
15
+
16
+ Then add the following lines in your Gemfile to include the plugin:
17
+ <pre><code>group :locomotive_plugins do
18
+ gem 'locomotive_search_ext_plugin'
19
+ end</code></pre>
20
+
21
+ This plugin also requires the LocomotiveCMS Search gem. To add it add the following to your gemfile:
22
+ @gem 'locomotivecms-search', require: 'locomotive/search/mongoid'@
23
+
24
+ h2. Usage
25
+
26
+ h3. Configuring Plugin
27
+
28
+ This plugin requires no configuration.
29
+
30
+ h3. Liquid Drops
31
+
32
+ This plugin has no Liquid Drops
33
+
34
+ h3. Liquid Tags
35
+
36
+ This plugin provides several tags that will improve the search functionallity.
37
+
38
+ The following is a list of tags and their options(and defaults):
39
+
40
+ * search_assign
41
+ ** Used to turn a search result into a page or content_entry
42
+ ** Usage:
43
+ @{% search_assign <search_result> to <variabel to assign to>: <variable for content_type name> %}@
44
+ ** Example:
45
+ <pre><code>{% for result in site.search %}
46
+ {% search_assign result to object: flag %}
47
+ {% if object %}
48
+ {% link_to object%}
49
+ {{ object_description }}
50
+ {% if flag %}
51
+ <p>{{flag}}</p>
52
+ {% else %}
53
+ <p>Page</p>
54
+ {% endif %}
55
+ {% endif %}
56
+ {% endfor %}</code></pre>
57
+ * search_custom_type
58
+ ** Used to do a search on a specific model using specific fields
59
+ ** Usage:
60
+ @{% search_custom_type <content_type> on <comma seperated fields> to <variable to store results>%}@
61
+ ** Example:
62
+ <pre><code><form id="members" action="/members">
63
+ <input type="text" name="category_id" />
64
+ </form>
65
+ {% search_custom_type members on fields category_id to results %}
66
+ {% for member in members %}
67
+ {{member.name}}
68
+ {% endfor %}</code></pre>
69
+
70
+
71
+ h3. Liquid Filters
72
+
73
+ This plugin has no Liquid Filters
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,39 @@
1
+ require "activesearch/proxy"
2
+
3
+ module ActiveSearch
4
+ class Proxy
5
+ include Enumerable
6
+
7
+ def initialize(text, conditions, options = {}, &implementation)
8
+ @text = text
9
+ @conditions = conditions
10
+ @options = options
11
+ @implementation = implementation
12
+ end
13
+
14
+ def each(&block)
15
+ @implementation.call(@text, @conditions).each do |result|
16
+ block.call(Result.new(result, @text, @options), generate_score(result))
17
+ end
18
+ end
19
+
20
+ protected
21
+
22
+ def generate_score(result)
23
+ score = 0
24
+ result.each do |k,v|
25
+ score += 1 if text_words.include?(k)
26
+ if v.class == String
27
+ v.split.each do |word|
28
+ score += 1 if text_words.include?(word)
29
+ end
30
+ end
31
+ end
32
+ score
33
+ end
34
+
35
+ def text_words
36
+ @text_words ||= @text.scan(/\w+|\n/)
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,23 @@
1
+ require "locomotive/search_ext/plugin/version"
2
+ require "locomotive/search_ext/plugin/assign"
3
+ require "locomotive/search_ext/plugin/custom_type"
4
+ require "activesearch/proxy_score"
5
+
6
+ module Locomotive
7
+ module SearchExt
8
+ class Plugin
9
+ include Locomotive::Plugin
10
+
11
+ def self.default_plugin_id
12
+ 'search_ext'
13
+ end
14
+
15
+ def self.liquid_tags
16
+ {
17
+ :assign => Assign,
18
+ :custom_type => CustomType
19
+ }
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,51 @@
1
+ module Locomotive
2
+ module SearchExt
3
+ class Assign < ::Liquid::Tag
4
+ include ActionView::Helpers::SanitizeHelper
5
+ Syntax = /(#{::Liquid::VariableSignature}+)\s*to\s*(#{::Liquid::VariableSignature}+):\s*(#{::Liquid::VariableSignature}+)/
6
+
7
+ def initialize(tag_name, markup, tokens, context)
8
+ if markup =~ Syntax
9
+ @results = $1
10
+ @target = $2
11
+ @description = "#{@target}_description"
12
+ @flag = $3
13
+ @options = {}
14
+ markup.scan(::Liquid::TagAttributes) { |key, value| @options[key.to_sym] = value.gsub(/"|'/, '') }
15
+ else
16
+ raise ::Liquid::SyntaxError.new("Syntax Error in 'search_assign' - Valid syntax: search_assign results to variable: pagetag")
17
+ end
18
+
19
+ super
20
+ end
21
+
22
+ def render(context)
23
+ @site = context.registers[:site]
24
+ @result = context[@results][0]
25
+ result_data = false
26
+
27
+ if @result['content_type_slug']
28
+ # its a model entry
29
+ model = @site.content_types.where(slug: @result['content_type_slug']).first
30
+ model_item = model.entries.find(@result['original_id']).to_liquid
31
+ result_data = model_item if model_item
32
+ context[@flag.to_s] = model.name
33
+ context[@description.to_s] = @result
34
+ else
35
+ # it is a page
36
+ page = @site.pages.find(@result['original_id']).to_liquid
37
+ result_data = page if page
38
+ context[@flag.to_s] = false
39
+
40
+ res = @result['highlighted']['searchable_content']
41
+
42
+ context[@description] = strip_tags(res)
43
+ end
44
+ context[@target.to_s] = result_data
45
+ Rails.logger.error(result_data)
46
+ ""
47
+ end
48
+ end
49
+ end
50
+ end
51
+
@@ -0,0 +1,100 @@
1
+ #require 'ruby-prof'
2
+
3
+ module Locomotive
4
+ module SearchExt
5
+ class CustomType < ::Liquid::Tag
6
+
7
+ Syntax = /(#{::Liquid::QuotedFragment})\s*on fields\s*(#{::Liquid::QuotedFragment})\s*to\s*(#{::Liquid::QuotedFragment})/
8
+
9
+ def initialize(tag_name, markup, tokens, context)
10
+ if markup =~ Syntax
11
+ @content_type_slug = $1
12
+ @fields = $2.split(',')
13
+ @target = $3
14
+ else
15
+ raise ::Liquid::SyntaxError.new("Syntax Error in 'search_custom_type - Valid syntax: search_custom_type 'content_type_slug' on fields 'field_a, field_b' to results")
16
+ end
17
+
18
+ #@site = context.registers[:site]
19
+ #content_type = @site.content_type.where(slug: @content_type_slug).first
20
+ #raise ::Liquid::SyntaxError.new("Syntax Error in 'search_custom_type': #{@content_type_slug} is not a content type ") unless content_type
21
+ #real_fields = content_type.entries_custom_fields.collect(&:label)
22
+ #@fields.each do |field|
23
+ # unless real_fields.include?(field)
24
+ # raise ::Liquid::SyntaxError.new("Syntax Error in 'search_custom_type':#{field} is not a field on #{@content_type_slug}")
25
+ # end
26
+ #end
27
+ super
28
+ end
29
+
30
+ def render(context)
31
+ @site = context.registers[:site]
32
+ content_type = @site.content_types.where(slug: @content_type_slug).first
33
+ raise ::Liquid::Error.new("Error in 'search_custom_type': #{@content_type_slug} is not a content type ") unless content_type
34
+ @fields.each do |field|
35
+ unless content_type.entries.first.respond_to?(field)
36
+ raise ::Liquid::Error.new("Error in 'search_custom_type':#{field} is not a field on #{@content_type_slug}")
37
+ end
38
+ end
39
+
40
+ #profile = RubyProf.profile do
41
+ search = nil
42
+ search_terms = context.registers[:controller].params[:search]
43
+ if search_terms and !search_terms.empty?
44
+ Rails.logger.warn "Params: #{search_terms}, { \"site_id\" => #{@site.id}, \"content_type_slug\" => #{@content_type_slug} }"
45
+ search = ::ActiveSearch.search(
46
+ search_terms,
47
+ {
48
+ "site_id" => @site.id,
49
+ "content_type_slug" => @content_type_slug,
50
+ }
51
+ )
52
+ end
53
+
54
+ @fields.select! do |field|
55
+ param = context.registers[:controller].params[field]
56
+ param && !param.empty?
57
+ end
58
+
59
+ if search
60
+ slugs = {}
61
+ search.each do |result,score|
62
+ Rails.logger.warn "search: #{result}"
63
+ slugs[result['_slug']] = score
64
+ end
65
+ end
66
+
67
+ Rails.logger.warn "Seach Slugs: #{slugs}"
68
+ final_results = []
69
+ content_type.ordered_entries(context["with_scope"]).each do |entry|
70
+ if !search or slugs.has_key?(entry._slug)
71
+ if match_fields(context, entry)
72
+ idx = 0
73
+ idx = slugs[entry._slug] if search
74
+ final_results[idx] ||= []
75
+ final_results[idx] << entry.to_liquid
76
+ end
77
+ end
78
+ end
79
+
80
+ context[@target.to_s] = final_results.reverse.flatten.compact
81
+ #end
82
+ # Print a graph profile to text
83
+ #printer = RubyProf::MultiPrinter.new(profile)
84
+ #printer.print(:path => '/home/greeneca/colibri/tmp/profile')
85
+
86
+ ""
87
+ end
88
+
89
+ private
90
+
91
+ def match_fields(context, model_item)
92
+ @fields.each do |field|
93
+ return false unless model_item[field].to_s == context.registers[:controller].params[field]
94
+ end
95
+ true
96
+ end
97
+ end
98
+ end
99
+ end
100
+
@@ -0,0 +1,5 @@
1
+ module Locomotive
2
+ module SearchExt
3
+ VERSION = "1.0.0"
4
+ end
5
+ end
@@ -0,0 +1 @@
1
+ require 'locomotive/search_ext/plugin'
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'locomotive/search_ext/plugin/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "locomotive_search_ext_plugin"
8
+ spec.version = Locomotive::SearchExt::VERSION
9
+ spec.authors = ["Charlie Greene"]
10
+ spec.email = ["greeneca@gmail.com"]
11
+ spec.description = %q{Provides search display tag.}
12
+ spec.summary = %q{Provides a simple search display tag.}
13
+ spec.homepage = "http://colibri-software.com"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "locomotive_plugins", '~> 1.0'
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.3"
24
+ spec.add_development_dependency "rake"
25
+ end
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: locomotive_search_ext_plugin
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Charlie Greene
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-10-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: locomotive_plugins
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '1.3'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '1.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Provides search display tag.
56
+ email:
57
+ - greeneca@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - .gitignore
63
+ - Gemfile
64
+ - LICENSE.txt
65
+ - README.textile
66
+ - Rakefile
67
+ - lib/activesearch/proxy_score.rb
68
+ - lib/locomotive/search_ext/plugin.rb
69
+ - lib/locomotive/search_ext/plugin/assign.rb
70
+ - lib/locomotive/search_ext/plugin/custom_type.rb
71
+ - lib/locomotive/search_ext/plugin/version.rb
72
+ - lib/locomotive_search_ext_plugin.rb
73
+ - locomotive_search_ext_plugin.gemspec
74
+ homepage: http://colibri-software.com
75
+ licenses:
76
+ - MIT
77
+ metadata: {}
78
+ post_install_message:
79
+ rdoc_options: []
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ! '>='
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ! '>='
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ requirements: []
93
+ rubyforge_project:
94
+ rubygems_version: 2.2.2
95
+ signing_key:
96
+ specification_version: 4
97
+ summary: Provides a simple search display tag.
98
+ test_files: []