Module: Mobility::Plugins::Presence

Defined in:
lib/mobility/plugins/presence.rb

Overview

Applies presence filter to values fetched from backend and to values set on backend. Included by default, but can be disabled with presence: false option.

Backend Accessors collapse

Class Method Summary collapse

Class Method Details

.apply(attributes, option) ⇒ Object

Applies presence plugin to attributes.

Parameters:



15
16
17
# File 'lib/mobility/plugins/presence.rb', line 15

def self.apply(attributes, option)
  attributes.backend_class.include(self) if option
end

Instance Method Details

#read(locale, **options) ⇒ Object

Gets the translated value for provided locale from configured backend.

Parameters:

  • locale (Symbol)

    Locale to read

  • options (Hash)

    a customizable set of options

Options Hash (**options):

  • presence (Boolean)

    false to disable presence filter.

Returns:

  • (Object)

    Value of translation



23
24
25
26
27
# File 'lib/mobility/plugins/presence.rb', line 23

def read(locale, **options)
  return super if options.delete(:presence) == false
  value = super
  value == false ? value : Util.presence(value)
end

#write(locale, value, **options) ⇒ Object

Updates translation for provided locale without calling backend's methods to persist the changes.

Parameters:

  • locale (Symbol)

    Locale to write

  • value (Object)

    Value to write

  • options (Hash)

    a customizable set of options

Options Hash (**options):

  • presence (Boolean)

    false to disable presence filter.

Returns:

  • (Object)

    Updated value



33
34
35
36
# File 'lib/mobility/plugins/presence.rb', line 33

def write(locale, value, **options)
  return super if options.delete(:presence) == false
  super(locale, value == false ? value : Util.presence(value), options)
end