Module: Runestone::ActiveRecord::BaseMethods

Extended by:
ActiveSupport::Concern
Defined in:
lib/runestone/active_record/base_methods.rb

Instance Method Summary collapse

Instance Method Details

#create_runestonesObject



76
77
78
# File 'lib/runestone/active_record/base_methods.rb', line 76

def create_runestones
  Runestone::IndexingJob.perform_later(self, :create_runestones!)
end

#create_runestones!Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/runestone/active_record/base_methods.rb', line 80

def create_runestones!
  conn = Runestone::Model.connection
  self.runestone_settings.each do |index_name, settings|
    settings.each do |setting|
      rdata = setting.extract_attributes(self)

      ts_column_names = %w(record_type record_id name dictionary data vector).map { |name| conn.quote_column_name(name) }
      ts_values = [
        conn.quote(conn.send(:type_map).lookup('varchar').serialize(self.class.base_class.name)),
        conn.quote(conn.send(:type_map).lookup('uuid').serialize(id)),
        index_name == :default ? 'NULL' : conn.quote(conn.send(:type_map).lookup('varchar').serialize(index_name.to_s)),
        conn.quote(conn.send(:type_map).lookup('varchar').serialize(setting.dictionary)),
        conn.quote(conn.send(:type_map).lookup('jsonb').serialize(rdata)),
        setting.vectorize(rdata).join(' || ')
      ]
      conn.execute(<<-SQL)
        INSERT INTO #{Runestone::Model.quoted_table_name} (#{ts_column_names.join(",")})
        VALUES (#{ts_values.join(',')})
      SQL

      Runestone::Corpus.add(*setting.corpus(rdata))
    end
  end
end

#update_runestonesObject



105
106
107
# File 'lib/runestone/active_record/base_methods.rb', line 105

def update_runestones
  Runestone::IndexingJob.preform_later(self, :update_runestones!)
end

#update_runestones!(if_changed = true) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/runestone/active_record/base_methods.rb', line 109

def update_runestones!(if_changed = true)
  conn = Runestone::Model.connection
  self.runestone_settings.each do |index_name, settings|
    settings.each do |setting|
      next if if_changed && !setting.changed?(self)
      rdata = setting.extract_attributes(self)

      if conn.execute(<<-SQL).cmd_tuples == 0
          UPDATE #{Runestone::Model.quoted_table_name}
          SET
            data = #{conn.quote(conn.send(:type_map).lookup('jsonb').serialize(rdata))},
            vector = #{setting.vectorize(rdata).join(' || ')}
          WHERE record_type = #{conn.quote(conn.send(:type_map).lookup('varchar').serialize(self.class.base_class.name))}
          AND record_id = #{conn.quote(conn.send(:type_map).lookup('integer').serialize(id))}
          AND name #{index_name == :default ? 'IS NULL' : "= " + conn.quote(conn.send(:type_map).lookup('integer').serialize(index_name))}
          AND dictionary = #{conn.quote(conn.send(:type_map).lookup('integer').serialize(setting.dictionary))}
          SQL
        create_runestones!
      else
        Runestone::Corpus.add(*setting.corpus(rdata))
      end

    end
  end
end