Skip to content

Instantly share code, notes, and snippets.

@PavelPenkov
Forked from anonymous/repr.rb
Last active October 7, 2016 08:20
Show Gist options
  • Save PavelPenkov/1d0b861239a1f81054dcf32abfe31e93 to your computer and use it in GitHub Desktop.
Save PavelPenkov/1d0b861239a1f81054dcf32abfe31e93 to your computer and use it in GitHub Desktop.
module Settings
DISCRIMINATOR = :type
TYPE_TO_CLASS = Hash[Setting.subclasses.map { |klass| [klass.name.underscore, klass] }].freeze
class SettingRepresenter < Representable::Decorator
include ::Representable::JSON
property :name
property DISCRIMINATOR, skip_parse: true, getter: lambda {|options| options[:represented].class.name.underscore }
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