Module: Unidom::Common::Concerns::ExactColumn::ClassMethods

Defined in:
app/models/unidom/common/concerns/exact_column.rb

Instance Method Summary collapse

Instance Method Details

#exact_column(*names) ⇒ Object

配置精确索引列。如: class SomeModel

include Unidom::Common::Concerns::ExactColumn
exact_column :phone_number, :identification_number

end



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'app/models/unidom/common/concerns/exact_column.rb', line 22

def exact_column(*names)

  exact_column_names = exact_column_names||[]
  exact_column_names += names

  names.each do |name|
    name = name.to_s
    instance_eval do
      scope :"#{name}_is", ->(value) { where "#{name}_exact_signature" => exact_signature(self, name, value) }
      before_save do send "#{name}_exact_signature=", self.class.exact_signature(self.class, name, send(name)) end
    end
  end

end

#exact_signature(klass, name, value, secret_key_base: ) ⇒ Object

计算精确索引列的值。此方法被 exact_column 方法调用。



39
40
41
42
43
# File 'app/models/unidom/common/concerns/exact_column.rb', line 39

def exact_signature(klass, name, value, secret_key_base: Rails.application.secrets[:secret_key_base])
  text = "#{secret_key_base}/#{klass.table_name}##{name}=#{value}"
  #text = "#{Rails.application.secrets[:secret_key_base]}@#{Rails.root}/#{klass.table_name}##{name}=#{value}"
  "#{Digest::MD5.digest(text)}#{Digest::SHA512.digest(text)}"
end