Skip to content

Let subclassed serializers inherit attributes from the superclass #48

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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