To edit pages or tickets please login with username/password: aaf/aaf

Changeset 329

Show
Ignore:
Timestamp:
02/18/08 20:36:08 (8 months ago)
Author:
jk
Message:

prevent multiple registrations of same class (happened in dev mode)

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/plugin/acts_as_ferret/lib/acts_as_ferret.rb

    r328 r329  
    217217    # re-register early loaded classes 
    218218    if pending_classes 
    219       pending_classes.each { |clazz| idx.register_class clazz, pending_classes_configs[clazz]
     219      pending_classes.each { |clazz| idx.register_class clazz, { :force_re_registration => true }.merge(pending_classes_configs[clazz])
    220220    end 
    221221 
  • trunk/plugin/acts_as_ferret/lib/index.rb

    r326 r329  
    3333    def register_class(clazz, options = {}) 
    3434      logger.info "register class #{clazz} with index #{index_name}" 
    35       index_definition[:registered_models] << clazz 
    36       @registered_models_config[clazz] = options 
    3735 
    38       # merge fields from this acts_as_ferret call with predefined fields 
    39       already_defined_fields = index_definition[:ferret_fields] 
    40       field_config = ActsAsFerret::build_field_config options[:fields] 
    41       field_config.update ActsAsFerret::build_field_config( options[:additional_fields] ) 
    42       field_config.each do |field, config| 
    43         if already_defined_fields.has_key?(field) 
    44           logger.info "ignoring redefinition of ferret field #{field}" if shared?  
    45         else 
    46           already_defined_fields[field] = config 
    47           logger.info "adding new field #{field} from class #{clazz.name} to index #{index_name}" 
     36      if force = options.delete(:force_re_registration) 
     37        index_definition[:registered_models].delete(clazz) 
     38      end 
     39 
     40      if index_definition[:registered_models].include?(clazz) 
     41        logger.info("refusing re-registration of class #{clazz}") 
     42      else 
     43        index_definition[:registered_models] << clazz 
     44        @registered_models_config[clazz] = options 
     45 
     46        # merge fields from this acts_as_ferret call with predefined fields 
     47        already_defined_fields = index_definition[:ferret_fields] 
     48        field_config = ActsAsFerret::build_field_config options[:fields] 
     49        field_config.update ActsAsFerret::build_field_config( options[:additional_fields] ) 
     50        field_config.each do |field, config| 
     51          if already_defined_fields.has_key?(field) 
     52            logger.info "ignoring redefinition of ferret field #{field}" if shared?  
     53          else 
     54            already_defined_fields[field] = config 
     55            logger.info "adding new field #{field} from class #{clazz.name} to index #{index_name}" 
     56          end 
     57        end 
     58 
     59        # update default field list to be used by the query parser, unless it  
     60        # was explicitly given by user. 
     61        # 
     62        # It will include all content fields *not* marked as :untokenized. 
     63        # This fixes the otherwise failing CommentTest#test_stopwords. Basically 
     64        # this means that by default only tokenized fields (which all fields are 
     65        # by default) will be searched. If you want to search inside the contents  
     66        # of an untokenized field, you'll have to explicitly specify it in your  
     67        # query. 
     68        unless index_definition[:user_default_field] 
     69          # grab all tokenized fields 
     70          ferret_fields = index_definition[:ferret_fields] 
     71          index_definition[:ferret][:default_field] = ferret_fields.keys.select do |field| 
     72            ferret_fields[field][:index] != :untokenized 
     73          end 
     74          logger.info "default field list for index #{index_name}: #{index_definition[:ferret][:default_field].inspect}" 
    4875        end 
    4976      end 
    50        
    51       # update default field list to be used by the query parser, unless it  
    52       # was explicitly given by user. 
    53       # 
    54       # It will include all content fields *not* marked as :untokenized. 
    55       # This fixes the otherwise failing CommentTest#test_stopwords. Basically 
    56       # this means that by default only tokenized fields (which all fields are 
    57       # by default) will be searched. If you want to search inside the contents  
    58       # of an untokenized field, you'll have to explicitly specify it in your  
    59       # query. 
    60       unless index_definition[:user_default_field] 
    61         # grab all tokenized fields 
    62         ferret_fields = index_definition[:ferret_fields] 
    63         index_definition[:ferret][:default_field] = ferret_fields.keys.select do |field| 
    64           ferret_fields[field][:index] != :untokenized 
    65         end 
    66         logger.info "default field list for index #{index_name}: #{index_definition[:ferret][:default_field].inspect}" 
    67       end 
    6877 
    69       index_definition 
     78      return index_definition 
    7079    end 
    7180 

To edit pages or tickets please login with username/password: aaf/aaf