lib/immoscout/models/concerns/renderable.rb in immoscout-1.3.2 vs lib/immoscout/models/concerns/renderable.rb in immoscout-1.4.0
- old
+ new
@@ -5,33 +5,35 @@
# rubocop:disable Metrics/BlockLength because this is how an ActiveSupport
# concern looks like
module Immoscout
module Models
module Concerns
+ # Includes functionality to serialize a Ruby model properly.
module Renderable
extend ActiveSupport::Concern
included do
def as_json
wrapped? ? to_json_wrapped : to_json_unwrapped
end
- def to_json
+ def to_json(*)
as_json.to_json
end
alias_method :to_s, :to_json
- private
+ protected
def wrapped?
self.class.try(:json_wrapper)
end
# rubocop:disable Metrics/PerceivedComplexity because this is the
# bare minimum logic
# rubocop:disable Metrics/MethodLength dito
# rubocop:disable Metrics/CyclomaticComplexity dito
+ # rubocop:disable Metrics/AbcSize dito
def to_h
self.class.properties.each_with_object({}) do |(key, value), memo|
# skip if it's readonly and should not be exposed in #as_json
readonly = value.fetch(:readonly, false)
next if readonly.try(:call, self) || readonly == true
@@ -52,10 +54,11 @@
end
end
# rubocop:enable Metrics/PerceivedComplexity
# rubocop:enable Metrics/MethodLength
# rubocop:enable Metrics/CyclomaticComplexity
+ # rubocop:enable Metrics/AbcSize
def to_json_wrapped
{ self.class.try(:json_wrapper) => to_json_unwrapped }
end
@@ -63,12 +66,9 @@
to_h
.compact
.stringify_keys
.deep_transform_keys { |key| key.camelize :lower }
end
- end
-
- class_methods do
end
end
end
end
end