class ActionBlocks::TableBuilder

Attributes

dashboard[RW]
subspace[RW]
workspace[RW]

Public Instance Methods

after_build(_parent, *_args) click to toggle source

Create a field reference for each column

# File lib/action_blocks/builders/table_builder.rb, line 50
def after_build(_parent, *_args)
  (@columns || []).each do |c|
    @dsl_delegate.col(c)
  end
end
allowed_columns(_user) click to toggle source
# File lib/action_blocks/builders/table_builder.rb, line 60
def allowed_columns(_user)
  model_associations = model.active_model.reflect_on_all_associations(:belongs_to)
  model_association_columns = model_associations.map { |ma| ma.name.to_s + '_id' }
  columns = [:id] + @columns + model_association_columns
  columns.uniq
end
before_build(parent, *args) click to toggle source
# File lib/action_blocks/builders/table_builder.rb, line 41
def before_build(parent, *args)
  @dashboard = parent
  @subspace = @dashboard.subspace
  @workspace = @subspace.workspace
  @title = id.to_s.titleize
  @id = args[0]
end
builder_engine(params:, user:) click to toggle source
# File lib/action_blocks/builders/table_builder.rb, line 215
def builder_engine(params:, user:)
  klass = model.active_model
  selection_match_reqs = selection_match_requirements(user)
  record = selection_record(params: params, user: user)
  filter_reqs = filter_requirements(user: user, record: record)

  # pp({
  #   record: record,
  #   select_fields: select_fields,
  #   selection_match_reqs: selection_match_reqs,
  #   filter_reqs: filter_reqs
  # })

  data_engine = ActionBlocks::DataEngine.new(klass,
    select_fields: select_fields,
    selection_match_reqs: selection_match_reqs,
    selection_filter_reqs: filter_reqs
  )
  data_engine
end
builder_to_json(params:, user:) click to toggle source
# File lib/action_blocks/builders/table_builder.rb, line 236
def builder_to_json(params:, user:)
  engine = builder_engine(params: params, user: user)
  return engine.to_json
end
data_select_fields() click to toggle source

Reason to move logic to model would be to centralize user level access to columns

# File lib/action_blocks/builders/table_builder.rb, line 94
def data_select_fields
  @table_columns.map(&:field)
end
filter_requirements(user:, record:) click to toggle source
# File lib/action_blocks/builders/table_builder.rb, line 82
def filter_requirements(user:, record:)
  filter_reqs = []
  if selection && record
    filter_reqs << selection.record_filter_reqs(user: user, record: record)
  end
  return filter_reqs.flatten
end
hashify(_user) click to toggle source
# File lib/action_blocks/builders/table_builder.rb, line 127
def hashify(_user)
  {
    title: @title,
    key: key,
    type: type,
    column_keys: allowed_columns(nil),
    view: @view,
    model_key: model.key,
    table_columns: @table_columns.map(&:hashify)
  }
end
key() click to toggle source
# File lib/action_blocks/builders/table_builder.rb, line 56
def key
  "table-#{workspace.id}_#{subspace.id}_#{dashboard.id}_#{@id}"
end
scope_args(params:, user:) click to toggle source

Given params subspace_model_id and/or workspace_model_id Find records and create named arguments If subspace_model_id was 4 and this table was in a subspace belonging to Work Order arguments would be { work_order: WorkOrder.find(4) }

# File lib/action_blocks/builders/table_builder.rb, line 143
def scope_args(params:, user:)
  if scope
    subspace_variable_name = subspace.model.try(:id).try(:to_sym) # returns a label such as :work_order
    dashboard_variable_name = dashboard.model.try(:id).try(:to_sym) # returns a label such as :work_order
    args = {}
    if subspace_variable_name && scope.parameters.include?([:keyreq, subspace_variable_name])
      subspace_record = subspace.model.active_model.find(params[:subspace_model_id])
      # Todo: check user has read access to subspace record
      args[subspace_variable_name] = subspace_record
    end
    if dashboard_variable_name && scope.parameters.include?([:keyreq, dashboard_variable_name])
      dashboard_record = dashboard.model.active_model.find(params[:dashboard_model_id])
      # Todo: check user has read access to dashboard_record
      args[dashboard_variable_name] = dashboard_record
    end
    if scope.parameters.include?([:keyreq, :current_user])
      args[:current_user] = user
    end
    args
  else
    {}
  end
end
scope_to_json(params:, user:) click to toggle source

Legacy Support

# File lib/action_blocks/builders/table_builder.rb, line 206
def scope_to_json(params:, user:)
  if scope.parameters.length > 0
    s = scope.call(scope_args(params: params, user: user))
  else
    s = scope.call()
  end
  return s.to_json
end
select_fields() click to toggle source
# File lib/action_blocks/builders/table_builder.rb, line 67
def select_fields()
  [
    data_select_fields,
    view_link_select_fields
  ].flatten
end
selection_match_requirements(user) click to toggle source
# File lib/action_blocks/builders/table_builder.rb, line 74
def selection_match_requirements(user)
  if selection
    selection.match_reqs(user)
  else
    []
  end
end
selection_record(params:, user: nil) click to toggle source

Given params subspace_model_id and/or workspace_model_id Get the parent selection record

# File lib/action_blocks/builders/table_builder.rb, line 169
def selection_record(params:, user: nil)
  if selection
    subspace_model_name = subspace.model.try(:id).try(:to_sym) # returns a label such as :work_order
    dashboard_model_name = dashboard.model.try(:id).try(:to_sym) # returns a label such as :work_order
    selection_model_name = selection.base_model.try(:id).try(:to_sym)

    if !subspace_model_name.blank? && selection_model_name == subspace_model_name
      record = subspace.model.active_model.find(params[:subspace_model_id])
    end
    if !dashboard_model_name.blank? && selection_model_name == dashboard_model_name
      record = dashboard.model.active_model.find(params[:dashboard_model_id])
    end
    if dashboard_model_name == subspace_model_name
      raise "Ambiguous model nesting.  ActionBlock validation should have prevented this."
    end
    if dashboard_model_name != selection_model_name && subspace_model_name != selection_model_name
      raise "Invalid selection model.  ActionBlock validation should have prevented this."
    end
    record
  else
    nil
  end
end
to_json(params:, user:) click to toggle source
# File lib/action_blocks/builders/table_builder.rb, line 193
def to_json(params:, user:)
  if scope
    scope_to_json(params: params, user: user)
  else
    pp({
      params: params,
      user: user
    })
    builder_to_json(params: params, user: user)
  end
end
validate_scope() click to toggle source
# File lib/action_blocks/builders/table_builder.rb, line 22
def validate_scope
  return unless scope
  errors.add(:scope, 'requires Proc -> () {}') if scope.class != Proc
  if scope && scope.class == Proc
    valid_parameters = [%i[keyreq current_user]]
    valid_parameters << [:keyreq, dashboard.model.id.to_sym] if dashboard.model
    valid_parameters << [:keyreq, subspace.model.id.to_sym] if subspace.model
    invalid_parameters = scope.parameters - valid_parameters
    errors.add(:scope, "has invalid parameter: #{invalid_parameters}  Allowed: #{valid_parameters}") unless invalid_parameters.empty?
  end
end
validate_selection_model() click to toggle source
# File lib/action_blocks/builders/table_builder.rb, line 35
def validate_selection_model
  if dashboard.model != selection_model && subspace.model != selection_model
    errors.add(:selection_model, "selection model not in subspace or dashboard")
  end
end