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.
@@ -1,5 +1,6 @@
1
1
  require "active_record"
2
2
  require "acts_as_publicable/acts_as_publicable"
3
+ require "acts_as_publicable/routes"
3
4
 
4
5
  if defined?(ActiveRecord::Base)
5
6
  ActiveRecord::Base.send :include, ActsAsPublicable::ActiveRecordExtension
@@ -18,7 +18,12 @@ module ActsAsPublicable
18
18
 
19
19
  def publish!
20
20
  self.published = true
21
- save
21
+ save!
22
+ end
23
+
24
+ def unpublish!
25
+ self.published = false
26
+ save!
22
27
  end
23
28
 
24
29
  end
@@ -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
@@ -1,3 +1,3 @@
1
1
  module ActsAsPublicable
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -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
@@ -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.3
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-05 00:00:00 +02:00
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/acts_as_publicable_generator.rb
75
- - lib/generators/templates/migration.rb
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