Sha256: 448fe43ac00670d53f566658f49c7c9b18ee8eb7c5973cba7b3e932e27814590

Contents?: true

Size: 1.9 KB

Versions: 1

Compression:

Stored size: 1.9 KB

Contents

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 m, *args
            if m.to_s[/(.+)_list/]
                where scope: $1.singularize.classify
            else
                super
            end
        end

        def respond_to? m, include_private = false
            super || m.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

1 entries across 1 versions & 1 rubygems

Version Path
acts_as_favoritor-2.0.1 lib/acts_as_favoritor/favorite_scopes.rb