Skip to content

Instantly share code, notes, and snippets.

Created October 7, 2016 08:08
Show Gist options
  • Save anonymous/6fb1fc8d18246a5873fe9f87c769e40b to your computer and use it in GitHub Desktop.
Save anonymous/6fb1fc8d18246a5873fe9f87c769e40b to your computer and use it in GitHub Desktop.
module Settings
DISCRIMINATOR = :type_name
TYPE_TO_CLASS = Hash[Setting.subclasses.map { |klass| [klass.type_name.to_s, klass] }].freeze
class SettingRepresenter < Representable::Decorator
include ::Representable::JSON
property :name
property DISCRIMINATOR, skip_parse: true
end
class MetaRepresenter < Representable::Decorator
include ::Representable::JSON
representer_selector = lambda |options| { # Irrelevant }
class_selector = lambda do |options|
type = options[:input][DISCRIMINATOR.to_s]
TYPE_TO_CLASS[type] || ::Theme::Settings::Setting
end
collection :groups, class: Group do
property :title
collection :sub_groups, class: SubGroup do
property :title
collection :settings, decorator: representer_selector, class: class_selector
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment