Class: Uchi::Field::HasMany

Inherits:
Uchi::Field show all
Defined in:
app/components/uchi/field/has_many.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) ⇒ HasMany

Returns a new instance of HasMany.



61
62
63
64
# File 'app/components/uchi/field/has_many.rb', line 61

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::HasMany.new(:users).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



83
84
85
86
87
88
# File 'app/components/uchi/field/has_many.rb', line 83

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



90
91
92
# File 'app/components/uchi/field/has_many.rb', line 90

def group_as(_action)
  :associations
end

#param_keyObject



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

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