Module: Serviceable

Extended by:
ActiveSupport::Concern
Included in:
Space
Defined in:
app/models/concerns/serviceable.rb

Overview

Add multiple Service(s) to an object.

Examples:

Add a service to an object

service = Service.new(creator: User.first, name: :eventbrite, token: 'FOO')
Space.first.services << service

Class Method Summary collapse

Class Method Details

.serviceable?(serviceable_type, serviceable_id, user_id) ⇒ Boolean

Can a service be created?

Examples:

Check whether or not a User(id: 1) can attach a Service to a Space(id: 1)

Serviceable.serviceable?('space', 1, 1)

Parameters:

  • serviceable_type (String)

    the class of object to be serviced

  • serviceable_id (Integer)

    the id of the object to be serviced

  • user_id (Integer)

    the id of the user creating the service

Returns:

  • (Boolean)

    whether or not the entity's class includes serviceable



24
25
26
27
28
29
30
# File 'app/models/concerns/serviceable.rb', line 24

def self.serviceable?(serviceable_type, serviceable_id, user_id)
  return false if [serviceable_type, serviceable_id, user_id].any?(&:nil?)

  klass = serviceable_type.camelize.constantize

  klass.include?(Serviceable) and klass.exists?(id: serviceable_id) and User.exists?(id: user_id)
end