Module: SupplejackApi::Concerns::RecordSerializable

Extended by:
ActiveSupport::Concern
Included in:
RecordSerializer
Defined in:
app/serializers/supplejack_api/concerns/record_serializable.rb

Instance Method Summary collapse

Instance Method Details

#field_restricted?(conditional_field, condition) ⇒ Boolean

Returns:

  • (Boolean)

64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'app/serializers/supplejack_api/concerns/record_serializable.rb', line 64

def field_restricted?(conditional_field, condition)
  field_values = field_value(conditional_field).to_a
  restricted = false

  field_values.each do |value|
    if (condition.is_a?(Regexp) && value.match(condition)) ||
       (condition.is_a?(String) && value.include?(condition))
      restricted = true
    end
    break if restricted
  end

  restricted
end

#field_value(field, _options = {}) ⇒ Object

REFACTOR – Used in concept_serializer.rb too


30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'app/serializers/supplejack_api/concerns/record_serializable.rb', line 30

def field_value(field, _options = {})
  value = if RecordSchema.fields[field].try(:search_value) && RecordSchema.fields[field].try(:store) == false
            RecordSchema.fields[field].search_value.call(object)
          else
            object.public_send(field)
          end

  value = RecordSchema.fields[field].try(:default_value) if value.nil? rescue nil

  if RecordSchema.fields[field].try(:date_format)
    value = format_date(value, RecordSchema.fields[field].try(:date_format))
  end

  value
end

#format_date(date, format) ⇒ Object


50
51
52
# File 'app/serializers/supplejack_api/concerns/record_serializable.rb', line 50

def format_date(date, format)
  date.strftime(format) rescue date
end

#include_individual_fields!(hash) ⇒ Object


110
111
112
113
114
115
116
117
118
# File 'app/serializers/supplejack_api/concerns/record_serializable.rb', line 110

def include_individual_fields!(hash)
  if options[:fields].present?
    options[:fields].each do |field|
      hash[field] = record.send(field)
    end
  end

  hash
end

#remove_field_values(restricted_fields, hash) ⇒ Object


79
80
81
82
83
# File 'app/serializers/supplejack_api/concerns/record_serializable.rb', line 79

def remove_field_values(restricted_fields, hash)
  restricted_fields.each do |field|
    hash[field.to_sym] = nil
  end
end

#remove_restricted_fields!(hash) ⇒ Object


19
20
21
22
23
24
25
26
27
# File 'app/serializers/supplejack_api/concerns/record_serializable.rb', line 19

def remove_restricted_fields!(hash)
  role_field_restrictions.each do |conditional_field, restrictions|
    restrictions.each do |condition, restricted_fields|
      if field_restricted?(conditional_field, condition)
        remove_field_values(restricted_fields, hash)
      end
    end
  end
end

#roleObject


46
47
48
# File 'app/serializers/supplejack_api/concerns/record_serializable.rb', line 46

def role
  @role ||= options[:scope].role.to_sym rescue nil
end

#role_field_restrictionsObject


54
55
56
57
58
59
60
61
62
# File 'app/serializers/supplejack_api/concerns/record_serializable.rb', line 54

def role_field_restrictions
  restrictions = []

  if role && RecordSchema.roles[role] && RecordSchema.roles[role].field_restrictions.present?
    restrictions = RecordSchema.roles[role].field_restrictions
  end

  restrictions
end

#serializable_hashObject

Returns a hash including all desirable record attributes and its associations


86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'app/serializers/supplejack_api/concerns/record_serializable.rb', line 86

def serializable_hash
  hash = attributes
  groups = (options[:groups] & RecordSchema.groups.keys) || []
  fields = Set.new
  groups.each do |group|
    fields.merge(RecordSchema.groups[group].try(:fields))
  end

  # hash[:id] = field_value(:record_id, options) if fields.any?
  fields.each do |field|
    hash[field] = field_value(field, options)
  end

  include_individual_fields!(hash)
  remove_restricted_fields!(hash)

  hash[:next_page] = object.next_page if object.next_page.present?
  hash[:next_record] = object.next_record if object.next_record.present?
  hash[:previous_page] = object.previous_page if object.previous_page.present?
  hash[:previous_record] = object.previous_record if object.previous_record.present?

  hash
end

#to_xml(*_args) ⇒ Object


15
16
17
# File 'app/serializers/supplejack_api/concerns/record_serializable.rb', line 15

def to_xml(*_args)
  serializable_hash.to_xml(root: 'record')
end