From d043229d0cd4ced16896954d7a1245af5e8d6063 Mon Sep 17 00:00:00 2001
From: Sean <11340230+scarroll32@users.noreply.github.com>
Date: Wed, 13 Apr 2022 00:14:18 +0200
Subject: [PATCH 1/2] Ransack search with ActsAsTaggableOn
---
.../docs/going-further/acts-as-taggable-on.md | 76 +++++++++++++++++--
1 file changed, 70 insertions(+), 6 deletions(-)
diff --git a/docs/docs/going-further/acts-as-taggable-on.md b/docs/docs/going-further/acts-as-taggable-on.md
index 0500f980..f3b3420e 100644
--- a/docs/docs/going-further/acts-as-taggable-on.md
+++ b/docs/docs/going-further/acts-as-taggable-on.md
@@ -3,10 +3,16 @@ title: Acts-as-taggable-on
sidebar_position: 13
---
+## Using Acts As Taggable On
+
If you have an `ActiveRecord` model and you're using [acts-as-taggable-on](https://github.com/mbleigh/acts-as-taggable-on),
-chances are you might want to search on tagged fields.
+chances are you might want to search on tagged fields. Follow the instructions to install the gem and then set up your project files.
+
+### Configure the model
-Suppose you have this model:
+`app/models/tasks.rb`
+
+You can call the tagging field anything you like, it just needs to be plural. No migration is needed as this is stored in the internal ActsAsTaggable tables (`tags` and `taggings`).
```rb
class Task < ApplicationRecord
@@ -14,7 +20,35 @@ class Task < ApplicationRecord
end
```
-and you have the following two instances of `Task`:
+### Controller
+
+Add a field to strong params in the controller. Use the singular name with `_list`.
+
+`app/controllers/tasks_controller.rb`
+
+```rb
+def strong_params
+ params
+ .require(:tasks)
+ .permit(:task, :example_field, :project_list)
+```
+
+### Form
+
+We need to `send` the tag fieldname to our model, also using the singular naming.
+
+```
+
+ <%= f.label :project_list %>
+ <%= f.text_field :project_list, value: @task.send(:project_list).to_s %>
+
+```
+
+Now we can collect our data via the form, with tags separated by commas.
+
+## Ransack Search
+
+Imagine you have the following two instances of `Task`:
```rb
{ id: 1, name: 'Clean up my room', projects: [ 'Home', 'Personal' ] }
@@ -31,19 +65,49 @@ When you're writing a `Ransack` search form, you can choose any of the following
<% end %>
```
-### Option a - match keys exactly
+### Option A - Match keys exactly
Option `a` will match keys exactly. This is the solution to choose if you want to distinguish 'Home' from 'Homework': searching for 'Home' will return just the `Task` with id 1. It also allows searching for more than one tag at once (comma separated):
- `Home, Personal` will return task 1
- `Home, Homework` will return task 1 and 2
-### Option b - match key combinations
+### Option B - match key combinations
Option `b` will match all keys exactly. This is the solution if you wanna search for specific combinations of tags:
- `Home` will return nothing, as there is no Task with just the `Home` tag
- `Home, Personal` will return task 1
-### Option c - match substrings
+### Option C - match substrings
Option `c` is used to match substrings. This is useful when you don't care for the exact tag, but only for part of it:
- `Home` will return task 1 and 2 (`/Home/` matches both `"Home"` and `"Homework"`)
+
+### Option D - select from a list of tags
+
+In Option D we allow the user to select a list of valid tags and then search againt them. We use the plural name here.
+
+```
+
+ <%= f.label :projects_name, 'Project' %>
+ <%= f.select :projects_name_in, ActsAsTaggableOn::Tag.distinct.order(:name).pluck(:name) %>
+
+
+## Multitenancy
+
+ActsAsTaggableOn allows scoping of tags based on another field on the model. Suppose we have a `language` field on the model, as an effective second level key. We would adjust our model to look like this:
+
+```rb
+class Task < ApplicationRecord
+ acts_as_taggable_on :projects
+ acts_as_taggable_tenant :language
+end
+```
+
+The Ransack search is then filtered using the `for_tenant` method
+
+```
+
+ <%= f.label :projects_name, 'Project' %>
+ <%= f.select :projects_name_in, ActsAsTaggableOn::Tag.for_tenant('fr').distinct.order(:name).pluck(:name) %>
+
+
From 1df58150991e4efe519b604b8c27b91a55cd121d Mon Sep 17 00:00:00 2001
From: Sean <11340230+scarroll32@users.noreply.github.com>
Date: Wed, 13 Apr 2022 00:18:24 +0200
Subject: [PATCH 2/2] Update acts-as-taggable-on.md
---
docs/docs/going-further/acts-as-taggable-on.md | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/docs/docs/going-further/acts-as-taggable-on.md b/docs/docs/going-further/acts-as-taggable-on.md
index f3b3420e..7b4095b7 100644
--- a/docs/docs/going-further/acts-as-taggable-on.md
+++ b/docs/docs/going-further/acts-as-taggable-on.md
@@ -86,11 +86,12 @@ Option `c` is used to match substrings. This is useful when you don't care for t
In Option D we allow the user to select a list of valid tags and then search againt them. We use the plural name here.
-```
+```erb
<%= f.label :projects_name, 'Project' %>
<%= f.select :projects_name_in, ActsAsTaggableOn::Tag.distinct.order(:name).pluck(:name) %>
+```
## Multitenancy