Skip to content

Commit

Permalink
attribute_highlights returns an array of highlights
Browse files Browse the repository at this point in the history
  • Loading branch information
musaffa authored and Dalton committed Feb 11, 2021
1 parent cd71c83 commit ac8e710
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 3 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
## Bug fixes

* [#695](https://github.com/toptal/chewy/pull/695): Clear the scroll by id after completing scroll_batches ([@socialchorus][])
* [#749](https://github.com/toptal/chewy/pull/749): Avoid importing everything when given an empty relation ([@JF-Lalonde][][@dalthon][])
* [#749](https://github.com/toptal/chewy/pull/749): Avoid importing everything when given an empty relation ([@JF-Lalonde][] [@dalthon][])
* [#736](https://github.com/toptal/chewy/pull/736): Fix nil children when using witchcraft ([@taylor-au][])

* [#755](https://github.com/toptal/chewy/pull/755): attribute_highlights returns an array of highlights ([@musaffa][] [@dalthon][])
## Changes

* [#753](https://github.com/toptal/chewy/pull/753): Add support for direct_import parameter to skip objects reloading ([@TikiTDO][][@dalthon][])
Expand Down Expand Up @@ -556,6 +556,7 @@
[@mkcode]: https://github.com/@mkcode
[@mrbrdo]: https://github.com/@mrbrdo
[@mrzasa]: https://github.com/mrzasa
[@musaffa]: https://github.com/musaffa
[@nattfodd]: https://github.com/nattfodd
[@okliv]: https://github.com/okliv
[@olancheg]: https://github.com/olancheg
Expand Down
12 changes: 11 additions & 1 deletion lib/chewy/type/wrapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ def method_missing(method, *args, &block)
m = method.to_s
if (name = highlight_name(m))
highlight(name)
elsif (name = highlight_names(m))
highlights(name)
elsif @attributes.key?(m)
@attributes[m]
elsif attribute_defined?(m)
Expand All @@ -60,7 +62,7 @@ def method_missing(method, *args, &block)

def respond_to_missing?(method, include_private = false)
m = method.to_s
highlight_name(m) || @attributes.key?(m) || attribute_defined?(m) || super
highlight_name(m) || highlight_names(m) || @attributes.key?(m) || attribute_defined?(m) || super
end

private
Expand All @@ -69,6 +71,10 @@ def highlight_name(method)
method.sub(/_highlight\z/, '') if method.end_with?('_highlight')
end

def highlight_names(method)
method.sub(/_highlights\z/, '') if method.end_with?('_highlights')
end

def attribute_defined?(attribute)
self.class.root && self.class.root.children.find { |a| a.name.to_s == attribute }.present?
end
Expand All @@ -77,6 +83,10 @@ def highlight(attribute)
_data['highlight'][attribute].first if highlight?(attribute)
end

def highlights(attribute)
_data['highlight'][attribute] if highlight?(attribute)
end

def highlight?(attribute)
_data.key?('highlight') && _data['highlight'].key?(attribute)
end
Expand Down
1 change: 1 addition & 0 deletions spec/chewy/query_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
specify { expect(subject.offset(6).count).to eq(3) }
specify { expect(subject.query(match: {name: 'name3'}).highlight(fields: {name: {}}).first.name).to eq('Name3') }
specify { expect(subject.query(match: {name: 'name3'}).highlight(fields: {name: {}}).first.name_highlight).to eq('<em>Name3</em>') }
specify { expect(subject.query(match: {name: 'name3'}).highlight(fields: {name: {}}).first.name_highlights).to eq(['<em>Name3</em>']) }
specify { expect(subject.query({}).highlight(fields: {name: {}}).first.name_highlight).to eq(nil) }
specify { expect(subject.query(match: {name: 'name3'}).highlight(fields: {name: {}}).first._data['_source']['name']).to eq('Name3') }
specify { expect(subject.types(:product).count).to eq(3) }
Expand Down
1 change: 1 addition & 0 deletions spec/chewy/type/wrapper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@

it do
is_expected.to respond_to(:name_highlight)
.and respond_to(:name_highlights)
.and have_attributes(
name: 'Martin',
name_highlight: '<b>Mar</b>tin'
Expand Down

0 comments on commit ac8e710

Please sign in to comment.