acts_as_publishable 0.1.0 → 0.2

Sign up to get free protection for your applications and to get access to all the features.
data/Manifest CHANGED
@@ -1,5 +1,10 @@
1
1
  README.rdoc
2
2
  Rakefile
3
+ acts_as_publishable.gemspec
3
4
  lib/acts_as_publishable.rb
4
5
  lib/acts_as_publishable/acts_as_publishable.rb
6
+ spec/acts_as_publishable_spec.rb
7
+ spec/rspec_immediate_feedback_formatter.rb
8
+ spec/spec.opts
9
+ spec/spec_helper.rb
5
10
  Manifest
data/README.rdoc CHANGED
@@ -36,6 +36,12 @@ This finds all instances where published? returns true.
36
36
 
37
37
  The find_published finder method accepts any of the normal options as the default find method accepts.
38
38
 
39
+ == TODO
40
+
41
+ * Option for configuring whether publish_now should be OR'd or AND'd with the published_from and published_to columns
42
+ * Add publish! and unpublish! instance methods
43
+ * Update README to reflect functionality (config options, validation and the scope)
44
+
39
45
  == License
40
46
 
41
47
  acts_as_publishable is distributed under the Simplified BSD License, copyright (c) 2010 Rasmus Bang Grouleff
data/Rakefile CHANGED
@@ -2,12 +2,12 @@ require 'rubygems'
2
2
  require 'rake'
3
3
  require 'echoe'
4
4
 
5
- Echoe.new('acts_as_publishable', '0.1.0') do |p|
6
- p.description = "Makes Active Record models publishable"
5
+ Echoe.new('acts_as_publishable', '0.2') do |p|
6
+ p.description = "Rails gem that adds functionality to make Active Record models publishable"
7
7
  p.url = "http://github.com/rbgrouleff/acts_as_publishable"
8
8
  p.author = "Rasmus Bang Grouleff"
9
9
  p.email = "rasmus@anybite.com"
10
- p.ignore_pattern = ["tmp/*", "script/*"]
10
+ p.ignore_pattern = ["tmp/*", "script/*", "watchr-runner.rb"]
11
11
  p.development_dependencies = []
12
12
  end
13
13
 
@@ -2,21 +2,21 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{acts_as_publishable}
5
- s.version = "0.1.0"
5
+ s.version = "0.2"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Rasmus Bang Grouleff"]
9
- s.date = %q{2010-06-12}
10
- s.description = %q{Makes Active Record models publishable}
9
+ s.date = %q{2010-06-23}
10
+ s.description = %q{Rails gem that adds functionality to make Active Record models publishable}
11
11
  s.email = %q{rasmus@anybite.com}
12
12
  s.extra_rdoc_files = ["README.rdoc", "lib/acts_as_publishable.rb", "lib/acts_as_publishable/acts_as_publishable.rb"]
13
- s.files = ["README.rdoc", "Rakefile", "lib/acts_as_publishable.rb", "lib/acts_as_publishable/acts_as_publishable.rb", "Manifest", "acts_as_publishable.gemspec"]
13
+ s.files = ["README.rdoc", "Rakefile", "acts_as_publishable.gemspec", "lib/acts_as_publishable.rb", "lib/acts_as_publishable/acts_as_publishable.rb", "spec/acts_as_publishable_spec.rb", "spec/rspec_immediate_feedback_formatter.rb", "spec/spec.opts", "spec/spec_helper.rb", "Manifest"]
14
14
  s.homepage = %q{http://github.com/rbgrouleff/acts_as_publishable}
15
15
  s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Acts_as_publishable", "--main", "README.rdoc"]
16
16
  s.require_paths = ["lib"]
17
17
  s.rubyforge_project = %q{acts_as_publishable}
18
18
  s.rubygems_version = %q{1.3.7}
19
- s.summary = %q{Makes Active Record models publishable}
19
+ s.summary = %q{Rails gem that adds functionality to make Active Record models publishable}
20
20
 
21
21
  if s.respond_to? :specification_version then
22
22
  current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
@@ -7,27 +7,30 @@ module ActsAsPublishable
7
7
  def acts_as_publishable(options = {})
8
8
  metaclass = (class << self; self; end)
9
9
 
10
- return if metaclass.included_modules.include?(ActsAsPublishable::SingletonMethods)
11
-
12
- send :extend, ActsAsPublishable::SingletonMethods
10
+ return if metaclass.included_modules.include?(ActsAsPublishable::InstanceMethods)
13
11
 
14
12
  cattr_accessor :publish_now_column, :published_from_column, :published_to_column, :default_published_now
15
13
 
16
14
  self.publish_now_column = options[:publish_now_column] || 'publish_now'
17
15
  self.published_from_column = options[:published_from_column] || 'published_from'
18
16
  self.published_to_column = options[:published_to_column] || 'published_to'
19
- self.default_published_now = options[:default_published_now]
17
+ self.default_published_now = (options[:default_published_now] == true)
18
+
19
+ named_scope :published, :conditions => ["#{publish_now_column} = :published OR (#{published_from_column} <= :published_from AND #{published_to_column} >= :published_to)", {:published => true, :published_from => Time.now, :published_to => Time.now}]
20
+
21
+ before_save { |publishable| publishable[publish_now_column.to_sym] = default_published_now if publishable[publish_now_column.to_sym].nil?; true }
22
+
23
+ validate_chronology
20
24
 
21
25
  send :include, ActsAsPublishable::InstanceMethods
22
26
  end
23
27
 
24
-
25
- end
26
-
27
- module SingletonMethods
28
- def find_published(options = {})
29
- scope = scoped(:conditions => ["#{publish_now_column} = :published OR (#{published_from_column} <= :published_from AND #{published_to_column} >= :published_to)", {:published => true, :published_from => Time.now, :published_to => Time.now}])
30
- scope.find(:all, options)
28
+ def validate_chronology
29
+ validates_each published_to_column do |publishable, attribute, value|
30
+ unless value.blank? || publishable[published_from_column.to_sym].blank?
31
+ publishable.errors.add(attribute, :earlier_than_published_from, :default => "cannot be set earlier than #{published_from_column.to_s.humanize}") unless publishable[published_from_column.to_sym] < value
32
+ end
33
+ end
31
34
  end
32
35
  end
33
36
 
@@ -0,0 +1,166 @@
1
+ $LOAD_PATH << File.dirname(__FILE__)
2
+
3
+ require 'spec_helper'
4
+
5
+ describe "Publishable with default columns" do
6
+
7
+ before :all do
8
+ ActiveRecord::Schema.define(:version => 1) do
9
+ create_table :posts do |t|
10
+ t.boolean :publish_now
11
+ t.datetime :published_from
12
+ t.datetime :published_to
13
+ end
14
+ end
15
+
16
+ class Post < ActiveRecord::Base
17
+ acts_as_publishable
18
+ end
19
+ end
20
+
21
+ after :all do
22
+ ActiveRecord::Schema.define(:version => 1) do
23
+ drop_table :posts
24
+ end
25
+ end
26
+
27
+ before :each do
28
+ @post = Post.new
29
+ end
30
+
31
+ it "has a named scope" do
32
+ Post.should respond_to(:published)
33
+ end
34
+
35
+ it "has a class attribute for the publish_now column" do
36
+ Post.publish_now_column.should == 'publish_now'
37
+ end
38
+
39
+ it "has a class attribute for the published_from column" do
40
+ Post.published_from_column.should == 'published_from'
41
+ end
42
+
43
+ it "has a class attribute for the published_to column" do
44
+ Post.published_to_column.should == 'published_to'
45
+ end
46
+
47
+ it "has a published? instance method" do
48
+ @post.published?.should be_false
49
+ end
50
+
51
+ it "has an instance attribute called publish_now" do
52
+ @post.publish_now.should be_false
53
+ end
54
+
55
+ it "has an instance attribute called published_from" do
56
+ @post.should respond_to(:published_from)
57
+ end
58
+
59
+ it "has an instance attribute called published_to" do
60
+ @post.should respond_to(:published_to)
61
+ end
62
+
63
+ it "has an instance attribute called publish_now" do
64
+ @post.should respond_to(:publish_now)
65
+ end
66
+
67
+ it "should persist publish_now to the database" do
68
+ @post.publish_now = true
69
+ @post.save
70
+ Post.find(@post.id).publish_now.should be_true
71
+ end
72
+
73
+ it "should persist published_to and published_from dates" do
74
+ tomorrow = Time.parse 'Sun Jun 20 17:03:03 +0200 2010'
75
+ a_day_ago = Time.parse 'Fri Jun 18 17:03:03 +0200 2010'
76
+ @post.published_from = a_day_ago
77
+ @post.published_to = tomorrow
78
+ @post.save_without_validation
79
+ Post.find(@post.id).published_from.should eql(a_day_ago)
80
+ Post.find(@post.id).published_to.should eql(tomorrow)
81
+ end
82
+
83
+ it "is published if publish_now is true" do
84
+ @post.publish_now = true
85
+ @post.save
86
+ @post.published?.should be_true
87
+ end
88
+
89
+ it "should be invalid if published_to is set in the past" do
90
+ @post.published_to = 1.day.ago
91
+ @post.published_from = Time.now
92
+ @post.valid?.should_not be_true
93
+ end
94
+ end
95
+
96
+ describe "Publishable with default_published_now set to true" do
97
+ before :all do
98
+ ActiveRecord::Schema.define(:version => 1) do
99
+ create_table :posts do |t|
100
+ t.boolean :publish_now
101
+ t.datetime :published_from
102
+ t.datetime :published_to
103
+ end
104
+ end
105
+
106
+ class Post < ActiveRecord::Base
107
+ acts_as_publishable :default_published_now => true
108
+ end
109
+ end
110
+
111
+ after :all do
112
+ ActiveRecord::Schema.define(:version => 1) do
113
+ drop_table :posts
114
+ end
115
+ end
116
+
117
+ before :each do
118
+ @post = Post.new
119
+ end
120
+
121
+ it "should be published by default" do
122
+ @post.save
123
+ @post.publish_now.should be_true
124
+
125
+ end
126
+ end
127
+
128
+ describe "Publishable with non-default column names" do
129
+ before :all do
130
+ ActiveRecord::Schema.define(:version => 1) do
131
+ create_table :publishables do |t|
132
+ t.boolean :now
133
+ t.datetime :from
134
+ t.datetime :to
135
+ end
136
+ end
137
+
138
+ class Publishable < ActiveRecord::Base
139
+ acts_as_publishable :publish_now_column => :now, :published_from_column => :from, :published_to_column => :to
140
+ end
141
+ end
142
+
143
+ after :all do
144
+ ActiveRecord::Schema.define(:version => 1) do
145
+ drop_table :publishables
146
+ end
147
+ end
148
+
149
+ before :each do
150
+ @publishable = Publishable.new
151
+ end
152
+
153
+ it "should have a now column" do
154
+ @publishable.should respond_to(:now)
155
+ end
156
+
157
+ it "should persist to and from dates" do
158
+ tomorrow = Time.parse 'Sun Jun 20 17:03:03 +0200 2010'
159
+ a_day_ago = Time.parse 'Fri Jun 18 17:03:03 +0200 2010'
160
+ @publishable.from = a_day_ago
161
+ @publishable.to = tomorrow
162
+ @publishable.save
163
+ Publishable.find(@publishable.id).from.should eql(a_day_ago)
164
+ Publishable.find(@publishable.id).to.should eql(tomorrow)
165
+ end
166
+ end
@@ -0,0 +1,53 @@
1
+ require 'spec/runner/formatter/base_text_formatter'
2
+
3
+ # Code is based on standard SpecdocFormatter, but will print full error details as soon as they are found.
4
+ # Successful or pending examples are written only as a dot in the output. Header is only printed if errors occur.
5
+ #
6
+ # To use it, add the following to your spec/spec.opts:
7
+ # --require
8
+ # lib/rspec_immediate_feedback_formatter.rb
9
+ # --format
10
+ # Spec::Runner::Formatter::ImmediateFeedbackFormatter
11
+
12
+ module Spec
13
+ module Runner
14
+ module Formatter
15
+ class ImmediateFeedbackFormatter < BaseTextFormatter
16
+
17
+ def add_example_group(example_group)
18
+ super
19
+ @current_group = example_group.description
20
+ end
21
+
22
+ def example_failed(example, counter, failure)
23
+ if @current_group
24
+ output.puts
25
+ output.puts @current_group
26
+ @current_group = nil # only print the group name once
27
+ end
28
+
29
+ message = if failure.expectation_not_met?
30
+ "- #{example.description} (FAILED - #{counter})"
31
+ else
32
+ "- #{example.description} (ERROR - #{counter})"
33
+ end
34
+
35
+ output.puts(failure.expectation_not_met? ? red(message) : magenta(message))
36
+ dump_failure(counter, failure) # dump stacktrace immediately
37
+ output.flush
38
+ end
39
+
40
+ def example_passed(example)
41
+ output.print green(".")
42
+ output.flush
43
+ end
44
+
45
+ def example_pending(example, message)
46
+ super
47
+ output.print yellow(".")
48
+ output.flush
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1,5 @@
1
+ --require spec/rspec_immediate_feedback_formatter.rb
2
+ --format Spec::Runner::Formatter::ImmediateFeedbackFormatter
3
+ --colour
4
+ --loadby mtime
5
+ --backtrace
@@ -0,0 +1,7 @@
1
+ $LOAD_PATH << File.join(File.dirname(__FILE__), '..', 'lib')
2
+
3
+ require 'rubygems'
4
+ require 'active_record'
5
+ require 'acts_as_publishable'
6
+
7
+ ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
metadata CHANGED
@@ -1,13 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: acts_as_publishable
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 15
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 1
9
- - 0
10
- version: 0.1.0
8
+ - 2
9
+ version: "0.2"
11
10
  platform: ruby
12
11
  authors:
13
12
  - Rasmus Bang Grouleff
@@ -15,11 +14,11 @@ autorequire:
15
14
  bindir: bin
16
15
  cert_chain: []
17
16
 
18
- date: 2010-06-12 00:00:00 +02:00
17
+ date: 2010-06-23 00:00:00 +02:00
19
18
  default_executable:
20
19
  dependencies: []
21
20
 
22
- description: Makes Active Record models publishable
21
+ description: Rails gem that adds functionality to make Active Record models publishable
23
22
  email: rasmus@anybite.com
24
23
  executables: []
25
24
 
@@ -32,10 +31,14 @@ extra_rdoc_files:
32
31
  files:
33
32
  - README.rdoc
34
33
  - Rakefile
34
+ - acts_as_publishable.gemspec
35
35
  - lib/acts_as_publishable.rb
36
36
  - lib/acts_as_publishable/acts_as_publishable.rb
37
+ - spec/acts_as_publishable_spec.rb
38
+ - spec/rspec_immediate_feedback_formatter.rb
39
+ - spec/spec.opts
40
+ - spec/spec_helper.rb
37
41
  - Manifest
38
- - acts_as_publishable.gemspec
39
42
  has_rdoc: true
40
43
  homepage: http://github.com/rbgrouleff/acts_as_publishable
41
44
  licenses: []
@@ -75,6 +78,6 @@ rubyforge_project: acts_as_publishable
75
78
  rubygems_version: 1.3.7
76
79
  signing_key:
77
80
  specification_version: 3
78
- summary: Makes Active Record models publishable
81
+ summary: Rails gem that adds functionality to make Active Record models publishable
79
82
  test_files: []
80
83