Module: ShareableModels::Models::Sharer

Defined in:
lib/shareable_models/models/sharer.rb

Overview

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.

Instance Method Summary collapse

Instance Method Details

#allow_edit(resource, to) ⇒ Object

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



168
169
170
171
172
173
174
175
176
177
# 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

#can_edit?(resource) ⇒ Boolean

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

Returns:

  • (Boolean)


133
134
135
136
137
# 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

#can_read?(resource) ⇒ Boolean

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

Returns:

  • (Boolean)


150
151
152
153
154
# 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

#leave(resource) ⇒ Object

Current sharer leaves a shareable object.

Parameters:

resource

Resource to throw out the sharer.

Returns:

True if it’s ok



114
115
116
# File 'lib/shareable_models/models/sharer.rb', line 114

def leave(resource)
  throw_out(resource, self, false)
end

#prevent_edit(resource, to) ⇒ Object

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



192
193
194
195
196
197
# 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

#share(resource, to, edit = false) ⇒ Object

Share a given resource with a sharer model.

Parameters:

resource

Resource to share. It must includes shareable module

to

Model to share the resource. It must includes sharer module

edit

Boolean indicating if it has permissions to edit shared resources. False by default.

Returns:

True if it’s saved



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/shareable_models/models/sharer.rb', line 42

def share(resource, to, edit = false)
  check_resource(resource)
  check_sharer(to)
  return false unless can_edit?(resource)
  # Save new share
  shared_resources.build(
    shared_to: to,
    resource: resource,
    edit: edit
  )
  save!
end

#share_with_me(resource, from, edit = false) ⇒ Object

Share the given resource with this model. You can understand this method as inverse of share.

Parameters:

resource

Resource to share. It must includes shareable module

from

Model who share the resource. It must includes sharer module

edit

Boolean indicating if it has permissions to edit shared resources. False by default.

Returns:

True if it’s saved



71
72
73
74
75
76
# File 'lib/shareable_models/models/sharer.rb', line 71

def share_with_me(resource, from, edit = false)
  check_resource(resource)
  check_sharer(from)
  # Save new share
  from.share(resource, self, edit)
end

#sharer?Boolean

Method to determine if a model can share and receive elements. It always true because the class include this module.

Returns

true

Returns:

  • (Boolean)


23
24
25
# File 'lib/shareable_models/models/sharer.rb', line 23

def sharer?
  true
end

#throw_out(resource, sharer, edit = true) ⇒ Object

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



95
96
97
98
99
100
101
102
# 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