Class: V1::ApplicationSerializer

Inherits:
ActiveModel::Serializer
  • Object
show all
Defined in:
app/serializers/v1/application_serializer.rb

Instance Method Summary collapse

Instance Method Details

#attributes(*args) ⇒ Object


4
5
6
7
8
9
10
11
12
13
# File 'app/serializers/v1/application_serializer.rb', line 4

def attributes(*args)
  hash = super
  seed_data
  serialization_columns
  hash = hash.merge(load_attributes(load_serialization_config_for))
  hash = hash.merge(load_attributes(load_serialization_config_for('expand'))) if param_expand?
  hash = hash.merge(load_attributes(load_serialization_config_for('staff'))) if param_staff? || param_admin?
  hash = hash.merge(load_attributes(load_serialization_config_for('admin'))) if param_admin?
  hash
end

#load_attributes(data) ⇒ Object


35
36
37
38
39
40
41
# File 'app/serializers/v1/application_serializer.rb', line 35

def load_attributes(data)
  hash = {}
  data.each do |attribute|
    hash[@serialization_columns[attribute] || attribute] = @object[attribute]
  end
  hash
end

#load_serialization_config_for(type = 'attributes') ⇒ Object


15
16
17
# File 'app/serializers/v1/application_serializer.rb', line 15

def load_serialization_config_for(type = 'attributes')
  @seed_data[@object.class.name.tableize][type]
end

#param_admin?Boolean


23
24
25
# File 'app/serializers/v1/application_serializer.rb', line 23

def param_admin?
  @instance_options[:admin]
end

#param_expand?Boolean


31
32
33
# File 'app/serializers/v1/application_serializer.rb', line 31

def param_expand?
  @instance_options[:expand]
end

#param_staff?Boolean


27
28
29
# File 'app/serializers/v1/application_serializer.rb', line 27

def param_staff?
  @instance_options[:staff]
end

#seed_dataObject


47
48
49
# File 'app/serializers/v1/application_serializer.rb', line 47

def seed_data
  @seed_data ||= File.exist?(seed_file_name) ? YAML.load_file(seed_file_name) : []
end

#seed_file_nameObject


43
44
45
# File 'app/serializers/v1/application_serializer.rb', line 43

def seed_file_name
  @seed_file_name ||= Rails.root.join('product', 'json_returns.yml')
end

#serialization_columnsObject


19
20
21
# File 'app/serializers/v1/application_serializer.rb', line 19

def serialization_columns
  @serialization_columns ||= @seed_data[@object.class.name.tableize]['columns']
end