Class: Uchi::Field::BelongsTo

Inherits:
Uchi::Field show all
Defined in:
app/components/uchi/field/belongs_to.rb

Defined Under Namespace

Classes: Edit, Index, Show

Constant Summary collapse

DEFAULT_COLLECTION_QUERY =
->(query) { query }.freeze

Constants included from Configuration

Configuration::DEFAULT_READER

Instance Attribute Summary

Attributes inherited from Uchi::Field

#name, #repository

Instance Method Summary collapse

Methods inherited from Uchi::Field

#column_name, #edit_component, #edit_component_class, #index_component, #index_component_class, #permitted_param, #show_component, #show_component_class, #value

Methods included from Configuration

#on, #reader, #searchable, #searchable?, #sortable, #sortable?

Constructor Details

#initialize(name) ⇒ BelongsTo

Returns a new instance of BelongsTo.



72
73
74
75
# File 'app/components/uchi/field/belongs_to.rb', line 72

def initialize(name)
  super
  @collection_query = DEFAULT_COLLECTION_QUERY
end

Instance Method Details

#collection_query(query_proc = Configuration::Unset) ⇒ self, Proc

Sets or gets a custom query for filtering the collection of associated records.

When called with an argument, sets the query and returns self for chaining. When called without arguments, returns the current query.

Examples:

Setting

Field::BelongsTo.new(:company).collection_query(->(query) {
  query.where(active: true)
})

Getting

field.collection_query # => #<Proc...>

Parameters:

  • query_proc (Proc, Symbol) (defaults to: Configuration::Unset)

    A callable that receives an ActiveRecord query and returns a modified query.

Returns:

  • (self, Proc)

    Returns self for method chaining when setting, or the query proc when getting



94
95
96
97
98
99
# File 'app/components/uchi/field/belongs_to.rb', line 94

def collection_query(query_proc = Configuration::Unset)
  return @collection_query if query_proc == Configuration::Unset

  @collection_query = query_proc
  self
end

#group_as(_action) ⇒ Object



101
102
103
# File 'app/components/uchi/field/belongs_to.rb', line 101

def group_as(_action)
  :attributes
end

#param_keyObject



105
106
107
108
109
# File 'app/components/uchi/field/belongs_to.rb', line 105

def param_key
  # TODO: This is too naive. We need to match this to the actual foreign
  # key of the model.
  :"#{name}_id"
end