Sha256: 691390347d284d5f0dbacad5e0699fa77e0fb14a0a46edd03a5088b886092903

Contents?: true

Size: 1.77 KB

Versions: 2

Compression:

Stored size: 1.77 KB

Contents

module FavouriteObject
  class Favourite < ActiveRecord::Base
	self.table_name = "favourite_object_favourites"

	belongs_to :target, polymorphic: true
	belongs_to :owner, polymorphic: true

	# Params for creating the notification message.
	serialize :params, Hash

	mattr_accessor :views
	@@views = {
	  message: {
		template_path: Proc.new {|n| "favourite_object/#{n.target_type.underscore}/message" }
	  }
	}

	if ActiveRecord::VERSION::MAJOR < 4
	  attr_accessible :target, :owner
	end

	def self.find_with_target(owner, target_id, target_type, third_party_flag=false)
		if third_party_flag
			 return Favourite.for_owner(owner).find_or_initialize_by(third_party_id: target_id,
				third_party_type: target_type, third_party_flag: true)
		end

		Favourite.for_owner(owner).find_or_initialize_by(target_id: target_id,
			target_type: target_type)
	end

	def self.for_owner(owner)
		where(owner_id: owner.id)
		.where(owner_type: owner.class.base_class)
	end

	def message
	  ActionView::Base.new(
			 Rails.configuration.paths["app/views"]).render(
			 :template => self.class.views[:message][:template_path].call(self), :formats => [:html],
			 :locals => {object: self.target }, :layout => false)
	end

	#toggles the is_favourited status
	def toggle
		if is_favourited
			self.is_favourited = false
		else
			self.is_favourited = true
		end
		self.save
	end

	def favourite
		self.is_favourited = true
		self.save
	end

	def un_favourite
		self.is_favourited = false
		self.save
	end

	def self.is_favourited?(owner, target_id, target_type, third_party_flag=false)
		favourite = self.find_with_target(owner, target_id, target_type, third_party_flag)

		return false if favourite.blank? || favourite.is_favourited == false
		true
	end

	def self.with_type(type)
		where('target_type = ?', type)
	end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
favourite_object-0.1.1 app/models/favourite_object/favourite.rb
favourite_object-0.1.0 app/models/favourite_object/favourite.rb