Sha256: e9590f2b8d355d9e0ce485786947cf354af20b974012a0f59942ba0254a83bf3

Contents?: true

Size: 1.26 KB

Versions: 10

Compression:

Stored size: 1.26 KB

Contents

# frozen_string_literal: true

module ActsAsFavoritor
  module FavoriteScopes
    DEFAULT_PARENTS = [ApplicationRecord, ActiveRecord::Base].freeze

    def method_missing(method, *args)
      if method.to_s[/(.+)_list/]
        where(scope: $1.singularize.to_sym)
      else
        super
      end
    end

    def respond_to_missing?(method, include_private = false)
      super || method.to_s[/(.+)_list/]
    end

    def for_favoritor(favoritor)
      where(
        favoritor_id: favoritor.id,
        favoritor_type: parent_class_name(favoritor)
      )
    end

    def for_favoritable(favoritable)
      where(
        favoritable_id: favoritable.id,
        favoritable_type: parent_class_name(favoritable)
      )
    end

    def for_favoritor_type(favoritor_type)
      where(favoritor_type: favoritor_type)
    end

    def for_favoritable_type(favoritable_type)
      where(favoritable_type: favoritable_type)
    end

    def unblocked
      where(blocked: false)
    end

    def blocked
      where(blocked: true)
    end

    private

    def parent_class_name(object)
      if DEFAULT_PARENTS.include?(object.class.superclass) ||
         !object.class.respond_to?(:base_class)
        return object.class.name
      end

      object.class.base_class.name
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
acts_as_favoritor-6.0.2 lib/acts_as_favoritor/favorite_scopes.rb
acts_as_favoritor-6.0.1 lib/acts_as_favoritor/favorite_scopes.rb
acts_as_favoritor-6.0.0 lib/acts_as_favoritor/favorite_scopes.rb
acts_as_favoritor-5.0.3 lib/acts_as_favoritor/favorite_scopes.rb
acts_as_favoritor-5.0.2 lib/acts_as_favoritor/favorite_scopes.rb
acts_as_favoritor-5.0.0 lib/acts_as_favoritor/favorite_scopes.rb
acts_as_favoritor-4.0.3 lib/acts_as_favoritor/favorite_scopes.rb
acts_as_favoritor-4.0.2 lib/acts_as_favoritor/favorite_scopes.rb
acts_as_favoritor-4.0.1 lib/acts_as_favoritor/favorite_scopes.rb
acts_as_favoritor-4.0.0 lib/acts_as_favoritor/favorite_scopes.rb