Class: CiviCrm::OptionValue

Inherits:
BaseResource show all
Defined in:
lib/civicrm/resources/option_value.rb

Constant Summary collapse

@@_option_value_cache =
{}

Class Method Summary collapse

Methods included from Actions::Destroy

#delete, #delete!

Methods included from Actions::Update

#save, #update

Methods included from Actions::Create

included

Methods included from Actions::Find

included

Methods included from Actions::List

included

Methods inherited from Resource

#as_json, #attribute, #attributes, build_from, entity, entity_class_name, #initialize, #inspect, #method_missing, #refresh_from, #to_hash, #to_json, #to_s

Constructor Details

This class inherits a constructor from CiviCrm::Resource

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class CiviCrm::Resource

Class Method Details

.[](group, name, cache: true, create: Rails.env.development?) ⇒ Object

Raises:



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/civicrm/resources/option_value.rb', line 7

def self.[](group, name, cache: true, create: Rails.env.development?)
  cache_key = [group, name]

  if cache && @@_option_value_cache.key?(cache_key)
    return @@_option_value_cache[cache_key]
  end

  option = find_by(
    "name" => name,
    "option_group_id.title" => group
  )

  if !option && create
    group = OptionGroup.find_by!(title: group)
    option = create(option_group_id: group.id, name: name)
  end

  if option
    @@_option_value_cache[cache_key] = option.value if cache
    return option.value
  end

  raise Errors::NotFound.new(
    "#{group} with name=#{name}"
  )
end