Class: BaseSerializer

Inherits:
ActiveModel::Serializer
  • Object
show all
Defined in:
app/serializers/base_serializer.rb

Instance Method Summary collapse

Instance Method Details

#abilitiesObject


27
28
29
# File 'app/serializers/base_serializer.rb', line 27

def abilities
  { update: can?(:update, object), destroy: can?(:destroy, object) }
end

#cache(keys = []) ⇒ Object


13
14
15
16
17
# File 'app/serializers/base_serializer.rb', line 13

def cache(keys = [])
  Rails.cache.fetch(['serializer', *keys]) do
    yield
  end
end

#can?(*args) ⇒ Boolean

Returns:

  • (Boolean)

23
24
25
# File 'app/serializers/base_serializer.rb', line 23

def can?(*args)
  current_ability.can?(*args)
end

#current_abilityObject


19
20
21
# File 'app/serializers/base_serializer.rb', line 19

def current_ability
  @current_ability ||= Ability.new(current_user)
end

#owner?(obj = nil) ⇒ Boolean

Returns:

  • (Boolean)

2
3
4
5
6
7
8
9
10
11
# File 'app/serializers/base_serializer.rb', line 2

def owner?(obj = nil)
  return false if current_user.blank?

  obj = object if obj.blank?
  if obj.is_a?(User)
    return obj.id == current_user.id
  else
    return obj.user_id == current_user.id
  end
end