review_and_approve 0.0.1.alpha → 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -18,14 +18,56 @@ Or install it yourself as:
18
18
 
19
19
  ## Usage
20
20
 
21
+ ### Setup
21
22
  call `review_and_approve` in an ActiveRecord class
22
23
 
23
24
  ```ruby
24
25
  class Product < ActiveRecord::Base
25
- review_and_approve :by => [:as_json, :to_json] #defaults to :by => [:as_json] - list of methods whose output will be cached for delta comparisons
26
+ review_and_approve
27
+ attr_accessible :field1, :field2.. #Make sure attr_accessible is defined properly
26
28
  end
27
29
  ```
28
30
 
31
+ Use Cancan or other authorization mechanism to define a custom ability to 'publish' the records
32
+ Make sure your controller has access to current_ability method and it returns true or false on can? :publish, @product
33
+
34
+ In your controller and views, allow the users to set the published flag on the model when you are ready to publish
35
+
36
+ #### Customizations
37
+
38
+ ```ruby
39
+ review_and_approve :by => [:as_json, :to_json]
40
+ # defaults to :by => [:as_json]
41
+ # list of methods whose output will be cached for delta comparisons
42
+
43
+ review_and_approve :field => :published
44
+ # Override the field used to track whether we are publishing or not.
45
+ ```
46
+
47
+ ### Showing differences from published version
48
+ A method called `published_version` is defined on the model that provides access to cached methods as of the last published version. For example:
49
+
50
+ ```ruby
51
+ @product.published_version(:as_json)
52
+ # => returns the as_json hash from the last time the product was published
53
+
54
+ # Rendering the differences
55
+ render :partial => "review_and_approve/delta",
56
+ :locals => {:published => @product.published_version(:as_json),
57
+ :current => @product.as_json}
58
+ # Given two hashes (before/after), this will render the changes in a table
59
+
60
+ #styling the differences
61
+ Check out delta.css in the gem and override the styles in your application
62
+ ```
63
+
64
+ ## Limitations
65
+ * only certified on Rails 3.1, ruby 1.9.2
66
+ * Will use Rails.cache to store the output of methods provided to review_and_approve. We currently assume that the cache has infinite space -i.e. the cached value is never lost
67
+ - possibly use the database in future for storing cached values
68
+ * Currently depends on the application to call attr_accessible
69
+
70
+
29
71
  ## Contributing
30
72
 
31
73
  1. Fork it
@@ -1,8 +1,3 @@
1
- .review_and_approve table tr td{
2
- border: 1px solid black;
3
- }
4
-
5
-
6
1
  .review_and_approve
7
2
  {
8
3
  font-family: "Lucida Sans Unicode", "Lucida Grande", Sans-Serif;
@@ -1,6 +1,7 @@
1
1
  require "review_and_approve/version"
2
- require "review_and_approve/engine"
2
+ require "review_and_approve/engine" if defined? Rails
3
3
  require "review_and_approve/model_additions"
4
+ require "review_and_approve/controller_additions"
4
5
  require "review_and_approve/railtie" if defined? Rails
5
6
 
6
7
  module ReviewAndApprove
@@ -0,0 +1,13 @@
1
+ module ReviewAndApprove
2
+ module ControllerAdditions
3
+ def self.extended(base)
4
+ base.send :define_method, :review_ability do |&block|
5
+ Thread.current[:reviewAndApprove_current_ability] = self.send :current_ability
6
+ block.call
7
+ end
8
+
9
+ base.around_filter :review_ability
10
+ end
11
+
12
+ end
13
+ end
@@ -1,42 +1,54 @@
1
1
  module ReviewAndApprove
2
2
  module ModelAdditions
3
- class ModelValidator < ActiveModel::Validator
4
- def validate(record)
5
- if Thread.current[:reviewAndApprove_current_ability].cannot?(:publish, record) and record.published
6
- record.errors[:published] << "can not be marked as true by this user"
7
- end
8
- end
9
- end
10
-
11
3
  def review_and_approve(*args)
4
+ # Extracting options:
5
+ # 1. methods to cache - option :by, default value [:as_json]
6
+ # 2. attribute to track as published - option :field, default value :publish
12
7
  methods = [:as_json]
8
+ field = :publish
13
9
  args.each do |arg|
14
10
  if arg.is_a? Hash
15
- if arg[:by].present?
11
+ if !arg[:by].nil?
16
12
  methods = arg[:by]
17
13
  end
14
+ if !arg[:field].nil?
15
+ field = arg[:field]
16
+ end
18
17
  end
19
18
  end
20
- before_save do
21
- if Thread.current[:reviewAndApprove_current_ability].cannot? :publish, self
22
- self.published = false
23
- end
24
- true
25
- end
19
+
20
+ # define the field as an attribute on the model
21
+ attr_accessor field
26
22
 
27
23
  after_save do
28
- if self.published
24
+ published = self.send(field)
25
+ #If we are publishing the record
26
+ if published and (published==true or published=="true" or self.send(field).to_i>0 rescue false) #in case the field gets set to "0" and "1"
29
27
  methods.each do |method|
28
+ # Refresh all caches
30
29
  Rails.cache.write("ReviewAndApprove_#{self.class.name}_#{self.id}_#{method}", self.send(method))
31
30
  end
32
31
  end
32
+
33
+ true
33
34
  end
34
35
 
35
36
  send(:define_method, :published_version) do |method_name|
36
37
  Rails.cache.read("ReviewAndApprove_#{self.class.name}_#{self.id}_#{method_name}")
37
38
  end
38
39
 
39
- send(:validates_with, ReviewAndApprove::ModelAdditions::ModelValidator)
40
+ send(:define_method, :mass_assignment_authorizer) do |role = :default|
41
+ # force add the :publish attribute into attr_accessible
42
+ super(role) + [field]
43
+ end
44
+
45
+ validates_each field do |record, attr, value|
46
+ able = Thread.current[:reviewAndApprove_current_ability].try(:can?, :publish, record)
47
+ # if user can not publish the record, create an error.
48
+ if !able and value and (value==true or value=="true" or value.to_i>0 rescue false)
49
+ record.errors[attr] << "can not be marked as true by this user"
50
+ end
51
+ end
40
52
  end
41
53
  end
42
54
  end
@@ -5,5 +5,11 @@ module ReviewAndApprove
5
5
  extend ModelAdditions
6
6
  end
7
7
  end
8
+
9
+ initializer "review_and_approve.contoller_additions" do
10
+ ActiveSupport.on_load(:action_controller) do
11
+ extend ReviewAndApprove::ControllerAdditions # ActiveSupport::Concern
12
+ end
13
+ end
8
14
  end
9
15
  end
@@ -1,3 +1,3 @@
1
1
  module ReviewAndApprove
2
- VERSION = "0.0.1.alpha"
2
+ VERSION = "0.0.1"
3
3
  end
metadata CHANGED
@@ -1,8 +1,8 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: review_and_approve
3
3
  version: !ruby/object:Gem::Version
4
- prerelease: 6
5
- version: 0.0.1.alpha
4
+ prerelease:
5
+ version: 0.0.1
6
6
  platform: ruby
7
7
  authors:
8
8
  - Paramveer Singh
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2013-05-15 00:00:00 Z
13
+ date: 2013-05-18 00:00:00 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rspec
@@ -29,11 +29,66 @@ dependencies:
29
29
  requirement: &id002 !ruby/object:Gem::Requirement
30
30
  none: false
31
31
  requirements:
32
- - - "="
32
+ - - ">="
33
33
  - !ruby/object:Gem::Version
34
- version: 0.8.7
34
+ version: "0"
35
35
  type: :development
36
36
  version_requirements: *id002
37
+ - !ruby/object:Gem::Dependency
38
+ name: supermodel
39
+ prerelease: false
40
+ requirement: &id003 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: "0"
46
+ type: :runtime
47
+ version_requirements: *id003
48
+ - !ruby/object:Gem::Dependency
49
+ name: mocha
50
+ prerelease: false
51
+ requirement: &id004 !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: "0"
57
+ type: :development
58
+ version_requirements: *id004
59
+ - !ruby/object:Gem::Dependency
60
+ name: rails
61
+ prerelease: false
62
+ requirement: &id005 !ruby/object:Gem::Requirement
63
+ none: false
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: "0"
68
+ type: :runtime
69
+ version_requirements: *id005
70
+ - !ruby/object:Gem::Dependency
71
+ name: cancan
72
+ prerelease: false
73
+ requirement: &id006 !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: "0"
79
+ type: :runtime
80
+ version_requirements: *id006
81
+ - !ruby/object:Gem::Dependency
82
+ name: debugger
83
+ prerelease: false
84
+ requirement: &id007 !ruby/object:Gem::Requirement
85
+ none: false
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: "0"
90
+ type: :development
91
+ version_requirements: *id007
37
92
  description: Adds Review and Approval functionality for content sites.
38
93
  email:
39
94
  - paramveer.singh@hikeezee.com
@@ -47,6 +102,7 @@ files:
47
102
  - app/assets/stylesheets/review_and_approve/application.css
48
103
  - app/assets/stylesheets/review_and_approve/delta.css
49
104
  - app/views/review_and_approve/_delta.html.erb
105
+ - lib/review_and_approve/controller_additions.rb
50
106
  - lib/review_and_approve/engine.rb
51
107
  - lib/review_and_approve/model_additions.rb
52
108
  - lib/review_and_approve/railtie.rb
@@ -71,9 +127,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
71
127
  required_rubygems_version: !ruby/object:Gem::Requirement
72
128
  none: false
73
129
  requirements:
74
- - - ">"
130
+ - - ">="
75
131
  - !ruby/object:Gem::Version
76
- version: 1.3.1
132
+ version: "0"
77
133
  requirements: []
78
134
 
79
135
  rubyforge_project: