Class: Sharkey::Link
- Inherits:
-
Object
- Object
- Sharkey::Link
- Includes:
- DataMapper::Resource
- Defined in:
- lib/sharkey/models.rb
Overview
Represents a single HyperLink specified by the user.
Class Method Summary collapse
-
.by_category(category_id) ⇒ Object
Returns all Links that have a Category with ‘category_id`.
-
.by_tag(tag_id) ⇒ Object
Returns all Links that have a Tag with ‘tag_id`.
-
.create_link(title, url, added_at, tags, category, comment) ⇒ Object
Creates a new Link with specified parameters.
- .update_link(id, title, url, tags, category, comment) ⇒ Object
Instance Method Summary collapse
- #toggle_favorite ⇒ Object
-
#visit ⇒ Object
Increases the visit count by one.
-
#visited? ⇒ Boolean
Tells if this link was ever visited.
Class Method Details
.by_category(category_id) ⇒ Object
Returns all Links that have a Category with ‘category_id`
138 139 140 141 142 143 144 145 146 147 148 149 |
# 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) ⇒ Object
Returns all Links that have a Tag with ‘tag_id`
124 125 126 127 128 129 130 131 132 133 134 135 |
# 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) ⇒ Object
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
Creates a new Link with specified parameters.
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/sharkey/models.rb', line 68 def self.create_link(title, url, added_at, , 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 = [] if (not .nil?) and (not .empty?) .each do |tag| # If Sharkey::Tag exists, return it. # Otherwise, create it << 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: , category: Sharkey::Category.get(category), comment: comment || "") end |
.update_link(id, title, url, tags, category, comment) ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/sharkey/models.rb', line 100 def self.update_link(id, title, url, , category, comment) return if id.nil? or url.nil? # This array will contain the Tags objects # created here = [] if (not .nil?) and (not .empty?) .each do |tag| # If Sharkey::Tag exists, return it. # Otherwise, create it << Sharkey::Tag.first_or_create(name: tag) end end Sharkey::Link.get(id).update(title: title, url: url, tags: , category: Sharkey::Category.get(category), comment: comment || ""); end |
Instance Method Details
#toggle_favorite ⇒ Object
151 152 153 |
# File 'lib/sharkey/models.rb', line 151 def toggle_favorite self.update(favorite: (not self.favorite)); end |
#visit ⇒ Object
Increases the visit count by one
161 162 163 164 |
# 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? ⇒ Boolean
Tells if this link was ever visited
156 157 158 |
# File 'lib/sharkey/models.rb', line 156 def visited? self.visit_count != 0 end |