Module: NoBrainer::Criteria::VirtualAttributes

Extended by:
ActiveSupport::Concern
Defined in:
lib/no_brainer/criteria/virtual_attributes.rb

Instance Method Summary collapse

Instance Method Details

#compile_rql_pass2Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/no_brainer/criteria/virtual_attributes.rb', line 4

def compile_rql_pass2
  rql = super

  if model.virtual_fields
    rql = rql.map do |_doc|
      model.virtual_fields.reduce(_doc) do |doc, field|
        field_rql = model.fields[field][:virtual].call(doc, RethinkDB::RQL.new)
        if field_rql.nil?
          doc
        else
          unless field_rql.is_a?(RethinkDB::RQL)
            raise "The virtual attribute `#{model}.#{field}' should return a RQL expression"
          end
          doc.merge(field => field_rql)
        end
      end
    end
  end

  rql
end