Permalink
Browse files

Initial commit. Copied code from existing project. Not tested.

  • Loading branch information...
thetron committed Jun 4, 2011
0 parents commit 9226286c0d24ca3c9556ff057fa12d52e637a10d
Showing with 207 additions and 0 deletions.
  1. +1 −0 .autotest
  2. +4 −0 .gitignore
  3. +2 −0 .rspec
  4. +14 −0 Gemfile
  5. +2 −0 Rakefile
  6. BIN lib/.DS_Store
  7. +43 −0 lib/mongoid_token.rb
  8. +3 −0 lib/version.rb
  9. +22 −0 mongoid_token.gemspec
  10. BIN spec/.DS_Store
  11. +94 −0 spec/mongoid/token_spec.rb
  12. +22 −0 spec/spec_helper.rb
@@ -0,0 +1 @@
+require 'autotest/growl'
@@ -0,0 +1,4 @@
+*.gem
+.bundle
+Gemfile.lock
+pkg/*
2 .rspec
@@ -0,0 +1,2 @@
+--color
+--format documentation
14 Gemfile
@@ -0,0 +1,14 @@
+source "http://rubygems.org"
+
+# Specify your gem's dependencies in mongoid_token.gemspec
+gemspec
+
+group :test do
+ gem 'database_cleaner'
+ gem 'rspec'
+ gem 'autotest'
+ gem 'autotest-growl'
+ gem 'mongoid', '~> 2.0'
+ gem 'bson_ext'
+ gem 'mongoid-rspec'
+end
@@ -0,0 +1,2 @@
+require 'bundler'
+Bundler::GemHelper.install_tasks
Binary file not shown.
@@ -0,0 +1,43 @@
+module Mongoid
+ module Token
+ extend ActiveSupport::Concern
+
+ included do
+
+ end
+
+ module ClassMethods
+ def token(*args)
+ options = args.extract_options!
+ options[:length] ||= 4
+ options[:with] ||= :alphanumeric
+
+ self.field :token, :type => String
+ self.before_create :generate_token
+
+ set_callback(:create, :before) do |document|
+ document.create_token(options[:length], options[:with])
+ end
+ end
+ end
+
+ module InstanceMethods
+ def find_by_token(token)
+ self.class.where(:conditions => {:token => token}).limit(1)
+ end
+
+ def to_param
+ self.token
+ end
+
+ protected
+ def create_token(length, characters)
+ self.token = generate_token(length, characters) while self.token == nil || self.class.exists?(:conditions => {:token => self.token})
+ end
+
+ def generate_token(length, characters)
+ ActiveSupport::SecureRandom.hex(length)[0...length]
+ end
+ end
+ end
+end
@@ -0,0 +1,3 @@
+module MongoidToken
+ VERSION = "0.0.1"
+end
@@ -0,0 +1,22 @@
+# -*- encoding: utf-8 -*-
+$:.push File.expand_path("../lib", __FILE__)
+require "version"
+
+Gem::Specification.new do |s|
+ s.name = "mongoid_token"
+ s.version = MongoidToken::VERSION
+ s.platform = Gem::Platform::RUBY
+ s.authors = ["Nicholas Bruning"]
+ s.email = ["nicholas@bruning.com.au"]
+ # requires active_support > 3.0.0
+ s.homepage = ""
+ s.summary = %q{A little random, unique token generator for Mongoid documents.}
+ s.description = %q{Mongoid_token is a gem for creating random, unique tokens for mongoid documents, when you want shorter URLs.}
+
+ s.rubyforge_project = "mongoid_token"
+
+ s.files = `git ls-files`.split("\n")
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
+ s.require_paths = ["lib"]
+end
Binary file not shown.
@@ -0,0 +1,94 @@
+require File.join(File.dirname(__FILE__), %w[.. spec_helper])
+
+class Post
+ include Mongoid::Document
+ include Mongoid::Publishable
+ field :title
+end
+
+describe Mongoid::Publishable do
+ before :each do
+ @draft = Post.create(:published_at => nil)
+ @published = Post.create(:published_at => Time.now - 5.minutes)
+ @scheduled = Post.create(:published_at => Time.now + 12.hours)
+ end
+
+ it "should have a date and timestamp to represent publish state" do
+ Post.should have_field(:published_at).of_type(DateTime).with_default_value_of(nil)
+ end
+
+ it "should be published if the published date is in the past" do
+ @published.is_draft?.should equal false
+ @published.is_published?.should equal true
+ @published.is_scheduled?.should equal false
+ end
+
+ it "should be a draft if the published date is not set" do
+ @draft.is_draft?.should == true
+ @draft.is_published?.should == false
+ @draft.is_scheduled?.should == false
+ end
+
+ it "should be scheduled if the published date is in the future" do
+ @scheduled.is_draft?.should == false
+ @scheduled.is_published?.should == false
+ @scheduled.is_scheduled?.should == true
+ end
+
+ it "should be publishable" do
+ @draft.should respond_to :publish!
+ @draft.publish!
+ @draft.is_published?.should equal true
+ @draft.published_at.to_i.should <= Time.now.to_i
+ end
+
+ it "should be unpublishable" do
+ @published.should respond_to :unpublish!
+ @published.unpublish!
+ @published.is_draft?.should equal true
+ @published.published_at.should be nil
+ end
+
+ it "should be scheduleable" do
+ @draft.should respond_to :schedule!
+ future = (Time.now + 12.hours).to_datetime
+ @draft.schedule!(future)
+ @draft.is_scheduled?.should equal true
+ @draft.published_at.to_i.should == future.to_i
+ end
+
+ it "should return all draft models" do
+ Post.drafts.count.should equal 1
+ Post.drafts.first.should == @draft
+ end
+
+ it "should return all published models" do
+ Post.published.count.should equal 1
+ Post.published.first.should == @published
+ end
+
+ it "should return all scheduled models" do
+ Post.scheduled.count.should equal 1
+ Post.scheduled.first.should == @scheduled
+ end
+
+ it "should return published posts in descending date order" do
+ @draft.publish!
+ @scheduled.publish!
+ last_stamp = Time.now
+ Post.published.each do |post|
+ post.published_at.should <= last_stamp
+ last_stamp = post.published_at
+ end
+ end
+
+ it "should return scheduled posts in ascending date order" do
+ @draft.schedule!(Time.now + 1.hour)
+ @published.schedule!(Time.now + 5.days)
+ last_stamp = Time.now
+ Post.scheduled.each do |post|
+ post.published_at.should > last_stamp
+ last_stamp = post.published_at
+ end
+ end
+end
@@ -0,0 +1,22 @@
+$: << File.expand_path("../../lib", __FILE__)
+
+require 'database_cleaner'
+require 'mongoid'
+require 'mongoid-rspec'
+require 'mongoid_publishable'
+
+RSpec.configure do |config|
+ config.include Mongoid::Matchers
+ config.before(:suite) do
+ DatabaseCleaner.strategy = :truncation
+ end
+
+ config.after(:each) do
+ DatabaseCleaner.clean
+ end
+end
+
+Mongoid.configure do |config|
+ config.master = Mongo::Connection.new.db("mongoid_publishable_test")
+end
+

0 comments on commit 9226286

Please sign in to comment.