publishable 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +3 -0
- data/LICENSE +20 -0
- data/README +0 -0
- data/Rakefile +21 -0
- data/VERSION +1 -0
- data/db/schema.rb +5 -0
- data/lib/publishable.rb +68 -0
- data/publishable.gemspec +53 -0
- data/rails/init.rb +2 -0
- data/spec/publishable_spec.rb +126 -0
- data/spec/spec.opts +4 -0
- data/spec/spec_helper.rb +12 -0
- metadata +68 -0
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Martin Linkhorst
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
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.
|
data/README
ADDED
File without changes
|
data/Rakefile
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
require 'spec/rake/spectask'
|
4
|
+
|
5
|
+
Spec::Rake::SpecTask.new(:spec)
|
6
|
+
task :default => :spec
|
7
|
+
|
8
|
+
begin
|
9
|
+
require 'jeweler'
|
10
|
+
Jeweler::Tasks.new do |gemspec|
|
11
|
+
gemspec.name = "publishable"
|
12
|
+
gemspec.summary = "Adds publishing functionality to your active record model"
|
13
|
+
gemspec.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."
|
14
|
+
gemspec.email = "m.linkhorst@googlemail.com"
|
15
|
+
gemspec.homepage = "http://github.com/linki/publishable"
|
16
|
+
gemspec.authors = ["Martin Linkhorst"]
|
17
|
+
end
|
18
|
+
Jeweler::GemcutterTasks.new
|
19
|
+
rescue LoadError
|
20
|
+
puts "Jeweler not available. Install it with: sudo gem install jeweler"
|
21
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
data/db/schema.rb
ADDED
data/lib/publishable.rb
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
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
|
+
module ClassMethods
|
9
|
+
def publishable(column = :published_at, options = {})
|
10
|
+
|
11
|
+
named_scope :published, lambda { |*args|
|
12
|
+
if args.first.nil? || TRUE_VALUES.include?(args.first)
|
13
|
+
{ :conditions => ["#{quoted_table_name}.'#{publishable_column}' IS NOT NULL AND #{quoted_table_name}.'#{publishable_column}' <= ?", Time.now.utc] }
|
14
|
+
else
|
15
|
+
{ :conditions => ["#{quoted_table_name}.'#{publishable_column}' IS NULL OR #{quoted_table_name}.'#{publishable_column}' > ?", Time.now.utc] }
|
16
|
+
end
|
17
|
+
}
|
18
|
+
|
19
|
+
class_eval do
|
20
|
+
class << self
|
21
|
+
attr_accessor :publishable_column
|
22
|
+
|
23
|
+
def unpublished
|
24
|
+
published(false)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
self.publishable_column = column
|
29
|
+
|
30
|
+
def published?
|
31
|
+
!!published
|
32
|
+
end
|
33
|
+
|
34
|
+
def unpublished?
|
35
|
+
!published?
|
36
|
+
end
|
37
|
+
|
38
|
+
def published
|
39
|
+
send(self.class.publishable_column) && send(self.class.publishable_column) <= Time.now
|
40
|
+
end
|
41
|
+
|
42
|
+
def published=(boolean)
|
43
|
+
TRUE_VALUES.include?(boolean) ? publish : unpublish
|
44
|
+
end
|
45
|
+
|
46
|
+
def publish(time = Time.now)
|
47
|
+
self.send("#{self.class.publishable_column}=", time.utc) unless published?
|
48
|
+
end
|
49
|
+
|
50
|
+
def unpublish
|
51
|
+
self.send("#{self.class.publishable_column}=", nil)
|
52
|
+
end
|
53
|
+
|
54
|
+
def publish!
|
55
|
+
unless published?
|
56
|
+
publish
|
57
|
+
save(false)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
def unpublish!
|
62
|
+
unpublish
|
63
|
+
update_attribute(self.class.publishable_column, published_at)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
data/publishable.gemspec
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{publishable}
|
8
|
+
s.version = "0.1.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Martin Linkhorst"]
|
12
|
+
s.date = %q{2009-12-03}
|
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
|
+
s.email = %q{m.linkhorst@googlemail.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE",
|
17
|
+
"README"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".gitignore",
|
21
|
+
"LICENSE",
|
22
|
+
"README",
|
23
|
+
"Rakefile",
|
24
|
+
"VERSION",
|
25
|
+
"db/schema.rb",
|
26
|
+
"lib/publishable.rb",
|
27
|
+
"publishable.gemspec",
|
28
|
+
"rails/init.rb",
|
29
|
+
"spec/publishable_spec.rb",
|
30
|
+
"spec/spec.opts",
|
31
|
+
"spec/spec_helper.rb"
|
32
|
+
]
|
33
|
+
s.homepage = %q{http://github.com/linki/publishable}
|
34
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
35
|
+
s.require_paths = ["lib"]
|
36
|
+
s.rubygems_version = %q{1.3.5}
|
37
|
+
s.summary = %q{Adds publishing functionality to your active record model}
|
38
|
+
s.test_files = [
|
39
|
+
"spec/publishable_spec.rb",
|
40
|
+
"spec/spec_helper.rb"
|
41
|
+
]
|
42
|
+
|
43
|
+
if s.respond_to? :specification_version then
|
44
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
45
|
+
s.specification_version = 3
|
46
|
+
|
47
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
48
|
+
else
|
49
|
+
end
|
50
|
+
else
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
data/rails/init.rb
ADDED
@@ -0,0 +1,126 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), "/spec_helper")
|
2
|
+
|
3
|
+
class Model < ActiveRecord::Base
|
4
|
+
include Publishable
|
5
|
+
publishable
|
6
|
+
end
|
7
|
+
|
8
|
+
describe Publishable do
|
9
|
+
ActiveRecord::Base.establish_connection :adapter => 'sqlite3', :database => 'db/test.sqlite3'
|
10
|
+
load('db/schema.rb')
|
11
|
+
|
12
|
+
before do
|
13
|
+
Model.delete_all
|
14
|
+
@model = Model.new
|
15
|
+
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 #stub me
|
25
|
+
@model.should be_published
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should be published? when :published_at is in the past" do
|
29
|
+
now = Time.now; Time.stubs(:now).returns(now)
|
30
|
+
@model.published_at = Time.now - 1.day
|
31
|
+
@model.should be_published
|
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
|
39
|
+
|
40
|
+
it "should ask :publishable_column" do
|
41
|
+
Model.expects(:publishable_column).returns(:published_at)
|
42
|
+
@model.published?
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
describe "publish" do
|
47
|
+
it "should respond to publish" do
|
48
|
+
@model.should respond_to(:publish)
|
49
|
+
end
|
50
|
+
|
51
|
+
it "should set :published_at to current time with no parameter" do
|
52
|
+
now = Time.now; Time.expects(:now).returns(now)
|
53
|
+
@model.expects(:published_at=).with(now).returns(true)
|
54
|
+
@model.publish
|
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(true)
|
60
|
+
@model.publish(tomorrow)
|
61
|
+
end
|
62
|
+
|
63
|
+
it "should not set :published_at again if already published?" do
|
64
|
+
@model.expects(:published?).once.returns(true)
|
65
|
+
@model.stubs(:published_at=).never
|
66
|
+
@model.publish
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
describe "publish!" do
|
71
|
+
it "should respond to publish!" do
|
72
|
+
@model.should respond_to(:publish!)
|
73
|
+
end
|
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
|
80
|
+
end
|
81
|
+
|
82
|
+
describe "unpublished?" do
|
83
|
+
it "should be unpublished? when not published?" do
|
84
|
+
@model.expects(:published?).returns(false)
|
85
|
+
@model.should be_unpublished
|
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
|
92
|
+
end
|
93
|
+
|
94
|
+
describe "named scopes" do
|
95
|
+
before do
|
96
|
+
@model_1 = Model.create!(:published_at => nil)
|
97
|
+
@model_2 = Model.create!(:published_at => Time.now.utc - 1.day)
|
98
|
+
@model_3 = Model.create!(:published_at => Time.now.utc)
|
99
|
+
@model_4 = Model.create!(:published_at => Time.now.utc + 1.day)
|
100
|
+
end
|
101
|
+
|
102
|
+
it "should find published records" do
|
103
|
+
Model.published.all.should == [@model_2, @model_3]
|
104
|
+
Model.published(true).all.should == [@model_2, @model_3]
|
105
|
+
end
|
106
|
+
|
107
|
+
it "should find unpublished records" do
|
108
|
+
Model.unpublished.all.should == [@model_1, @model_4]
|
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.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.should == :made_available_on
|
124
|
+
end
|
125
|
+
end
|
126
|
+
end
|
data/spec/spec.opts
ADDED
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: publishable
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Martin Linkhorst
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-12-03 00:00:00 +10:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
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
|
+
email: m.linkhorst@googlemail.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- LICENSE
|
24
|
+
- README
|
25
|
+
files:
|
26
|
+
- .gitignore
|
27
|
+
- LICENSE
|
28
|
+
- README
|
29
|
+
- Rakefile
|
30
|
+
- VERSION
|
31
|
+
- db/schema.rb
|
32
|
+
- lib/publishable.rb
|
33
|
+
- publishable.gemspec
|
34
|
+
- rails/init.rb
|
35
|
+
- spec/publishable_spec.rb
|
36
|
+
- spec/spec.opts
|
37
|
+
- spec/spec_helper.rb
|
38
|
+
has_rdoc: true
|
39
|
+
homepage: http://github.com/linki/publishable
|
40
|
+
licenses: []
|
41
|
+
|
42
|
+
post_install_message:
|
43
|
+
rdoc_options:
|
44
|
+
- --charset=UTF-8
|
45
|
+
require_paths:
|
46
|
+
- lib
|
47
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
48
|
+
requirements:
|
49
|
+
- - ">="
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: "0"
|
52
|
+
version:
|
53
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: "0"
|
58
|
+
version:
|
59
|
+
requirements: []
|
60
|
+
|
61
|
+
rubyforge_project:
|
62
|
+
rubygems_version: 1.3.5
|
63
|
+
signing_key:
|
64
|
+
specification_version: 3
|
65
|
+
summary: Adds publishing functionality to your active record model
|
66
|
+
test_files:
|
67
|
+
- spec/publishable_spec.rb
|
68
|
+
- spec/spec_helper.rb
|