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 939510d commit 45c868f
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 @@ -41,7 +48,7 @@ def id
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 @@ -63,7 +70,11 @@ def id
}
},
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 45c868f

Please sign in to comment.