printing_press 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+ group :development do
9
+ gem "shoulda", ">= 0"
10
+ gem "bundler", "~> 1.0.0"
11
+ gem "jeweler", "~> 1.6.4"
12
+ gem "rcov", ">= 0"
13
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,20 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ git (1.2.5)
5
+ jeweler (1.6.4)
6
+ bundler (~> 1.0)
7
+ git (>= 1.2.5)
8
+ rake
9
+ rake (0.9.2.2)
10
+ rcov (0.9.11)
11
+ shoulda (2.11.3)
12
+
13
+ PLATFORMS
14
+ ruby
15
+
16
+ DEPENDENCIES
17
+ bundler (~> 1.0.0)
18
+ jeweler (~> 1.6.4)
19
+ rcov
20
+ shoulda
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Edmond Leung
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,19 @@
1
+ = printing_press
2
+
3
+ Description goes here.
4
+
5
+ == Contributing to printing_press
6
+
7
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
8
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
9
+ * Fork the project
10
+ * Start a feature/bugfix branch
11
+ * Commit and push until you are happy with your contribution
12
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2011 Edmond Leung. See LICENSE.txt for
18
+ further details.
19
+
data/Rakefile ADDED
@@ -0,0 +1,54 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "printing_press"
18
+ gem.homepage = "http://github.com/eleung/printing_press"
19
+ gem.license = "MIT"
20
+ gem.summary = "Adds the ability to mark models as publishable"
21
+ gem.description = "Used in projects where you need to be able to save drafts of models and allow users to publish them at a later date."
22
+ gem.email = "eleung@thewonderfactory.com"
23
+ gem.authors = ["Edmond Leung"]
24
+ gem.files = FileList["[A-Z]*", "{generators,lib,test}/**/*", 'lib/printing_press/templates/.gitignore']
25
+ # dependencies defined in Gemfile
26
+ end
27
+ Jeweler::RubygemsDotOrgTasks.new
28
+
29
+ require 'rake/testtask'
30
+ Rake::TestTask.new(:test) do |test|
31
+ test.libs << 'lib' << 'test'
32
+ test.pattern = 'test/**/test_*.rb'
33
+ test.verbose = true
34
+ end
35
+
36
+ require 'rcov/rcovtask'
37
+ Rcov::RcovTask.new do |test|
38
+ test.libs << 'test'
39
+ test.pattern = 'test/**/test_*.rb'
40
+ test.verbose = true
41
+ test.rcov_opts << '--exclude "gems/*"'
42
+ end
43
+
44
+ task :default => :test
45
+
46
+ require 'rake/rdoctask'
47
+ Rake::RDocTask.new do |rdoc|
48
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
49
+
50
+ rdoc.rdoc_dir = 'rdoc'
51
+ rdoc.title = "printing_press #{version}"
52
+ rdoc.rdoc_files.include('README*')
53
+ rdoc.rdoc_files.include('lib/**/*.rb')
54
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,18 @@
1
+ require 'rails/generators'
2
+ require 'rails/generators/migration'
3
+ require 'rails/generators/active_record/migration'
4
+
5
+ module PrintingPress
6
+ class PublishableGenerator < Rails::Generators::NamedBase
7
+ include Rails::Generators::Migration
8
+ extend ActiveRecord::Generators::Migration
9
+
10
+ source_root File.expand_path('../templates', __FILE__)
11
+
12
+ desc 'Generates (but does not run) a migration that is a clone of the specified resource.'
13
+
14
+ def create_migration_file
15
+ migration_template "create_published_table.rb", "db/migrate/create_published_#{file_path.gsub('/', '_').pluralize}.rb"
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,16 @@
1
+ class CreatePublished<%= class_name.pluralize %> < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :published_<%= class_name.pluralize.underscore %> do |t|
4
+ <% class_name.singularize.constantize.columns.each do |column| next if column.name == "id" %>t.<%= column.type.to_sym %> :<%= column.name %><% column.scale.nil? ? '' : ", :scale => #{column.scale}" %><% column.precision.nil? ? '' : ", :scale => #{column.precision}" %>
5
+ <% end %>
6
+ end
7
+
8
+ # You'll need to copy the indexes manually
9
+ end
10
+
11
+ def self.down
12
+ # Don't forget to drop any indexes created above here
13
+
14
+ drop_table :published_<%= class_name.pluralize.underscore %>
15
+ end
16
+ end
@@ -0,0 +1,39 @@
1
+ module PrintingPress
2
+ module Controller
3
+
4
+ def self.included(base)
5
+ base.before_filter :set_printing_press_enabled_for_controller
6
+ base.before_filter :setup_printing_press
7
+ base.send :extend, ClassMethods
8
+ end
9
+
10
+ module ClassMethods
11
+ def acts_as_publication(models = [], options = {})
12
+ models = [ models] unless models.is_a?(Array)
13
+
14
+ class_attribute :printing_press_models
15
+ self.printing_press_models = models
16
+ end
17
+ end
18
+
19
+ protected
20
+
21
+ def should_acts_as_publication?
22
+ true
23
+ end
24
+
25
+ private
26
+
27
+ def set_printing_press_enabled_for_controller
28
+ ::PrintingPress.enabled_for_controller = should_acts_as_publication?
29
+ end
30
+
31
+ def setup_printing_press
32
+ if ::PrintingPress.enabled_for_controller?
33
+ (self.class.try(:printing_press_models) || []).each do |model_name|
34
+ model_name.to_s.classify.constantize.try(:printing_press_on)
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,75 @@
1
+ module PrintingPress
2
+ module Model
3
+
4
+ def self.included(base)
5
+ base.send :extend, ClassMethods
6
+ end
7
+
8
+ module ClassMethods
9
+ def publishable(options = {})
10
+ send :include, InstanceMethods
11
+
12
+ class_attribute :published_table_name
13
+ self.published_table_name = options[:table_name] || "published_#{table_name}"
14
+
15
+ class_attribute :dependencies
16
+ self.dependencies = options[:dependent_on] || []
17
+
18
+ class_attribute :printing_press_enabled_for_model
19
+ end
20
+
21
+ def printing_press_off
22
+ self.printing_press_enabled_for_model = false
23
+ set_table_name reset_table_name
24
+ end
25
+
26
+ def printing_press_on
27
+ self.printing_press_enabled_for_model = true
28
+ set_table_name self.published_table_name
29
+ end
30
+
31
+ def drafts
32
+ self.find_by_sql("SELECT o.* FROM #{self.reset_table_name} o, #{self.published_table_name} p WHERE (o.id = p.id AND o.updated_at != p.updated_at)") +
33
+ self.find_by_sql("SELECT * FROM #{self.reset_table_name} WHERE id NOT IN (SELECT id FROM #{self.published_table_name}) GROUP BY id")
34
+ end
35
+ end
36
+
37
+ module InstanceMethods
38
+ def published_version
39
+ @published_version ||= self.class.find_by_sql("SELECT * FROM #{self.class.published_table_name} WHERE id = #{self.id}").first
40
+ end
41
+
42
+ def publish
43
+ return if published?
44
+
45
+ remove_published_version if not published_version.nil?
46
+ connection.insert("INSERT INTO #{self.class.published_table_name} SELECT * FROM #{self.class.reset_table_name} WHERE id = #{self.id}")
47
+
48
+ # We need to handle belongs_to dependencies as well
49
+ self.class.dependencies.each do |dependency|
50
+ self.try(dependency).try(:publish)
51
+ end
52
+ end
53
+
54
+ def published?
55
+ not published_version.nil? and published_version.updated_at == self.updated_at
56
+ end
57
+
58
+ def destroy
59
+ remove_published_version
60
+ super
61
+ end
62
+
63
+ def published_at
64
+ published_version.nil? ? nil : published_version.updated_at
65
+ end
66
+
67
+ private
68
+
69
+ def remove_published_version
70
+ connection.delete("DELETE FROM #{self.class.published_table_name} WHERE id = #{self.id}")
71
+ end
72
+ end
73
+
74
+ end
75
+ end
@@ -0,0 +1,3 @@
1
+ module PrintingPress
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,29 @@
1
+ require 'printing_press/controller'
2
+ require 'printing_press/publishable'
3
+ require 'printing_press/version_number'
4
+
5
+ module PrintingPress
6
+ def self.enabled_for_controller?
7
+ !!printing_press_store[:request_enabled_for_controller]
8
+ end
9
+
10
+ def self.enabled_for_controller=(value)
11
+ printing_press_store[:request_enabled_for_controller] = value
12
+ end
13
+
14
+ private
15
+
16
+ def self.printing_press_store
17
+ Thread.current[:printing_press] ||= {
18
+ :request_enabled_for_controller => true
19
+ }
20
+ end
21
+ end
22
+
23
+ ActiveSupport.on_load(:active_record) do
24
+ include PrintingPress::Model
25
+ end
26
+
27
+ ActiveSupport.on_load(:action_controller) do
28
+ include PrintingPress::Controller
29
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,18 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'test/unit'
11
+ require 'shoulda'
12
+
13
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
14
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
15
+ require 'printing_press'
16
+
17
+ class Test::Unit::TestCase
18
+ end
@@ -0,0 +1,7 @@
1
+ require 'helper'
2
+
3
+ class TestPrintingPress < Test::Unit::TestCase
4
+ should "probably rename this file and start testing for real" do
5
+ flunk "hey buddy, you should probably rename this file and start testing for real"
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,140 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: printing_press
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Edmond Leung
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-11-19 00:00:00 -05:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ requirement: &id001 !ruby/object:Gem::Requirement
23
+ none: false
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ hash: 3
28
+ segments:
29
+ - 0
30
+ version: "0"
31
+ type: :development
32
+ name: shoulda
33
+ prerelease: false
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ requirement: &id002 !ruby/object:Gem::Requirement
37
+ none: false
38
+ requirements:
39
+ - - ~>
40
+ - !ruby/object:Gem::Version
41
+ hash: 23
42
+ segments:
43
+ - 1
44
+ - 0
45
+ - 0
46
+ version: 1.0.0
47
+ type: :development
48
+ name: bundler
49
+ prerelease: false
50
+ version_requirements: *id002
51
+ - !ruby/object:Gem::Dependency
52
+ requirement: &id003 !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ~>
56
+ - !ruby/object:Gem::Version
57
+ hash: 7
58
+ segments:
59
+ - 1
60
+ - 6
61
+ - 4
62
+ version: 1.6.4
63
+ type: :development
64
+ name: jeweler
65
+ prerelease: false
66
+ version_requirements: *id003
67
+ - !ruby/object:Gem::Dependency
68
+ requirement: &id004 !ruby/object:Gem::Requirement
69
+ none: false
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ hash: 3
74
+ segments:
75
+ - 0
76
+ version: "0"
77
+ type: :development
78
+ name: rcov
79
+ prerelease: false
80
+ version_requirements: *id004
81
+ description: Used in projects where you need to be able to save drafts of models and allow users to publish them at a later date.
82
+ email: eleung@thewonderfactory.com
83
+ executables: []
84
+
85
+ extensions: []
86
+
87
+ extra_rdoc_files:
88
+ - LICENSE.txt
89
+ - README.rdoc
90
+ files:
91
+ - Gemfile
92
+ - Gemfile.lock
93
+ - LICENSE.txt
94
+ - README.rdoc
95
+ - Rakefile
96
+ - VERSION
97
+ - lib/generators/printing_press/publishable/publishable_generator.rb
98
+ - lib/generators/printing_press/publishable/templates/create_published_table.rb
99
+ - lib/printing_press.rb
100
+ - lib/printing_press/controller.rb
101
+ - lib/printing_press/publishable.rb
102
+ - lib/printing_press/version_number.rb
103
+ - test/helper.rb
104
+ - test/test_printing_press.rb
105
+ has_rdoc: true
106
+ homepage: http://github.com/eleung/printing_press
107
+ licenses:
108
+ - MIT
109
+ post_install_message:
110
+ rdoc_options: []
111
+
112
+ require_paths:
113
+ - lib
114
+ required_ruby_version: !ruby/object:Gem::Requirement
115
+ none: false
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ hash: 3
120
+ segments:
121
+ - 0
122
+ version: "0"
123
+ required_rubygems_version: !ruby/object:Gem::Requirement
124
+ none: false
125
+ requirements:
126
+ - - ">="
127
+ - !ruby/object:Gem::Version
128
+ hash: 3
129
+ segments:
130
+ - 0
131
+ version: "0"
132
+ requirements: []
133
+
134
+ rubyforge_project:
135
+ rubygems_version: 1.3.7
136
+ signing_key:
137
+ specification_version: 3
138
+ summary: Adds the ability to mark models as publishable
139
+ test_files: []
140
+