acts_as_publicable 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +5 -0
- data/.infinity_test +3 -0
- data/.rspec +3 -0
- data/Gemfile +3 -0
- data/README.textile +58 -0
- data/Rakefile +10 -0
- data/acts_as_publicable.gemspec +28 -0
- data/lib/acts_as_publicable.rb +6 -0
- data/lib/acts_as_publicable/acts_as_publicable.rb +26 -0
- data/lib/acts_as_publicable/version.rb +3 -0
- data/lib/generators/USAGE +9 -0
- data/lib/generators/acts_as_publicable_generator.rb +13 -0
- data/lib/generators/templates/migration.rb +9 -0
- data/spec/acts_as_publicable/acts_as_publicable_spec.rb +28 -0
- data/spec/schema.rb +12 -0
- data/spec/spec_helper.rb +27 -0
- data/spec/support/models.rb +6 -0
- metadata +109 -0
data/.infinity_test
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/README.textile
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
This gem will give you only two scopes and a method for handling
|
2
|
+
published/unpublished stuff. Nothing remarkable bur saved me a lot of typing
|
3
|
+
when I wrote it for a project with an huge number of publishable models.
|
4
|
+
Suggestions are more than welcome.
|
5
|
+
|
6
|
+
h2. How It Works
|
7
|
+
|
8
|
+
In order to use in your model, write something like the following:
|
9
|
+
|
10
|
+
<pre>
|
11
|
+
class Articile < ActiveRecord::Base
|
12
|
+
acts_as_publicable
|
13
|
+
end
|
14
|
+
</pre>
|
15
|
+
|
16
|
+
With that you get two very simple scopes:
|
17
|
+
|
18
|
+
<pre>
|
19
|
+
ruby-1.9.2-p180 :001 > Article.unpublished
|
20
|
+
Article Load (0.2ms) SELECT `articles`.* FROM `articles` WHERE (published is false)
|
21
|
+
=> []
|
22
|
+
ruby-1.9.2-p180 :002 > Article.published
|
23
|
+
Article Load (0.2ms) SELECT `articles`.* FROM `articles` WHERE (published is true)
|
24
|
+
=> []
|
25
|
+
</pre>
|
26
|
+
|
27
|
+
and a method to publish an article:
|
28
|
+
|
29
|
+
<pre>
|
30
|
+
ruby-1.9.2-p180 :011 > @article=Article.first
|
31
|
+
Article Load (0.2ms) SELECT `articles`.* FROM `articles` LIMIT 1
|
32
|
+
ruby-1.9.2-p180 :012 > @article.publish!
|
33
|
+
SQL (0.1ms) BEGIN
|
34
|
+
AREL (0.2ms) UPDATE `articles` SET `published` = 1, `updated_at` = '2011-04-05 08:50:45' WHERE (`articles`.`id` = 1)
|
35
|
+
SQL (48.3ms) COMMIT
|
36
|
+
=> true
|
37
|
+
</pre>
|
38
|
+
|
39
|
+
h2. Generator
|
40
|
+
|
41
|
+
You can use a generator to add the published field to your target model:
|
42
|
+
|
43
|
+
rails g acts_as_publicable model_name
|
44
|
+
|
45
|
+
It will generate a simple migration that will add the neeeded field.
|
46
|
+
|
47
|
+
h2. Roadmap
|
48
|
+
|
49
|
+
* comment code
|
50
|
+
* add a date for handling a better publishing concept
|
51
|
+
|
52
|
+
h2. Copyright
|
53
|
+
|
54
|
+
This program is free software. It comes without any warranty,
|
55
|
+
to the extent permitted by applicable law. You can redistribute
|
56
|
+
it and/or modify it under the terms of the Do What The Fuck You
|
57
|
+
Want To Public License, Version 2, as published by Sam Hocevar.
|
58
|
+
See http://sam.zoy.org/wtfpl/COPYING for more details.
|
data/Rakefile
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "acts_as_publicable/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "acts_as_publicable"
|
7
|
+
s.version = ActsAsPublicable::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["lucapette"]
|
10
|
+
s.email = ["lucapette@gmail.com"]
|
11
|
+
s.homepage = "https://github.com/lucapette/acts_as_publicable"
|
12
|
+
s.summary = %q{a simple ActiveRecord extension for handling publishable stuff}
|
13
|
+
s.description = %q{This gem will give you only two scopes and a method for handling
|
14
|
+
published/unpublished stuff. Nothing remarkable bur saved me a lot of typing
|
15
|
+
when I wrote it for a project with an huge number of publishable models.
|
16
|
+
Suggestions are more than welcome.}
|
17
|
+
|
18
|
+
s.add_development_dependency 'sqlite3'
|
19
|
+
s.add_dependency "rails", ">= 3.0.0"
|
20
|
+
s.add_development_dependency "rspec-rails", ">= 2.5.0"
|
21
|
+
|
22
|
+
s.rubyforge_project = "acts_as_publicable"
|
23
|
+
|
24
|
+
s.files = `git ls-files`.split("\n")
|
25
|
+
s.test_files = `git ls-files -- {spec,features}/*`.split("\n")
|
26
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
27
|
+
s.require_paths = ["lib"]
|
28
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module ActsAsPublicable
|
2
|
+
|
3
|
+
class FieldNotPresentError < StandardError; end
|
4
|
+
module ActiveRecordExtension
|
5
|
+
|
6
|
+
extend ActiveSupport::Concern
|
7
|
+
|
8
|
+
module ClassMethods
|
9
|
+
def acts_as_publicable
|
10
|
+
raise ActsAsPublicable::FieldNotPresentError unless column_names.include?(:published.to_s)
|
11
|
+
scope :published, where("published is true")
|
12
|
+
scope :unpublished, where("published is false")
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
module InstanceMethods
|
17
|
+
|
18
|
+
def publish!
|
19
|
+
self.published = true
|
20
|
+
save
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
class ActsAsPublicableGenerator < Rails::Generators::NamedBase
|
2
|
+
include Rails::Generators::Migration
|
3
|
+
source_root File.expand_path('../templates', __FILE__)
|
4
|
+
|
5
|
+
def manifest
|
6
|
+
migration_template 'migration.rb', "db/migrate/add_publicable_to_#{table_name}"
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.next_migration_number(path)
|
10
|
+
Time.now.utc.strftime("%Y%m%d%H%M%S")
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe "acts_as_publicable" do
|
4
|
+
it "raises an error when it's not present published field" do
|
5
|
+
lambda {
|
6
|
+
class Post
|
7
|
+
acts_as_publicable
|
8
|
+
end
|
9
|
+
}.should raise_error ActsAsPublicable::FieldNotPresentError
|
10
|
+
end
|
11
|
+
|
12
|
+
it "adds published scope" do
|
13
|
+
Article.should respond_to(:published)
|
14
|
+
end
|
15
|
+
|
16
|
+
it "adds unpublished scope" do
|
17
|
+
Article.should respond_to(:unpublished)
|
18
|
+
end
|
19
|
+
|
20
|
+
describe "#publish!" do
|
21
|
+
it "publish an article" do
|
22
|
+
@article = Article.create!(:title=>"Awesome article")
|
23
|
+
@article.publish!
|
24
|
+
@article.should be_published
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
data/spec/schema.rb
ADDED
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
def load_models!
|
2
|
+
ActiveRecord::Base.descendants.each do |model|
|
3
|
+
model.delete_all
|
4
|
+
Object.class_eval { remove_const model.name if const_defined?(model.name) }
|
5
|
+
end
|
6
|
+
|
7
|
+
load File.dirname(__FILE__) + "/support/models.rb"
|
8
|
+
end
|
9
|
+
|
10
|
+
Bundler.setup
|
11
|
+
require "rails/all"
|
12
|
+
Bundler.require(:default)
|
13
|
+
|
14
|
+
require "rspec/rails"
|
15
|
+
require "acts_as_publicable"
|
16
|
+
|
17
|
+
|
18
|
+
ActiveRecord::Base.establish_connection :adapter => "sqlite3", :database => ":memory:"
|
19
|
+
load "schema.rb"
|
20
|
+
|
21
|
+
Dir[File.dirname(__FILE__) + "/support/**/*.rb"].each {|f| require f}
|
22
|
+
|
23
|
+
RSpec.configure do |config|
|
24
|
+
config.before do
|
25
|
+
load_models!
|
26
|
+
end
|
27
|
+
end
|
metadata
ADDED
@@ -0,0 +1,109 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: acts_as_publicable
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.0.1
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- lucapette
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2011-04-05 00:00:00 +02:00
|
14
|
+
default_executable:
|
15
|
+
dependencies:
|
16
|
+
- !ruby/object:Gem::Dependency
|
17
|
+
name: sqlite3
|
18
|
+
prerelease: false
|
19
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
20
|
+
none: false
|
21
|
+
requirements:
|
22
|
+
- - ">="
|
23
|
+
- !ruby/object:Gem::Version
|
24
|
+
version: "0"
|
25
|
+
type: :development
|
26
|
+
version_requirements: *id001
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rails
|
29
|
+
prerelease: false
|
30
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
31
|
+
none: false
|
32
|
+
requirements:
|
33
|
+
- - ">="
|
34
|
+
- !ruby/object:Gem::Version
|
35
|
+
version: 3.0.0
|
36
|
+
type: :runtime
|
37
|
+
version_requirements: *id002
|
38
|
+
- !ruby/object:Gem::Dependency
|
39
|
+
name: rspec-rails
|
40
|
+
prerelease: false
|
41
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
42
|
+
none: false
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: 2.5.0
|
47
|
+
type: :development
|
48
|
+
version_requirements: *id003
|
49
|
+
description: |-
|
50
|
+
This gem will give you only two scopes and a method for handling
|
51
|
+
published/unpublished stuff. Nothing remarkable bur saved me a lot of typing
|
52
|
+
when I wrote it for a project with an huge number of publishable models.
|
53
|
+
Suggestions are more than welcome.
|
54
|
+
email:
|
55
|
+
- lucapette@gmail.com
|
56
|
+
executables: []
|
57
|
+
|
58
|
+
extensions: []
|
59
|
+
|
60
|
+
extra_rdoc_files: []
|
61
|
+
|
62
|
+
files:
|
63
|
+
- .gitignore
|
64
|
+
- .infinity_test
|
65
|
+
- .rspec
|
66
|
+
- Gemfile
|
67
|
+
- README.textile
|
68
|
+
- Rakefile
|
69
|
+
- acts_as_publicable.gemspec
|
70
|
+
- lib/acts_as_publicable.rb
|
71
|
+
- lib/acts_as_publicable/acts_as_publicable.rb
|
72
|
+
- lib/acts_as_publicable/version.rb
|
73
|
+
- lib/generators/USAGE
|
74
|
+
- lib/generators/acts_as_publicable_generator.rb
|
75
|
+
- lib/generators/templates/migration.rb
|
76
|
+
- spec/acts_as_publicable/acts_as_publicable_spec.rb
|
77
|
+
- spec/schema.rb
|
78
|
+
- spec/spec_helper.rb
|
79
|
+
- spec/support/models.rb
|
80
|
+
has_rdoc: true
|
81
|
+
homepage: https://github.com/lucapette/acts_as_publicable
|
82
|
+
licenses: []
|
83
|
+
|
84
|
+
post_install_message:
|
85
|
+
rdoc_options: []
|
86
|
+
|
87
|
+
require_paths:
|
88
|
+
- lib
|
89
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
90
|
+
none: false
|
91
|
+
requirements:
|
92
|
+
- - ">="
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: "0"
|
95
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
96
|
+
none: false
|
97
|
+
requirements:
|
98
|
+
- - ">="
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
version: "0"
|
101
|
+
requirements: []
|
102
|
+
|
103
|
+
rubyforge_project: acts_as_publicable
|
104
|
+
rubygems_version: 1.6.2
|
105
|
+
signing_key:
|
106
|
+
specification_version: 3
|
107
|
+
summary: a simple ActiveRecord extension for handling publishable stuff
|
108
|
+
test_files: []
|
109
|
+
|