Class: PostJson::QueryTranslator

Inherits:
Object
  • Object
show all
Includes:
FinderMethods
Defined in:
lib/post_json/query_translator.rb

Instance Method Summary collapse

Methods included from FinderMethods

#any?, #blank?, #count, #delete, #delete_all, #destroy, #destroy_all, #empty?, #exists?, #find, #find_by, #find_by!, #find_each, #find_in_batches, #first, #first!, #first_or_create, #first_or_initialize, #ids, #last, #last!, #load, #many?, #pluck, #select, #size, #take, #take!, #to_a, #to_sql, #where_values_hash

Methods included from ArgumentMethods

#assert_valid_indifferent_keys, #flatten_arguments, #join_arguments

Methods included from QueryMethods

#add_query, #except, #except!, #limit, #limit!, #offset, #offset!, #only, #only!, #order, #order!, #page, #page!, #query_clone, #query_tree, #query_tree_renew!, #reorder, #reorder!, #reverse_order, #reverse_order!, #where, #where!

Constructor Details

#initialize(relation) ⇒ QueryTranslator

Returns a new instance of QueryTranslator.


6
7
8
# File 'lib/post_json/query_translator.rb', line 6

def initialize(relation)
  @relation = relation
end

Instance Method Details

#create(attributes = {}) ⇒ Object


48
49
50
# File 'lib/post_json/query_translator.rb', line 48

def create(attributes = {})
  relation_query.create(attributes.with_indifferent_access)
end

#each(&block) ⇒ Object


22
23
24
# File 'lib/post_json/query_translator.rb', line 22

def each(&block)
  relation_query.each(&block)
end

#execute(ignore_dynamic_indexes = false, &block) ⇒ Object


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/post_json/query_translator.rb', line 26

def execute(ignore_dynamic_indexes = false, &block)
  if ignore_dynamic_indexes == true || model_class.use_dynamic_index != true
    block.call(relation_query)
  else
    result = block.call(relation_query)
    select_query = ActiveRecord::Base.connection.last_select_query
    select_duration = ActiveRecord::Base.connection.last_select_query_duration * 1000
    if model_class.use_dynamic_index == true &&
       model_class.create_dynamic_index_milliseconds_threshold < select_duration
      selectors = select_query.scan(/.*?json_selector\('(.*?)', \"post_json_documents\"\.__doc__body\)/).flatten.uniq
      model_class.create_dynamic_indexes(selectors)
    end
    result
  end
end

#model_classObject


10
11
12
# File 'lib/post_json/query_translator.rb', line 10

def model_class
  @relation.klass
end

#primary_keyObject


14
15
16
# File 'lib/post_json/query_translator.rb', line 14

def primary_key
  model_class.primary_key
end

#relation_queryObject


42
43
44
45
46
# File 'lib/post_json/query_translator.rb', line 42

def relation_query
  active_record_send_invocations.inject(@relation) do |query, send_arguments|
    query.send(*send_arguments)
  end
end

#table_nameObject


18
19
20
# File 'lib/post_json/query_translator.rb', line 18

def table_name
  model_class.table_name
end