Class: FavouriteObject::Favourite
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- FavouriteObject::Favourite
- Defined in:
- app/models/favourite_object/favourite.rb
Constant Summary collapse
- @@views =
{ message: { template_path: Proc.new {|n| "favourite_object/#{n.target_type.underscore}/message" } } }
Class Method Summary collapse
- .find_with_target(owner, target_id, target_type, third_party_flag = false) ⇒ Object
- .for_owner(owner) ⇒ Object
- .is_favourited?(owner, target_id, target_type, third_party_flag = false) ⇒ Boolean
- .with_type(type) ⇒ Object
Instance Method Summary collapse
- #favourite ⇒ Object
- #message ⇒ Object
-
#toggle ⇒ Object
toggles the is_favourited status.
- #un_favourite ⇒ Object
Class Method Details
.find_with_target(owner, target_id, target_type, third_party_flag = false) ⇒ Object
22 23 24 25 26 27 28 29 30 |
# File 'app/models/favourite_object/favourite.rb', line 22 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 |
.for_owner(owner) ⇒ Object
32 33 34 35 |
# File 'app/models/favourite_object/favourite.rb', line 32 def self.for_owner(owner) where(owner_id: owner.id) .where(owner_type: owner.class.base_class) end |
.is_favourited?(owner, target_id, target_type, third_party_flag = false) ⇒ Boolean
64 65 66 67 68 69 |
# File 'app/models/favourite_object/favourite.rb', line 64 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 |
.with_type(type) ⇒ Object
71 72 73 |
# File 'app/models/favourite_object/favourite.rb', line 71 def self.with_type(type) where('target_type = ?', type) end |
Instance Method Details
#favourite ⇒ Object
54 55 56 57 |
# File 'app/models/favourite_object/favourite.rb', line 54 def favourite self.is_favourited = true self.save end |
#message ⇒ Object
37 38 39 40 41 42 |
# File 'app/models/favourite_object/favourite.rb', line 37 def 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 |
#toggle ⇒ Object
toggles the is_favourited status
45 46 47 48 49 50 51 52 |
# File 'app/models/favourite_object/favourite.rb', line 45 def toggle if is_favourited self.is_favourited = false else self.is_favourited = true end self.save end |
#un_favourite ⇒ Object
59 60 61 62 |
# File 'app/models/favourite_object/favourite.rb', line 59 def un_favourite self.is_favourited = false self.save end |