Skip to content

Step by step : Advanced configuration

Louis Sommer edited this page Feb 27, 2021 · 1 revision

Welcome to this tutorial !

Let's get you through some more advanced configuration for the abyme gem. If you'd rather configure it manually, or if you haven't read it first, head over to our manual configuration tutorial where we keep it simple.

Models

If you're still following, we have a Project, that has_many :tasks, and a Task has_many :comments. abyme provides a way to avoid the painful configuration in the controller strong params. Here's what it looks like :

# models/project.rb
class Project < ApplicationRecord
  include Abyme::Model
  has_many :tasks, inverse_of: :project
  # ...
  abymize :tasks, permit: [:description, :title]
end

# models/task.rb
class Task < ApplicationRecord
  include Abyme::Model
  has_many :comments, inverse_of: :task
  # ...
  abymize :comments, permit: :all_attributes
end

Adding those options will allow you to retrieve those attributes thanks to the Project.abyme_attributes class method. Here's what it returns :

  # Project.abyme_attributes
  {
    tasks_attributes: [
      :description, 
      :title,
      :id,
      :_destroy,
      comments_attributes: [:content, :id, :_destroy]
    ]
  }

For now, we only have a content attribute on our Comment model ; but in the future, if we add new columns to our model, those will be automatically permitted. 💡 Note that we're leaving out the :created_at, :updated_at attributes. 💡 You can also allow all attributes and pass a reject: [] option. 💡 If you call abymize with allow_destroy: false, the permitted params will not include :_destroy.

In the controller, you can then call Project.abyme_attributes, or just abyme_attributes in your strong params :

def project_params
  params.require(:project).permit(:title, :description, abyme_attributes)
end

In the views

🚧 In construction ! 🚧

You can have a look at the doc for now.

Clone this wiki locally