Skip to content

Commit

Permalink
Let subclassed serializers inherit attributes from the superclass
Browse files Browse the repository at this point in the history
  • Loading branch information
floriandejonckheere committed Aug 11, 2022
1 parent 7b1d7d4 commit bad4111
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lib/cache_crispies/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Base
# @param other [Class] the inheriting child class
# @return [void]
def self.inherited(other)
other.instance_variable_set(:@attributes, [])
other.instance_variable_set(:@attributes, @attributes.dup || [])
other.instance_variable_set(:@nesting, [])
other.instance_variable_set(:@conditions, [])
end
Expand Down
17 changes: 14 additions & 3 deletions spec/cache_crispies/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,17 @@
require 'ostruct'

describe CacheCrispies::Base do
class NutritionSerializer < CacheCrispies::Base
class CaloriesSerializer < CacheCrispies::Base
serialize :calories
end

class NutritionSerializer < CaloriesSerializer
serialize :fat,
:carbohydrates,
:sodium,
:protein
end

class CacheCrispiesTestSerializer < CacheCrispies::Base
serialize :id, :company, to: String

Expand Down Expand Up @@ -45,7 +52,7 @@ def visible?
brand: 'Cookie Crisp',
company: 'General Mills',
deeply_nested: true,
nutrition_info: OpenStruct.new(calories: 1_000),
nutrition_info: OpenStruct.new(calories: 1_000, fat: 20, carbohydrates: 60, sodium: 30, protein: 10),
organic: 'true',
legal: OpenStruct.new(parent_company: 'Disney probably')
)
Expand All @@ -67,7 +74,11 @@ def visible?
}
},
nutrition_info: {
calories: 1000
calories: 1000,
fat: 20,
carbohydrates: 60,
sodium: 30,
protein: 10
},
organic: true,
parent_company: 'Disney probably'
Expand Down

0 comments on commit bad4111

Please sign in to comment.