Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
joshmcarthur committed Dec 14, 2012
0 parents commit 0b0a1a1
Show file tree
Hide file tree
Showing 11 changed files with 205 additions and 0 deletions.
17 changes: 17 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
*.gem
*.rbc
.bundle
.config
.yardoc
Gemfile.lock
InstalledFiles
_yardoc
coverage
doc/
lib/bundler/man
pkg
rdoc
spec/reports
test/tmp
test/version_tmp
tmp
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
source 'https://rubygems.org'

# Specify your gem's dependencies in has_publishing.gemspec
gemspec
22 changes: 22 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Copyright (c) 2012 3months

MIT License

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# HasPublishing

TODO: Write a gem description

## Installation

Add this line to your application's Gemfile:

gem 'has_publishing'

And then execute:

$ bundle

Or install it yourself as:

$ gem install has_publishing

## Usage

TODO: Write usage instructions here

## Contributing

1. Fork it
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create new Pull Request
1 change: 1 addition & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require "bundler/gem_tasks"
19 changes: 19 additions & 0 deletions has_publishing.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# -*- encoding: utf-8 -*-
lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'has_publishing/version'

Gem::Specification.new do |gem|
gem.name = "has_publishing"
gem.version = HasPublishing::VERSION
gem.authors = ["Josh McArthur @ 3months"]
gem.email = ["[email protected]"]
gem.description = %q{Add ability to publish/embargo models}
gem.summary = %q{Add publishing to your ActiveRecord models}
gem.homepage = ""

gem.files = `git ls-files`.split($/)
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
gem.require_paths = ["lib"]
end
4 changes: 4 additions & 0 deletions lib/has_publishing.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
require "has_publishing/version"

module HasPublishing
end
4 changes: 4 additions & 0 deletions lib/has_publishing/class_methods.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module HasPublishing
module ClassMethods
end
end
32 changes: 32 additions & 0 deletions lib/has_publishing/has_publishing.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
require 'has_publishing/class_methods'
require 'has_publishing/instance_methods'

class << ActiveRecord::Base
def has_publishing

# Include instance methods
include Publishing::InstanceMethods

# Include dynamic class methods
extend Publishing::ClassMethods

# This default scope allows published and draft viewing modes to share the
# same code. This is good. However if you need to access the other, draft
# from published or published from draft e.g from admin for editing, then
# you must explicitly use .unscoped to remove the default scope.
default_scope Rails.env.split('_').last == "published" ? lambda { where("#{self.table_name}.kind = 'published'").where(["#{self.table_name}.embargoed_until IS NULL OR ? > #{self.table_name}.embargoed_until", Time.zone.now.round]) } : where(:kind => "draft")

scope :published, lambda { where("kind = 'published' AND (embargoed_until IS NULL OR ? > embargoed_until)", Time.zone.now.round) }
scope :draft, where("kind = 'draft'")

scope :embargoed, lambda { where("embargoed_until IS NOT NULL AND ? > embargoed_until", Time.zone.now.round) }
scope :not_embargoed, lambda { where("embargoed_until IS NULL OR embargoed_until < ?", Time.zone.now.round) }

before_create :set_draft
after_save :set_dirty

belongs_to :published, :class_name => self.name, :foreign_key => :published_id, :dependent => :destroy
has_one :draft, :class_name => self.name, :foreign_key => :published_id

end
end
70 changes: 70 additions & 0 deletions lib/has_publishing/instance_methods.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
module HasPublishing
module InstanceMethods


# Publishing "states"
# Draft = draft only (no published version, i.e new)
# Dirty = published + draft dirty (this is a kind of Draft state,
# has been published but draft has been edited since)
# Published = published + draft NOT dirty
# Under imbargo = published but future dated (embargo_until field)


def publish(extra_attrs = {})
self.class.unscoped {
return false unless draft?
if published.nil? # first time publishing
published_obj = self.class.create!(attributes.merge(:kind => 'published', :published_at => Time.zone.now, :dirty => nil).merge(extra_attrs))
self.published_id = published_obj.id
else
published.update_attributes!(attributes.merge(:kind => 'published', :published_id => nil, :published_at => Time.zone.now, :dirty => nil).merge(extra_attrs))
end
self.class.record_timestamps = false # want same updated_at
update_attributes!(:published_at => published.published_at, :dirty => false, :updated_at => published.updated_at)
self.class.record_timestamps = true
return published
}
end

def withdraw
self.class.unscoped {
return false unless draft? && ever_published?
self.class.record_timestamps = false # want same updated_at
published.update_attributes!(:kind => 'withdrawn') and update_attributes!(published.attributes.merge({:kind => 'draft', :published_id => published_id, :published_at => nil, :dirty => false}))
self.class.record_timestamps = true
return published
}
end

def draft?
kind == 'draft'
end

def published?
self.class.unscoped { (draft? && published && published.kind == 'published') || kind == 'published' }
end

def withdrawn?
self.class.unscoped { (draft? && published && published.kind == 'withdrawn') || kind == 'withdrawn' }
end

def ever_published?
published? || withdrawn?
end

def under_embargo?
published? && embargoed_until && embargoed_until > Time.zone.now
end

private

def set_dirty
self.class.unscoped { update_attributes(:dirty => true) if draft? && published && published.updated_at != updated_at && !dirty? && !withdrawn? }
end

def set_draft
self.kind = 'draft' if kind.nil?
end

end
end
3 changes: 3 additions & 0 deletions lib/has_publishing/version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module HasPublishing
VERSION = "0.0.1"
end

0 comments on commit 0b0a1a1

Please sign in to comment.