Class: LookupKey

Inherits:
ApplicationRecord show all
Includes:
Authorizable, Classification, HiddenValue, KeyType
Defined in:
app/models/lookup_keys/lookup_key.rb

Direct Known Subclasses

PuppetclassLookupKey

Constant Summary collapse

VALIDATOR_TYPES =
[N_("regexp"), N_("list")]
KEY_DELM =
","
EQ_DELM =
"="
VALUE_REGEX =
/\A[^#{KEY_DELM}]+#{EQ_DELM}[^#{KEY_DELM}]+(#{KEY_DELM}[^#{KEY_DELM}]+#{EQ_DELM}[^#{KEY_DELM}]+)*\Z/
MATCHERS_INHERITANCE =
['hostgroup', 'organization', 'location'].freeze

Constants included from KeyType

KeyType::KEY_TYPES

Constants included from HiddenValue

HiddenValue::HIDDEN_VALUE

Class Method Summary collapse

Instance Method Summary collapse

Methods included from HiddenValue

#hidden_value, #safe_value

Methods included from Authorizable

#authorized?, #check_permissions_after_save

Methods included from PermissionName

#permission_name

Methods inherited from ApplicationRecord

graphql_type, #logger, logger

Methods included from AuditAssociations::AssociationsDefinitions

#audit_associations, #audited, #configure_dirty_associations, #normalize_associations

Class Method Details

.find_by_name(str) ⇒ Object

to prevent errors caused by find_resource from override_values controller


64
65
66
# File 'app/models/lookup_keys/lookup_key.rb', line 64

def self.find_by_name(str)
  nil
end

.hidden_valueObject


47
48
49
# File 'app/models/lookup_keys/lookup_key.rb', line 47

def self.hidden_value
  HIDDEN_VALUE
end

.inherited(child) ⇒ Object


36
37
38
39
40
41
42
43
44
45
# File 'app/models/lookup_keys/lookup_key.rb', line 36

def self.inherited(child)
  child.instance_eval do
    scoped_search :on => :key, :aliases => [:parameter], :complete_value => true, :default_order => true
    scoped_search :on => :override, :complete_value => {:true => true, :false => false}
    scoped_search :on => :merge_overrides, :complete_value => {:true => true, :false => false}
    scoped_search :on => :merge_default, :complete_value => {:true => true, :false => false}
    scoped_search :on => :avoid_duplicates, :complete_value => {:true => true, :false => false}
  end
  super
end

Instance Method Details

#audit_classObject


72
73
74
# File 'app/models/lookup_keys/lookup_key.rb', line 72

def audit_class
  self
end

#default_value_before_type_castObject


110
111
112
113
# File 'app/models/lookup_keys/lookup_key.rb', line 110

def default_value_before_type_cast
  return self[:default_value] if errors[:default_value].present?
  LookupKey.format_value_before_type_cast(default_value, key_type)
end

#overridden?(obj) ⇒ Boolean

Returns:

  • (Boolean)

123
124
125
126
# File 'app/models/lookup_keys/lookup_key.rb', line 123

def overridden?(obj)
  return false unless obj.respond_to? :lookup_values
  overridden_value(obj).present?
end

#overridden_value(obj) ⇒ Object

check if obj has a lookupvalue that relates to this key and return it we cannot search the database, in case the lookup value hasn't been saved yet


130
131
132
133
134
# File 'app/models/lookup_keys/lookup_key.rb', line 130

def overridden_value(obj)
  obj.lookup_values.detect do |lookup_value|
    lookup_value.lookup_key_id == id
  end
end

#pathObject


99
100
101
102
# File 'app/models/lookup_keys/lookup_key.rb', line 99

def path
  path = self[:path]
  path.presence || array2path(Setting["Default_parameters_Lookup_Path"])
end

#path=(v) ⇒ Object


104
105
106
107
108
# File 'app/models/lookup_keys/lookup_key.rb', line 104

def path=(v)
  return unless v
  using_default = v.tr("\r", "") == array2path(Setting["Default_parameters_Lookup_Path"])
  self[:path] = using_default ? nil : v
end

#path_elementsObject


115
116
117
118
119
120
121
# File 'app/models/lookup_keys/lookup_key.rb', line 115

def path_elements
  path.split.map do |paths|
    paths.split(KEY_DELM).map do |element|
      element
    end
  end
end

#puppet?Boolean

Returns:

  • (Boolean)

136
137
138
# File 'app/models/lookup_keys/lookup_key.rb', line 136

def puppet?
  false
end

#reject_invalid_lookup_values(attributes) ⇒ Object


68
69
70
# File 'app/models/lookup_keys/lookup_key.rb', line 68

def reject_invalid_lookup_values(attributes)
  attributes[:match].empty?
end

#sorted_valuesObject


140
141
142
143
# File 'app/models/lookup_keys/lookup_key.rb', line 140

def sorted_values
  prio = path.downcase.split
  lookup_values.sort_by { |val| [prio.index(val.path), val.match] }
end

#supports_merge?Boolean

Returns:

  • (Boolean)

80
81
82
# File 'app/models/lookup_keys/lookup_key.rb', line 80

def supports_merge?
  ['array', 'hash'].include?(key_type)
end

#supports_uniq?Boolean

Returns:

  • (Boolean)

84
85
86
# File 'app/models/lookup_keys/lookup_key.rb', line 84

def supports_uniq?
  key_type == 'array'
end

#to_labelObject


76
77
78
# File 'app/models/lookup_keys/lookup_key.rb', line 76

def to_label
  "#{audit_class}::#{key}"
end

#to_paramObject


88
89
90
91
92
93
# File 'app/models/lookup_keys/lookup_key.rb', line 88

def to_param
  # to_param is used in views to create a link to the lookup_key.
  # If the key has whitespace in it the link will break so this replaced the whitespace.
  search_key = key.tr(' ', '_') unless key.nil?
  Parameterizable.parameterize("#{id}-#{search_key}")
end

#to_sObject


95
96
97
# File 'app/models/lookup_keys/lookup_key.rb', line 95

def to_s
  key
end