Class: FavouriteObject::Favourite

Inherits:
ActiveRecord::Base
  • Object
show all
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

Instance Method Summary collapse

Class Method Details

.for_owner(owner) ⇒ Object



22
23
24
25
# File 'app/models/favourite_object/favourite.rb', line 22

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

Returns:

  • (Boolean)


54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'app/models/favourite_object/favourite.rb', line 54

def self.is_favourited?(owner, target_id, target_type, third_party_flag=false)
    if third_party_flag 
        favourite = FavouriteObject::Favourite.where(owner: owner, third_party_id: target_id.to_s,
                        third_party_type: target_type).first
    else
        favourite = FavouriteObject::Favourite.where(owner: owner, target_id: target_id,
                        target_type: target_type).first
    end

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

    return true
end

.with_type(type) ⇒ Object



68
69
70
# File 'app/models/favourite_object/favourite.rb', line 68

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

Instance Method Details

#favouriteObject



44
45
46
47
# File 'app/models/favourite_object/favourite.rb', line 44

def favourite
  self.is_favourited = true
  self.save
end

#messageObject



27
28
29
30
31
32
# File 'app/models/favourite_object/favourite.rb', line 27

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

#toggleObject

toggles the is_favourited status



35
36
37
38
39
40
41
42
# File 'app/models/favourite_object/favourite.rb', line 35

def toggle
  if is_favourited
    self.is_favourited = false
  else
    self.is_favourited = true
  end
  self.save
end

#un_favouriteObject



49
50
51
52
# File 'app/models/favourite_object/favourite.rb', line 49

def un_favourite
  self.is_favourited = false
  self.save
end