Class: Uchi::Field::HasAndBelongsToMany
- Inherits:
-
Uchi::Field
- Object
- Uchi::Field
- Uchi::Field::HasAndBelongsToMany
- Defined in:
- app/components/uchi/field/has_and_belongs_to_many.rb
Defined Under Namespace
Constant Summary collapse
- DEFAULT_COLLECTION_QUERY =
->(query) { query }.freeze
Constants included from Configuration
Instance Attribute Summary
Attributes inherited from Uchi::Field
Instance Method Summary collapse
- #associated_repository(record:) ⇒ Object
- #collection(record:) ⇒ Object
-
#collection_query(query_proc = Configuration::Unset) ⇒ self, Proc
Sets or gets a custom query for filtering the collection of associated records.
- #group_as(_action) ⇒ Object
-
#initialize(name) ⇒ HasAndBelongsToMany
constructor
A new instance of HasAndBelongsToMany.
-
#param_key ⇒ Object
Returns the key to use for this field in params.
- #permitted_param ⇒ Object
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
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 |