Module: Recommendable::ActsAsRecommendedTo::StashMethods

Defined in:
lib/recommendable/acts_as_recommended_to.rb

Instance Method Summary collapse

Instance Method Details

#stash(object) ⇒ Object

Creates a Recommendable::Stash to associate self to a passed object. This will remove the item from this user’s recommendations. If self is currently found to have liked or disliked the object, nothing will happen. It will, however, be unignored.

Parameters:

  • object (Object)

    the object you want self to stash.

Returns:

  • true if object has been stashed

Raises:

  • (UnrecommendableError)

    if you have not declared the passed object’s model to ‘act_as_recommendable`



274
275
276
277
278
279
280
281
282
283
# File 'lib/recommendable/acts_as_recommended_to.rb', line 274

def stash object
  raise UnrecommendableError unless object.recommendable?
  return if rated?(object) || stashed?(object)

  run_hook :before_stash, object
  stashed_items.create! :stashable_id => object.id, :stashable_type => object.class
  run_hook :after_stash, object

  true
end

#stashedArray

Get a list of records that self has currently stashed for later

Returns:

  • (Array)

    an array of ActiveRecord objects that self has stashed



310
311
312
# File 'lib/recommendable/acts_as_recommended_to.rb', line 310

def stashed
  Recommendable.recommendable_classes.map { |klass| stashed_for klass }.flatten
end

#stashed?(object) ⇒ Boolean

Checks to see if self has already stashed a passed object for later.

Parameters:

  • object (Object)

    the object you want to check

Returns:

  • (Boolean)

    true if self has stashed object, false if not



289
290
291
# File 'lib/recommendable/acts_as_recommended_to.rb', line 289

def stashed? object
  stashed_items.exists? :stashable_id => object.id, :stashable_type => object.class.base_class.to_s
end

#unstash(object) ⇒ Object

Destroys a Recommendable::Stash currently associating self with object

Parameters:

  • object (Object)

    the object you want to remove from self’s stash

Returns:

  • true if object is stashed, nil if nothing happened



297
298
299
300
301
302
303
304
305
# File 'lib/recommendable/acts_as_recommended_to.rb', line 297

def unstash object
  stash = stashed_items.where(:stashable_id => object.id, :stashable_type => object.class.base_class.to_s)
  if stash.exists?
    run_hook :before_unstash, object
    stash.first.destroy
    run_hook :after_unstash, object
    true
  end
end