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::StashedItem 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:



174
175
176
177
178
179
180
181
# File 'lib/recommendable/acts_as_recommended_to.rb', line 174

def stash(object)
  raise RecordNotRecommendableError unless object.recommendable?
  return if rated?(object) || stashed?(object)
  unignore(object)
  unpredict(object)
  stashed_items.create!(:stashable_id => object.id, :stashable_type => object.class.to_s)
  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



202
203
204
# File 'lib/recommendable/acts_as_recommended_to.rb', line 202

def stashed
  stashed_items.map {|item| item.stashable}
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



187
188
189
# File 'lib/recommendable/acts_as_recommended_to.rb', line 187

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

#stashed_for(klass) ⇒ Array

Get a list of records belonging to a passed class that self currently has stashed away for later.

Parameters:

  • klass (Class, String, Symbol)

    the class of records. Can be the class constant, or a String/Symbol representation of the class name.

Returns:

  • (Array)

    an array of ActiveRecord objects that self has stashed belonging to klass



211
212
213
# File 'lib/recommendable/acts_as_recommended_to.rb', line 211

def stashed_for(klass)
  klass.to_s.classify.constantize.find stash_for(klass).map(&:stashable_id)
end

#unstash(object) ⇒ Object

Destroys a Recommendable::StashedItem 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



195
196
197
# File 'lib/recommendable/acts_as_recommended_to.rb', line 195

def unstash(object)
  true if stashed_items.where(:stashable_id => object.id, :stashable_type => object.class.to_s).first.try(:destroy)
end