has_publishing 0.0.3 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +15 -4
- data/lib/generators/has_publishing/publishing_generator.rb +19 -18
- data/lib/generators/has_publishing/templates/{migration.rb.erb → migration.erb} +1 -1
- data/lib/has_publishing/configuration.rb +1 -1
- data/lib/has_publishing/instance_methods.rb +2 -2
- data/lib/has_publishing/version.rb +1 -1
- data/spec/has_publishing_spec.rb +1 -1
- metadata +6 -6
data/README.md
CHANGED
@@ -2,10 +2,12 @@
|
|
2
2
|
|
3
3
|
Mark models as `has_publishing` to publish, draft and embargo models. Easy peasy!
|
4
4
|
|
5
|
+
[![Build Status](https://travis-ci.org/3months/has_publishing.png?branch=master)](https://travis-ci.org/3months/has_publishing)
|
6
|
+
|
5
7
|
## Features
|
6
8
|
|
7
9
|
* `published`, `draft`, `embargoed` scopes for easy filtering/finding
|
8
|
-
* Rails environment-based default scoping: if your site is using `
|
10
|
+
* Rails environment-based default scoping: if your site is using `draft`, draft/embargoed records will still be found - if you use `RAILS_ENV=published`, though, only published records will be found.
|
9
11
|
* In use in production on multiple sites
|
10
12
|
* Covered by automated tests
|
11
13
|
|
@@ -39,14 +41,13 @@ bundle exec rails generate migration [YOUR MODEL NAME] embargoed_until:datetime
|
|
39
41
|
|
40
42
|
Publishing is typically used in an environment where there may be two installations of the Rails application sharing a common database. This at least is the set up that `has_publishing` is designed to operate in - something like the following:
|
41
43
|
|
42
|
-
|
43
44
|
```
|
44
|
-
|-- Admin RAILS_ENV=
|
45
|
+
|-- Admin RAILS_ENV=draft --| >>>>> SharedDatabase <<<<<< |-- Published Site RAILS_ENV=published --|
|
45
46
|
```
|
46
47
|
|
47
48
|
Because of this, the gem applies a default_scope to all instances of this model to either:
|
48
49
|
|
49
|
-
* Only return published records if `Rails.env` matches `HasPublishing.config.published_rails_environment`
|
50
|
+
* Only return published records if `Rails.env` matches `HasPublishing.config.published_rails_environment` (which by default is `'published'`)
|
50
51
|
* Only return draft records otherwise
|
51
52
|
|
52
53
|
This prevents 'duplicate' records from appearing for the user (since each 'record' has two representations - 'draft' and 'published/withdrawn')
|
@@ -57,6 +58,16 @@ This prevents 'duplicate' records from appearing for the user (since each 'recor
|
|
57
58
|
2. If you want the default scope to apply properly, ensure that you set the Rails environment of your **published** application in `HasPublishing.config.published_rails_environment`.
|
58
59
|
|
59
60
|
|
61
|
+
## Injecting your own attributes to be saved
|
62
|
+
|
63
|
+
When you call `publish!` and `withdraw!` you can pass a hash of attributes with it to be updated with your `ActiveRecord::Model` object.
|
64
|
+
|
65
|
+
This is usefull if you are using a gem like [ancestry](https://github.com/stefankroes/ancestry "ancestry"), for example:
|
66
|
+
|
67
|
+
``` ruby
|
68
|
+
@page = Page.find_by_slug('foo-bar-page')
|
69
|
+
@page.publish!(:parent => (@page.parent.published unless @page.is_root?))
|
70
|
+
```
|
60
71
|
|
61
72
|
## Contributing
|
62
73
|
|
@@ -1,28 +1,29 @@
|
|
1
1
|
require 'rails/generators'
|
2
2
|
require 'rails/generators/migration'
|
3
3
|
|
4
|
-
|
5
|
-
|
4
|
+
module HasPublishing
|
5
|
+
class PublishingGenerator < ::Rails::Generators::NamedBase
|
6
|
+
include Rails::Generators::Migration
|
6
7
|
|
7
|
-
|
8
|
-
|
9
|
-
|
8
|
+
def self.source_root
|
9
|
+
@source_root ||= File.join(File.dirname(__FILE__), 'templates')
|
10
|
+
end
|
10
11
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
12
|
+
def self.next_migration_number(dirname)
|
13
|
+
if ActiveRecord::Base.timestamped_migrations
|
14
|
+
Time.new.utc.strftime("%Y%m%d%H%M%S")
|
15
|
+
else
|
16
|
+
"%.3d" % (current_migration_number(dirname) + 1)
|
17
|
+
end
|
16
18
|
end
|
17
|
-
end
|
18
19
|
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
end
|
20
|
+
def create_migration_file
|
21
|
+
set_local_assigns!
|
22
|
+
migration_template 'migration.erb', "db/migrate/publishing_fields_for_#{@table_name}.rb"
|
23
|
+
end
|
24
24
|
|
25
|
-
|
26
|
-
|
25
|
+
def set_local_assigns!
|
26
|
+
@table_name = file_name.pluralize
|
27
|
+
end
|
27
28
|
end
|
28
29
|
end
|
@@ -1,6 +1,6 @@
|
|
1
1
|
class PublishingFieldsFor<%= @table_name %> < ActiveRecord::Migration
|
2
2
|
def self.change
|
3
|
-
change_table
|
3
|
+
change_table :<%= @table_name %> do |t|
|
4
4
|
t.datetime :published_at
|
5
5
|
t.datetime :embargoed_until
|
6
6
|
t.string :kind, :null => false, :index => true
|
@@ -28,11 +28,11 @@ module HasPublishing
|
|
28
28
|
}
|
29
29
|
end
|
30
30
|
|
31
|
-
def withdraw!
|
31
|
+
def withdraw!(extra_attrs = {})
|
32
32
|
self.class.unscoped {
|
33
33
|
return false unless draft? && ever_published?
|
34
34
|
self.class.record_timestamps = false # want same updated_at
|
35
|
-
published.update_attributes!(:kind => 'withdrawn') and update_attributes!(published.attributes.merge({:kind => 'draft', :published_id => published_id, :published_at => nil, :dirty => false}))
|
35
|
+
published.update_attributes!(:kind => 'withdrawn') and update_attributes!(published.attributes.merge({:kind => 'draft', :published_id => published_id, :published_at => nil, :dirty => false}).merge(extra_attrs))
|
36
36
|
self.class.record_timestamps = true
|
37
37
|
return published
|
38
38
|
}
|
data/spec/has_publishing_spec.rb
CHANGED
@@ -67,7 +67,7 @@ describe "has_publishing" do
|
|
67
67
|
|
68
68
|
describe "default configuration" do
|
69
69
|
it { HasPublishing.config.scope_records.should be_true }
|
70
|
-
it { HasPublishing.config.published_rails_environment.should eq "
|
70
|
+
it { HasPublishing.config.published_rails_environment.should eq "published" }
|
71
71
|
end
|
72
72
|
|
73
73
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: has_publishing
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2013-04-08 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activerecord
|
@@ -90,7 +90,7 @@ files:
|
|
90
90
|
- Rakefile
|
91
91
|
- has_publishing.gemspec
|
92
92
|
- lib/generators/has_publishing/publishing_generator.rb
|
93
|
-
- lib/generators/has_publishing/templates/migration.
|
93
|
+
- lib/generators/has_publishing/templates/migration.erb
|
94
94
|
- lib/has_publishing.rb
|
95
95
|
- lib/has_publishing/class_methods.rb
|
96
96
|
- lib/has_publishing/configuration.rb
|
@@ -112,7 +112,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
112
112
|
version: '0'
|
113
113
|
segments:
|
114
114
|
- 0
|
115
|
-
hash:
|
115
|
+
hash: 3211080172926096539
|
116
116
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
117
117
|
none: false
|
118
118
|
requirements:
|
@@ -121,10 +121,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
121
121
|
version: '0'
|
122
122
|
segments:
|
123
123
|
- 0
|
124
|
-
hash:
|
124
|
+
hash: 3211080172926096539
|
125
125
|
requirements: []
|
126
126
|
rubyforge_project:
|
127
|
-
rubygems_version: 1.8.
|
127
|
+
rubygems_version: 1.8.23
|
128
128
|
signing_key:
|
129
129
|
specification_version: 3
|
130
130
|
summary: Add publishing to your ActiveRecord models
|