plok 1.1.0 → 1.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/app/models/concerns/plok/searchable.rb +20 -11
- data/lib/generators/plok/search/result_object_generator.rb +41 -0
- data/lib/generators/plok/search/templates/result_object.html.erb +4 -0
- data/lib/generators/plok/search/templates/result_object.rb +19 -0
- data/lib/plok/search/result_objects/base.rb +12 -1
- data/lib/plok/version.rb +1 -1
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fe8e289922e36071642ae46ff78b49eded873476b9721668a5c9df0f8d51bd4f
|
4
|
+
data.tar.gz: 06ee8edde78842db587ee3e0076b9a791cd78247d9987ba07a5441bd0f03c270
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 562556d7edcf1db8f174203ba55fd3e7cf111ffdab3b1b63d0532a44f3d09d955c9ff7545e83a08e0ed7cf22dff9afd2b14fd5e0857217eae08448f47d8f2915
|
7
|
+
data.tar.gz: c2d565bdfdd0aae5244dd82d9e7bcd04dcb672a7cb7fcc7f8fe40bf5cdd87f3f085a4f33a51c912db353b76d633c0b53b9e5a98a40704b1d0e0d3640c78ec2de
|
@@ -16,17 +16,17 @@ module Plok::Searchable
|
|
16
16
|
end
|
17
17
|
|
18
18
|
def trigger_indices_save!
|
19
|
-
self.class.searchable_fields_list.each do |namespace,
|
20
|
-
|
21
|
-
if
|
19
|
+
self.class.searchable_fields_list.each do |namespace, fields|
|
20
|
+
fields.each do |field|
|
21
|
+
if field[:name] == :flexible_content
|
22
22
|
save_flexible_content_search_indices!(namespace) && next
|
23
23
|
end
|
24
24
|
|
25
|
-
if respond_to?(:translatable?) && self.class.translatable_fields_list.include?(
|
26
|
-
save_translatable_search_index!(namespace,
|
25
|
+
if respond_to?(:translatable?) && self.class.translatable_fields_list.include?(field[:name])
|
26
|
+
save_translatable_search_index!(namespace, field[:name]) && next
|
27
27
|
end
|
28
28
|
|
29
|
-
save_search_index!(namespace,
|
29
|
+
save_search_index!(namespace, field[:name], conditions: field[:conditions])
|
30
30
|
end
|
31
31
|
end
|
32
32
|
end
|
@@ -51,7 +51,11 @@ module Plok::Searchable
|
|
51
51
|
end
|
52
52
|
end
|
53
53
|
|
54
|
-
def save_search_index!(namespace, key, value: nil, locale: nil)
|
54
|
+
def save_search_index!(namespace, key, value: nil, locale: nil, conditions: [])
|
55
|
+
if conditions.any?
|
56
|
+
return unless conditions.map { |c| send(c) }.all?
|
57
|
+
end
|
58
|
+
|
55
59
|
value = if value.present?
|
56
60
|
value
|
57
61
|
elsif ActiveRecord::Base.connection.column_exists?(self.class.table_name, key)
|
@@ -81,18 +85,23 @@ module Plok::Searchable
|
|
81
85
|
end
|
82
86
|
|
83
87
|
module ClassMethods
|
84
|
-
def searchable_field(namespace, key)
|
85
|
-
return if searchable_fields_list.dig(namespace.to_sym)&.
|
88
|
+
def searchable_field(namespace, key, conditions)
|
89
|
+
return if !!searchable_fields_list.dig(namespace.to_sym)&.detect do |field|
|
90
|
+
field[:name] == key.to_sym
|
91
|
+
end
|
86
92
|
|
87
93
|
if searchable_fields_list[namespace.to_sym].blank?
|
88
94
|
searchable_fields_list[namespace.to_sym] = []
|
89
95
|
end
|
90
96
|
|
91
|
-
searchable_fields_list[namespace.to_sym] <<
|
97
|
+
searchable_fields_list[namespace.to_sym] << {
|
98
|
+
name: key.to_sym,
|
99
|
+
conditions: conditions
|
100
|
+
}
|
92
101
|
end
|
93
102
|
|
94
103
|
def plok_searchable(namespace: nil, fields: [], conditions: [])
|
95
|
-
fields.each { |key| searchable_field(namespace, key) }
|
104
|
+
fields.each { |key| searchable_field(namespace, key, conditions) }
|
96
105
|
end
|
97
106
|
|
98
107
|
def searchable_fields_list
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'rails/generators/base'
|
2
|
+
|
3
|
+
class Plok::Search::ResultObjectGenerator < Rails::Generators::Base
|
4
|
+
source_root File.expand_path('templates', __dir__)
|
5
|
+
class_option :name, type: :string
|
6
|
+
class_option :namespace, type: :string
|
7
|
+
|
8
|
+
def create
|
9
|
+
# TODO: Check for missing params
|
10
|
+
copy_file 'result_object.rb', class_path
|
11
|
+
gsub_file(class_path, '[name]', options.name)
|
12
|
+
gsub_file(class_path, '[namespace]', options.namespace)
|
13
|
+
gsub_file(class_path, '[class_name]', to_snakecase(options.name))
|
14
|
+
gsub_file(class_path, '[namespace_module_name]', to_snakecase(options.namespace))
|
15
|
+
|
16
|
+
copy_file 'result_object.html.erb', markup_path
|
17
|
+
gsub_file(markup_path, '[name]', options.name)
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def app_name
|
23
|
+
Rails.application.class.name.split('::').first
|
24
|
+
end
|
25
|
+
|
26
|
+
def class_path
|
27
|
+
"lib/plok/search/result_objects/#{options.namespace}/#{options.name}.rb"
|
28
|
+
end
|
29
|
+
|
30
|
+
def markup_path
|
31
|
+
"app/views/plok/search/result_objects/#{options.namespace}/_#{options.name}.html.erb"
|
32
|
+
end
|
33
|
+
|
34
|
+
def namespace_class_spec_path
|
35
|
+
"spec/lib/plok/search/#{options.namespace}_spec.rb"
|
36
|
+
end
|
37
|
+
|
38
|
+
def to_snakecase(name)
|
39
|
+
name.to_s.camelcase
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module Plok::Search::ResultObjects
|
2
|
+
# This class should be used to further manipulate any of the data provided
|
3
|
+
# through #search_context or Plok::Search::ResultObjects::Base.
|
4
|
+
#
|
5
|
+
# A search context class is accessible through #search_context. This
|
6
|
+
# gives you access to #search_context.controller, which can be used to
|
7
|
+
# call routes upon.
|
8
|
+
#
|
9
|
+
# Example of: If an autocomplete requires additional data to be rendered in
|
10
|
+
# the partial (think another model, or an API call), one could override
|
11
|
+
# the #locals method to include more variables. You could do this directly
|
12
|
+
# in the partial as well, but this way we have separation of concerns.
|
13
|
+
class [namespace_module_name]::[class_name] < Plok::Search::ResultObjects::Base
|
14
|
+
def url
|
15
|
+
# TODO: Add a path to where people should end up by clicking the result.
|
16
|
+
#search_context.controller.[namespace]_[name]_path(searchable)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -52,12 +52,23 @@ module Plok
|
|
52
52
|
{ "#{partial_target}": index.searchable, index: index }
|
53
53
|
end
|
54
54
|
|
55
|
+
def namespace
|
56
|
+
search_context.namespace.to_s.underscore
|
57
|
+
end
|
58
|
+
|
55
59
|
def partial
|
56
60
|
"#{partial_path}/#{partial_target}"
|
57
61
|
end
|
58
62
|
|
59
63
|
def partial_path
|
60
|
-
|
64
|
+
# TODO: This is a fallback for older Plok versions whose result object
|
65
|
+
# partials still reside in app/views/backens/search/*. Best to remove
|
66
|
+
# this at a later stage, but for now they can be backwards compatible.
|
67
|
+
if File.exists?("app/views/#{namespace}/search/_#{partial_target}.html.erb")
|
68
|
+
return "#{namespace}/search/"
|
69
|
+
end
|
70
|
+
|
71
|
+
"plok/search/result_objects/#{namespace}"
|
61
72
|
end
|
62
73
|
|
63
74
|
def partial_target
|
data/lib/plok/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: plok
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.1.
|
4
|
+
version: 1.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Davy Hellemans
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2022-11-
|
12
|
+
date: 2022-11-09 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rails
|
@@ -108,8 +108,11 @@ files:
|
|
108
108
|
- db/migrate/20221031143932_add_namespace_to_search_indices.rb
|
109
109
|
- lib/generators/plok/search/USAGE
|
110
110
|
- lib/generators/plok/search/class_generator.rb
|
111
|
+
- lib/generators/plok/search/result_object_generator.rb
|
111
112
|
- lib/generators/plok/search/templates/namespace_class.rb
|
112
113
|
- lib/generators/plok/search/templates/namespace_class_spec.rb
|
114
|
+
- lib/generators/plok/search/templates/result_object.html.erb
|
115
|
+
- lib/generators/plok/search/templates/result_object.rb
|
113
116
|
- lib/generators/plok/sidebar/USAGE
|
114
117
|
- lib/generators/plok/sidebar/sidebar_generator.rb
|
115
118
|
- lib/generators/plok/sidebar/templates/_menu_item.html.erb
|