Class: ActionBlocks::SummaryFieldBuilder

Inherits:
FieldBlock show all
Includes:
SummaryFieldAggregationFunctions
Defined in:
lib/action_blocks/builders/model_builder.rb

Instance Attribute Summary collapse

Attributes inherited from FieldBlock

#field_type, #parent_model

Attributes inherited from BaseBuilder

#dsl_delegate, #id

Instance Method Summary collapse

Methods included from SummaryFieldAggregationFunctions

#concat, #count, #every

Methods inherited from FieldBlock

#key, #match_requirements

Methods inherited from BlockType

#is_block?, #type

Methods inherited from BaseBuilder

#after_load, array_fields, block_type, builds, builds_many, delegate_class, #evaluate, #freeze, includes_scheme_helpers, #initialize, #is_block?, #key, references, sets, sets_many, #ui_reference, #valid?

Constructor Details

This class inherits a constructor from ActionBlocks::BaseBuilder

Instance Attribute Details

#aggregationObject

Returns the value of attribute aggregation.



257
258
259
# File 'lib/action_blocks/builders/model_builder.rb', line 257

def aggregation
  @aggregation
end

#base_modelObject

Returns the value of attribute base_model.



257
258
259
# File 'lib/action_blocks/builders/model_builder.rb', line 257

def base_model
  @base_model
end

#parent_referenceObject

Returns the value of attribute parent_reference.



257
258
259
# File 'lib/action_blocks/builders/model_builder.rb', line 257

def parent_reference
  @parent_reference
end

Returns the value of attribute related_model.



257
258
259
# File 'lib/action_blocks/builders/model_builder.rb', line 257

def related_model
  @related_model
end

Returns the value of attribute related_model_id.



257
258
259
# File 'lib/action_blocks/builders/model_builder.rb', line 257

def related_model_id
  @related_model_id
end

Instance Method Details

#after_build(parent, *args) ⇒ Object



278
279
280
# File 'lib/action_blocks/builders/model_builder.rb', line 278

def after_build(parent, *args)
  @parent_model.fields.append(self)
end

#before_build(parent, *args) ⇒ Object

validates :aggregate, inclusion: { in: %i[sum count avg min max string_agg] }



265
266
267
268
269
270
271
272
273
274
275
276
# File 'lib/action_blocks/builders/model_builder.rb', line 265

def before_build(parent, *args)
  @base_model = parent.base_model # required lookup for match conditions
  @related_model = parent.related_model # required lookup for match conditions
  @related_model_id = parent.related_model_id # required lookup for match conditions

  @field_type = 'summary'
  @field_name = args[0]
  @parent_reference = parent
  @parent_model = @parent_reference.base_model

  @aggregate = args[1]
end

#hashify(user) ⇒ Object



310
311
312
313
314
315
# File 'lib/action_blocks/builders/model_builder.rb', line 310

def hashify(user)
  {
    type: :summary,
    id: @id
  }
end

#select_requirements(select_as_prefix = nil) ⇒ Object

summary_reqs = [{

type: :summary
root_klass: Rate,
select_req: { field_name: :count_of_rates, path: [:id], aggregate: ->(*args) { count(*args) } },
match_reqs: [{
  base_path: [WorkOrder, :rate_sheet_id], # work_order
  predicate: :eq,
  related_path: [:rate_sheet_id]   # rate
}]

}]



292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
# File 'lib/action_blocks/builders/model_builder.rb', line 292

def select_requirements(select_as_prefix = nil)
  path_and_function = instance_exec(&@aggregate)
  path = path_and_function[:path]
  function = path_and_function[:function]

  {
    type: :summary,
    root_klass: parent_reference.related_model.active_model,
    select_req: {
      field_name: @field_name,
      path: path,
      function: function
    },
    match_reqs: parent_reference.match_reqs,
    filter_reqs: filters.map(&:match_reqs)
  }
end