@thekingoftruth, . Filter github, , .
, HABTM.
:
searchable do
text :name, :description, :excerpt
text :venue_name do
venue.name if venue.present?
end
text :artist_name do
artists.map { |a| a.name if a.present? } if artists.present?
end
end
, :
(: config/initializers/sunspot.rb)
module Sunspot
module DataExtractor
class AttributeExtractor
def initialize(attribute_name)
@attribute_name = attribute_name
end
def value_for(object)
Filter.new( object.send(@attribute_name) ).value
end
end
class BlockExtractor
def initialize(&block)
@block = block
end
def value_for(object)
Filter.new( Util.instance_eval_or_call(object, &@block) ).value
end
end
class Constant
def initialize(value)
@value = value
end
def value_for(object)
Filter.new(@value).value
end
end
class Filter
def initialize(value)
@value = value
end
def value
if @value.is_a? String
strip_control_characters_from_string @value
elsif @value.is_a? Array
@value.map { |v| strip_control_characters_from_string v }
elsif @value.is_a? Hash
@value.inject({}) do |hash, (k, v)|
hash.merge( strip_control_characters_from_string(k) => strip_control_characters_from_string(v) )
end
else
@value
end
end
def strip_control_characters_from_string(value)
return value unless value.is_a? String
value.chars.inject("") do |str, char|
unless char.ascii_only? && (char.ord < 32 || char.ord == 127)
str << char
end
str
end
end
end
end
end