Sha256: fd943165043194afb73aa716abb28fc18102ba721aa7d21b4c09a8eb610148fa

Contents?: true

Size: 1.8 KB

Versions: 4

Compression:

Stored size: 1.8 KB

Contents

# frozen_string_literal: true

module ActsAsFavoritor
  module FavoriteScopes
    # Allows magic names on send(scope + '_list') - returns favorite records of
    # certain scope.
    # e.g. favoritors == favoritors.send('favorite_list')
    def method_missing(method, *args)
      if method.to_s[/(.+)_list/]
        where(scope: $1.singularize)
      else
        super
      end
    end

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

    def all_list
      all
    end

    # returns favorite records where favoritor is the record passed in.
    def for_favoritor(favoritor)
      where(
        favoritor_id: favoritor.id,
        favoritor_type: parent_class_name(favoritor)
      )
    end

    # returns favorite records where favoritable is the record passed in.
    def for_favoritable(favoritable)
      where(
        favoritable_id: favoritable.id,
        favoritable_type: parent_class_name(favoritable)
      )
    end

    # returns favorite records where favoritor_type is the record passed in.
    def for_favoritor_type(favoritor_type)
      where(favoritor_type: favoritor_type)
    end

    # returns favorite records where favoritable_type is the record passed in.
    def for_favoritable_type(favoritable_type)
      where(favoritable_type: favoritable_type)
    end

    # returns favorite records from past 2 weeks with default parameter.
    def recent(from)
      where('created_at > ?', (from || 2.weeks.ago).to_s(:db))
    end

    # returns favorite records in descending order.
    def descending
      order('favorites.created_at desc')
    end

    # returns unblocked favorite records.
    def unblocked
      where(blocked: false)
    end

    # returns blocked favorite records.
    def blocked
      where(blocked: true)
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
acts_as_favoritor-3.0.0 lib/acts_as_favoritor/favorite_scopes.rb
acts_as_favoritor-2.1.2 lib/acts_as_favoritor/favorite_scopes.rb
acts_as_favoritor-2.1.1 lib/acts_as_favoritor/favorite_scopes.rb
acts_as_favoritor-2.1.0 lib/acts_as_favoritor/favorite_scopes.rb