Sha256: 18380c398c6bed6b904271623feee8e6b36ea290b6f316ab97ad2f3549f66475

Contents?: true

Size: 1.49 KB

Versions: 37

Compression:

Stored size: 1.49 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)
    base.send :has_many, :bookmarks, :dependent => :destroy, :as => :user
    base.send :has_many, :searches,  :dependent => :destroy, :as => :user
    base.send :include, InstanceMethods
  end

  # The following methods will be included in any active model object
  # that calls "is_blacklight_user"
  module InstanceMethods

    def has_bookmarks?
      bookmarks.count > 0
    end
    
    def has_searches?
      searches.count > 0
    end
    
    def bookmarked_document_ids
      self.bookmarks.map{|bm|bm.document_id}
    end
    
    # see #current_bookmark_for, is easier
    def document_is_bookmarked?(document_id)
      bookmarked_document_ids.include? document_id
    end
    
    # returns a Bookmark object if there is one for document_id, else
    # nil. 
    def existing_bookmark_for(document_id)
      # to_a, we don't want to go to the database, we want to use cached
      # copy. 
      self.bookmarks.to_a.find {|b| b.document_id == document_id}
    end
    
    def documents_to_bookmark=(docs)
      docs.each do |doc|
        self.bookmarks.create(doc) unless bookmarked_document_ids.include?(doc[:document_id])
      end
    end

  end # /InstanceMethods

end

Version data entries

37 entries across 37 versions & 1 rubygems

Version Path
blacklight-3.4.2 lib/blacklight/user.rb
blacklight-3.4.1 lib/blacklight/user.rb
blacklight-3.4.0 lib/blacklight/user.rb
blacklight-3.3.2 lib/blacklight/user.rb
blacklight-3.3.1 lib/blacklight/user.rb
blacklight-3.3.0 lib/blacklight/user.rb
blacklight-3.2.2 lib/blacklight/user.rb
blacklight-3.2.1 lib/blacklight/user.rb
blacklight-3.2.0 lib/blacklight/user.rb
blacklight-3.2.0pre2 lib/blacklight/user.rb
blacklight-3.2.0pre1 lib/blacklight/user.rb
blacklight-3.1.2 lib/blacklight/user.rb
blacklight-3.1.1 lib/blacklight/user.rb
blacklight-3.1.0 lib/blacklight/user.rb
blacklight-3.0.0 lib/blacklight/user.rb
blacklight-3.0.0pre8 lib/blacklight/user.rb
blacklight-3.0.0pre7 lib/blacklight/user.rb