is_publishable 0.0.1
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/README.md +66 -0
- data/lib/generators/publishable_generator.rb +28 -0
- data/lib/generators/templates/publishable_migration.rb.erb +11 -0
- data/lib/is_publishable/version.rb +3 -0
- data/lib/is_publishable.rb +51 -0
- metadata +115 -0
data/README.md
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
# is_publishable
|
2
|
+
|
3
|
+
Adds publishing capabilities to a model. Supports marking a model as published/unpublished as well as publishing models in the future.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'is_publishable'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install is_publishable
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
To add publishing to a model, first run the generator. Let's assume the model's name is Article
|
22
|
+
|
23
|
+
$ rails generate publishable Article
|
24
|
+
|
25
|
+
This will create the following migration:
|
26
|
+
|
27
|
+
class AddPublishingToArticles < ActiveRecord::Migration
|
28
|
+
def up
|
29
|
+
add_column :articles, :published, :boolean, default: false
|
30
|
+
add_column :articles, :published_at, :datetime
|
31
|
+
end
|
32
|
+
def down
|
33
|
+
remove_column :articles, :published
|
34
|
+
remove_column :articles, :published_at
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
After this, simply add `publishable` to your model (be sure to add published and published_at to your attr_accessible)
|
39
|
+
|
40
|
+
class Article < ActiveRecord::Base
|
41
|
+
attr_accessible ...
|
42
|
+
publishable
|
43
|
+
end
|
44
|
+
|
45
|
+
The following scopes are now available:
|
46
|
+
|
47
|
+
* published -- returns all Article that published=true and published_at is a date in the past
|
48
|
+
* unpublished -- returns all Articles that are published=false or published=true and published_at is a date in the future
|
49
|
+
|
50
|
+
|
51
|
+
The follow instance methods are now available:
|
52
|
+
* published? -- returns true if published=true and published_at is a date in the past
|
53
|
+
* unpublished? -- returns true if published=false or published=true and published_at is a date in the future
|
54
|
+
|
55
|
+
|
56
|
+
## TODO
|
57
|
+
|
58
|
+
* Write tests for generator
|
59
|
+
|
60
|
+
## Contributing
|
61
|
+
|
62
|
+
1. Fork it
|
63
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
64
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
65
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
66
|
+
5. Create new Pull Request
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'rails/generators/active_record'
|
2
|
+
|
3
|
+
class PublishableGenerator < ActiveRecord::Generators::Base
|
4
|
+
desc "Create a migration to add publishing to your model. " +
|
5
|
+
"Usage: rails g publishable Model"
|
6
|
+
|
7
|
+
def self.source_root
|
8
|
+
@source_root ||= File.expand_path('../templates', __FILE__)
|
9
|
+
end
|
10
|
+
|
11
|
+
def generate_migration
|
12
|
+
migration_template "publishable_migration.rb.erb", "db/migrate/#{migration_file_name}"
|
13
|
+
end
|
14
|
+
|
15
|
+
protected
|
16
|
+
|
17
|
+
def migration_name
|
18
|
+
"add_publishing_to_#{name.underscore.pluralize}"
|
19
|
+
end
|
20
|
+
|
21
|
+
def migration_file_name
|
22
|
+
"#{migration_name}.rb"
|
23
|
+
end
|
24
|
+
|
25
|
+
def migration_class_name
|
26
|
+
migration_name.camelize
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
class <%= migration_class_name %> < ActiveRecord::Migration
|
2
|
+
def up
|
3
|
+
add_column :<%= table_name %>, :published, :boolean, default: false
|
4
|
+
add_column :<%= table_name %>, :published_at, :datetime
|
5
|
+
end
|
6
|
+
|
7
|
+
def down
|
8
|
+
remove_column :<%= table_name %>, :published
|
9
|
+
remove_column :<%= table_name %>, :published_at
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
require "is_publishable/version"
|
2
|
+
|
3
|
+
module IsPublishable
|
4
|
+
def self.included(base)
|
5
|
+
base.extend ClassMethods
|
6
|
+
end
|
7
|
+
|
8
|
+
module ClassMethods
|
9
|
+
|
10
|
+
def publishable args = {}
|
11
|
+
|
12
|
+
class_eval do
|
13
|
+
include IsPublishable::InstanceMethods
|
14
|
+
end
|
15
|
+
|
16
|
+
instance_eval do
|
17
|
+
|
18
|
+
# create before_save hook
|
19
|
+
before_save do
|
20
|
+
if published and published_at.nil?
|
21
|
+
self.published_at = Time.zone.now
|
22
|
+
elsif published.nil?
|
23
|
+
self.published = false
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
# create scopes
|
28
|
+
scope :published, lambda { where(:published => true).where("published_at < ?", Time.zone.now) }
|
29
|
+
scope :unpublished, lambda { where("published = ? OR published_at > ?", false, Time.zone.now) }
|
30
|
+
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
|
36
|
+
module InstanceMethods
|
37
|
+
|
38
|
+
def published?(time = Time.zone.now)
|
39
|
+
published and published_at.present? and published_at < time
|
40
|
+
end
|
41
|
+
|
42
|
+
def unpublished?
|
43
|
+
!published or (published_at and published_at > Time.zone.now)
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
class ActiveRecord::Base
|
50
|
+
include IsPublishable
|
51
|
+
end
|
metadata
ADDED
@@ -0,0 +1,115 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: is_publishable
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Brendan Stennett
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-11-07 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: activerecord
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: sqlite3
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: rake
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
description: Gives a resouce the ability to be published against a certain date
|
79
|
+
email:
|
80
|
+
- brendan@unknowncollective.com
|
81
|
+
executables: []
|
82
|
+
extensions: []
|
83
|
+
extra_rdoc_files: []
|
84
|
+
files:
|
85
|
+
- lib/generators/publishable_generator.rb
|
86
|
+
- lib/generators/templates/publishable_migration.rb.erb
|
87
|
+
- lib/is_publishable/version.rb
|
88
|
+
- lib/is_publishable.rb
|
89
|
+
- README.md
|
90
|
+
homepage: ''
|
91
|
+
licenses: []
|
92
|
+
post_install_message:
|
93
|
+
rdoc_options: []
|
94
|
+
require_paths:
|
95
|
+
- lib
|
96
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
97
|
+
none: false
|
98
|
+
requirements:
|
99
|
+
- - ! '>='
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '0'
|
102
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
103
|
+
none: false
|
104
|
+
requirements:
|
105
|
+
- - ! '>='
|
106
|
+
- !ruby/object:Gem::Version
|
107
|
+
version: '0'
|
108
|
+
requirements: []
|
109
|
+
rubyforge_project:
|
110
|
+
rubygems_version: 1.8.24
|
111
|
+
signing_key:
|
112
|
+
specification_version: 3
|
113
|
+
summary: Created published and published_at attrbutes for a model and allows them
|
114
|
+
to be queried against.
|
115
|
+
test_files: []
|