What is a Smart Collection?
A Smart Collection is a Forest Collection based on your API implementation. It allows you to reconcile fields of data coming from different or external sources in a single tabular view (by default), without having to physically store them into your database. Fields of data could be coming from many other sources such as other B2B SaaS (e.g. Zendesk, Salesforce, Stripe), in-memory database, message broker, etc.This is an advanced notion. If you’re just starting with Forest, you should skip this for now.
customer_statsallowing us to see all customers who have placed orders, the number of order placed and the total amount of those orders.
For an example of advanced customization and featuring an Amazon S3 integration, you can see here how we’ve stored in our live demo the companies’ legal documents on Amazon S3 and how we’ve implemented a Smart Collection to access and manipulate them.
Creating a Smart Collection
- Rails
- Django
- Laravel
First, we declare the
CustomerStat collection in the lib/forest-liana/collections/ directory.In this Smart Collection, we want to display for each customer its email address, the number of orders made (in a field orders_count) and the sum of the price of all those orders (in a field total_amount).You can check out the list of available field options if you need it.The option
is_searchable: true added to your collection allows to display the search bar. Note that you will have to implement the search yourself by including it into your own get logic in your collection controller.Implementing the GET (all records)
At this time, there’s no Smart Collection Implementation because no route in your app handles the API call yet.- Rails
- Django
- Laravel
In the repository You then need to create a route pointing to your collection’s index action to get all your collection’s records.
lib/forest_liana/controllers/, we’ve created a controller file customer_stats.rb to implement API behind the Smart Collection.The logic here is to index all the customers that have made orders (with their email), to count the number of orders made and to sum up the price of all the orders.The limit and offset variables are used to paginate your collection according to the number of records per page set in your UI.We have implemented a search logic to catch if a search query (accessible through params[:search]) has been performed and to return all records for which the email field matches the search.Finally, the last step is to serialize the response data in the expected format which is simply a standard JSON API document. We use the JSON API Serializer library for this task.In this example we have only implemented the GET all records action but you can also add the following actions: GET specific records, PUT, DELETE and POST. These are shown in the next page explaining how a Smart Collection can be used to access and manipulate data stored in Amazon S3.