Module: Mobility::Plugins::Presence

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

Overview

Note:

For performance reasons, the presence plugin filters only for empty strings, not other values continued “blank” like empty arrays.

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

.[](value) ⇒ Object



43
44
45
# File 'lib/mobility/plugins/presence.rb', line 43

def self.[](value)
  (value == "") ? nil : value
end

.apply(attributes, option) ⇒ Object

Applies presence plugin to attributes.

Parameters:



19
20
21
# File 'lib/mobility/plugins/presence.rb', line 19

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



27
28
29
# File 'lib/mobility/plugins/presence.rb', line 27

def read(locale, **options)
  options.delete(:presence) == false ? super : Presence[super]
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



34
35
36
37
38
39
40
# File 'lib/mobility/plugins/presence.rb', line 34

def write(locale, value, **options)
  if options.delete(:presence) == false
    super
  else
    super(locale, Presence[value], options)
  end
end