Sha256: c1c9610f375ce54a59c8878cf88e58d61cb445491f1a3b260d4ccf039976d517

Contents?: true

Size: 1.4 KB

Versions: 1

Compression:

Stored size: 1.4 KB

Contents

class SerializerFieldFilter
  module Relation
    extend ActiveSupport::Concern
    included do
      [:has_many, :belongs_to, :has_one].each do |relation_method_name|
        class_eval <<-RUBY
          def self.#{relation_method_name}_with_filter(resource_name, serializer: nil)
            attribute resource_name
            resource_method_module = Module.new do
              define_method resource_name do
                filter = instance_options[:field_filter]&.scope_of(resource_name)
                resource_options = {
                  serializer: serializer,
                  fields: filter&.root_fields,
                  field_filter: filter
                }
                resources = defined?(super) ? super() : object.send(resource_name)
                if "#{relation_method_name}" == "has_many"
                  resources.to_a.map do |resource|
                    serializer_for_resource(resource, resource_options) if resource.present?
                  end
                else
                  serializer_for_resource(resources, resource_options) if resources.present?
                end
              end
            end
            prepend resource_method_module
          end
        RUBY
      end

      private

      def serializer_for_resource(resource, resource_options)
        ActiveModelSerializers::SerializableResource.new(resource, resource_options).as_json
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
serializer_field_filter-0.0.3 lib/serializer_field_filter/relation.rb