Module: ErpSearch::Extensions::ActiveRecord::HasDynamicSolrSearch::ClassMethods

Defined in:
lib/erp_search/extensions/active_record/has_dynamic_solr_search.rb

Instance Method Summary collapse

Instance Method Details

#convert_xtype_to_attribute_type(xtype) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
# 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

#has_dynamic_solr_searchObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/erp_search/extensions/active_record/has_dynamic_solr_search.rb', line 31

def has_dynamic_solr_search
  include HasDynamicSolrSearch::InstanceMethods   
  
  extend Sunspot::Rails::Searchable::ClassMethods
  include Sunspot::Rails::Searchable::InstanceMethods

  class_attribute :sunspot_options
  
  before_save :mark_for_auto_indexing_or_removal
  after_save :perform_index_tasks

  after_destroy do |searchable|
    searchable.remove_from_index
  end
  
  # init search setup
  self.sunspot_setup                       
end

#is_searchable?Boolean

Returns:

  • (Boolean)


27
28
29
# 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 = {}) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# 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