ismasan-ar_publish_control 0.0.3

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/History.txt ADDED
@@ -0,0 +1,4 @@
1
+ == 0.0.1 2008-11-17
2
+
3
+ * 1 major enhancement:
4
+ * Initial release
data/Manifest.txt ADDED
@@ -0,0 +1,15 @@
1
+ History.txt
2
+ Manifest.txt
3
+ PostInstall.txt
4
+ README.rdoc
5
+ Rakefile
6
+ ar_publish_control.gemspec
7
+ lib/ar_publish_control.rb
8
+ script/console
9
+ script/destroy
10
+ script/generate
11
+ spec/ar_publish_control_spec.rb
12
+ spec/spec.opts
13
+ spec/spec_helper.rb
14
+ tasks/db.rake
15
+ tasks/rspec.rake
data/PostInstall.txt ADDED
@@ -0,0 +1,7 @@
1
+
2
+ For more information on ar_publish_control, see http://ar_publish_control.rubyforge.org
3
+
4
+ NOTE: Change this information in PostInstall.txt
5
+ You can also delete it if you don't want it.
6
+
7
+
data/README.rdoc ADDED
@@ -0,0 +1,152 @@
1
+ This is a gem version of http://github.com/avdgaag/acts_as_publishable ( a Rails plugin)
2
+ Thanks to Arjan van der Gaag for his original work in that plugin, which this gem is heavily based off.
3
+
4
+ His plugin is Copyright (c) 2007 Arjan van der Gaag, released under the MIT license
5
+
6
+ = ar_publish_control
7
+
8
+ http://github.com/ismasan/ar_publish_control
9
+
10
+ == DESCRIPTION:
11
+
12
+ Add start/end publishing dates to your ActiveRecord models.
13
+
14
+ == FEATURES/PROBLEMS:
15
+
16
+ Adds published, unpublished and published_only(boolean) named_scopes to your models.
17
+
18
+ == SYNOPSIS:
19
+
20
+ class Post < ActiveRecord::Base
21
+ publish_control
22
+ end
23
+
24
+ # creating
25
+ @post = Post.create(params[:post])
26
+
27
+ @post.publish!
28
+
29
+ # With start and end dates
30
+ @post.update_attributes :publish_at => Time.now, :unpublish_at => 2.weeks.from_now
31
+
32
+ # Or, in your views...
33
+ <% form_for @post do |f| %>
34
+ ...
35
+ <p>
36
+ <%= f.label :is_published, "Publish" %>
37
+ <%= f.check_box :is_published %>
38
+ </p>
39
+ <p>
40
+ <%= f.label :publisht_at, "From" %>
41
+ <%= f.date_select :publish_at %>
42
+ </p>
43
+ <p>
44
+ <%= f.label :unpublisht_at, "To" %>
45
+ <%= f.date_select :unpublish_at %>
46
+ </p>
47
+ ...
48
+ <% end %>
49
+
50
+ # in your controllers
51
+ def index
52
+ @posts = Post.published
53
+ end
54
+
55
+ def show
56
+ @post = Post.published.find(params[:id])
57
+ end
58
+
59
+ # You can nest scopes:
60
+ @post = current_user.posts.published(params[:id])
61
+
62
+ @posts = current_user.posts.published.paginate(:page => params[:page])
63
+
64
+ # unpublished ones
65
+ @posts = Post.unpublished
66
+
67
+ # All posts if logged_in?, only published otherwise
68
+ @posts = Post.published_only?( logged_in? ).paginate(:page => params[:page])
69
+
70
+ # Unpublish
71
+ @post.unpublish!
72
+
73
+ # You can access the ones marked as "published" but with a publish date in the future
74
+ @upcoming_post = Post.upcoming
75
+
76
+ == REQUIREMENTS:
77
+
78
+ ActiveRecord
79
+
80
+ == INSTALL:
81
+
82
+ If you haven't yet, add github.com to your gem sources (you only need to do that once):
83
+
84
+ gem sources -a http://gems.github.com
85
+
86
+ Now you can install the normal way:
87
+
88
+ sudo gem install ismasan-ar_publish_control
89
+
90
+ Add the necessary fields to you schema, in a migration:
91
+
92
+ class AddPublishableToPosts < ActiveRecord::Migration
93
+ def self.up
94
+ add_column :posts, :is_published, :boolean
95
+ add_column :posts, :publish_at, :datetime
96
+ add_column :posts, :unpublish_at, :datetime
97
+ end
98
+ def self.down
99
+ remove_column :posts, :is_published
100
+ remove_column :posts, :publish_at
101
+ remove_column :posts, :unpublish_at
102
+ end
103
+ end
104
+
105
+ rake db:migrate
106
+
107
+ Then, in your Rails app's environment:
108
+
109
+ config.gem 'ismasan-ar_publish_control',:lib => 'ar_publish_control',:source => "http://gems.github.com"
110
+
111
+ ...Or in Merb or elsewhere
112
+
113
+ require "ar_publish_control"
114
+
115
+ If you wan to unpack the gem to you app's "vendor" directory:
116
+
117
+ rake gems:unpack
118
+
119
+ == TEST
120
+
121
+ Test are in the spec directory (rspec). To run them, first initialize the test sqlite database
122
+
123
+ rake db:create
124
+
125
+ Now you can run the specs
126
+
127
+ rake spec
128
+
129
+ == LICENSE:
130
+
131
+ (The MIT License)
132
+
133
+ Copyright (c) 2008 Ismael Celis
134
+
135
+ Permission is hereby granted, free of charge, to any person obtaining
136
+ a copy of this software and associated documentation files (the
137
+ 'Software'), to deal in the Software without restriction, including
138
+ without limitation the rights to use, copy, modify, merge, publish,
139
+ distribute, sublicense, and/or sell copies of the Software, and to
140
+ permit persons to whom the Software is furnished to do so, subject to
141
+ the following conditions:
142
+
143
+ The above copyright notice and this permission notice shall be
144
+ included in all copies or substantial portions of the Software.
145
+
146
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
147
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
148
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
149
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
150
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
151
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
152
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,28 @@
1
+ %w[rubygems rake rake/clean fileutils newgem rubigen].each { |f| require f }
2
+ require File.dirname(__FILE__) + '/lib/ar_publish_control'
3
+
4
+ # Generate all the Rake tasks
5
+ # Run 'rake -T' to see list of generated tasks (from gem root directory)
6
+ $hoe = Hoe.new('ar_publish_control', ArPublishControl::VERSION) do |p|
7
+ p.developer('Ismael Celis', 'ismaelct@gmail.com')
8
+ p.changes = p.paragraphs_of("History.txt", 0..1).join("\n\n")
9
+ p.post_install_message = 'PostInstall.txt' # TODO remove if post-install message not required
10
+ p.rubyforge_name = p.name # TODO this is default value
11
+ # p.extra_deps = [
12
+ # ['activesupport','>= 2.0.2'],
13
+ # ]
14
+ p.extra_dev_deps = [
15
+ ['newgem', ">= #{::Newgem::VERSION}"]
16
+ ]
17
+
18
+ p.clean_globs |= %w[**/.DS_Store tmp *.log]
19
+ path = (p.rubyforge_name == p.name) ? p.rubyforge_name : "\#{p.rubyforge_name}/\#{p.name}"
20
+ p.remote_rdoc_dir = File.join(path.gsub(/^#{p.rubyforge_name}\/?/,''), 'rdoc')
21
+ p.rsync_args = '-av --delete --ignore-errors'
22
+ end
23
+
24
+ require 'newgem/tasks' # load /tasks/*.rake
25
+ Dir['tasks/**/*.rake'].each { |t| load t }
26
+
27
+ # TODO - want other tests/tasks run by default? Add them to the list
28
+ # task :default => [:spec, :features]
@@ -0,0 +1,38 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{ar_publish_control}
5
+ s.version = "0.0.3"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Ismael Celis"]
9
+ s.date = %q{2008-11-18}
10
+ s.description = %q{Publish control for ActiveRecord}
11
+ s.email = ["ismaelct@gmail.com"]
12
+ s.extra_rdoc_files = ["History.txt", "Manifest.txt", "PostInstall.txt", "README.rdoc"]
13
+ s.files = ["History.txt", "Manifest.txt", "PostInstall.txt", "README.rdoc", "Rakefile", "ar_publish_control.gemspec", "lib/ar_publish_control.rb", "script/console", "script/destroy", "script/generate", "spec/ar_publish_control_spec.rb", "spec/spec.opts", "spec/spec_helper.rb", "tasks/db.rake", "tasks/rspec.rake"]
14
+ s.has_rdoc = false
15
+ s.homepage = %q{http://www.estadobeta.com}
16
+ s.post_install_message = %q{PostInstall.txt}
17
+ s.rdoc_options = ["--main", "README.rdoc"]
18
+ s.require_paths = ["lib"]
19
+ s.rubyforge_project = %q{ar_publish_control}
20
+ s.rubygems_version = %q{1.3.1}
21
+ s.summary = %q{Publish control for ActiveRecord, with start and optional end dates}
22
+
23
+ if s.respond_to? :specification_version then
24
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
25
+ s.specification_version = 2
26
+
27
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
28
+ s.add_development_dependency(%q<newgem>, [">= 1.1.0"])
29
+ s.add_development_dependency(%q<hoe>, [">= 1.8.0"])
30
+ else
31
+ s.add_dependency(%q<newgem>, [">= 1.1.0"])
32
+ s.add_dependency(%q<hoe>, [">= 1.8.0"])
33
+ end
34
+ else
35
+ s.add_dependency(%q<newgem>, [">= 1.1.0"])
36
+ s.add_dependency(%q<hoe>, [">= 1.8.0"])
37
+ end
38
+ end
@@ -0,0 +1,210 @@
1
+ $:.unshift(File.dirname(__FILE__)) unless
2
+ $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
3
+
4
+ module ArPublishControl
5
+ VERSION = '0.0.3'
6
+ # This is a gem version of http://github.com/avdgaag/acts_as_publishable ( a Rails plugin)
7
+ # Thanks to Avdaag for his awesome, super readable code which I ripped off for this gem.
8
+ #
9
+ # Specify this act if you want to show or hide your object based on date/time settings. This act lets you
10
+ # specify two dates two form a range in which the model is publicly available; it is unavailable outside it.
11
+ #
12
+ # == Usage
13
+ #
14
+ # You can add this behaviour to your model like so:
15
+ #
16
+ # class Post < ActiveRecord::Base
17
+ # publish_control
18
+ # end
19
+ #
20
+ # Then you can use it as follows:
21
+ #
22
+ # post = Post.create(:title => 'Hello world')
23
+ # post.published? # => true
24
+ #
25
+ # post.publish!
26
+ # post.published? # => true
27
+ #
28
+ # post.unpublish!
29
+ # post.published? # => false
30
+ #
31
+ # You can use two named_scopes to find the published or unpublished objects.
32
+ # You can chain them with other scopes and use them on association collections:
33
+ #
34
+ # Post.all.size # => 15
35
+ # Post.published.size # => 10
36
+ # Post.unpublished.size # => 5
37
+ # @post.comments.published
38
+ #
39
+ # Post.recent.published
40
+ #
41
+ # There's a third named_scope that you can pass a boolean in order to find only published items or all of them
42
+ # This is useful in controller for permission-based publish control
43
+ #
44
+ # @post = Post.published.find(params[:id])
45
+ # @comments = @post.comments.only_published( logged_in? )
46
+ #
47
+ module Publishable
48
+
49
+ def self.included(base) #:nodoc:
50
+ base.extend ClassMethods
51
+ end
52
+
53
+ module ClassMethods
54
+ # == Configuration options
55
+ #
56
+ # Right now this plugin has only one configuration option. Models with no publication dates
57
+ # are by default published, not unpublished. If you want to hide your model when it has no
58
+ # explicit publication date set, you can turn off this behaviour with the
59
+ # +publish_by_default+ (defaults to <tt>true</tt>) option like so:
60
+ #
61
+ # class Post < ActiveRecord::Base
62
+ # publish_control :publish_by_default => false
63
+ # end
64
+ #
65
+ # == Database Schema
66
+ #
67
+ # The model that you're publishing needs to have two special date attributes:
68
+ #
69
+ # * publish_at
70
+ # * unpublish_at
71
+ #
72
+ # These attributes have no further requirements or required validations; they
73
+ # just need to be <tt>datetime</tt>-columns.
74
+ #
75
+ # You can use a migration like this to add these columns to your model:
76
+ #
77
+ # class AddPublicationDatesToPosts < ActiveRecord::Migration
78
+ # def self.up
79
+ # add_column :posts, :publish_at, :datetime
80
+ # add_column :posts, :unpublish_at, :datetime
81
+ # end
82
+ #
83
+ # def self.down
84
+ # remove_column :posts, :publish_at
85
+ # remove_column :posts, :unpublish_at
86
+ # end
87
+ # end
88
+ #
89
+ def publish_control(options = { :publish_by_default => true })
90
+ # don't allow multiple calls
91
+ return if self.included_modules.include?(ArPublishControl::Publishable::InstanceMethods)
92
+ send :include, ArPublishControl::Publishable::InstanceMethods
93
+
94
+ named_scope :published, lambda{{:conditions => published_conditions}}
95
+ named_scope :unpublished, lambda{{:conditions => unpublished_conditions}}
96
+ named_scope :upcoming, lambda{{:conditions => upcoming_conditions}}
97
+
98
+ named_scope :published_only, lambda {|*args|
99
+ bool = (args.first.nil? ? true : (args.first)) # nil = true by default
100
+ bool ? {:conditions => published_conditions} : {}
101
+ }
102
+ before_validation :init_publish_date # don't allow empty publish_at
103
+ before_create :publish_by_default if options[:publish_by_default]
104
+ end
105
+
106
+ # Takes a block whose containing SQL queries are limited to
107
+ # published objects. You can pass a boolean flag indicating
108
+ # whether this scope should be applied or not--for example,
109
+ # you might want to disable it when the user is an administrator.
110
+ # By default the scope is applied.
111
+ #
112
+ # Example usage:
113
+ #
114
+ # Post.published_only(!logged_in?)
115
+ # @post.comments.published_only(!logged_in?)
116
+ #
117
+
118
+ protected
119
+
120
+ # returns a string for use in SQL to filter the query to unpublished results only
121
+ # Meant for internal use only
122
+ def unpublished_conditions
123
+ t = Time.now
124
+ ["(#{table_name}.is_published = ? OR #{table_name}.publish_at > ?) OR (#{table_name}.unpublish_at IS NOT NULL AND #{table_name}.unpublish_at < ?)",false,t,t]
125
+ end
126
+
127
+ # return a string for use in SQL to filter the query to published results only
128
+ # Meant for internal use only
129
+ def published_conditions
130
+ t = Time.now
131
+ ["(#{table_name}.is_published = ? AND #{table_name}.publish_at <= ?) AND (#{table_name}.unpublish_at IS NULL OR #{table_name}.unpublish_at > ?)",true,t,t]
132
+ end
133
+
134
+ def upcoming_conditions
135
+ t = Time.now
136
+ ["(#{table_name}.is_published = ? AND #{table_name}.publish_at > ?)",true,t]
137
+ end
138
+ end
139
+
140
+ module InstanceMethods
141
+
142
+ # Publish at is NOW if not set
143
+ def init_publish_date
144
+ write_attribute(:publish_at, Time.now) if publish_at.nil?
145
+ end
146
+
147
+ # ActiveRecrod callback fired on +before_create+ to make
148
+ # sure a new object always gets a publication date; if
149
+ # none is supplied it defaults to the creation date.
150
+ def publish_by_default
151
+ write_attribute(:is_published, true) if is_published.nil?
152
+ end
153
+
154
+ # Validate that unpublish_at is greater than publish_at
155
+ # publish_at must not be nil
156
+ def validate
157
+ if unpublish_at && publish_at && unpublish_at <= publish_at
158
+ errors.add(:unpublish_at,"should be greater than publication date or empty")
159
+ end
160
+ end
161
+
162
+ public
163
+
164
+ # Return whether the current object is published or not
165
+ def published?
166
+ (is_published? && (publish_at <=> Time.now) <= 0) && (unpublish_at.nil? || (unpublish_at <=> Time.now) >= 0)
167
+ end
168
+
169
+ def upcoming?
170
+ (is_published? && publish_at > Time.now)
171
+ end
172
+
173
+ # Indefinitely publish the current object right now
174
+ def publish
175
+ return if published?
176
+ self.is_published = true
177
+ self.publish_at = Time.now
178
+ self.unpublish_at = nil
179
+ end
180
+
181
+ # Same as publish, but immediatly saves the object.
182
+ # Raises an error when saving fails.
183
+ def publish!
184
+ publish
185
+ save!
186
+ end
187
+
188
+ # Immediatly unpublish the current object
189
+ def unpublish
190
+ return unless published?
191
+ self.is_published = false
192
+ end
193
+
194
+ # Same as unpublish, but immediatly saves the object.
195
+ # Raises an error when saving files.
196
+ def unpublish!
197
+ unpublish
198
+ save!
199
+ end
200
+
201
+ end
202
+ end
203
+
204
+
205
+ end
206
+
207
+ require 'rubygems'
208
+ require 'active_record'
209
+
210
+ ActiveRecord::Base.send :include, ArPublishControl::Publishable
data/script/console ADDED
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+ # File: script/console
3
+ irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'
4
+
5
+ libs = " -r irb/completion"
6
+ # Perhaps use a console_lib to store any extra methods I may want available in the cosole
7
+ # libs << " -r #{File.dirname(__FILE__) + '/../lib/console_lib/console_logger.rb'}"
8
+ libs << " -r #{File.dirname(__FILE__) + '/../lib/ar_publish_control.rb'}"
9
+ puts "Loading ar_publish_control gem"
10
+ exec "#{irb} #{libs} --simple-prompt"
data/script/destroy ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
3
+
4
+ begin
5
+ require 'rubigen'
6
+ rescue LoadError
7
+ require 'rubygems'
8
+ require 'rubigen'
9
+ end
10
+ require 'rubigen/scripts/destroy'
11
+
12
+ ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
+ RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
14
+ RubiGen::Scripts::Destroy.new.run(ARGV)
data/script/generate ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
3
+
4
+ begin
5
+ require 'rubigen'
6
+ rescue LoadError
7
+ require 'rubygems'
8
+ require 'rubigen'
9
+ end
10
+ require 'rubigen/scripts/generate'
11
+
12
+ ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
+ RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
14
+ RubiGen::Scripts::Generate.new.run(ARGV)
@@ -0,0 +1,146 @@
1
+ require File.dirname(__FILE__) + '/spec_helper.rb'
2
+
3
+ describe Comment do
4
+ before(:each) do
5
+ Post.destroy_all
6
+ @published_post = Post.create do |p|
7
+ p.title = 'some post'
8
+ p.publish_at = 1.day.ago
9
+ p.unpublish_at = 2.days.from_now
10
+ end
11
+
12
+ @unpublished_post = Post.create do |p|
13
+ p.title = 'some other post'
14
+ p.publish_at = 1.day.from_now
15
+ p.unpublish_at = 2.days.from_now
16
+ end
17
+
18
+ @published_comment_in_published_post = @published_post.comments.create do |c|
19
+ c.body = 'some comment'
20
+ c.publish_at = 2.day.ago
21
+ c.unpublish_at = 2.days.from_now
22
+ end
23
+
24
+ @published_comment_in_unpublished_post = @unpublished_post.comments.create do |c|
25
+ c.body = 'some other comment'
26
+ c.publish_at = 1.day.ago
27
+ c.unpublish_at = 2.days.from_now
28
+ end
29
+
30
+ @unpublished_comment_in_published_post = @published_post.comments.create do |c|
31
+ c.body = 'some other comment 2'
32
+ c.publish_at = 1.day.from_now
33
+ c.unpublish_at = 2.days.from_now
34
+ end
35
+
36
+ @unupublished_comment_in_unpublished_post = @unpublished_post.comments.create do |c|
37
+ c.body = 'some other comment 3'
38
+ c.publish_at = 1.day.from_now
39
+ c.unpublish_at = 2.days.from_now
40
+ end
41
+
42
+ end
43
+
44
+ it "should find published comment at class level" do
45
+ Comment.published.size.should == 2
46
+ end
47
+
48
+ it "should find published comments in collection" do
49
+ @published_post.comments.published.size.should == 1
50
+ end
51
+
52
+ it "should find published comments in a collection with conditions" do
53
+ @published_post.published_comments.size.should == 1
54
+ end
55
+
56
+ it "should work with named scope at class level" do
57
+ Comment.published.size.should == 2
58
+ end
59
+
60
+ it "should work with named scope at collection level" do
61
+ @published_post.comments.published.size.should == 1
62
+ end
63
+
64
+ it "should find unpublished with named scope" do
65
+ Comment.unpublished.size.should == 2
66
+ @published_post.comments.unpublished.size.should == 1
67
+ end
68
+
69
+ it "should chain correctly with other scopes" do
70
+ Comment.published.desc.should == [@published_comment_in_unpublished_post,@published_comment_in_published_post]
71
+ end
72
+
73
+ it "should chain correctly with other scopes on collections" do
74
+ @unpublished_comment_in_published_post.publish_at = @published_comment_in_published_post.publish_at + 1.hour
75
+ @unpublished_comment_in_published_post.save
76
+ @published_post.comments.reload
77
+ @published_post.comments.published.desc.should == [@unpublished_comment_in_published_post,@published_comment_in_published_post]
78
+ end
79
+
80
+ it "should find all with conditional flag" do
81
+ Comment.published_only.size.should == 2
82
+ Comment.published_only(true).size.should == 2
83
+ @published_post.comments.published_only(true).first.should == @published_comment_in_published_post
84
+ @published_post.comments.published_only(true).include?(@unpublished_comment_in_published_post).should be_false
85
+ @published_post.comments.published_only(false).include?(@unpublished_comment_in_published_post).should be_true
86
+ @published_post.comments.published_only(false).include?(@published_comment_in_published_post).should be_true
87
+ end
88
+
89
+ it "should validate that unpublish_at is greater than publish_at" do
90
+ @published_comment_in_published_post.unpublish_at = @published_comment_in_published_post.publish_at - 1.minute
91
+ @published_comment_in_published_post.save
92
+ @published_comment_in_published_post.should_not be_valid
93
+ @published_comment_in_published_post.errors.on(:unpublish_at).should == "should be greater than publication date or empty"
94
+ end
95
+
96
+ end
97
+
98
+ # describe Post, 'with no dates' do
99
+ # it "should be published" do
100
+ # puts "DESTROYING #{Post.count}============="
101
+ # Post.destroy_all
102
+ # puts "====================================="
103
+ # post = Post.create(:title => 'some post aaaaaaaaaaaaaaaaaaaaaaaaaaaa')
104
+ # post.published?.should be_true
105
+ # Post.published.include?(post).should be_true
106
+ # end
107
+ # end
108
+
109
+ describe Post, "unpublished by default" do
110
+ before(:each) do
111
+ Article.destroy_all
112
+ @a1 = Article.create(:title=>'a1')
113
+ end
114
+
115
+ it "should be valid" do
116
+ @a1.should be_valid
117
+ end
118
+
119
+ it "should be unpublished by default" do
120
+ @a1.published?.should_not be_true
121
+ Article.published.size.should == 0
122
+ end
123
+
124
+ it "should publish" do
125
+ @a1.publish!
126
+ @a1.published?.should be_true
127
+ end
128
+ end
129
+
130
+ describe Post, 'upcoming' do
131
+ before(:each) do
132
+ Post.destroy_all
133
+ @p1 = Post.create(:title => 'p1',:is_published => true,:publish_at => 1.day.from_now) #upcoming
134
+ @p2 = Post.create(:title => 'p2',:is_published => true,:publish_at => 1.week.from_now)#upcoming
135
+ @p3 = Post.create(:title => 'p3',:is_published => false,:publish_at => 1.day.from_now)#unpublished
136
+ @p4 = Post.create(:title => 'p4',:is_published => true)#published
137
+ end
138
+
139
+ it "should have upcoming" do
140
+ @p1.upcoming?.should be_true
141
+ @p2.upcoming?.should be_true
142
+ @p3.upcoming?.should be_false
143
+ @p4.upcoming?.should be_false
144
+ Post.upcoming.size.should == 2
145
+ end
146
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1 @@
1
+ --colour
@@ -0,0 +1,47 @@
1
+ begin
2
+ require 'spec'
3
+ rescue LoadError
4
+ require 'rubygems'
5
+ gem 'rspec'
6
+ require 'spec'
7
+ end
8
+
9
+ $:.unshift(File.dirname(__FILE__) + '/../lib')
10
+ require 'ar_publish_control'
11
+
12
+ ActiveRecord::Base.establish_connection(
13
+ :adapter=>'sqlite3',
14
+ :dbfile=> File.join(File.dirname(__FILE__),'db','test.db')
15
+ )
16
+
17
+ #ActiveRecord::Base.connection.query_cache_enabled = false
18
+
19
+ LOGGER = Logger.new(File.dirname(__FILE__)+'/log/test.log')
20
+ ActiveRecord::Base.logger = LOGGER
21
+
22
+ class Comment < ActiveRecord::Base
23
+ belongs_to :post
24
+
25
+ named_scope :recent, lambda{|limit|
26
+ {:order => 'created_at desc',:limit => limit}
27
+ }
28
+
29
+ named_scope :desc, :order => 'publish_at desc'
30
+
31
+ publish_control
32
+
33
+ end
34
+
35
+ class Post < ActiveRecord::Base
36
+
37
+ has_many :comments, :dependent => :destroy
38
+
39
+ has_many :published_comments, :class_name => 'Comment', :conditions => Comment.published_conditions
40
+
41
+ publish_control
42
+
43
+ end
44
+
45
+ class Article < ActiveRecord::Base
46
+ publish_control :publish_by_default => false
47
+ end
data/tasks/db.rake ADDED
@@ -0,0 +1,57 @@
1
+ database_config = {
2
+ :adapter=>'sqlite3',
3
+ :dbfile=> File.join(File.dirname(__FILE__),'..','spec','db','test.db')
4
+ }
5
+
6
+ ActiveRecord::Base.establish_connection(database_config)
7
+ # define a migration
8
+ class TestSchema < ActiveRecord::Migration
9
+ def self.up
10
+ create_table :posts do |t|
11
+ t.string :title
12
+ t.boolean :is_published
13
+ t.datetime :publish_at
14
+ t.datetime :unpublish_at
15
+ t.timestamps
16
+ end
17
+
18
+ create_table :articles do |t|
19
+ t.string :title
20
+ t.boolean :is_published
21
+ t.datetime :publish_at
22
+ t.datetime :unpublish_at
23
+ t.timestamps
24
+ end
25
+
26
+ create_table :comments do |t|
27
+ t.text :body
28
+ t.boolean :is_published
29
+ t.integer :post_id
30
+ t.datetime :publish_at
31
+ t.datetime :unpublish_at
32
+ t.timestamps
33
+ end
34
+ end
35
+
36
+ def self.down
37
+ drop_table :posts
38
+ drop_table :comments
39
+ drop_table :articles
40
+ end
41
+ end
42
+
43
+
44
+ namespace :db do
45
+ desc "migrate test schema"
46
+ task :create do
47
+ File.unlink(database_config[:dbfile])
48
+ ActiveRecord::Base.connection # this creates the DB
49
+ # run the migration
50
+ TestSchema.migrate(:up)
51
+ end
52
+
53
+ desc "Destroy test schema"
54
+ task :destroy do
55
+ TestSchema.migrate(:down)
56
+ end
57
+ end
data/tasks/rspec.rake ADDED
@@ -0,0 +1,21 @@
1
+ begin
2
+ require 'spec'
3
+ rescue LoadError
4
+ require 'rubygems'
5
+ require 'spec'
6
+ end
7
+ begin
8
+ require 'spec/rake/spectask'
9
+ rescue LoadError
10
+ puts <<-EOS
11
+ To use rspec for testing you must install rspec gem:
12
+ gem install rspec
13
+ EOS
14
+ exit(0)
15
+ end
16
+
17
+ desc "Run the specs under spec/models"
18
+ Spec::Rake::SpecTask.new do |t|
19
+ t.spec_opts = ['--options', "spec/spec.opts"]
20
+ t.spec_files = FileList['spec/**/*_spec.rb']
21
+ end
metadata ADDED
@@ -0,0 +1,89 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ismasan-ar_publish_control
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
5
+ platform: ruby
6
+ authors:
7
+ - Ismael Celis
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-11-18 00:00:00 -08:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: newgem
17
+ version_requirement:
18
+ version_requirements: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 1.1.0
23
+ version:
24
+ - !ruby/object:Gem::Dependency
25
+ name: hoe
26
+ version_requirement:
27
+ version_requirements: !ruby/object:Gem::Requirement
28
+ requirements:
29
+ - - ">="
30
+ - !ruby/object:Gem::Version
31
+ version: 1.8.0
32
+ version:
33
+ description: Publish control for ActiveRecord
34
+ email:
35
+ - ismaelct@gmail.com
36
+ executables: []
37
+
38
+ extensions: []
39
+
40
+ extra_rdoc_files:
41
+ - History.txt
42
+ - Manifest.txt
43
+ - PostInstall.txt
44
+ - README.rdoc
45
+ files:
46
+ - History.txt
47
+ - Manifest.txt
48
+ - PostInstall.txt
49
+ - README.rdoc
50
+ - Rakefile
51
+ - ar_publish_control.gemspec
52
+ - lib/ar_publish_control.rb
53
+ - script/console
54
+ - script/destroy
55
+ - script/generate
56
+ - spec/ar_publish_control_spec.rb
57
+ - spec/spec.opts
58
+ - spec/spec_helper.rb
59
+ - tasks/db.rake
60
+ - tasks/rspec.rake
61
+ has_rdoc: false
62
+ homepage: http://www.estadobeta.com
63
+ post_install_message: PostInstall.txt
64
+ rdoc_options:
65
+ - --main
66
+ - README.rdoc
67
+ require_paths:
68
+ - lib
69
+ required_ruby_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: "0"
74
+ version:
75
+ required_rubygems_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: "0"
80
+ version:
81
+ requirements: []
82
+
83
+ rubyforge_project: ar_publish_control
84
+ rubygems_version: 1.2.0
85
+ signing_key:
86
+ specification_version: 2
87
+ summary: Publish control for ActiveRecord, with start and optional end dates
88
+ test_files: []
89
+