Sync v0.3.0 Release Notes
Release Date: 2014-03-03 // about 10 years ago-
🚀 This release focuses on improving and extending the model DSL for setting up automatic syncing in advanced use cases.
- ➕ Adds the ability for advanced channel scoping
There were multiple feature requests asking for a way to sync differently scoped lists of the same model automatically and interdependently (e.g all todos of a user and all todos of a project). This can now be accomplished by explicitly defining scopes on the model via the new
sync_scope
method:class Todo < ActiveRecord::Base belongs_to :project belongs_to :user sync :all sync_scope :by_user(user), -> { where(user_id: user.id) } sync_scope :by_project(project), -> { where(project_id: project.id) } end
and then use these scopes to narrow the rendering of sync partials like this:
<%= sync partial: "todo", resource: Todo.by_user(@user) %> <%= sync_new partial: "todo", resource: Todo.new, scope: Todo.by_user(@user) %>
Please take a look at the docs and the readme for a more thorough explanation and examples on how to use this new feature.
- ➕ Adds the ability to explicitly update parent associations via
sync_touch
Breaking Changes:
- 🔀 If you're using the scope feature to narrow the syncing of new records in the
sync_new
call, you will now have to add this scope when calling thesync
helper method as well:
<%= sync partial: 'todo_comment', collection: @comments, scope: @todo %> <%= sync_new partial: 'todo_comment', resource: Comment.new, scope: @todo %>
If you're in addition using the controller way of manually syncing partials, you will now also have to add the scope parameter to the sync_destroy call like this:
sync_destroy @comment, scope: @comment.todo
Why is this?
Before this version there was only a global destroy channel for every record, so an unscoped
sync_destroy
call was just enough to remove all partials from all subscribed clients when a record has been destroyed. As of 0.3.0 the destroy channel will be used not only to remove partials when a record is destroyed, but also when partials for that record need to be added to/removed from different sets throughout the application when it is updated.- ⚡️ The
:scope
parameter for thesync
method has been replaced with:default_scope
. Make sure you update your code accordingly. If you're using the default scope feature, be sure to alway add the corresponding option to your views like this:
class Todo < ActiveRecord::Base belongs_to :organization sync :all, default_scope: :organization end
<%= sync partial: "todo", resource: @todos, default_scope: @organization %> <%= sync_new partial: "todo", resource: Todo.new, default_scope: @organization %>
- ⚡️ The parent model defined by the
:default_scope
parameter will no longer be automatically updated via sync. Please use the new explicitsync_touch
method instead.
Old Syntax:
class Todo < ActiveRecord::Base belongs_to :project belongs_to :user sync :all, scope: :project end
New Syntax:
class Todo < ActiveRecord::Base belongs_to :project belongs_to :user sync :all, default_scope: :project sync_touch :project, :user end
This will sync all partials of the parent model
project
anduser
, whenever a todo is created/updated/deleted.