review_and_approve 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -60,16 +60,19 @@ review_and_approve :cache_key => Proc.new{|object, method_name| #Generate key st
60
60
  ```
61
61
 
62
62
  ### Showing differences from published version
63
- A method called `published_version` is defined on the model that provides access to cached methods as of the last published version. For example:
63
+ Methods called `published_version` and `current_version` are defined on the model that provides access to cached methods as of the last published or current versions. For example:
64
64
 
65
65
  ```ruby
66
66
  @product.published_version(:as_json)
67
67
  # => returns the as_json hash from the last time the product was published
68
68
 
69
+ @product.current_version(:as_json)
70
+ # => returns the as_json hash for the current iteration. It is advisable to use this to avoid getting bogged down by changes to date/time and other fields because of just saving and retrieving from your cache(i.e. database)
71
+
69
72
  # Rendering the differences
70
73
  render :partial => "review_and_approve/delta",
71
74
  :locals => {:published => @product.published_version(:as_json),
72
- :current => @product.as_json}
75
+ :current => @product.current_version(:as_json)}
73
76
  # Given two hashes (before/after), this will render the changes in a table
74
77
 
75
78
  #styling the differences
@@ -1,5 +1,6 @@
1
1
  <% published ||= {} %>
2
- <% diff_keys = published.diff(current).keys %>
2
+ <% diff = ReviewAndApprove::HashDiff.diff(published,current) %>
3
+ <% diff_keys = diff.keys %>
3
4
  <div class="review_and_approve">
4
5
  <% if diff_keys.count > 0 %>
5
6
  <table>
@@ -13,14 +14,7 @@
13
14
 
14
15
  <tbody>
15
16
  <% diff_keys.each do |key| %>
16
- <tr>
17
- <td><%= key rescue "" %></td>
18
- <%# if !published.is_a? Hash and !published.is_a? Array %>
19
- <td><%= published || "<BLANK>" %></td>
20
- <td><%= current || "<BLANK>" %></td>
21
- <%# end %>
22
- </tr>
23
- <%#= render :partial => 'review_and_approve/line_item', :locals => {:published => published[key], :current => current[key], :key => key, :depth => 0} %>
17
+ <%= render :partial => 'review_and_approve/line_item', :locals => {:key => key, :diff_val => diff[key], :depth => -1} %>
24
18
  <% end %>
25
19
  </tbody>
26
20
 
@@ -1,30 +1,19 @@
1
- <% puts "in line item with" %>
2
- <% puts "published:#{published rescue ""}" %>
3
- <% puts "current:#{current rescue ""}" %>
4
- <% puts "key:#{key rescue ""}" %>
5
- <% current ||= {} %>
1
+
6
2
  <tr>
7
3
  <% (0..depth).each do %>
8
4
  <td></td>
9
5
  <% end %>
10
6
  <td><%= key rescue "" %></td>
11
- <% if !published.is_a? Hash and !published.is_a? Array %>
12
- <td><%= published || "<BLANK>" %></td>
13
- <td><%= current || "<BLANK>" %></td>
7
+ <% if diff_val.is_a? Array %>
8
+ <td><%= diff_val[0] || "<BLANK>" %></td>
9
+ <td><%= diff_val[1] || "<BLANK>" %></td>
14
10
  <% end %>
15
11
  </tr>
16
- <% if published.is_a? Array %>
17
- <% published.each do |item| %>
18
- <% if current. %>
19
- <% pub = (published.include? item) ? item : nil %>
20
- <% cur = (current.include? item) ? item : nil %>
21
- <%= render :partial => 'review_and_approve/line_item',
22
- :locals => {:depth => depth+1, :published => pub, :current => cur} %>
23
- <% end %>
12
+ <% if diff_val.is_a? Hash %>
13
+ <% diff_val.keys.each do |key| %>
14
+ <%= render :partial => 'review_and_approve/line_item', :locals => {:key => key, :diff_val => diff_key[key], :depth => depth+1} %>
24
15
  <% end %>
25
- <% end %>
26
- <% if published.is_a? Hash %>
27
- <% published.diff(current).keys.each do |key| %>
28
- <%= render :partial => 'review_and_approve/line_item', :locals => {:published => published[key], :current => current[key], :key => key, :depth => depth+1} %>
29
- <% end %>
30
- <% end %>
16
+ <% elsif !diff_val.is_a? Array or diff_val.length>2 %>
17
+ <% debugger %>
18
+ <% raise Exception.new %>
19
+ <% end %>
@@ -0,0 +1,18 @@
1
+ module ReviewAndApprove
2
+ module HashDiff
3
+ def diff(orig, other)
4
+ (orig.keys + other.keys).uniq.inject({}) do |memo, key|
5
+ unless orig[key] == other[key]
6
+ if orig[key].kind_of?(Hash) && other[key].kind_of?(Hash)
7
+ memo[key] = ReviewAndApprove::HashDiff.diff(orig[key], other[key])
8
+ else
9
+ memo[key] = [orig[key], other[key]]
10
+ end
11
+ end
12
+ memo
13
+ end
14
+ end
15
+
16
+ module_function :diff
17
+ end
18
+ end
@@ -36,20 +36,32 @@ module ReviewAndApprove
36
36
  #If we are publishing the record
37
37
  if published and (published==true or published=="true" or published=="on" or self.send(field).to_i>0 rescue false) #in case the field gets set to "0" and "1"
38
38
  methods.each do |method|
39
- # Refresh all caches
40
- cr = CacheRecord.find_or_initialize_by_key(key_proc.call(self, method))
39
+ # Refresh published cache
40
+ cr = CacheRecord.find_or_initialize_by_key("#{key_proc.call(self, method)}_published_version")
41
41
  cr.cache_data = self.send(method)
42
42
  cr.save
43
43
  end
44
44
  end
45
45
 
46
+ methods.each do |method|
47
+ #Refresh current value cache
48
+ cr = CacheRecord.find_or_initialize_by_key("#{key_proc.call(self, method)}_current_version")
49
+ cr.cache_data = self.send(method)
50
+ cr.save
51
+ end
52
+
46
53
  true
47
54
  end
48
55
 
49
56
  send(:define_method, :published_version) do |method_name|
50
- CacheRecord.find_by_key(key_proc.call(self, method_name)).cache_data rescue nil
57
+ CacheRecord.find_by_key("#{key_proc.call(self, method_name)}_published_version").cache_data rescue nil
51
58
  end
52
59
 
60
+ send(:define_method, :current_version) do |method_name|
61
+ CacheRecord.find_by_key("#{key_proc.call(self, method_name)}_current_version").cache_data rescue nil
62
+ end
63
+
64
+
53
65
  send(:define_method, :mass_assignment_authorizer) do |role = :default|
54
66
  # force add the :publish attribute into attr_accessible
55
67
  super(role) + [field]
@@ -1,3 +1,3 @@
1
1
  module ReviewAndApprove
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
3
3
  end
@@ -5,6 +5,7 @@ require "review_and_approve/controller_additions"
5
5
  require "review_and_approve/railtie" if defined? Rails
6
6
  require 'active_record'
7
7
  require "review_and_approve/cache_record"
8
+ require 'review_and_approve/hash_diff'
8
9
 
9
10
  module ReviewAndApprove
10
11
 
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: review_and_approve
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.5
5
+ version: 0.0.6
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-23 00:00:00 Z
13
+ date: 2013-05-29 00:00:00 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rspec
@@ -119,6 +119,7 @@ files:
119
119
  - lib/review_and_approve/cache_record.rb
120
120
  - lib/review_and_approve/controller_additions.rb
121
121
  - lib/review_and_approve/engine.rb
122
+ - lib/review_and_approve/hash_diff.rb
122
123
  - lib/review_and_approve/model_additions.rb
123
124
  - lib/review_and_approve/railtie.rb
124
125
  - lib/review_and_approve/version.rb