Class: Sharkey::Link

Inherits:
Object
  • Object
show all
Includes:
DataMapper::Resource
Defined in:
lib/sharkey/models.rb

Overview

Represents a single HyperLink specified by the user.

Class Method Summary collapse

Instance Method Summary collapse

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
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.

Parameters:

  • tags

    Array of Strings as tag names

  • added_at

    DateTime object or ‘nil` for DateTime.now

  • category

    An ID of existing category



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, 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


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, 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

Instance Method Details

#toggle_favoriteObject



151
152
153
# File 'lib/sharkey/models.rb', line 151

def toggle_favorite
  self.update(favorite: (not self.favorite));
end

#visitObject

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

Returns:

  • (Boolean)


156
157
158
# File 'lib/sharkey/models.rb', line 156

def visited?
  self.visit_count != 0
end