Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Tidy up Link object for easier explanation

  • Loading branch information...
commit 5ba03dd901ab020d22c7c812c706b9aba308a304 1 parent a3b03ad
@davidjrice davidjrice authored
Showing with 58 additions and 56 deletions.
  1. +58 −56 app/models/link.rb
View
114 app/models/link.rb
@@ -5,67 +5,22 @@ class Link
extend ActiveModel::Callbacks
extend ActiveModel::Naming
- def self.design
- @@design ||= Couch.client.design_docs['link']
- end
-
- @@design_doc = {
- 'by_view_count' => {
- 'map' => 'function(doc){ if(doc.type == "link"){ emit(doc.views, doc); }}',
- },
- 'by_session_id' => {
- 'map' => 'function(doc){ if(doc.type == "link" && doc.session_id != null){ emit(doc.session_id, doc); }}',
- },
- 'by_created_at' => {
- 'map' => 'function(doc){ if(doc.type == "link" && doc.created_at != null){ emit(doc.created_at, doc); }}',
- }
- }
-
- if Couch.client.design_docs.include?("link")
- Couch.client.delete_design_doc('link')
- Couch.client.save_design_doc('link', @@design_doc)
- else
- Couch.client.save_design_doc('link', @@design_doc)
- end
-
- define_model_callbacks :save
-
attr_accessor :url, :key, :views, :session_id, :created_at
@@keys = [:url, :key, :views, :session_id, :created_at]
-
+ define_model_callbacks :save
validates :url, :presence => true, :url => {:allow_nil => true}
before_save :generate_key
- def to_param
- self.key
- end
-
- def self.popular
- results = design.by_view_count(:descending => true).entries
- results.map { |result| new(result['value']) }
- end
-
- def self.by_session_id(session_id)
- results = design.by_session_id(:key => session_id).entries
- results.map { |result| new(result['value']) }
- end
-
- def self.recent
- results = design.by_created_at(:descending => true).entries
- results.map { |result| new(result['value']) }
- end
-
- def self.find(key)
- return nil unless key
- begin
- doc = Couch.client.get(key)
- self.new(doc)
- rescue Memcached::NotFound => e
- nil
+ def generate_key
+ while self.key.nil?
+ random = SecureRandom.hex(2)
+ self.key = random if self.class.find(random).nil?
end
end
+ # ActiveModel
+
def initialize(attributes = {})
@errors = ActiveModel::Errors.new(self)
attributes.each do |name, value|
@@ -76,6 +31,10 @@ def initialize(attributes = {})
self.created_at ||= Time.zone.now
end
+ def to_param
+ self.key
+ end
+
def persisted?
return false unless (key && valid?)
# TODO need a better way to track if an object is *dirty* or not...
@@ -98,11 +57,54 @@ def save
true
end
- def generate_key
- while self.key.nil?
- random = SecureRandom.hex(2)
- self.key = random if self.class.find(random).nil?
+ def self.find(key)
+ return nil unless key
+ begin
+ doc = Couch.client.get(key)
+ self.new(doc)
+ rescue Memcached::NotFound => e
+ nil
end
end
+ # Couchbase Views
+
+ def self.design
+ @@design ||= Couch.client.design_docs['link']
+ end
+
+ @@design_doc = {
+ 'by_view_count' => {
+ 'map' => 'function(doc){ if(doc.type == "link"){ emit(doc.views, doc); }}',
+ },
+ 'by_session_id' => {
+ 'map' => 'function(doc){ if(doc.type == "link" && doc.session_id != null){ emit(doc.session_id, doc); }}',
+ },
+ 'by_created_at' => {
+ 'map' => 'function(doc){ if(doc.type == "link" && doc.created_at != null){ emit(doc.created_at, doc); }}',
+ }
+ }
+
+ if Couch.client.design_docs.include?("link")
+ Couch.client.delete_design_doc('link')
+ Couch.client.save_design_doc('link', @@design_doc)
+ else
+ Couch.client.save_design_doc('link', @@design_doc)
+ end
+
+ def self.popular
+ results = design.by_view_count(:descending => true).entries
+ results.map { |result| new(result['value']) }
+ end
+
+ def self.by_session_id(session_id)
+ results = design.by_session_id(:key => session_id).entries
+ results.map { |result| new(result['value']) }
+ end
+
+ def self.recent
+ results = design.by_created_at(:descending => true).entries
+ results.map { |result| new(result['value']) }
+ end
+
end
Please sign in to comment.
Something went wrong with that request. Please try again.