Sha256: 5f216d3ee505ffde63c3751df54b36bb3b13f691475e0680f1f531ecb07ef105

Contents?: true

Size: 1.53 KB

Versions: 2

Compression:

Stored size: 1.53 KB

Contents

module ActiveHashRelation::AssociationFilters
  def filter_associations(resource, params, model: nil)
    unless model
      model = model_class_name(resource)
    end

    model.reflect_on_all_associations.map(&:name).each do |association|
      if params[association]
        association_name = association.to_s.titleize.split.join
        if self.configuration.has_filter_classes
          if self.configuration.use_unscoped
            association_filters = self.filter_class(association_name).new(
              association_name.singularize.constantize.unscoped.all,
              params[association]
            ).apply_filters
          else
            association_filters = self.filter_class(association_name).new(
              association_name.singularize.constantize.all,
              params[association]
            ).apply_filters
          end
        else
          if self.configuration.use_unscoped
            association_filters = ActiveHashRelation::FilterApplier.new(
              association_name.singularize.constantize.unscoped.all,
              params[association],
              include_associations: true
            ).apply_filters
          else
            association_filters = ActiveHashRelation::FilterApplier.new(
              association_name.singularize.constantize.all,
              params[association],
              include_associations: true
            ).apply_filters
          end
        end
        resource = resource.joins(association).merge(association_filters)
      end
    end

    return resource
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
active_hash_relation-1.0.3 lib/active_hash_relation/association_filters.rb
active_hash_relation-1.0.2 lib/active_hash_relation/association_filters.rb