class Sharkey::Link
Represents a single HyperLink specified by the user.
Public Class Methods
by_category(category_id)
click to toggle source
Returns all Links that have a Category
with ‘category_id`
# File lib/sharkey/models.rb, line 138 def self.by_category(category_id) # RANT: I don't know why I couldn't simply do something like # `Sharkey::Link.all(:category => Sharkey::Category.get(category_id))` # it seems so strange! # DataMapper's docs imply that we actually _can_, # so why...? categorizations = Sharkey::Categorization.all(:category_id => category_id) Sharkey::Link.all(:categorization => categorizations) end
by_tag(tag_id)
click to toggle source
Returns all Links that have a Tag
with ‘tag_id`
# File lib/sharkey/models.rb, line 124 def self.by_tag(tag_id) # RANT: I don't know why I couldn't simply do something like # `Sharkey::Link.all(:tag => Sharkey::Tag.get(tag_id))` # it seems so strange! # DataMapper's docs imply that we actually _can_, # so why...? taggings = Sharkey::Tagging.all(:tag_id => tag_id) Sharkey::Link.all(:taggings => taggings) end
create_link(title, url, added_at, tags, category, comment)
click to toggle source
Creates a new Link
with specified parameters.
@note Link#create is a reserved method for DataMapper
it actually inserts it on the database. This function differs from it on the sense that it also creates the Tags related to this Link
@param tags Array of Strings as tag names @param added_at DateTime object or ‘nil` for DateTime.now @param category An ID of existing category
# File lib/sharkey/models.rb, line 68 def self.create_link(title, url, added_at, tags, category, comment) # Silently fail return if url.nil? # Do not allow relative URLs! # Always assume HTTP if Addressable::URI.parse(url).relative? url = "http://#{url}" end # This array will contain the Tags objects # created here the_tags = [] if (not tags.nil?) and (not tags.empty?) tags.each do |tag| # If Sharkey::Tag exists, return it. # Otherwise, create it the_tags << Sharkey::Tag.first_or_create(name: tag) end end # Actually populating the database with # a new Link Sharkey::Link.create(title: title || "", url: url, added_at: added_at || DateTime.now, tags: the_tags, category: Sharkey::Category.get(category), comment: comment || "") end
update_link(id, title, url, tags, category, comment)
click to toggle source
# File lib/sharkey/models.rb, line 100 def self.update_link(id, title, url, tags, category, comment) return if id.nil? or url.nil? # This array will contain the Tags objects # created here the_tags = [] if (not tags.nil?) and (not tags.empty?) tags.each do |tag| # If Sharkey::Tag exists, return it. # Otherwise, create it the_tags << Sharkey::Tag.first_or_create(name: tag) end end Sharkey::Link.get(id).update(title: title, url: url, tags: the_tags, category: Sharkey::Category.get(category), comment: comment || ""); end
Public Instance Methods
toggle_favorite()
click to toggle source
# File lib/sharkey/models.rb, line 151 def toggle_favorite self.update(favorite: (not self.favorite)); end
visit()
click to toggle source
Increases the visit count by one
# File lib/sharkey/models.rb, line 161 def visit self.update(last_visit: DateTime.now); self.update(visit_count: (self.visit_count + 1)); end
visited?()
click to toggle source
Tells if this link was ever visited
# File lib/sharkey/models.rb, line 156 def visited? self.visit_count != 0 end