Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Use couchbase-model

  • Loading branch information...
commit 734ab6d004b876736da3c6da01f66e1888a12a71 1 parent 0fa0f8a
@avsej authored
View
3  Gemfile
@@ -25,7 +25,8 @@ group :test do
end
# Squish Application Dependencies
-gem "couchbase"
+gem "couchbase", "~> 1.2.0.dp"
+gem "couchbase-model", "~> 0.1.0"
gem "validate_url"
View
9 Gemfile.lock
@@ -42,8 +42,11 @@ GEM
execjs
coffee-script-source (1.2.0)
columnize (0.3.6)
- couchbase (1.0.0)
+ couchbase (1.2.0.dp)
+ yaji (~> 0.2.1)
yajl-ruby (~> 1.1.0)
+ couchbase-model (0.1.0)
+ couchbase (~> 1.2.0.dp)
erubis (2.7.0)
execjs (1.2.13)
multi_json (~> 1.0)
@@ -125,6 +128,7 @@ GEM
multi_json (>= 1.0.2)
validate_url (0.2.0)
activemodel (>= 3.0.0)
+ yaji (0.2.2)
yajl-ruby (1.1.0)
PLATFORMS
@@ -132,7 +136,8 @@ PLATFORMS
DEPENDENCIES
coffee-rails (~> 3.1.0)
- couchbase
+ couchbase (~> 1.2.0.dp)
+ couchbase-model (~> 0.1.0)
foreman
jquery-rails
rails (= 3.1.0)
View
38 app/models/link.rb
@@ -1,11 +1,13 @@
-class Link
+class Link < Couchbase::Model
include ActiveModel::Validations
include ActiveModel::Conversion
extend ActiveModel::Callbacks
extend ActiveModel::Naming
- attr_accessor :url, :key, :views, :session_id, :created_at
+ attribute :url, :key, :views, :session_id, :created_at
+ attribute :views, :default => 0
+ attribute :created_at, :default => lambda { Time.zone.now }
define_model_callbacks :save
validates :url, :presence => true, :url => {:allow_nil => true, :message => "This is not a valid URL"}
@@ -18,19 +20,6 @@ def generate_key
end
end
- # ActiveModel
-
- def initialize(attributes = {})
- @errors = ActiveModel::Errors.new(self)
- attributes.each do |name, value|
- setter = "#{name}="
- next unless respond_to?(setter)
- send(setter, value)
- end
- self.views ||= 0
- self.created_at ||= Time.zone.now
- end
-
def to_param
self.key
end
@@ -44,24 +33,7 @@ def persisted?
def save
return false unless valid?
run_callbacks :save do
- Couch.client.set(self.key, {
- :type => self.class.to_s.downcase,
- :url => self.url,
- :key => self.key,
- :views => self.views,
- :session_id => self.session_id,
- :created_at => self.created_at
- })
- end
- end
-
- def self.find(key)
- return nil unless key
- begin
- doc = Couch.client.get(key)
- self.new(doc)
- rescue Couchbase::Error::NotFound => e
- nil
+ super
end
end
View
18 config/couchbase.yml
@@ -0,0 +1,18 @@
+common: &common
+ hostname: localhost
+ port: 8091
+ username:
+ password:
+ pool: default
+
+production:
+ <<: *common
+ bucket: squish_production
+
+test:
+ <<: *common
+ bucket: squish_test
+
+development:
+ <<: *common
+ bucket: squish_development
View
2  config/initializers/couchbase.rb
@@ -0,0 +1,2 @@
+config = YAML::load(IO.read(Rails.root.join('config', 'couchbase.yml')))
+Couchbase.connection_options = config[Rails.env].with_indifferent_access
Please sign in to comment.
Something went wrong with that request. Please try again.