lib/immoscout/models/concerns/renderable.rb in immoscout-1.3.1 vs lib/immoscout/models/concerns/renderable.rb in immoscout-1.3.2

- old
+ new

@@ -1,7 +1,11 @@ +# frozen_string_literal: true + require 'json' +# rubocop:disable Metrics/BlockLength because this is how an ActiveSupport +# concern looks like module Immoscout module Models module Concerns module Renderable extend ActiveSupport::Concern @@ -20,15 +24,20 @@ 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 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 + # use :alias instead of key as json-key property = value.fetch(:alias, key) # use :default if present AND value is nil rendered = send(key) || value.fetch(:default, nil) memo[property] = \ @@ -40,10 +49,13 @@ rendered.try(:as_json) || rendered end memo end end + # rubocop:enable Metrics/PerceivedComplexity + # rubocop:enable Metrics/MethodLength + # rubocop:enable Metrics/CyclomaticComplexity def to_json_wrapped { self.class.try(:json_wrapper) => to_json_unwrapped } end @@ -59,5 +71,6 @@ end end end end end +# rubocop:enable Metrics/BlockLength