Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delegate indexed_name to setup of the joined model class #966

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 19 additions & 2 deletions sunspot/lib/sunspot/field.rb
Expand Up @@ -136,8 +136,7 @@ def set_indexed_name(options)
if options[:as]
options.delete(:as).to_s
else
name = options[:prefix] ? @name.to_s.sub(/^#{options[:prefix]}_/, '') : @name
"#{@type.indexed_name(name)}#{'m' if multiple? }#{'s' if @stored}#{'v' if more_like_this?}"
"#{@type.indexed_name(@name)}#{'m' if multiple? }#{'s' if @stored}#{'v' if more_like_this?}"
end
end

Expand Down Expand Up @@ -203,6 +202,7 @@ def initialize(name, type, options = {})
super(name, type, options)

@prefix = options.delete(:prefix)
@original_name = @prefix ? name.to_s.sub(/^#{@prefix}_/, '').to_sym : name
@join = options.delete(:join)
@clazz = options.delete(:clazz)
@target = options.delete(:target)
Expand All @@ -212,6 +212,17 @@ def initialize(name, type, options = {})
check_options(options)
end

def indexed_name
unless @indexed_name
factory = Sunspot::Setup.for(target).all_field_factories.
find { |f| f.field.type == @type && f.field.name == @original_name }

@indexed_name = factory && factory.field.indexed_name
end

@indexed_name
end

def from
Sunspot::Setup.for(target).field(@join[:from]).indexed_name
end
Expand All @@ -236,6 +247,12 @@ def target
@target = Util.full_const_get(@target) if @target.is_a?(String)
@target
end

private

def set_indexed_name(options)
super if options[:as]
end
end

class TypeField #:nodoc:
Expand Down
2 changes: 1 addition & 1 deletion sunspot/lib/sunspot/field_factory.rb
Expand Up @@ -11,7 +11,7 @@ module FieldFactory #:nodoc:all
# Base class for field factories.
#
class Abstract
attr_reader :name
attr_reader :name, :field

def initialize(name, options = {}, &block)
@name = name.to_sym
Expand Down