Class: Uchi::Field::HasAndBelongsToMany

Inherits:
Uchi::Field show all
Defined in:
app/components/uchi/field/has_and_belongs_to_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, #show_component, #show_component_class, #value

Methods included from Configuration

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

Constructor Details

#initialize(name) ⇒ HasAndBelongsToMany

Returns a new instance of HasAndBelongsToMany.



82
83
84
85
# File 'app/components/uchi/field/has_and_belongs_to_many.rb', line 82

def initialize(name)
  super
  @collection_query = DEFAULT_COLLECTION_QUERY
end

Instance Method Details

#associated_repository(record:) ⇒ Object

Raises:

  • (NameError)


68
69
70
71
72
73
74
75
# File 'app/components/uchi/field/has_and_belongs_to_many.rb', line 68

def associated_repository(record:)
  reflection = record.class.reflect_on_association(name)
  model = reflection.klass
  repository_class = Uchi::Repository.for_model(model)
  raise NameError, "No repository found for associated model #{model}" unless repository_class

  repository_class.new
end

#collection(record:) ⇒ Object



77
78
79
80
# File 'app/components/uchi/field/has_and_belongs_to_many.rb', line 77

def collection(record:)
  query = associated_repository(record: record).find_all
  @collection_query.call(query)
end

#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::HasAndBelongsToMany.new(:tags).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



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

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



111
112
113
# File 'app/components/uchi/field/has_and_belongs_to_many.rb', line 111

def group_as(_action)
  :associations
end

#param_keyObject

Returns the key to use for this field in params



116
117
118
# File 'app/components/uchi/field/has_and_belongs_to_many.rb', line 116

def param_key
  association.association_foreign_key.pluralize.to_sym
end

#permitted_paramObject



120
121
122
# File 'app/components/uchi/field/has_and_belongs_to_many.rb', line 120

def permitted_param
  {param_key => []}
end