active_metadata 0.2.0 → 0.2.1

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.
data/db/test.sqlite3 CHANGED
Binary file
@@ -1,7 +1,8 @@
1
1
  module ActiveMetadata
2
2
 
3
3
  CONFIG = File.exists?('config/active_metadata.yml') ? YAML.load_file('config/active_metadata.yml')[Rails.env] : {}
4
-
4
+ CONFIG['cache_expires_in'] ||= 60
5
+
5
6
  ## Define ModelMethods
6
7
  module Base
7
8
 
@@ -29,6 +30,14 @@ module ActiveMetadata
29
30
  end
30
31
 
31
32
  module InstanceMethods
33
+
34
+ def self.included(klass)
35
+ [:notes,:attachments,:history].each do |item|
36
+ klass.send(:define_method,"#{item.to_s}_cache_key".to_sym) do |field|
37
+ "active_metadata/#{item.to_s}/#{self.class}/#{metadata_id}/#{field}/"
38
+ end
39
+ end
40
+ end
32
41
 
33
42
  def metadata_id
34
43
  metadata_id_from = self.class.class_variable_get("@@metadata_id_from")
@@ -46,7 +55,7 @@ module ActiveMetadata
46
55
  else
47
56
  nil
48
57
  end
49
- end
58
+ end
50
59
 
51
60
  end # InstanceMethods
52
61
  end
@@ -8,17 +8,21 @@ module ActiveMetadata::Persistence::ActiveRecord::Attachment
8
8
 
9
9
  def save_attachment_for(field, file)
10
10
  attachment = Attachment.create! :document_id => metadata_id, :label => field, :attach => file, :created_by => current_user_id
11
+ reload_attachments_cache_for field
11
12
  self.send(:send_notification, field, "", attachment.attach.original_filename, :attachment_message, current_user_id)
12
13
  end
13
14
 
14
15
  def attachments_for(field)
15
- Attachment.all(:conditions => {:document_id => metadata_id,:label => field}, :order => "attach_updated_at DESC")
16
+ Rails.cache.fetch(attachments_cache_key(field), :expires_in => ActiveMetadata::CONFIG['cache_expires_in'].minutes) do
17
+ fetch_attachments_for field
18
+ end
16
19
  end
17
20
 
18
21
  def delete_attachment_for(field,id)
19
22
  a = Attachment.find(id)
20
23
  filename = a.attach.original_filename
21
24
  a.destroy
25
+ reload_attachments_cache_for field
22
26
  self.send(:send_notification, field, filename, "", :attachment_message)
23
27
  end
24
28
 
@@ -29,13 +33,25 @@ module ActiveMetadata::Persistence::ActiveRecord::Attachment
29
33
  a.updated_by = current_user_id
30
34
  a.save
31
35
  new_filename = a.attach.original_filename
32
-
36
+
37
+ reload_attachments_cache_for field
33
38
  self.send(:send_notification, field, old_filename, new_filename, :attachment_message, current_user_id)
34
39
  end
35
40
 
36
41
  def has_attachments_for field
37
- Attachment.count(:conditions => {:label => field, :document_id => metadata_id}) == 0 ? false : true
42
+ attachments_for(field).size == 0 ? false : true
38
43
  end
44
+
45
+ private
46
+
47
+ def reload_attachments_cache_for field
48
+ Rails.cache.write(attachments_cache_key(field),fetch_attachments_for(field), :expires_in => ActiveMetadata::CONFIG['cache_expires_in'].minutes )
49
+ end
50
+
51
+ def fetch_attachments_for field
52
+ Attachment.all(:conditions => {:document_id => metadata_id,:label => field}, :order => "attach_updated_at DESC")
53
+ end
54
+
39
55
 
40
56
  end
41
57
 
@@ -10,13 +10,27 @@ module ActiveMetadata::Persistence::ActiveRecord::History
10
10
  self.changes.each do |key, value|
11
11
  next if ActiveMetadata::CONFIG['history_skip_fields'].include?(key)
12
12
  History.create! :value => value[1],:document_id => metadata_id,:label => key.to_s, :created_by => current_user_id
13
+ invalidate_history_cache_for key.to_s
13
14
  self.send(:send_notification, key, value[0], value[1], :history_message,current_user_id)
14
15
  end
15
16
  end
16
17
 
17
18
  def history_for field
18
- History.all(:conditions => {:document_id => metadata_id,:label => field}, :order => "created_at DESC")
19
+ Rails.cache.fetch(history_cache_key(field), :expires_in => ActiveMetadata::CONFIG['cache_expires_in'].minutes) do
20
+ fetch_histories_for field
21
+ end
19
22
  end
20
23
 
24
+ private
25
+
26
+ def invalidate_history_cache_for field
27
+ Rails.cache.delete history_cache_key(field)
28
+ end
29
+
30
+ def fetch_histories_for field
31
+ History.all(:conditions => {:document_id => metadata_id,:label => field}, :order => "created_at DESC")
32
+ end
33
+
34
+
21
35
  end
22
36
  end
@@ -7,20 +7,23 @@ module ActiveMetadata::Persistence::ActiveRecord::Note
7
7
  module InstanceMethods
8
8
 
9
9
  def create_note_for(field, note)
10
- Note.create! :document_id => metadata_id,:label => field.to_s,:note => note, :created_by => current_user_id
10
+ Note.create! :document_id => metadata_id,:label => field.to_s,:note => note, :created_by => current_user_id
11
+ reload_notes_cache_for field
11
12
  self.send(:send_notification, field, "", note, :note_message, current_user_id)
12
13
  end
13
14
 
14
15
  def update_note(id, note)
15
16
  n = Note.find(id)
16
17
  old_value = n.note
17
- n.update_attributes! :note => note, :updated_by => current_user_id, :updated_at => Time.now.utc
18
-
18
+ n.update_attributes! :note => note, :updated_by => current_user_id, :updated_at => Time.now.utc
19
+ reload_notes_cache_for n.label
19
20
  self.send(:send_notification, n.label, old_value, note, :note_message, current_user_id)
20
21
  end
21
22
 
22
23
  def notes_for(field)
23
- Note.all(:conditions => {:label => field, :document_id => metadata_id}, :order => "updated_at DESC" )
24
+ Rails.cache.fetch(notes_cache_key(field), :expires_in => ActiveMetadata::CONFIG['cache_expires_in'].minutes) do
25
+ fetch_notes_for field
26
+ end
24
27
  end
25
28
 
26
29
  def note_for(field,id)
@@ -35,11 +38,23 @@ module ActiveMetadata::Persistence::ActiveRecord::Note
35
38
  n = Note.find(id)
36
39
  old_value = n.note
37
40
  n.destroy
41
+ reload_notes_cache_for field
38
42
  self.send(:send_notification, field, old_value, "", :note_message)
39
43
  end
40
44
 
41
- def has_notes_for field
42
- Note.count(:conditions => {:label => field, :document_id => metadata_id}) == 0 ? false : true
45
+ def has_notes_for field
46
+ notes_for(field).size == 0 ? false : true
43
47
  end
48
+
49
+ private
50
+
51
+ def reload_notes_cache_for field
52
+ Rails.cache.write(notes_cache_key(field),fetch_notes_for(field), :expires_in => ActiveMetadata::CONFIG['cache_expires_in'].minutes )
53
+ end
54
+
55
+ def fetch_notes_for field
56
+ Note.all(:conditions => {:label => field, :document_id => metadata_id}, :order => "updated_at DESC" )
57
+ end
58
+
44
59
  end
45
60
  end
@@ -1,3 +1,3 @@
1
1
  module ActiveMetadata
2
- VERSION = "0.2.0"
2
+ VERSION = "0.2.1"
3
3
  end
@@ -314,7 +314,6 @@ describe ActiveMetadata do
314
314
  it "should update an attachment" do
315
315
  @document.save_attachment_for(:name,@attachment)
316
316
  att = @document.attachments_for(:name).last
317
-
318
317
  @document.update_attachment_for :name,att.id,@attachment2
319
318
  att2 = @document.attachments_for(:name).last
320
319
 
data/spec/spec_helper.rb CHANGED
@@ -5,6 +5,7 @@ require "logger"
5
5
  require 'rspec/core'
6
6
  require "sqlite3"
7
7
  require "rack/test/uploaded_file"
8
+ require 'active_support/cache'
8
9
 
9
10
  $: << File.expand_path(File.dirname(__FILE__) + "/../app")
10
11
  gemfile = File.expand_path('../Gemfile', __FILE__)
@@ -25,6 +26,7 @@ ENV["ACTIVE_METADATA_ENV"] ||= 'test'
25
26
  ActiveRecord::Base.establish_connection YAML.load_file("config/database.yml")[ENV["RAILS_ENV"]]
26
27
  ActiveRecord::Base.logger = Logger.new "log/test.log"
27
28
  Rails.logger = ActiveRecord::Base.logger
29
+ RAILS_CACHE = ActiveSupport::Cache::MemoryStore.new
28
30
 
29
31
  # loading ruby files
30
32
  require "#{File.dirname(__FILE__)}/../lib/engine.rb"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_metadata
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,11 +10,11 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2011-10-11 00:00:00.000000000Z
13
+ date: 2011-10-13 00:00:00.000000000Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rspec-rails
17
- requirement: &2156180700 !ruby/object:Gem::Requirement
17
+ requirement: &2154095180 !ruby/object:Gem::Requirement
18
18
  none: false
19
19
  requirements:
20
20
  - - ! '>='
@@ -22,10 +22,10 @@ dependencies:
22
22
  version: '0'
23
23
  type: :development
24
24
  prerelease: false
25
- version_requirements: *2156180700
25
+ version_requirements: *2154095180
26
26
  - !ruby/object:Gem::Dependency
27
27
  name: sqlite3
28
- requirement: &2156178420 !ruby/object:Gem::Requirement
28
+ requirement: &2154154560 !ruby/object:Gem::Requirement
29
29
  none: false
30
30
  requirements:
31
31
  - - ! '>='
@@ -33,10 +33,10 @@ dependencies:
33
33
  version: '0'
34
34
  type: :development
35
35
  prerelease: false
36
- version_requirements: *2156178420
36
+ version_requirements: *2154154560
37
37
  - !ruby/object:Gem::Dependency
38
38
  name: sqlite3-ruby
39
- requirement: &2156154920 !ruby/object:Gem::Requirement
39
+ requirement: &2154205940 !ruby/object:Gem::Requirement
40
40
  none: false
41
41
  requirements:
42
42
  - - ! '>='
@@ -44,10 +44,10 @@ dependencies:
44
44
  version: '0'
45
45
  type: :development
46
46
  prerelease: false
47
- version_requirements: *2156154920
47
+ version_requirements: *2154205940
48
48
  - !ruby/object:Gem::Dependency
49
49
  name: cucumber
50
- requirement: &2156100000 !ruby/object:Gem::Requirement
50
+ requirement: &2154241320 !ruby/object:Gem::Requirement
51
51
  none: false
52
52
  requirements:
53
53
  - - ! '>='
@@ -55,10 +55,10 @@ dependencies:
55
55
  version: '0'
56
56
  type: :development
57
57
  prerelease: false
58
- version_requirements: *2156100000
58
+ version_requirements: *2154241320
59
59
  - !ruby/object:Gem::Dependency
60
60
  name: ci_reporter
61
- requirement: &2156079680 !ruby/object:Gem::Requirement
61
+ requirement: &2154310080 !ruby/object:Gem::Requirement
62
62
  none: false
63
63
  requirements:
64
64
  - - ! '>='
@@ -66,10 +66,10 @@ dependencies:
66
66
  version: '0'
67
67
  type: :development
68
68
  prerelease: false
69
- version_requirements: *2156079680
69
+ version_requirements: *2154310080
70
70
  - !ruby/object:Gem::Dependency
71
71
  name: rails
72
- requirement: &2156049960 !ruby/object:Gem::Requirement
72
+ requirement: &2154507860 !ruby/object:Gem::Requirement
73
73
  none: false
74
74
  requirements:
75
75
  - - =
@@ -77,10 +77,10 @@ dependencies:
77
77
  version: 3.0.1
78
78
  type: :runtime
79
79
  prerelease: false
80
- version_requirements: *2156049960
80
+ version_requirements: *2154507860
81
81
  - !ruby/object:Gem::Dependency
82
82
  name: activerecord
83
- requirement: &2156032700 !ruby/object:Gem::Requirement
83
+ requirement: &2154597420 !ruby/object:Gem::Requirement
84
84
  none: false
85
85
  requirements:
86
86
  - - =
@@ -88,10 +88,10 @@ dependencies:
88
88
  version: 3.0.1
89
89
  type: :runtime
90
90
  prerelease: false
91
- version_requirements: *2156032700
91
+ version_requirements: *2154597420
92
92
  - !ruby/object:Gem::Dependency
93
93
  name: paperclip
94
- requirement: &2156029320 !ruby/object:Gem::Requirement
94
+ requirement: &2154656840 !ruby/object:Gem::Requirement
95
95
  none: false
96
96
  requirements:
97
97
  - - ! '>='
@@ -99,10 +99,10 @@ dependencies:
99
99
  version: '0'
100
100
  type: :runtime
101
101
  prerelease: false
102
- version_requirements: *2156029320
102
+ version_requirements: *2154656840
103
103
  - !ruby/object:Gem::Dependency
104
104
  name: to_xls
105
- requirement: &2156024620 !ruby/object:Gem::Requirement
105
+ requirement: &2154806000 !ruby/object:Gem::Requirement
106
106
  none: false
107
107
  requirements:
108
108
  - - ! '>='
@@ -110,7 +110,7 @@ dependencies:
110
110
  version: '0'
111
111
  type: :runtime
112
112
  prerelease: false
113
- version_requirements: *2156024620
113
+ version_requirements: *2154806000
114
114
  description: First implementation will write metadata on mongodb
115
115
  email:
116
116
  - acampolonghi@gmail.com