Module: WolfCore::FriendlyModelId
- Extended by:
- ActiveSupport::Concern
- Includes:
- StringUtils
- Defined in:
- lib/wolf_core/utils/friendly_model_id.rb
Instance Method Summary
collapse
#base64_encoded?, #camelcase_to_spaces, #clean_phone_number, #deep_parse_json, #hash_str_to_json, #remove_blank_spaces, #remove_non_alphanumeric_chars, #split_address, #split_name, #to_kebab_case, #to_snake_case, #valid_json?, #valid_url?
Instance Method Details
#convert_candidate_to_friendly_id(candidate) ⇒ Object
71
72
73
74
75
76
77
78
|
# File 'lib/wolf_core/utils/friendly_model_id.rb', line 71
def convert_candidate_to_friendly_id(candidate)
candidate = Array(candidate)
candidate.map!(&:to_sym).uniq!
candidate.map do |field|
field_value = send(field)
parse_field_value(field_value)
end.join("-")
end
|
#find_unique_friendly_id ⇒ Object
55
56
57
58
59
60
61
|
# File 'lib/wolf_core/utils/friendly_model_id.rb', line 55
def find_unique_friendly_id
friendly_id_candidates_array.each do |candidate|
friendly_id = convert_candidate_to_friendly_id(candidate)
return friendly_id if friendly_id.present? && !friendly_id_used?(friendly_id)
end
nil
end
|
#friendly_id_candidates ⇒ Object
63
64
65
|
# File 'lib/wolf_core/utils/friendly_model_id.rb', line 63
def friendly_id_candidates
[:id]
end
|
#friendly_id_candidates_array ⇒ Object
67
68
69
|
# File 'lib/wolf_core/utils/friendly_model_id.rb', line 67
def friendly_id_candidates_array
Array(friendly_id_candidates)
end
|
#friendly_id_used?(friendly_id) ⇒ Boolean
80
81
82
|
# File 'lib/wolf_core/utils/friendly_model_id.rb', line 80
def friendly_id_used?(friendly_id)
self.class.where.not(id: id).find_by(friendly_id: friendly_id).present?
end
|
#generate_friendly_id ⇒ Object
47
48
49
50
51
52
53
|
# File 'lib/wolf_core/utils/friendly_model_id.rb', line 47
def generate_friendly_id
friendly_id = find_unique_friendly_id
return friendly_id if friendly_id.present?
default_candidate = Array(friendly_id_candidates_array.first) + [:id]
convert_candidate_to_friendly_id(default_candidate)
end
|
#parse_field_value(field_value) ⇒ Object
84
85
86
87
|
# File 'lib/wolf_core/utils/friendly_model_id.rb', line 84
def parse_field_value(field_value)
field_value = to_kebab_case(field_value)
remove_non_alphanumeric_chars(field_value, exceptions: "-")
end
|
#set_friendly_id ⇒ Object
41
42
43
44
45
|
# File 'lib/wolf_core/utils/friendly_model_id.rb', line 41
def set_friendly_id
return if friendly_id.present?
self.friendly_id = generate_friendly_id
end
|