Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add some logic for fetching and saving Links from couchbase using Act…

…iveModel
  • Loading branch information...
commit e4582a9502a80b392a03299d6df4407bd598350c 1 parent 5169d29
@davidjrice davidjrice authored
View
3  Gemfile
@@ -5,8 +5,7 @@ gem 'rails', '3.1.0'
# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'
-gem 'sqlite3'
-
+gem "couchbase-ruby-client", :git => "git://github.com/couchbase/couchbase-ruby-client.git", :require => "couchbase"
# Gems used only for assets and not required
# in production environments by default.
View
17 Gemfile.lock
@@ -1,3 +1,13 @@
+GIT
+ remote: git://github.com/couchbase/couchbase-ruby-client.git
+ revision: 16f6a9f8986abc0682e99631500fd45e231aac9f
+ specs:
+ couchbase-ruby-client (0.9.5)
+ curb (~> 0.7.15)
+ memcached (~> 1.3)
+ yaji (~> 0.0.9)
+ yajl-ruby (~> 0.8.2)
+
GEM
remote: http://rubygems.org/
specs:
@@ -41,6 +51,7 @@ GEM
coffee-script-source
execjs
coffee-script-source (1.1.2)
+ curb (0.7.15)
erubis (2.7.0)
execjs (1.2.4)
multi_json (~> 1.0)
@@ -53,6 +64,7 @@ GEM
i18n (>= 0.4.0)
mime-types (~> 1.16)
treetop (~> 1.4.8)
+ memcached (1.3)
mime-types (1.16)
multi_json (1.0.3)
polyglot (0.3.2)
@@ -91,7 +103,6 @@ GEM
hike (~> 1.2)
rack (~> 1.0)
tilt (!= 1.3.0, ~> 1.1)
- sqlite3 (1.3.4)
thor (0.14.6)
tilt (1.3.3)
treetop (1.4.10)
@@ -103,15 +114,17 @@ GEM
uglifier (1.0.3)
execjs (>= 0.3.0)
multi_json (>= 1.0.2)
+ yaji (0.0.9)
+ yajl-ruby (0.8.3)
PLATFORMS
ruby
DEPENDENCIES
coffee-rails (~> 3.1.0)
+ couchbase-ruby-client!
jquery-rails
rails (= 3.1.0)
sass-rails (~> 3.1.0)
- sqlite3
turn
uglifier
View
11 app/controllers/links_controller.rb
@@ -1,8 +1,17 @@
class LinksController < ApplicationController
def index
- @links = Link.most_viewed
+ @links = Link.popular
@link = Link.new
end
+ def create
+ @link = Link.new(params[:link])
+ if @link.save
+ redirect_to @link
+ else
+ render :new
+ end
+ end
+
end
View
52 app/models/link.rb
@@ -0,0 +1,52 @@
+class Link
+
+ include ActiveModel::Validations
+ include ActiveModel::Conversion
+ extend ActiveModel::Callbacks
+ extend ActiveModel::Naming
+
+ define_model_callbacks :save
+
+ attr_accessor :url, :key
+
+ validates :url, :presence => true
+ before_save :generate_key
+
+ def self.popular
+ []
+ end
+
+ def self.find(key)
+ begin
+ Couch.client.get(self.key)
+ rescue Memcached::NotFound => e
+ nil
+ end
+ end
+
+ def initialize(attributes = {})
+ @errors = ActiveModel::Errors.new(self)
+ attributes.each do |name, value|
+ send("#{name}=", value)
+ end
+ end
+
+ def persisted?
+ false
+ end
+
+ def save
+ Couch.client.set(self.key, {
+ :url => self.url,
+ :key => self.key
+ })
+ end
+
+ def generate_key
+ while self.key.nil?
+ random = SecureRandom.hex(2)
+ self.key = random if self.class.find(random).nil?
+ end
+ end
+
+end
View
4 app/views/links/index.html.erb
@@ -0,0 +1,4 @@
+<%= form_for @link do |f| %>
+ <%= f.text_field :url %>
+ <%= f.submit %>
+<%- end -%>
Please sign in to comment.
Something went wrong with that request. Please try again.