Skip to content

Instantly share code, notes, and snippets.

@ni-ka
Created May 21, 2012 12:49
Show Gist options
  • Save ni-ka/2762162 to your computer and use it in GitHub Desktop.
Save ni-ka/2762162 to your computer and use it in GitHub Desktop.
monkey patch to allow use of dynamic fields in boost function
module Sunspot
class FieldFactory::Dynamic
# avoid use of : in field name
def build(dynamic_name)
AttributeField.new("#{@name}_#{dynamic_name}", @type, @options.dup)
end
end
class Setup
# support using dynamicfields by using function { :"dynamic_field:field_name"}
def field(field_name)
if m = field_name.to_s.match(/([^:]*):(.*)/)
dynamic_field_factory(m[1]).build(m[2])
elsif field_factory = @field_factories_cache[field_name.to_sym]
field_factory.build
else
raise(
UnrecognizedFieldError,
"No field configured for #{@class_name} with name '#{field_name}'"
)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment