acts_as_publicable 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/acts_as_publicable.rb +1 -0
- data/lib/acts_as_publicable/acts_as_publicable.rb +6 -1
- data/lib/acts_as_publicable/routes.rb +13 -0
- data/lib/acts_as_publicable/version.rb +1 -1
- data/lib/generators/{USAGE → acts_as_publicable/USAGE} +0 -0
- data/lib/generators/acts_as_publicable/install_generator.rb +26 -0
- data/lib/generators/acts_as_publicable/migration_generator.rb +19 -0
- data/lib/generators/acts_as_publicable/templates/controller.rb +15 -0
- data/lib/generators/acts_as_publicable/templates/helper.rb +29 -0
- data/lib/generators/{templates → acts_as_publicable/templates}/migration.rb +0 -0
- data/spec/acts_as_publicable/acts_as_publicable_spec.rb +8 -0
- metadata +9 -5
- data/lib/generators/acts_as_publicable_generator.rb +0 -13
data/lib/acts_as_publicable.rb
CHANGED
@@ -0,0 +1,13 @@
|
|
1
|
+
module ActionDispatch::Routing
|
2
|
+
|
3
|
+
class Mapper
|
4
|
+
|
5
|
+
def acts_as_publicable(name)
|
6
|
+
base_route_name = name.to_s.underscore.pluralize
|
7
|
+
put "/#{base_route_name}/:id/publish/:type" => "#{base_route_name}#publish", :as => "#{base_route_name}_publish"
|
8
|
+
put "/#{base_route_name}/:id/unpublish/:type" => "#{base_route_name}#unpublish", :as => "#{base_route_name}_unpublish"
|
9
|
+
end
|
10
|
+
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|
File without changes
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module ActsAsPublicable
|
2
|
+
module Generators
|
3
|
+
|
4
|
+
class InstallGenerator < Rails::Generators::Base
|
5
|
+
source_root File.expand_path('../templates', __FILE__)
|
6
|
+
argument :name, :type => :string, :default => "publicables"
|
7
|
+
|
8
|
+
desc "install the common controller and the common helper in your app"
|
9
|
+
|
10
|
+
def copy_controller
|
11
|
+
template "controller.rb", "app/controllers/#{name.underscore}_controller.rb"
|
12
|
+
end
|
13
|
+
|
14
|
+
def copy_helper
|
15
|
+
template "helper.rb", "app/helpers/#{name.underscore}_helper.rb"
|
16
|
+
end
|
17
|
+
|
18
|
+
def add_route
|
19
|
+
route "put '/#{name.underscore.pluralize}/:id/:type' => '#{name.underscore.pluralize}#publish', :as => :publishable"
|
20
|
+
route "acts_as_publicable :#{name.underscore.singularize}"
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module ActsAsPublicable
|
2
|
+
module Generators
|
3
|
+
|
4
|
+
class MigrationGenerator < Rails::Generators::NamedBase
|
5
|
+
include Rails::Generators::Migration
|
6
|
+
source_root File.expand_path('../templates', __FILE__)
|
7
|
+
|
8
|
+
def manifest
|
9
|
+
migration_template 'migration.rb', "db/migrate/add_published_to_#{table_name}"
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.next_migration_number(path)
|
13
|
+
Time.now.utc.strftime("%Y%m%d%H%M%S")
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
class <%= name.camelize %>Controller < ApplicationController
|
2
|
+
|
3
|
+
def publish
|
4
|
+
@model = params[:type].classify.constantize.find(params[:id])
|
5
|
+
@model.publish!
|
6
|
+
redirect_to params[:url] || @model, :notice => I18n.t("helpers.#{params[:type]}.publish_message")
|
7
|
+
end
|
8
|
+
|
9
|
+
def unpublish
|
10
|
+
@model = params[:type].classify.constantize.find(params[:id])
|
11
|
+
@model.unpublish!
|
12
|
+
redirect_to params[:url] || @model, :notice => I18n.t("helpers.#{params[:type]}.unpublish_message")
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module <%= name.camelize %>Helper
|
2
|
+
|
3
|
+
def publish(model, options = {})
|
4
|
+
|
5
|
+
class_name = model.class.to_s.underscore
|
6
|
+
|
7
|
+
url_options = { :id => model.id, :type => class_name }
|
8
|
+
|
9
|
+
url_options[:url] = options[:url] if options[:url].present?
|
10
|
+
|
11
|
+
link_to(I18n.t("helpers.#{class_name}.publish"),<%= name %>_publish_path(url_options),:method=>:put)
|
12
|
+
end
|
13
|
+
|
14
|
+
def unpublish(model, options = {})
|
15
|
+
|
16
|
+
class_name = model.class.to_s.underscore
|
17
|
+
|
18
|
+
url_options = { :id => model.id, :type => class_name }
|
19
|
+
|
20
|
+
url_options[:url] = options[:url] if options[:url].present?
|
21
|
+
|
22
|
+
link_to(I18n.t("helpers.#{class_name}.unpublish"), <%= name %>_unpublish_path(url_options), :method=>:put)
|
23
|
+
end
|
24
|
+
|
25
|
+
def toggle_publish(model, options= {})
|
26
|
+
model.published ? unpublish(model, options) : publish(model, options)
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
File without changes
|
@@ -70,4 +70,12 @@ describe "acts_as_publicable" do
|
|
70
70
|
end
|
71
71
|
end
|
72
72
|
|
73
|
+
describe "#unpublish!" do
|
74
|
+
it "unpublish an article" do
|
75
|
+
@article = Article.create!(:title=>"Awesome article",:published => true)
|
76
|
+
@article.unpublish!
|
77
|
+
@article.should_not be_published
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
73
81
|
end
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: acts_as_publicable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.0.
|
5
|
+
version: 0.0.4
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- lucapette
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-04-
|
13
|
+
date: 2011-04-11 00:00:00 +02:00
|
14
14
|
default_executable:
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
@@ -69,10 +69,14 @@ files:
|
|
69
69
|
- acts_as_publicable.gemspec
|
70
70
|
- lib/acts_as_publicable.rb
|
71
71
|
- lib/acts_as_publicable/acts_as_publicable.rb
|
72
|
+
- lib/acts_as_publicable/routes.rb
|
72
73
|
- lib/acts_as_publicable/version.rb
|
73
|
-
- lib/generators/USAGE
|
74
|
-
- lib/generators/
|
75
|
-
- lib/generators/
|
74
|
+
- lib/generators/acts_as_publicable/USAGE
|
75
|
+
- lib/generators/acts_as_publicable/install_generator.rb
|
76
|
+
- lib/generators/acts_as_publicable/migration_generator.rb
|
77
|
+
- lib/generators/acts_as_publicable/templates/controller.rb
|
78
|
+
- lib/generators/acts_as_publicable/templates/helper.rb
|
79
|
+
- lib/generators/acts_as_publicable/templates/migration.rb
|
76
80
|
- spec/acts_as_publicable/acts_as_publicable_spec.rb
|
77
81
|
- spec/schema.rb
|
78
82
|
- spec/spec_helper.rb
|
@@ -1,13 +0,0 @@
|
|
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_published_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
|