Class: Uchi::Field::HasAndBelongsToMany
- Inherits:
-
Field
- Object
- Field
- Uchi::Field::HasAndBelongsToMany
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
Instance Method Summary
collapse
Constructor Details
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
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.
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_key ⇒ Object
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_param ⇒ Object
120
121
122
|
# File 'app/components/uchi/field/has_and_belongs_to_many.rb', line 120
def permitted_param
{param_key => []}
end
|