Module: ShareableModels::Models::Shareable

Defined in:
lib/shareable_models/models/shareable.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(from, to) ⇒ Object

Allow a sharer to edit the resource

Parameters:

from

Sharer that want to allow another to edit resource

to

Sharer that will be able to edit the resource

Returns:

True if it’s ok



127
128
129
# File 'lib/shareable_models/models/shareable.rb', line 127

def allow_edit(from, to)
  from.allow_edit(self, to)
end

#editable_by?(from) ⇒ Boolean

Check if a given sharer can edit this resource

Parameters:

from

Sharer that wants to edit the user.

Returns:

true if the sharer can edit it

Returns:

  • (Boolean)


97
98
99
# File 'lib/shareable_models/models/shareable.rb', line 97

def editable_by?(from)
  from.can_edit?(self)
end

#leave_by(sharer) ⇒ Object

Allow given sharer to leave the resource

Parameters:

sharer

Sharer that want to leave resource.

Returns:

True if it’s ok



79
80
81
# File 'lib/shareable_models/models/shareable.rb', line 79

def leave_by(sharer)
  sharer.leave(self)
end

#prevent_edit(from, to) ⇒ Object

Prevent a sharer to edit the resource. First parameter is to set same format of allow_edit.

Parameters:

from

Sharer that want to allow another to edit resource

to

Sharer that will be able to edit the resource

Returns:

True if it’s ok



144
145
146
# File 'lib/shareable_models/models/shareable.rb', line 144

def prevent_edit(from, to)
  from.prevent_edit(self, to)
end

#readable_by?(from) ⇒ Boolean

Check if a given sharer can see this resource

Parameters:

from

Sharer that wants to see the user.

Returns:

true if the sharer can see it

Returns:

  • (Boolean)


111
112
113
# File 'lib/shareable_models/models/shareable.rb', line 111

def readable_by?(from)
  from.can_read?(self)
end

#share_it(from, to, edit = false) ⇒ Object

Share current element with a sharer model.

Parameters:

from

Instance of model with share this resource

to

Instance of model to share with

edit

True if the user has edit permission. False by default

Returns:

true if its saved.



49
50
51
# File 'lib/shareable_models/models/shareable.rb', line 49

def share_it(from, to, edit = false)
  from.share(self, to, edit)
end

#shareable?Boolean

Method to determine if a model can be shared. It always true because the class includes this module.

Returns

true

Returns:

  • (Boolean)


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

def shareable?
  true
end

#shareable_ownerObject

Return the shareable owner



30
31
32
33
# File 'lib/shareable_models/models/shareable.rb', line 30

def shareable_owner
  return nil if shareable_options.nil? || shareable_options[:owner].nil?
  send(shareable_options[:owner])
end

#throw_out(from, to) ⇒ Object

Throw out an user from this resource

Parameters:

from

Sharer that want to leave resource.

to

Sharer that want to leave resource.

Returns:

True if it’s ok



65
66
67
# File 'lib/shareable_models/models/shareable.rb', line 65

def throw_out(from, to)
  from.throw_out(self, to)
end