mongoid_publishable 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1 @@
1
+ require 'autotest/growl'
@@ -0,0 +1,3 @@
1
+ pkg/*
2
+ *.gem
3
+ .bundle
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format documentation
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in mongoid_publishable.gemspec
4
+ gemspec
5
+
6
+ gem 'database_cleaner'
7
+ gem 'rspec'
8
+ gem 'autotest'
9
+ gem 'autotest-growl'
10
+ gem 'mongoid', '2.0.0.rc.8'
11
+ gem 'bson_ext'
12
+ gem 'mongoid-rspec'
@@ -0,0 +1,57 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ mongoid_publishable (0.9.0)
5
+
6
+ GEM
7
+ remote: http://rubygems.org/
8
+ specs:
9
+ ZenTest (4.5.0)
10
+ activemodel (3.0.5)
11
+ activesupport (= 3.0.5)
12
+ builder (~> 2.1.2)
13
+ i18n (~> 0.4)
14
+ activesupport (3.0.5)
15
+ autotest (4.4.6)
16
+ ZenTest (>= 4.4.1)
17
+ autotest-growl (0.2.9)
18
+ bson (1.2.4)
19
+ bson_ext (1.2.4)
20
+ builder (2.1.2)
21
+ database_cleaner (0.6.6)
22
+ diff-lcs (1.1.2)
23
+ i18n (0.5.0)
24
+ mongo (1.2.4)
25
+ bson (>= 1.2.4)
26
+ mongoid (2.0.0.rc.8)
27
+ activemodel (~> 3.0)
28
+ mongo (~> 1.2)
29
+ tzinfo (~> 0.3.22)
30
+ will_paginate (~> 3.0.pre)
31
+ mongoid-rspec (1.4.1)
32
+ mongoid (~> 2.0.0.rc.7)
33
+ mongoid-rspec
34
+ rspec (~> 2)
35
+ rspec (2.5.0)
36
+ rspec-core (~> 2.5.0)
37
+ rspec-expectations (~> 2.5.0)
38
+ rspec-mocks (~> 2.5.0)
39
+ rspec-core (2.5.1)
40
+ rspec-expectations (2.5.0)
41
+ diff-lcs (~> 1.1.2)
42
+ rspec-mocks (2.5.0)
43
+ tzinfo (0.3.25)
44
+ will_paginate (3.0.pre2)
45
+
46
+ PLATFORMS
47
+ ruby
48
+
49
+ DEPENDENCIES
50
+ autotest
51
+ autotest-growl
52
+ bson_ext
53
+ database_cleaner
54
+ mongoid (= 2.0.0.rc.8)
55
+ mongoid-rspec
56
+ mongoid_publishable!
57
+ rspec
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,52 @@
1
+ module Mongoid
2
+ module Publishable
3
+ def self.included(base)
4
+ base.field :published_at, :type => DateTime, :default => nil
5
+ base.extend ClassMethods
6
+ base.send :include, InstanceMethods
7
+ end
8
+
9
+ module ClassMethods
10
+ def scheduled
11
+ where(:published_at => {'$gt' => Time.now}).asc(:published_at)
12
+ end
13
+
14
+ def published
15
+ where(:published_at => {'$lte' => Time.now}).desc(:published_at)
16
+ end
17
+
18
+ def drafts
19
+ where(:published_at => nil).desc(:published_at)
20
+ end
21
+ end
22
+
23
+ module InstanceMethods
24
+ def is_scheduled?
25
+ return true if self.published_at && self.published_at > Time.now
26
+ false
27
+ end
28
+
29
+ def is_published?
30
+ return true if self.published_at && self.published_at <= Time.now
31
+ false
32
+ end
33
+
34
+ def is_draft?
35
+ return true if self.published_at.nil?
36
+ false
37
+ end
38
+
39
+ def schedule!(time)
40
+ update_attributes(:published_at => time)
41
+ end
42
+
43
+ def publish!
44
+ update_attributes(:published_at => Time.now)
45
+ end
46
+
47
+ def unpublish!
48
+ update_attributes(:published_at => nil)
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,5 @@
1
+ module Mongoid
2
+ module Publishable
3
+ VERSION = "1.0.0"
4
+ end
5
+ end
@@ -0,0 +1 @@
1
+ require File.join(File.dirname(__FILE__), 'mongoid/publishable')
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "mongoid/publishable/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "mongoid_publishable"
7
+ s.version = Mongoid::Publishable::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Nicholas Bruning"]
10
+ s.email = ["nicholas@bruning.com.au"]
11
+ s.homepage = "http://github.com/thetron/mongoid_publishable"
12
+ s.summary = %q{Add publishable behaviours to mongoid models.}
13
+ s.description = %q{This gem adds some fields, scopes and methods to simplify and declutter your models that need to be drafted and published.}
14
+
15
+ s.rubyforge_project = "mongoid_publishable"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+ end
@@ -0,0 +1,48 @@
1
+ # Mongoid Publishable
2
+
3
+ Make no mistakes - this is a simple gem. Turned out
4
+ [we](http://involved.com.au) use this sort of behaviour _a lot_ on our
5
+ projects, so just broke out into a gem for simplicity.
6
+
7
+ ## What does it do?
8
+
9
+ The gem add some fields, scopes and methods to your models which you
10
+ want to be able to publish and unpublish. You know, things like blog
11
+ posts and junk like that.
12
+
13
+ ## Getting started
14
+
15
+ Add the gem to your Gemfile:
16
+
17
+ gem 'mongoid_publishable'
18
+
19
+ And update your bundle:
20
+
21
+ $ bundle update
22
+
23
+ ## Installation
24
+
25
+ In your model just add the following line, like you would with any
26
+ other mongoid model extension:
27
+
28
+ include Mongoid::Publishable
29
+
30
+ ## Putting it to good use
31
+
32
+ Your models will now have a `published_at` date as well as these
33
+ instance methods (they should be self-explanatory):
34
+
35
+ is_scheduled?
36
+ is_published?
37
+ is_draft?
38
+ schedule!(date_time)
39
+ publish!
40
+ unpublish!
41
+
42
+ Additionally, you get a couple of scopes to help find what you're
43
+ looking for:
44
+
45
+ ModelName.scheduled # returns all scheduled models (published_at is
46
+ in the future)
47
+ ModelName.published # returns all published models
48
+ ModelName.drafts # returns all unpublished models
@@ -0,0 +1,94 @@
1
+ require File.join(File.dirname(__FILE__), %w[.. spec_helper])
2
+
3
+ class Post
4
+ include Mongoid::Document
5
+ include Mongoid::Publishable
6
+ field :title
7
+ end
8
+
9
+ describe Mongoid::Publishable do
10
+ before :each do
11
+ @draft = Post.create(:published_at => nil)
12
+ @published = Post.create(:published_at => Time.now - 5.minutes)
13
+ @scheduled = Post.create(:published_at => Time.now + 12.hours)
14
+ end
15
+
16
+ it "should have a date and timestamp to represent publish state" do
17
+ Post.should have_field(:published_at).of_type(DateTime).with_default_value_of(nil)
18
+ end
19
+
20
+ it "should be published if the published date is in the past" do
21
+ @published.is_draft?.should equal false
22
+ @published.is_published?.should equal true
23
+ @published.is_scheduled?.should equal false
24
+ end
25
+
26
+ it "should be a draft if the published date is not set" do
27
+ @draft.is_draft?.should == true
28
+ @draft.is_published?.should == false
29
+ @draft.is_scheduled?.should == false
30
+ end
31
+
32
+ it "should be scheduled if the published date is in the future" do
33
+ @scheduled.is_draft?.should == false
34
+ @scheduled.is_published?.should == false
35
+ @scheduled.is_scheduled?.should == true
36
+ end
37
+
38
+ it "should be publishable" do
39
+ @draft.should respond_to :publish!
40
+ @draft.publish!
41
+ @draft.is_published?.should equal true
42
+ @draft.published_at.to_i.should <= Time.now.to_i
43
+ end
44
+
45
+ it "should be unpublishable" do
46
+ @published.should respond_to :unpublish!
47
+ @published.unpublish!
48
+ @published.is_draft?.should equal true
49
+ @published.published_at.should be nil
50
+ end
51
+
52
+ it "should be scheduleable" do
53
+ @draft.should respond_to :schedule!
54
+ future = (Time.now + 12.hours).to_datetime
55
+ @draft.schedule!(future)
56
+ @draft.is_scheduled?.should equal true
57
+ @draft.published_at.to_i.should == future.to_i
58
+ end
59
+
60
+ it "should return all draft models" do
61
+ Post.drafts.count.should equal 1
62
+ Post.drafts.first.should == @draft
63
+ end
64
+
65
+ it "should return all published models" do
66
+ Post.published.count.should equal 1
67
+ Post.published.first.should == @published
68
+ end
69
+
70
+ it "should return all scheduled models" do
71
+ Post.scheduled.count.should equal 1
72
+ Post.scheduled.first.should == @scheduled
73
+ end
74
+
75
+ it "should return published posts in descending date order" do
76
+ @draft.publish!
77
+ @scheduled.publish!
78
+ last_stamp = Time.now
79
+ Post.published.each do |post|
80
+ post.published_at.should <= last_stamp
81
+ last_stamp = post.published_at
82
+ end
83
+ end
84
+
85
+ it "should return scheduled posts in ascending date order" do
86
+ @draft.schedule!(Time.now + 1.hour)
87
+ @published.schedule!(Time.now + 5.days)
88
+ last_stamp = Time.now
89
+ Post.scheduled.each do |post|
90
+ post.published_at.should > last_stamp
91
+ last_stamp = post.published_at
92
+ end
93
+ end
94
+ end
@@ -0,0 +1,22 @@
1
+ $: << File.expand_path("../../lib", __FILE__)
2
+
3
+ require 'database_cleaner'
4
+ require 'mongoid'
5
+ require 'mongoid-rspec'
6
+ require 'mongoid_publishable'
7
+
8
+ RSpec.configure do |config|
9
+ config.include Mongoid::Matchers
10
+ config.before(:suite) do
11
+ DatabaseCleaner.strategy = :truncation
12
+ end
13
+
14
+ config.after(:each) do
15
+ DatabaseCleaner.clean
16
+ end
17
+ end
18
+
19
+ Mongoid.configure do |config|
20
+ config.master = Mongo::Connection.new.db("mongoid_publishable_test")
21
+ end
22
+
metadata ADDED
@@ -0,0 +1,81 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mongoid_publishable
3
+ version: !ruby/object:Gem::Version
4
+ hash: 23
5
+ prerelease: false
6
+ segments:
7
+ - 1
8
+ - 0
9
+ - 0
10
+ version: 1.0.0
11
+ platform: ruby
12
+ authors:
13
+ - Nicholas Bruning
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-03-26 00:00:00 +11:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description: This gem adds some fields, scopes and methods to simplify and declutter your models that need to be drafted and published.
23
+ email:
24
+ - nicholas@bruning.com.au
25
+ executables: []
26
+
27
+ extensions: []
28
+
29
+ extra_rdoc_files: []
30
+
31
+ files:
32
+ - .autotest
33
+ - .gitignore
34
+ - .rspec
35
+ - Gemfile
36
+ - Gemfile.lock
37
+ - Rakefile
38
+ - lib/mongoid/publishable.rb
39
+ - lib/mongoid/publishable/version.rb
40
+ - lib/mongoid_publishable.rb
41
+ - mongoid_publishable.gemspec
42
+ - readme.md
43
+ - spec/mongoid/publishable_spec.rb
44
+ - spec/spec_helper.rb
45
+ has_rdoc: true
46
+ homepage: http://github.com/thetron/mongoid_publishable
47
+ licenses: []
48
+
49
+ post_install_message:
50
+ rdoc_options: []
51
+
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ hash: 3
60
+ segments:
61
+ - 0
62
+ version: "0"
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ none: false
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ hash: 3
69
+ segments:
70
+ - 0
71
+ version: "0"
72
+ requirements: []
73
+
74
+ rubyforge_project: mongoid_publishable
75
+ rubygems_version: 1.3.7
76
+ signing_key:
77
+ specification_version: 3
78
+ summary: Add publishable behaviours to mongoid models.
79
+ test_files:
80
+ - spec/mongoid/publishable_spec.rb
81
+ - spec/spec_helper.rb