Sha256: 19016c8399e892156eba177f45d53bff44112286aed956c9a09e813cfab472e2

Contents?: true

Size: 1.25 KB

Versions: 19

Compression:

Stored size: 1.25 KB

Contents

module DataMapper
  module Is
    module Searchable

      def is_searchable(options = {})
        search_repository = options.delete(:repository) || :search
        @search_repository = search_repository

        extend ClassMethods

        after(:save) do |success, *args|
          if success
            # We use the adapter directly to bypass our after :save,
            # and because create caches the repository and new_record? state.
            DataMapper.repository(search_repository).adapter.create([self])
          end
        end

        after(:destroy) do |success|
          if success
            # Since this is after the model has been destroyed, it is
            # a new record, and a simple to_query will return nil.
            query = model.to_query(repository, key, {})
            DataMapper.repository(search_repository).adapter.delete(query)
          end
        end
      end

      module ClassMethods
        def search(search_options = {}, options = {})
          docs = repository(@search_repository) { self.all(search_options) }
          ids = docs.collect { |doc| doc[:id] }
          self.all(options.merge(key.first => ids))
        end
      end # module ClassMethods

    end # module Searchable
  end # module Is
end # module DataMapper

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
dm-is-searchable-1.2.0 lib/dm-is-searchable/is/searchable.rb
dm-is-searchable-1.2.0.rc2 lib/dm-is-searchable/is/searchable.rb
dm-is-searchable-1.2.0.rc1 lib/dm-is-searchable/is/searchable.rb
dm-is-searchable-1.1.0 lib/dm-is-searchable/is/searchable.rb
dm-is-searchable-1.1.0.rc3 lib/dm-is-searchable/is/searchable.rb
dm-is-searchable-1.1.0.rc2 lib/dm-is-searchable/is/searchable.rb
dm-is-searchable-1.1.0.rc1 lib/dm-is-searchable/is/searchable.rb
dm-is-searchable-1.0.2 lib/dm-is-searchable/is/searchable.rb
dm-is-searchable-1.0.1 lib/dm-is-searchable/is/searchable.rb
dm-is-searchable-1.0.0 lib/dm-is-searchable/is/searchable.rb
dm-is-searchable-1.0.0.rc3 lib/dm-is-searchable/is/searchable.rb
dm-is-searchable-1.0.0.rc2 lib/dm-is-searchable/is/searchable.rb
dm-is-searchable-1.0.0.rc1 lib/dm-is-searchable/is/searchable.rb
dm-is-searchable-0.10.2 lib/dm-is-searchable/is/searchable.rb
dm-is-searchable-0.10.1 lib/dm-is-searchable/is/searchable.rb
dm-is-searchable-0.10.0 lib/dm-is-searchable/is/searchable.rb
dm-is-searchable-0.9.10 lib/dm-is-searchable/is/searchable.rb
dm-is-searchable-0.9.11 lib/dm-is-searchable/is/searchable.rb
dm-is-searchable-0.9.9 lib/dm-is-searchable/is/searchable.rb