Module: Reamaze::PortableHole::InstanceMethods

Defined in:
lib/portable-hole.rb

Overview

Make methods available to ActiveRecord models in the instance context

Instance Method Summary collapse

Instance Method Details

#get_portable_value(preferential, name, do_postprocess = false) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/portable-hole.rb', line 105

def get_portable_value(preferential, name, do_postprocess = false)
  preferential = Helpers.normalize(preferential)
  name         = Helpers.normalize(name)

  # Invoke the association
  prefs = send(preferential)

  # Try to find what they are looking for
  pref = prefs.detect{ |pref| pref.key == name }

  # If the pref isn't found, try to fallback on a default
  if pref.blank?
    value = nil
  else
    value = pref.value
  end

  value
end

#set_portable_value(preferential, name, value, do_preprocess = false) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/portable-hole.rb', line 84

def set_portable_value(preferential, name, value, do_preprocess = false)
  preferential = Helpers.normalize(preferential)
  name    = Helpers.normalize(name)

  # Invoke the association
  prefs = send(preferential)

  # If pref already exists, update it, otherwise add a new one
  pref = prefs.detect { |pref| pref.key == name }

  if pref.blank?
    pref = prefs.build :key      => name,
                       :value    => value,
                       :context  => preferential[1..-1]
  else
    pref.value = value
  end

  pref.value
end