Class: ActionBlocks::DataEngine

Inherits:
Object
  • Object
show all
Defined in:
lib/action_blocks/data_engine/data_engine.rb

Overview

Data Engine

Instance Method Summary collapse

Constructor Details

#initialize(root_klass, user: nil, table_alias_prefix: nil, select_reqs: [], select_fields: [], filter_reqs: [], selection_match_reqs: [], selection_filter_reqs: []) ⇒ DataEngine

Returns a new instance of DataEngine.



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/action_blocks/data_engine/data_engine.rb', line 4

def initialize(root_klass,
  user: nil,
  table_alias_prefix: nil,
  select_reqs: [],
  select_fields: [],
  filter_reqs: [],
  selection_match_reqs: [],
  selection_filter_reqs: []
)
  @root_klass = root_klass

  @filter_reqs = filter_reqs

  select_reqs_via_fields = select_fields.map(&:select_requirements)

  if [select_reqs].length > 0
    Rails.logger.warn "Passing select_reqs to Data Engine is deprecated."
  end
  all_select_reqs = [select_reqs, select_reqs_via_fields].flatten.compact

  select_reqs_for_field_engine = all_select_reqs.select { |r| r[:type].nil? }
  select_reqs_for_summary_engine = all_select_reqs.select { |r| r[:type] == :summary }

  if ActionBlocks.config[:should_authorize]
    if user.nil?
      raise "@user must be provided to data engine when should_authorize is configured"
    end
  end
  @user = user

  @fields_engine = ActionBlocks.config[:fields_engine].new(
    @root_klass,
    user: user,
    table_alias_prefix: table_alias_prefix,
    select_reqs: select_reqs_for_field_engine
  )

  @selections_engine = ActionBlocks.config[:selections_engine].new(
    @root_klass,
    user: user,
    table_alias_prefix: table_alias_prefix,
    selection_match_reqs: selection_match_reqs,
    selection_filter_reqs: selection_filter_reqs
  )

  # @filter_engine = ActionBlocks.config[:filter_engine].new(
  #   @root_klass,
  #   user: user,
  #   filter_reqs: filter_reqs,
  # )

  @summary_engine = ActionBlocks.config[:summary_engine].new(
    @root_klass,
    user: user,
    summary_reqs: select_reqs_for_summary_engine
  )

  # if ActionBlocks.config[:should_authorize]
  #   @authorization_engine = ActionBlocks.config[:authorization_engine].new(
  #     @root_klass,
  #     user: user
  #     # table_alias_prefix: table_alias_prefix,
  #     # select_reqs: select_reqs_for_field_engine
  #   )
  # end

  process
end

Instance Method Details

#first_to_jsonObject

Experimental



98
99
100
101
102
103
104
# File 'lib/action_blocks/data_engine/data_engine.rb', line 98

def first_to_json
  # SELECT row_to_json(r)
  sql = query.to_sql
  jsql = "select row_to_json(t)
      from (#{sql}) t"
  ActiveRecord::Base.connection.select_value(jsql)
end

#processObject



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/action_blocks/data_engine/data_engine.rb', line 73

def process
  @fields_engine.process
  @selections_engine.process
  @summary_engine.process
  # @filter_engine.process

  @filter_adapter = FilterAdapter.new(engine: @fields_engine, user: @user, filter_reqs: @filter_reqs)
  @filter_adapter.process

  if ActionBlocks.config[:should_authorize]
      @authorization_adapter = AuthorizationAdapter.new(engine: @fields_engine, user: @user)
      @authorization_adapter.process
  end

  
end

#queryObject



106
107
108
109
110
111
112
113
114
# File 'lib/action_blocks/data_engine/data_engine.rb', line 106

def query
  engine_queries = [
    @summary_engine.query,
    @selections_engine.query,
    @fields_engine.query,
    # @filter_engine.query,
  ]
  engine_queries.reduce(&:merge)
end

#to_jsonObject



90
91
92
93
94
95
# File 'lib/action_blocks/data_engine/data_engine.rb', line 90

def to_json
  sql = query.to_sql
  jsql = "select array_to_json(array_agg(row_to_json(t)))
      from (#{sql}) t"
  ActiveRecord::Base.connection.select_value(jsql)
end