module ErpSearch::Extensions::ActiveRecord::HasDynamicSolrSearch::ClassMethods

Public Instance Methods

convert_xtype_to_attribute_type(xtype) click to toggle source
# File lib/erp_search/extensions/active_record/has_dynamic_solr_search.rb, line 87
def convert_xtype_to_attribute_type(xtype)
  case xtype
  when 'numberfield'
    return 'integer'
  when 'checkbox'
    return 'boolean'
  when 'datefield'
    return 'date'
  when 'timefield'
    return 'time'
  else
    return 'string'
  end
end
is_searchable?() click to toggle source
# File lib/erp_search/extensions/active_record/has_dynamic_solr_search.rb, line 27
def is_searchable?
  respond_to?(:solr_search)
end
sunspot_setup(options={}) click to toggle source
# File lib/erp_search/extensions/active_record/has_dynamic_solr_search.rb, line 50
    def sunspot_setup(options={})
      klass = DynamicFormModel.get_constant(self.name)
      Sunspot::Setup.clear_setup_for_class(klass)
      definition = DynamicForm.get_form(self.name).definition_object rescue nil

      unless definition.nil?
        #Rails.logger.info "calling sunspot setup"
        Sunspot.setup(klass) do
          integer :id
          date :created_at do data.created_at end
          date :updated_at do data.updated_at end
          string :created_by do data.created_by.username rescue nil end
          string :updated_by do data.updated_by.username rescue nil end
          definition.each do |f|
            next unless f[:searchable]
            atype = convert_xtype_to_attribute_type(f[:xtype])
            # respond_to allows us to support both static and dynamic attributes on model
            if self.respond_to?(f[:name].to_sym)
              send('text', f[:name].to_sym) if atype == 'string' # enable full text search for strings
              send(atype, f[:name].to_sym) # configure attribute fields for scoping, faceting, ordering, etc
            else
              dyn_attr_key = DynamicDatum::DYNAMIC_ATTRIBUTE_PREFIX + f[:name]
              if f[:xtype] == 'related_combobox'
                extraction_block = proc { DynamicDatum.related_data_value(f[:extraParams]['model'], data.send(dyn_attr_key), f[:displayField]) }
              else
                extraction_block = proc { data.send(dyn_attr_key) }
              end
              send('text', f[:name].to_sym, &extraction_block) if atype == 'string' # enable full text search for strings
              send(atype, f[:name].to_sym, &extraction_block) # configure attribute fields for scoping, faceting, ordering, etc
            end
          end
        end
      end

      self.sunspot_options = options
    end

    def convert_xtype_to_attribute_type(xtype)
      case xtype
      when 'numberfield'
        return 'integer'
      when 'checkbox'
        return 'boolean'
      when 'datefield'
        return 'date'
      when 'timefield'
        return 'time'
      else
        return 'string'
      end
    end
  end

  module InstanceMethods

    def sunspot_setup(options={})
      self.class.sunspot_setup(options)
    end

  end 
end