pubdraft 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/.gitignore +17 -0
- data/.rspec +1 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +50 -0
- data/Rakefile +1 -0
- data/config/database.yml +3 -0
- data/db/.gitkeep +0 -0
- data/lib/pubdraft/version.rb +3 -0
- data/lib/pubdraft.rb +63 -0
- data/pubdraft.gemspec +23 -0
- data/spec/.gitkeep +0 -0
- data/spec/pubdraft_spec.rb +42 -0
- data/spec/spec_no_rails_helper.rb +20 -0
- metadata +106 -0
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Joe Sak
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
# Pubdraft
|
2
|
+
|
3
|
+
Quickly add published/drafted states to your ActiveRecord models, with helpful scopes and instance methods for querying and changing the states
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'pubdraft'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install pubdraft
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
# requires string column `state` to exist
|
25
|
+
# $ rails g migration AddStateToMyModels state
|
26
|
+
|
27
|
+
class MyModel < ActiveRecord::Base
|
28
|
+
pubdraft
|
29
|
+
end
|
30
|
+
|
31
|
+
record = MyModel.create!
|
32
|
+
record.published? #=> true
|
33
|
+
|
34
|
+
record.draft!
|
35
|
+
record.drafted? #=> true
|
36
|
+
|
37
|
+
record.publish!
|
38
|
+
record.published? #=> true
|
39
|
+
|
40
|
+
MyModel.published #=> [published records]
|
41
|
+
MyModel.drafted #=> [drafted records]
|
42
|
+
```
|
43
|
+
|
44
|
+
## Contributing
|
45
|
+
|
46
|
+
1. Fork it
|
47
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
48
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
49
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
50
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/config/database.yml
ADDED
data/db/.gitkeep
ADDED
File without changes
|
data/lib/pubdraft.rb
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
require "pubdraft/version"
|
2
|
+
|
3
|
+
module Pubdraft
|
4
|
+
module InstanceMethods
|
5
|
+
def self.included(base)
|
6
|
+
base.send :before_create, :set_pubdraft_state
|
7
|
+
base.attr_accessible :state
|
8
|
+
|
9
|
+
base.scope :published, base.where(:state => 'published')
|
10
|
+
base.scope :drafted, base.where(:state => 'drafted')
|
11
|
+
end
|
12
|
+
|
13
|
+
def published?
|
14
|
+
state == 'published'
|
15
|
+
end
|
16
|
+
|
17
|
+
def drafted?
|
18
|
+
state == 'drafted'
|
19
|
+
end
|
20
|
+
|
21
|
+
def publish!
|
22
|
+
publish && save
|
23
|
+
end
|
24
|
+
|
25
|
+
def publish
|
26
|
+
self.state = 'published'
|
27
|
+
end
|
28
|
+
|
29
|
+
def draft!
|
30
|
+
draft && save
|
31
|
+
end
|
32
|
+
|
33
|
+
def draft
|
34
|
+
self.state = 'drafted'
|
35
|
+
end
|
36
|
+
|
37
|
+
private
|
38
|
+
def set_pubdraft_state
|
39
|
+
return unless state.blank?
|
40
|
+
publish
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
module ClassMethods
|
45
|
+
def pubdraft
|
46
|
+
send(:include, InstanceMethods)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
module HelperMethods
|
51
|
+
def pubdraft_states_for_select
|
52
|
+
[['Published', 'published'], ['Drafted', 'drafted']]
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
if defined?(ActiveRecord::Base)
|
57
|
+
ActiveRecord::Base.extend Pubdraft::ClassMethods
|
58
|
+
end
|
59
|
+
|
60
|
+
if defined?(ApplicationHelper)
|
61
|
+
ApplicationHelper.extend Pubdraft::HelperMethods
|
62
|
+
end
|
63
|
+
end
|
data/pubdraft.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'pubdraft/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "pubdraft"
|
8
|
+
gem.version = Pubdraft::VERSION
|
9
|
+
gem.authors = ["Joe Sak"]
|
10
|
+
gem.email = ["joe@neotericdesign.com"]
|
11
|
+
gem.description = %q{Quickly add publish/draft state to your ActiveRecord models}
|
12
|
+
gem.summary = %q{Quick publish/draft state on ActiveRecord models}
|
13
|
+
|
14
|
+
gem.files = `git ls-files`.split($/)
|
15
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
16
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
17
|
+
gem.require_paths = ["lib"]
|
18
|
+
|
19
|
+
gem.add_development_dependency 'sqlite3'
|
20
|
+
gem.add_development_dependency 'activerecord', '3.2.9'
|
21
|
+
gem.add_development_dependency 'rspec'
|
22
|
+
gem.add_development_dependency 'database_cleaner'
|
23
|
+
end
|
data/spec/.gitkeep
ADDED
File without changes
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'spec_no_rails_helper'
|
2
|
+
require 'pubdraft'
|
3
|
+
|
4
|
+
class CreateTestData < ActiveRecord::Migration
|
5
|
+
unless table_exists?('fake_things')
|
6
|
+
create_table :fake_things do |t|
|
7
|
+
t.string :state
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
class FakeThing < ActiveRecord::Base
|
13
|
+
pubdraft
|
14
|
+
end
|
15
|
+
|
16
|
+
describe Pubdraft do
|
17
|
+
before { @class = FakeThing }
|
18
|
+
|
19
|
+
it "defaults to published state" do
|
20
|
+
obj = @class.create!
|
21
|
+
|
22
|
+
obj.should be_published
|
23
|
+
end
|
24
|
+
|
25
|
+
it "changes states" do
|
26
|
+
obj = @class.create!
|
27
|
+
|
28
|
+
obj.draft!
|
29
|
+
obj.should be_drafted
|
30
|
+
|
31
|
+
obj.publish!
|
32
|
+
obj.should be_published
|
33
|
+
end
|
34
|
+
|
35
|
+
it "has scopes" do
|
36
|
+
published = @class.create!
|
37
|
+
drafted = @class.create!(:state => 'drafted')
|
38
|
+
|
39
|
+
@class.published.should == [published]
|
40
|
+
@class.drafted.should == [drafted]
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'active_record'
|
2
|
+
require 'database_cleaner'
|
3
|
+
|
4
|
+
db = YAML.load(File.open('config/database.yml'))['test']
|
5
|
+
ActiveRecord::Base.establish_connection(db)
|
6
|
+
|
7
|
+
RSpec.configure do |config|
|
8
|
+
config.before(:suite) do
|
9
|
+
DatabaseCleaner.strategy = :transaction
|
10
|
+
DatabaseCleaner.clean_with(:truncation)
|
11
|
+
end
|
12
|
+
|
13
|
+
config.before(:each) do
|
14
|
+
DatabaseCleaner.start
|
15
|
+
end
|
16
|
+
|
17
|
+
config.after(:each) do
|
18
|
+
DatabaseCleaner.clean
|
19
|
+
end
|
20
|
+
end unless defined?(Rails)
|
metadata
ADDED
@@ -0,0 +1,106 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: pubdraft
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Joe Sak
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-11-15 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: sqlite3
|
16
|
+
requirement: &2165585040 !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: *2165585040
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: activerecord
|
27
|
+
requirement: &2165584200 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - =
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 3.2.9
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *2165584200
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: rspec
|
38
|
+
requirement: &2165583700 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
type: :development
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *2165583700
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: database_cleaner
|
49
|
+
requirement: &2165582980 !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
type: :development
|
56
|
+
prerelease: false
|
57
|
+
version_requirements: *2165582980
|
58
|
+
description: Quickly add publish/draft state to your ActiveRecord models
|
59
|
+
email:
|
60
|
+
- joe@neotericdesign.com
|
61
|
+
executables: []
|
62
|
+
extensions: []
|
63
|
+
extra_rdoc_files: []
|
64
|
+
files:
|
65
|
+
- .gitignore
|
66
|
+
- .rspec
|
67
|
+
- Gemfile
|
68
|
+
- LICENSE.txt
|
69
|
+
- README.md
|
70
|
+
- Rakefile
|
71
|
+
- config/database.yml
|
72
|
+
- db/.gitkeep
|
73
|
+
- lib/pubdraft.rb
|
74
|
+
- lib/pubdraft/version.rb
|
75
|
+
- pubdraft.gemspec
|
76
|
+
- spec/.gitkeep
|
77
|
+
- spec/pubdraft_spec.rb
|
78
|
+
- spec/spec_no_rails_helper.rb
|
79
|
+
homepage:
|
80
|
+
licenses: []
|
81
|
+
post_install_message:
|
82
|
+
rdoc_options: []
|
83
|
+
require_paths:
|
84
|
+
- lib
|
85
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
86
|
+
none: false
|
87
|
+
requirements:
|
88
|
+
- - ! '>='
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: '0'
|
91
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
92
|
+
none: false
|
93
|
+
requirements:
|
94
|
+
- - ! '>='
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
requirements: []
|
98
|
+
rubyforge_project:
|
99
|
+
rubygems_version: 1.8.6
|
100
|
+
signing_key:
|
101
|
+
specification_version: 3
|
102
|
+
summary: Quick publish/draft state on ActiveRecord models
|
103
|
+
test_files:
|
104
|
+
- spec/.gitkeep
|
105
|
+
- spec/pubdraft_spec.rb
|
106
|
+
- spec/spec_no_rails_helper.rb
|