Sha256: 1cbabfb0a34daf0b4b179932a872194186079e1cb8f966f7add9d265e79609bb

Contents?: true

Size: 1.2 KB

Versions: 2

Compression:

Stored size: 1.2 KB

Contents

# -*- encoding : utf-8 -*-
module Blacklight::User

  # This gives us an is_blacklight_user method that can be included in
  # the containing applications models. 
  # SEE ALSO:  The /lib/blacklight/engine.rb class for how when this 
  # is injected into the hosting application through ActiveRecord::Base extend
  def self.included(base)
    if base.respond_to? :has_many
      base.send :has_many, :bookmarks, :dependent => :destroy, :as => :user
      base.send :has_many, :searches,  :dependent => :destroy, :as => :user
    end
  end

  def has_bookmarks?
    bookmarks.any?
  end
    
  def has_searches?
    searches.any?
  end
    
  def bookmarked_document_ids
    self.bookmarks.pluck(:document_id)
  end
    
  # see #current_bookmark_for, is easier
  def document_is_bookmarked?(document_id)
    bookmarked_document_ids.include? document_id.to_s
  end
    
  # returns a Bookmark object if there is one for document_id, else
  # nil. 
  def existing_bookmark_for(document_id)
    self.bookmarks.where(:document_id => document_id).first
  end
    
  def documents_to_bookmark=(docs)
    docs.reject { |doc| document_is_bookmarked?(doc[:document_id]) }.each do |doc|
      self.bookmarks.create(doc) 
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
blacklight-5.0.0.pre3 lib/blacklight/user.rb
blacklight-5.0.0.pre2 lib/blacklight/user.rb