publishable 0.2.2 → 0.3.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/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.2
1
+ 0.3.0
data/db/schema.rb CHANGED
@@ -2,4 +2,7 @@ ActiveRecord::Schema.define(:version => 0) do
2
2
  create_table :albums, :force => true do |t|
3
3
  t.datetime :published_at
4
4
  end
5
+ create_table :features, :force => true do |t|
6
+ t.datetime :public_since
7
+ end
5
8
  end
data/lib/publishable.rb CHANGED
@@ -1,31 +1,37 @@
1
1
  require 'publishable/railtie' if defined?(Rails)
2
2
 
3
3
  module Publishable
4
+ def self.extended(base)
5
+ base.extend ClassMethods
6
+ end
7
+
4
8
  module ClassMethods
5
- def publishable
6
- scope :published, lambda { |time = Time.now|
7
- where('published_at IS NOT NULL AND published_at <= ?', time.utc)
8
- }
9
+ def publishable(options = {})
10
+ column_name = (options[:on] || :published_at).to_s
11
+
12
+ if respond_to?(:scope)
13
+ scope :published, lambda { |time = Time.now|
14
+ where("#{column_name} IS NOT NULL AND #{column_name} <= ?", time.utc)
15
+ }
9
16
 
10
- scope :unpublished, lambda { |time = Time.now|
11
- where('published_at IS NULL OR published_at > ?', time.utc)
12
- }
17
+ scope :unpublished, lambda { |time = Time.now|
18
+ where("#{column_name} IS NULL OR #{column_name} > ?", time.utc)
19
+ }
20
+ end
13
21
 
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
22
+ class_eval <<-EVIL, __FILE__, __LINE__ + 1
23
+ def published?(time = Time.now)
24
+ #{column_name} ? #{column_name} <= time : false
25
+ end
22
26
 
23
- def publish(time = Time.now)
24
- self.published_at = time unless published?(time)
25
- end
26
-
27
- def publish!(time = Time.now)
28
- publish(time) && save
27
+ def publish(time = Time.now)
28
+ self.#{column_name} = time unless published?(time)
29
+ end
30
+
31
+ def publish!(time = Time.now)
32
+ publish(time) && (!respond_to?(:save) || save)
33
+ end
34
+ EVIL
29
35
  end
30
36
  end
31
37
  end
data/publishable.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{publishable}
8
- s.version = "0.2.2"
8
+ s.version = "0.3.0"
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"]
@@ -1,10 +1,15 @@
1
1
  require File.expand_path('../spec_helper', __FILE__)
2
2
 
3
3
  class Album < ActiveRecord::Base
4
- extend Publishable::ClassMethods
4
+ extend Publishable
5
5
  publishable
6
6
  end
7
7
 
8
+ class Feature < ActiveRecord::Base
9
+ extend Publishable
10
+ publishable :on => :public_since
11
+ end
12
+
8
13
  describe Publishable do
9
14
  ActiveRecord::Base.establish_connection :adapter => 'sqlite3',
10
15
  :database => File.expand_path('../../db/test.sqlite3', __FILE__)
@@ -50,9 +55,9 @@ describe Publishable do
50
55
  describe "scope" do
51
56
  before do
52
57
  @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)
58
+ @album_2 = Album.create!(:published_at => Time.now - 60)
59
+ @album_3 = Album.create!(:published_at => Time.now)
60
+ @album_4 = Album.create!(:published_at => Time.now + 60)
56
61
  end
57
62
 
58
63
  it "should find published records" do
@@ -63,4 +68,31 @@ describe Publishable do
63
68
  Album.unpublished.should == [@album_1, @album_4]
64
69
  end
65
70
  end
71
+
72
+ describe "other column" do
73
+ it "should be publishable" do
74
+ @feature = Feature.new
75
+ @feature.should_not be_published
76
+ @feature.publish
77
+ @feature.should be_published
78
+ end
79
+
80
+ describe "scope" do
81
+ before do
82
+ Feature.delete_all
83
+ @feature_1 = Feature.create!(:public_since => nil)
84
+ @feature_2 = Feature.create!(:public_since => Time.now - 60)
85
+ @feature_3 = Feature.create!(:public_since => Time.now)
86
+ @feature_4 = Feature.create!(:public_since => Time.now + 60)
87
+ end
88
+
89
+ it "should find published records" do
90
+ Feature.published.should == [@feature_2, @feature_3]
91
+ end
92
+
93
+ it "should find unpublished records" do
94
+ Feature.unpublished.should == [@feature_1, @feature_4]
95
+ end
96
+ end
97
+ end
66
98
  end
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 2
8
- - 2
9
- version: 0.2.2
7
+ - 3
8
+ - 0
9
+ version: 0.3.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Martin Linkhorst