acts_as_approvable 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Jay Hayes
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.
@@ -0,0 +1,38 @@
1
+ = ActsAsApprovable
2
+
3
+ +acts_as_approvable+ is an ActiveRecord extension that allows you to flag models as pending
4
+ approval. One use of this would be so that changes would not show up in your views until the
5
+ model has been "approved".
6
+
7
+ == Installation
8
+ In +Gemfile+:
9
+
10
+ gem 'acts_as_approvable', :git => 'https://github.com/iamvery/acts_as_approvable.git'
11
+
12
+ In your application root, run:
13
+
14
+ $ bundle install
15
+
16
+ Generate the migration (warning: will overwrite any table named +Approvals+):
17
+
18
+ $ rails g acts_as_approvable:install
19
+
20
+ == Usage
21
+ Declare +acts_as_approvable+ on your models:
22
+
23
+ class Thing < ActiveRecord::Base
24
+ acts_as_approvable
25
+ end
26
+
27
+ Doing so will add the following methods to your model:
28
+
29
+ * thing.pending?
30
+ * thing.approved?
31
+ * thing.approve!
32
+ * thing.disapprove!
33
+
34
+ Both +pending?+ and +approved?+ exist as convenience methods. +approve!+ and +disapprove!+
35
+ will modify the +Approval+ model that your +acts_as_approvable+ model has.
36
+
37
+ == Compatibility
38
+ This gem is designed to work with Rails 3.x.
@@ -0,0 +1,10 @@
1
+ # encoding: UTF-8
2
+ require 'rubygems'
3
+ begin
4
+ require 'bundler/setup'
5
+ rescue LoadError
6
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
7
+ end
8
+
9
+ require 'rake'
10
+ task :default => :spec
@@ -0,0 +1,4 @@
1
+ require 'active_record'
2
+ require 'acts_as_approvable/approver'
3
+ require 'acts_as_approvable/version'
4
+ ActiveRecord::Base.send :include, ActsAsApprovable::Approver
@@ -0,0 +1,12 @@
1
+ class Approval < ActiveRecord::Base
2
+ belongs_to :approvable, :polymorphic => true
3
+ belongs_to :approver, :polymorphic => true
4
+
5
+ validates :approvable_id, :approvable_type, :presence => true
6
+ validates :approvable_id, :uniqueness => {:scope => :approvable_type}
7
+
8
+ # Scoped wrapped in lambdas because ActiveRecord's connection hasn't been established at the
9
+ # time of this classes' load.
10
+ scope :pending, lambda{ where(:approved => false) }
11
+ scope :approved_today, lambda{ where(:approved => true).where(arel_table[:updated_at].eq(Date.today)) }
12
+ end
@@ -0,0 +1,58 @@
1
+ require 'acts_as_approvable/approval'
2
+
3
+ module ActsAsApprovable
4
+ module Approver
5
+ def self.included(base)
6
+ base.extend ClassMethods
7
+ end
8
+
9
+ module ClassMethods
10
+ # Creates associations with +Approvals+ and add instance methods for getting approval status
11
+ def acts_as_approvable
12
+ # don't allow multiple calls
13
+ return if included_modules.include?(ActsAsApprovable::Approver::InstanceMethods)
14
+
15
+ # association with approval
16
+ has_one :approval, :as => :approvable
17
+
18
+ # access to all models that have been approved
19
+ scope :approved, joins(:approval).where(Approval.arel_table[:approved].eq(true))
20
+
21
+ # make sure every approvable model has an associated approval
22
+ after_create :create_pending_approval
23
+
24
+ include ActsAsApprovable::Approver::InstanceMethods
25
+ end
26
+ end
27
+
28
+ module InstanceMethods
29
+ def approved?
30
+ !!approval.try(:approved?)
31
+ end
32
+
33
+ def pending?
34
+ !approved?
35
+ end
36
+
37
+ def approve!(who = nil)
38
+ create_pending_approval if approval.nil?
39
+ approval.update_attributes :approved => true, :approver => who || nil if pending?
40
+ end
41
+
42
+ def disapprove!(who = nil)
43
+ create_pending_approval if approval.nil?
44
+ approval.update_attributes :approved => false, :approver => who || nil if approved?
45
+ end
46
+
47
+ def approver
48
+ approval.try :approver
49
+ end
50
+
51
+ private
52
+
53
+ def create_pending_approval
54
+ self.approval = Approval.create :approvable => self
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,10 @@
1
+ module ActsAsApprovable
2
+ module Version
3
+ MAJOR = 0
4
+ MINOR = 1
5
+ TINY = 0
6
+ PRE = nil
7
+
8
+ STRING = [MAJOR, MINOR, TINY, PRE].compact.join('.')
9
+ end
10
+ end
@@ -0,0 +1,28 @@
1
+ require 'rails/generators'
2
+ require 'rails/generators/migration'
3
+ require 'active_record'
4
+ require 'rails/generators/active_record'
5
+
6
+ module ActsAsApprovable
7
+ module Generators
8
+ class InstallGenerator < Rails::Generators::Base
9
+ include Rails::Generators::Migration
10
+
11
+ source_root File.expand_path("../templates", __FILE__)
12
+
13
+ # Implement the required interface for Rails::Generators::Migration.
14
+ def self.next_migration_number(dirname) #:nodoc:
15
+ next_migration_number = current_migration_number(dirname) + 1
16
+ if ActiveRecord::Base.timestamped_migrations
17
+ [Time.now.utc.strftime("%Y%m%d%H%M%S"), "%.14d" % next_migration_number].max
18
+ else
19
+ "%.3d" % next_migration_number
20
+ end
21
+ end
22
+
23
+ def copy_migration
24
+ migration_template 'install.rb', 'db/migrate/install_acts_as_approvable.rb'
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,16 @@
1
+ class <%= migration_class_name %> < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :approvals, :force => true do |t|
4
+ t.references :approvable, :polymorphic => true, :null => false
5
+ t.references :approver, :polymorphic => true
6
+ t.boolean :approved, :default => false
7
+ t.timestamps
8
+ end
9
+ add_index :approvals, [:approvable_id, :approvable_type], :unique => true
10
+ end
11
+
12
+ def self.down
13
+ remove_index :approvals, :column => [:approvable_id, :approvable_type]
14
+ drop_table :approvals
15
+ end
16
+ end
metadata ADDED
@@ -0,0 +1,87 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: acts_as_approvable
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.1.0
6
+ platform: ruby
7
+ authors:
8
+ - Jay Hayes
9
+ - Christoph Lupprich
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+
14
+ date: 2011-12-02 00:00:00 -06:00
15
+ default_executable:
16
+ dependencies:
17
+ - !ruby/object:Gem::Dependency
18
+ name: rails
19
+ prerelease: false
20
+ requirement: &id001 !ruby/object:Gem::Requirement
21
+ none: false
22
+ requirements:
23
+ - - ~>
24
+ - !ruby/object:Gem::Version
25
+ version: "3"
26
+ type: :runtime
27
+ version_requirements: *id001
28
+ - !ruby/object:Gem::Dependency
29
+ name: rake
30
+ prerelease: false
31
+ requirement: &id002 !ruby/object:Gem::Requirement
32
+ none: false
33
+ requirements:
34
+ - - ">="
35
+ - !ruby/object:Gem::Version
36
+ version: "0"
37
+ type: :development
38
+ version_requirements: *id002
39
+ description: Make an ActiveRecord model approvable.
40
+ email:
41
+ executables: []
42
+
43
+ extensions: []
44
+
45
+ extra_rdoc_files: []
46
+
47
+ files:
48
+ - lib/acts_as_approvable/approval.rb
49
+ - lib/acts_as_approvable/approver.rb
50
+ - lib/acts_as_approvable/version.rb
51
+ - lib/acts_as_approvable.rb
52
+ - lib/generators/acts_as_approvable/install_generator.rb
53
+ - lib/generators/acts_as_approvable/templates/install.rb
54
+ - lib/generators/acts_as_approvable/USAGE
55
+ - MIT-LICENSE
56
+ - Rakefile
57
+ - README.rdoc
58
+ has_rdoc: true
59
+ homepage: https://github.com/iamvery/acts_as_approvable
60
+ licenses: []
61
+
62
+ post_install_message:
63
+ rdoc_options: []
64
+
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ none: false
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: "0"
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: "0"
79
+ requirements: []
80
+
81
+ rubyforge_project:
82
+ rubygems_version: 1.6.2
83
+ signing_key:
84
+ specification_version: 3
85
+ summary: Make an ActiveRecord model approvable.
86
+ test_files: []
87
+