publishable 0.1.3 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.gitignore +20 -1
- data/LICENSE +5 -5
- data/README.textile +4 -10
- data/Rakefile +38 -13
- data/VERSION +1 -1
- data/db/schema.rb +1 -1
- data/lib/publishable/railtie.rb +9 -0
- data/lib/publishable.rb +22 -63
- data/publishable.gemspec +10 -6
- data/spec/publishable_spec.rb +39 -99
- data/spec/spec.opts +2 -4
- data/spec/spec_helper.rb +5 -6
- metadata +32 -8
- data/rails/init.rb +0 -2
data/.document
ADDED
data/.gitignore
CHANGED
data/LICENSE
CHANGED
@@ -1,5 +1,5 @@
|
|
1
|
-
Copyright (c)
|
2
|
-
|
1
|
+
Copyright (c) 2010 Martin Linkhorst
|
2
|
+
|
3
3
|
Permission is hereby granted, free of charge, to any person obtaining
|
4
4
|
a copy of this software and associated documentation files (the
|
5
5
|
"Software"), to deal in the Software without restriction, including
|
@@ -7,14 +7,14 @@ without limitation the rights to use, copy, modify, merge, publish,
|
|
7
7
|
distribute, sublicense, and/or sell copies of the Software, and to
|
8
8
|
permit persons to whom the Software is furnished to do so, subject to
|
9
9
|
the following conditions:
|
10
|
-
|
10
|
+
|
11
11
|
The above copyright notice and this permission notice shall be
|
12
12
|
included in all copies or substantial portions of the Software.
|
13
|
-
|
13
|
+
|
14
14
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
15
|
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
16
|
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
17
|
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
18
|
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
19
|
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
-
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.textile
CHANGED
@@ -6,15 +6,11 @@ h1. Publishable
|
|
6
6
|
|
7
7
|
h2. Setup
|
8
8
|
|
9
|
-
you need a datetime column in your table and call publishable in your class body
|
9
|
+
you need a datetime column called published_at in your table and call publishable in your class body
|
10
10
|
|
11
11
|
<pre>
|
12
|
-
class
|
12
|
+
class Album < ActiveRecord::Base
|
13
13
|
publishable
|
14
|
-
|
15
|
-
# OR
|
16
|
-
|
17
|
-
publishable :made_public_on # if your column is not named published_at
|
18
14
|
end
|
19
15
|
</pre>
|
20
16
|
|
@@ -24,12 +20,10 @@ h2. Methods
|
|
24
20
|
album = Album.new
|
25
21
|
|
26
22
|
album.published? # => false
|
27
|
-
album.publish!
|
23
|
+
album.publish! # calls :save afterwards
|
28
24
|
album.published? # => true
|
29
25
|
|
30
26
|
Album.published.all # => finds all published albums
|
31
27
|
</pre>
|
32
28
|
|
33
|
-
|
34
|
-
|
35
|
-
Copyright (c) 2009 Martin Linkhorst, released under the MIT license.
|
29
|
+
Copyright (c) 2010 Martin Linkhorst, released under the MIT license.
|
data/Rakefile
CHANGED
@@ -1,21 +1,46 @@
|
|
1
1
|
require 'rubygems'
|
2
2
|
require 'rake'
|
3
|
-
require 'spec/rake/spectask'
|
4
|
-
|
5
|
-
Spec::Rake::SpecTask.new(:spec)
|
6
|
-
task :default => :spec
|
7
3
|
|
8
4
|
begin
|
9
5
|
require 'jeweler'
|
10
|
-
Jeweler::Tasks.new do |
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "publishable"
|
8
|
+
gem.summary = "Adds publishing functionality to your active record model"
|
9
|
+
gem.description = "Provides methods to publish and unpublish your active record models based on a datetime. Also adds named scopes to nicely filter your records. Does not touch any controller or views."
|
10
|
+
gem.email = "m.linkhorst@googlemail.com"
|
11
|
+
gem.homepage = "http://github.com/linki/publishable"
|
12
|
+
gem.authors = ["Martin Linkhorst"]
|
13
|
+
gem.add_development_dependency "rspec", ">= 1.2.9"
|
14
|
+
gem.files.include 'lib/**/*.rb'
|
15
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
17
16
|
end
|
18
17
|
Jeweler::GemcutterTasks.new
|
19
18
|
rescue LoadError
|
20
|
-
puts "Jeweler not available. Install it with:
|
21
|
-
end
|
19
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
20
|
+
end
|
21
|
+
|
22
|
+
require 'spec/rake/spectask'
|
23
|
+
Spec::Rake::SpecTask.new(:spec) do |spec|
|
24
|
+
spec.libs << 'lib' << 'spec'
|
25
|
+
spec.spec_files = FileList['spec/**/*_spec.rb']
|
26
|
+
end
|
27
|
+
|
28
|
+
Spec::Rake::SpecTask.new(:rcov) do |spec|
|
29
|
+
spec.libs << 'lib' << 'spec'
|
30
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
31
|
+
spec.rcov = true
|
32
|
+
end
|
33
|
+
|
34
|
+
task :spec => :check_dependencies
|
35
|
+
|
36
|
+
task :default => :spec
|
37
|
+
|
38
|
+
require 'rake/rdoctask'
|
39
|
+
Rake::RDocTask.new do |rdoc|
|
40
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
41
|
+
|
42
|
+
rdoc.rdoc_dir = 'rdoc'
|
43
|
+
rdoc.title = "publishable #{version}"
|
44
|
+
rdoc.rdoc_files.include('README*')
|
45
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
46
|
+
end
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1
|
1
|
+
0.2.1
|
data/db/schema.rb
CHANGED
data/lib/publishable.rb
CHANGED
@@ -1,72 +1,31 @@
|
|
1
|
+
require 'publishable/railtie' if defined?(Rails)
|
2
|
+
|
1
3
|
module Publishable
|
2
|
-
TRUE_VALUES = ["true", true, "1", 1] unless const_defined?(:TRUE_VALUES)
|
3
|
-
|
4
|
-
def self.included(base)
|
5
|
-
base.extend(ClassMethods)
|
6
|
-
end
|
7
|
-
|
8
4
|
module ClassMethods
|
9
|
-
def publishable
|
10
|
-
|
11
|
-
|
12
|
-
if args.first.nil? || TRUE_VALUES.include?(args.first)
|
13
|
-
{ :conditions => ["#{quoted_table_name}.#{quoted_publishable_column_name} IS NOT NULL AND #{quoted_table_name}.#{quoted_publishable_column_name} <= ?", Time.now.utc] }
|
14
|
-
else
|
15
|
-
{ :conditions => ["#{quoted_table_name}.#{quoted_publishable_column_name} IS NULL OR #{quoted_table_name}.#{quoted_publishable_column_name} > ?", Time.now.utc] }
|
16
|
-
end
|
5
|
+
def publishable
|
6
|
+
scope :published, lambda { |time = Time.now|
|
7
|
+
where('published_at IS NOT NULL AND published_at <= ?', time.utc)
|
17
8
|
}
|
18
9
|
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
def unpublished
|
24
|
-
published(false)
|
25
|
-
end
|
26
|
-
|
27
|
-
def quoted_publishable_column_name
|
28
|
-
ActiveRecord::Base.connection.quote_column_name(publishable_column_name)
|
29
|
-
end
|
30
|
-
end
|
31
|
-
|
32
|
-
self.publishable_column_name = column
|
33
|
-
|
34
|
-
def published?
|
35
|
-
!!published
|
36
|
-
end
|
37
|
-
|
38
|
-
def unpublished?
|
39
|
-
!published?
|
40
|
-
end
|
41
|
-
|
42
|
-
def published
|
43
|
-
send(self.class.publishable_column_name) && send(self.class.publishable_column_name) <= Time.now
|
44
|
-
end
|
45
|
-
|
46
|
-
def published=(boolean)
|
47
|
-
TRUE_VALUES.include?(boolean) ? publish : unpublish
|
48
|
-
end
|
49
|
-
|
50
|
-
def publish(time = Time.now)
|
51
|
-
self.send("#{self.class.publishable_column_name}=", time.utc) unless published?
|
52
|
-
end
|
53
|
-
|
54
|
-
def unpublish
|
55
|
-
self.send("#{self.class.publishable_column_name}=", nil)
|
56
|
-
end
|
10
|
+
scope :unpublished, lambda { |time = Time.now|
|
11
|
+
where('published_at IS NULL OR published_at > ?', time.utc)
|
12
|
+
}
|
57
13
|
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
14
|
+
include InstanceMethods
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
module InstanceMethods
|
19
|
+
def published?(time = Time.now)
|
20
|
+
published_at ? published_at <= time : false
|
21
|
+
end
|
64
22
|
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
23
|
+
def publish(time = Time.now)
|
24
|
+
self.published_at = time unless published?
|
25
|
+
end
|
26
|
+
|
27
|
+
def publish!(time = Time.now)
|
28
|
+
publish(time) && save
|
70
29
|
end
|
71
30
|
end
|
72
31
|
end
|
data/publishable.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{publishable}
|
8
|
-
s.version = "0.1
|
8
|
+
s.version = "0.2.1"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Martin Linkhorst"]
|
12
|
-
s.date = %q{
|
12
|
+
s.date = %q{2010-09-29}
|
13
13
|
s.description = %q{Provides methods to publish and unpublish your active record models based on a datetime. Also adds named scopes to nicely filter your records. Does not touch any controller or views.}
|
14
14
|
s.email = %q{m.linkhorst@googlemail.com}
|
15
15
|
s.extra_rdoc_files = [
|
@@ -17,15 +17,16 @@ Gem::Specification.new do |s|
|
|
17
17
|
"README.textile"
|
18
18
|
]
|
19
19
|
s.files = [
|
20
|
-
".
|
20
|
+
".document",
|
21
|
+
".gitignore",
|
21
22
|
"LICENSE",
|
22
23
|
"README.textile",
|
23
24
|
"Rakefile",
|
24
25
|
"VERSION",
|
25
26
|
"db/schema.rb",
|
26
27
|
"lib/publishable.rb",
|
28
|
+
"lib/publishable/railtie.rb",
|
27
29
|
"publishable.gemspec",
|
28
|
-
"rails/init.rb",
|
29
30
|
"spec/publishable_spec.rb",
|
30
31
|
"spec/spec.opts",
|
31
32
|
"spec/spec_helper.rb"
|
@@ -33,7 +34,7 @@ Gem::Specification.new do |s|
|
|
33
34
|
s.homepage = %q{http://github.com/linki/publishable}
|
34
35
|
s.rdoc_options = ["--charset=UTF-8"]
|
35
36
|
s.require_paths = ["lib"]
|
36
|
-
s.rubygems_version = %q{1.3.
|
37
|
+
s.rubygems_version = %q{1.3.7}
|
37
38
|
s.summary = %q{Adds publishing functionality to your active record model}
|
38
39
|
s.test_files = [
|
39
40
|
"spec/publishable_spec.rb",
|
@@ -44,10 +45,13 @@ Gem::Specification.new do |s|
|
|
44
45
|
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
45
46
|
s.specification_version = 3
|
46
47
|
|
47
|
-
if Gem::Version.new(Gem::
|
48
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
49
|
+
s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
|
48
50
|
else
|
51
|
+
s.add_dependency(%q<rspec>, [">= 1.2.9"])
|
49
52
|
end
|
50
53
|
else
|
54
|
+
s.add_dependency(%q<rspec>, [">= 1.2.9"])
|
51
55
|
end
|
52
56
|
end
|
53
57
|
|
data/spec/publishable_spec.rb
CHANGED
@@ -1,126 +1,66 @@
|
|
1
|
-
require File.
|
1
|
+
require File.expand_path('../spec_helper', __FILE__)
|
2
2
|
|
3
|
-
class
|
4
|
-
|
3
|
+
class Album < ActiveRecord::Base
|
4
|
+
extend Publishable::ClassMethods
|
5
5
|
publishable
|
6
6
|
end
|
7
7
|
|
8
8
|
describe Publishable do
|
9
|
-
ActiveRecord::Base.establish_connection :adapter
|
9
|
+
ActiveRecord::Base.establish_connection :adapter => 'sqlite3',
|
10
|
+
:database => File.expand_path('../../db/test.sqlite3', __FILE__)
|
10
11
|
load('db/schema.rb')
|
11
12
|
|
12
13
|
before do
|
13
|
-
|
14
|
-
@
|
14
|
+
Album.delete_all
|
15
|
+
@album = Album.new
|
15
16
|
end
|
16
|
-
|
17
|
-
describe "published?" do
|
18
|
-
it "should respond to published?" do
|
19
|
-
@model.should respond_to(:published?)
|
20
|
-
end
|
21
|
-
|
22
|
-
it "should be published? when :published_at is now" do
|
23
|
-
now = Time.now; Time.stubs(:now).returns(now)
|
24
|
-
@model.published_at = Time.now
|
25
|
-
@model.should be_published
|
26
|
-
end
|
27
17
|
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
end
|
33
|
-
|
34
|
-
it "should not be published when :published_at is in the future" do
|
35
|
-
now = Time.now; Time.stubs(:now).returns(now)
|
36
|
-
@model.published_at = Time.now + 1.day
|
37
|
-
@model.should_not be_published
|
38
|
-
end
|
18
|
+
it "should not be published" do
|
19
|
+
# nil or false
|
20
|
+
@album.published_at.should be_false
|
21
|
+
@album.should_not be_published
|
39
22
|
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
end
|
23
|
+
# in the future
|
24
|
+
@album.published_at = Time.now + 60
|
25
|
+
@album.should_not be_published
|
44
26
|
end
|
45
|
-
|
46
|
-
describe "publish" do
|
47
|
-
it "should respond to publish" do
|
48
|
-
@model.should respond_to(:publish)
|
49
|
-
end
|
50
27
|
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
end
|
56
|
-
|
57
|
-
it "should set :published_at to given time if parameter present" do
|
58
|
-
tomorrow = Time.now + 1.day
|
59
|
-
@model.expects(:published_at=).with(tomorrow).returns(tomorrow)
|
60
|
-
@model.publish(tomorrow)
|
61
|
-
end
|
28
|
+
it "should be published" do
|
29
|
+
# now
|
30
|
+
@album.published_at = Time.now
|
31
|
+
@album.should be_published
|
62
32
|
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
@model.publish
|
67
|
-
end
|
33
|
+
# in the past
|
34
|
+
@album.published_at = Time.now - 60
|
35
|
+
@album.should be_published
|
68
36
|
end
|
69
37
|
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
it "should publish and save" do
|
76
|
-
@model.expects(:publish).returns(true)
|
77
|
-
@model.expects(:save).with(false).returns(true)
|
78
|
-
@model.publish!
|
79
|
-
end
|
38
|
+
it "should be publishable" do
|
39
|
+
@album.should_not be_published
|
40
|
+
@album.publish
|
41
|
+
@album.should be_published
|
80
42
|
end
|
81
43
|
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
end
|
87
|
-
|
88
|
-
it "should not be unpublished? when published?" do
|
89
|
-
@model.expects(:published?).returns(true)
|
90
|
-
@model.should_not be_unpublished
|
91
|
-
end
|
44
|
+
it "should persist" do
|
45
|
+
@album.expects(:publish).returns(true)
|
46
|
+
@album.expects(:save)
|
47
|
+
@album.publish!
|
92
48
|
end
|
93
|
-
|
94
|
-
describe "
|
49
|
+
|
50
|
+
describe "scope" do
|
95
51
|
before do
|
96
|
-
@
|
97
|
-
@
|
98
|
-
@
|
99
|
-
@
|
52
|
+
@album_1 = Album.create!(:published_at => nil)
|
53
|
+
@album_2 = Album.create!(:published_at => Time.now.utc - 60)
|
54
|
+
@album_3 = Album.create!(:published_at => Time.now.utc)
|
55
|
+
@album_4 = Album.create!(:published_at => Time.now.utc + 60)
|
100
56
|
end
|
101
|
-
|
57
|
+
|
102
58
|
it "should find published records" do
|
103
|
-
|
104
|
-
Model.published(true).all.should == [@model_2, @model_3]
|
59
|
+
Album.published.should == [@album_2, @album_3]
|
105
60
|
end
|
106
|
-
|
61
|
+
|
107
62
|
it "should find unpublished records" do
|
108
|
-
|
109
|
-
Model.published(false).all.should == [@model_1, @model_4]
|
110
|
-
end
|
111
|
-
end
|
112
|
-
|
113
|
-
describe "database column" do
|
114
|
-
it "should default to :published_at" do
|
115
|
-
Model.publishable_column_name.should == :published_at
|
116
|
-
end
|
117
|
-
|
118
|
-
it "should be possible to change the column name" do
|
119
|
-
class Model2 < ActiveRecord::Base
|
120
|
-
include Publishable
|
121
|
-
publishable :made_available_on
|
122
|
-
end
|
123
|
-
Model2.publishable_column_name.should == :made_available_on
|
63
|
+
Album.unpublished.should == [@album_1, @album_4]
|
124
64
|
end
|
125
65
|
end
|
126
66
|
end
|
data/spec/spec.opts
CHANGED
data/spec/spec_helper.rb
CHANGED
@@ -1,12 +1,11 @@
|
|
1
|
-
|
2
|
-
require '
|
1
|
+
$:.unshift File.expand_path('../../lib', __FILE__)
|
2
|
+
require 'publishable'
|
3
3
|
require 'spec'
|
4
|
+
require 'spec/autorun'
|
4
5
|
|
6
|
+
require 'sqlite3'
|
5
7
|
require 'active_record'
|
6
|
-
require 'active_support'
|
7
|
-
|
8
|
-
require 'publishable'
|
9
8
|
|
10
9
|
Spec::Runner.configure do |config|
|
11
10
|
config.mock_with :mocha
|
12
|
-
end
|
11
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,12 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: publishable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 2
|
8
|
+
- 1
|
9
|
+
version: 0.2.1
|
5
10
|
platform: ruby
|
6
11
|
authors:
|
7
12
|
- Martin Linkhorst
|
@@ -9,10 +14,24 @@ autorequire:
|
|
9
14
|
bindir: bin
|
10
15
|
cert_chain: []
|
11
16
|
|
12
|
-
date:
|
17
|
+
date: 2010-09-29 00:00:00 +02:00
|
13
18
|
default_executable:
|
14
|
-
dependencies:
|
15
|
-
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: rspec
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ">="
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
segments:
|
29
|
+
- 1
|
30
|
+
- 2
|
31
|
+
- 9
|
32
|
+
version: 1.2.9
|
33
|
+
type: :development
|
34
|
+
version_requirements: *id001
|
16
35
|
description: Provides methods to publish and unpublish your active record models based on a datetime. Also adds named scopes to nicely filter your records. Does not touch any controller or views.
|
17
36
|
email: m.linkhorst@googlemail.com
|
18
37
|
executables: []
|
@@ -23,6 +42,7 @@ extra_rdoc_files:
|
|
23
42
|
- LICENSE
|
24
43
|
- README.textile
|
25
44
|
files:
|
45
|
+
- .document
|
26
46
|
- .gitignore
|
27
47
|
- LICENSE
|
28
48
|
- README.textile
|
@@ -30,8 +50,8 @@ files:
|
|
30
50
|
- VERSION
|
31
51
|
- db/schema.rb
|
32
52
|
- lib/publishable.rb
|
53
|
+
- lib/publishable/railtie.rb
|
33
54
|
- publishable.gemspec
|
34
|
-
- rails/init.rb
|
35
55
|
- spec/publishable_spec.rb
|
36
56
|
- spec/spec.opts
|
37
57
|
- spec/spec_helper.rb
|
@@ -45,21 +65,25 @@ rdoc_options:
|
|
45
65
|
require_paths:
|
46
66
|
- lib
|
47
67
|
required_ruby_version: !ruby/object:Gem::Requirement
|
68
|
+
none: false
|
48
69
|
requirements:
|
49
70
|
- - ">="
|
50
71
|
- !ruby/object:Gem::Version
|
72
|
+
segments:
|
73
|
+
- 0
|
51
74
|
version: "0"
|
52
|
-
version:
|
53
75
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
76
|
+
none: false
|
54
77
|
requirements:
|
55
78
|
- - ">="
|
56
79
|
- !ruby/object:Gem::Version
|
80
|
+
segments:
|
81
|
+
- 0
|
57
82
|
version: "0"
|
58
|
-
version:
|
59
83
|
requirements: []
|
60
84
|
|
61
85
|
rubyforge_project:
|
62
|
-
rubygems_version: 1.3.
|
86
|
+
rubygems_version: 1.3.7
|
63
87
|
signing_key:
|
64
88
|
specification_version: 3
|
65
89
|
summary: Adds publishing functionality to your active record model
|
data/rails/init.rb
DELETED