module ShareableModels::Models::Sharer
Define a model as shareable. A shareable model can be shared between other models called sharers. Imagine a platform with privates articles, you could want to share your awesome article with other person, so make it shareable.
Public Instance Methods
Allow a sharer to edit the resource
Parameters:¶ ↑
- resource
-
Resource model to allow edit the user
- to
-
Sharer
that will be able to edit the resource
Returns:¶ ↑
True if it’s ok
# File lib/shareable_models/models/sharer.rb, line 168 def allow_edit(resource, to) return false unless can_edit?(resource) return true if to.can_edit?(resource) share_resource = shared_resources.find_by(shared_to: to, resource: resource) if share_resource.nil? share(resource, to, true) else share_resource.update(edit: true) end end
Check if the current sharer can edit a given resource. We need to check if the user share this element or someone share it with him.
Parameters:¶ ↑
- resource
-
Resource model sharer wants to check
Returns:¶ ↑
True or false based on permission
# File lib/shareable_models/models/sharer.rb, line 133 def can_edit?(resource) check_resource(resource) resource.shareable_owner == self || shared_with_me.where(edit: true).exists?(edit: true, resource: resource) end
Check if the current sharer can read a given resource. We need to check if the user share this element or someone share it with him.
Parameters:¶ ↑
- resource
-
Resource model sharer wants to check
Returns:¶ ↑
True or false
# File lib/shareable_models/models/sharer.rb, line 150 def can_read?(resource) check_resource(resource) resource.shareable_owner == self || shared_with_me.exists?(resource: resource) end
Prevent a sharer to edit the resource. First parameter is to set same format of allow_edit.
Parameters:¶ ↑
- resource
-
Resource to prevent an user to edit
- to
-
Sharer
that will be able to edit the resource
Returns:¶ ↑
True if it’s ok
# File lib/shareable_models/models/sharer.rb, line 192 def prevent_edit(resource, to) return false unless can_edit?(resource) share_resource = shared_resources.find_by(shared_to: to, resource: resource) return true if share_resource.nil? share_resource.update(edit: false) end
Stop sharing a shareable model with a sharer. You can throw out creator of shareable model.
Parameters:¶ ↑
- resource
-
Resource to throw out the sharer.
- sharer
-
Sharer
model to disable share. - edit
-
Check if sharer has permissions to edit before throw out. It’s true by default, but if an user try to leave a resource we must not check this.
Returns:¶ ↑
True if it’s ok
# File lib/shareable_models/models/sharer.rb, line 95 def throw_out(resource, sharer, edit = true) check_resource(resource) check_sharer(sharer) return false if (edit && !self.can_edit?(resource)) || resource.shareable_owner == sharer relation = resource.shared_with.find_by(shared_to: sharer) relation.nil? ? true : relation.destroy.destroyed? end
Private Instance Methods
Check if from and destination models have included the modules
Parameters:¶ ↑
- resource
-
Instance of resource to share
Returns:¶ ↑
Fail if there are a bad condition
# File lib/shareable_models/models/sharer.rb, line 211 def check_resource(resource) fail "#{resource} class must include Shareable module" unless resource.respond_to?(:shareable?) end