mongoid_ext 0.6.1 → 0.6.2

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -5,6 +5,7 @@ gem 'uuidtools', '>= 2.1.1'
5
5
  gem 'i18n'
6
6
  gem 'tzinfo'
7
7
  gem 'differ', '>= 0.1.2'
8
+ gem 'encryptor', '~> 1.1.3'
8
9
 
9
10
  group :development do
10
11
  gem "yard", "~> 0.6.0"
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.6.1
1
+ 0.6.2
@@ -16,6 +16,7 @@ rescue LoadError
16
16
  end
17
17
 
18
18
  require 'mongoid_ext/patches'
19
+ require 'mongoid_ext/encryptor'
19
20
 
20
21
  # types
21
22
  require 'mongoid_ext/types/open_struct'
@@ -0,0 +1,33 @@
1
+ module MongoidExt
2
+ module Encryptor
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ require 'encryptor'
7
+ end
8
+
9
+ module ClassMethods
10
+ def encrypted_field(name, options = {})
11
+ key = options.delete(:key)
12
+ raise ArgumentError, ":key option must be given" if key.nil?
13
+
14
+ field(name, options)
15
+ alias_method :"#{name}_encrypted", name
16
+
17
+ define_method(name) do
18
+ value = [send(:"#{name}_encrypted").to_s].pack('H*')
19
+
20
+ return if value.blank?
21
+ Marshal.load(::Encryptor.decrypt(value, :key => key))
22
+ end
23
+
24
+ define_method("#{name}=") do |v|
25
+ marshaled = Marshal.dump(v)
26
+ enc_value = ::Encryptor.encrypt(marshaled, :key => key).unpack('H*')[0]
27
+
28
+ write_attribute(name, enc_value)
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -41,7 +41,11 @@ module MongoidExt
41
41
  end
42
42
 
43
43
  def get
44
- @io ||= gridfs.get(grid_filename)
44
+ @io ||= begin
45
+ gridfs.get(grid_filename)
46
+ rescue Mongo::GridFileNotFound
47
+ return nil
48
+ end
45
49
  end
46
50
 
47
51
  def reset
@@ -33,7 +33,9 @@ module Versioning
33
33
  end
34
34
 
35
35
  @rolling_back = true
36
- save!
36
+ r = save!
37
+ @rolling_back = false
38
+ r
37
39
  end
38
40
 
39
41
  def load_version(pos = nil)
@@ -51,9 +53,30 @@ module Versioning
51
53
  version1 = self.version_at(pos1)
52
54
  version2 = self.version_at(pos2)
53
55
 
56
+ Differ.diff(version1.content(key), version2.content(key)).format_as(format).html_safe
57
+ end
58
+
59
+ def diff_by_word(key, pos1, pos2, format = :html)
60
+ version1 = self.version_at(pos1)
61
+ version2 = self.version_at(pos2)
62
+
54
63
  Differ.diff_by_word(version1.content(key), version2.content(key)).format_as(format).html_safe
55
64
  end
56
65
 
66
+ def diff_by_line(key, pos1, pos2, format = :html)
67
+ version1 = self.version_at(pos1)
68
+ version2 = self.version_at(pos2)
69
+
70
+ Differ.diff_by_line(version1.content(key), version2.content(key)).format_as(format).html_safe
71
+ end
72
+
73
+ def diff_by_char(key, pos1, pos2, format = :html)
74
+ version1 = self.version_at(pos1)
75
+ version2 = self.version_at(pos2)
76
+
77
+ Differ.diff_by_char(version1.content(key), version2.content(key)).format_as(format).html_safe
78
+ end
79
+
57
80
  def current_version
58
81
  version_klass.new(:data => self.attributes, self.class.versionable_options[:owner_field] => (self.updated_by_id_was || self.updated_by_id), :created_at => Time.now)
59
82
  end
@@ -107,7 +130,7 @@ module Versioning
107
130
  validates_presence_of :target_id
108
131
 
109
132
  def content(key)
110
- cdata = self.data[key]
133
+ cdata = self.data[key.to_s]
111
134
  if cdata.respond_to?(:join)
112
135
  cdata.join(" ")
113
136
  else
@@ -162,10 +185,10 @@ module Versioning
162
185
  uuser_id = send(self.versionable_options[:owner_field]+"_was")||send(self.versionable_options[:owner_field])
163
186
  if !self.new? && !data.empty? && uuser_id
164
187
  max_versions = self.versionable_options[:max_versions].to_i
165
- if self.version_ids.size >= max_versions
166
- old = self.version_ids.slice!(0, max_versions)
188
+ if max_versions > 0 && self.version_ids.size >= max_versions
189
+ old = self.version_ids.slice!(0, max_versions-1)
167
190
  self.class.skip_callback(:save, :before, :save_version)
168
- self.version_klass.delete_all(:_ids => old)
191
+ self.version_klass.where(:_id.in => old).delete_all
169
192
  self.save
170
193
  self.class.set_callback(:save, :before, :save_version)
171
194
  end
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{mongoid_ext}
8
- s.version = "0.6.1"
8
+ s.version = "0.6.2"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["David A. Cuadrado"]
12
- s.date = %q{2011-06-25}
12
+ s.date = %q{2011-07-15}
13
13
  s.default_executable = %q{mongoid_console}
14
14
  s.description = %q{mongoid plugins}
15
15
  s.email = %q{krawek@gmail.com}
@@ -30,6 +30,7 @@ Gem::Specification.new do |s|
30
30
  "lib/mongoid_ext.rb",
31
31
  "lib/mongoid_ext/criteria_ext.rb",
32
32
  "lib/mongoid_ext/document_ext.rb",
33
+ "lib/mongoid_ext/encryptor.rb",
33
34
  "lib/mongoid_ext/file.rb",
34
35
  "lib/mongoid_ext/file_list.rb",
35
36
  "lib/mongoid_ext/file_server.rb",
@@ -58,6 +59,7 @@ Gem::Specification.new do |s|
58
59
  "test/helper.rb",
59
60
  "test/models.rb",
60
61
  "test/support/custom_matchers.rb",
62
+ "test/test_encryptor.rb",
61
63
  "test/test_filter.rb",
62
64
  "test/test_modifiers.rb",
63
65
  "test/test_paranoia.rb",
@@ -87,6 +89,7 @@ Gem::Specification.new do |s|
87
89
  s.add_runtime_dependency(%q<i18n>, [">= 0"])
88
90
  s.add_runtime_dependency(%q<tzinfo>, [">= 0"])
89
91
  s.add_runtime_dependency(%q<differ>, [">= 0.1.2"])
92
+ s.add_runtime_dependency(%q<encryptor>, ["~> 1.1.3"])
90
93
  s.add_development_dependency(%q<yard>, ["~> 0.6.0"])
91
94
  s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
92
95
  s.add_development_dependency(%q<jeweler>, ["~> 1.5.2"])
@@ -101,6 +104,7 @@ Gem::Specification.new do |s|
101
104
  s.add_dependency(%q<i18n>, [">= 0"])
102
105
  s.add_dependency(%q<tzinfo>, [">= 0"])
103
106
  s.add_dependency(%q<differ>, [">= 0.1.2"])
107
+ s.add_dependency(%q<encryptor>, ["~> 1.1.3"])
104
108
  s.add_dependency(%q<yard>, ["~> 0.6.0"])
105
109
  s.add_dependency(%q<bundler>, ["~> 1.0.0"])
106
110
  s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
@@ -116,6 +120,7 @@ Gem::Specification.new do |s|
116
120
  s.add_dependency(%q<i18n>, [">= 0"])
117
121
  s.add_dependency(%q<tzinfo>, [">= 0"])
118
122
  s.add_dependency(%q<differ>, [">= 0.1.2"])
123
+ s.add_dependency(%q<encryptor>, ["~> 1.1.3"])
119
124
  s.add_dependency(%q<yard>, ["~> 0.6.0"])
120
125
  s.add_dependency(%q<bundler>, ["~> 1.0.0"])
121
126
  s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
@@ -1,3 +1,12 @@
1
+ class CreditCard # for encryptor
2
+ include Mongoid::Document
3
+ include MongoidExt::Encryptor
4
+
5
+ encrypted_field :number, :type => Integer, :key => "my password"
6
+ encrypted_field :data, :type => Hash, :key => "my password"
7
+ encrypted_field :extra, :key => "my password"
8
+ end
9
+
1
10
 
2
11
  class Event # for safe_update, and Timestamp
3
12
  include Mongoid::Document
@@ -0,0 +1,31 @@
1
+ require 'helper'
2
+
3
+ class StorageTest < Test::Unit::TestCase
4
+ context "Encrypting fields" do
5
+ setup do
6
+ @cc = CreditCard.create(:number => 12345, :data => {:month => 10, :year => 2014})
7
+ end
8
+
9
+ should "be able to load the number" do
10
+ @cc.reload
11
+ @cc.number.should == 12345
12
+ end
13
+
14
+ should "be able to load a hash or array" do
15
+ @cc.reload
16
+ @cc.data.should == {:month => 10, :year => 2014}
17
+ end
18
+
19
+ should "encrypt the field" do
20
+ @cc.reload
21
+ @cc.data_encrypted.should == 'd3f1d84f75f95027af7697f59c07437508ec98377a6d4104c7d7dc79967bf46b'
22
+ end
23
+
24
+ should "not fail with nil values" do
25
+ @cc.data = nil
26
+ @cc.save
27
+ @cc.reload
28
+ @cc.data.should == nil
29
+ end
30
+ end
31
+ end
@@ -24,9 +24,8 @@ class TestVersioning < Test::Unit::TestCase
24
24
  @blogpost.title = "sistemas operativos"
25
25
  @blogpost.save!
26
26
  @blogpost.reload
27
-
28
- @blogpost.diff(:title, "current", 0, :ascii).should == '{+"sistemas operativos"}'
29
- @blogpost.diff(:title, 0, "current", :ascii).should == '{-"sistemas operativos"}'
27
+ @blogpost.diff_by_word(:title, "current", 0, :ascii).should == "{\"operating\" >> \"sistemas\"} {\"systems\" >> \"operativos\"}"
28
+ @blogpost.diff_by_line(:title, 0, "current", :ascii).should == "{\"sistemas operativos\" >> \"operating systems\"}"
30
29
  end
31
30
 
32
31
  should "be able to restore a previous version" do
@@ -49,6 +48,7 @@ class TestVersioning < Test::Unit::TestCase
49
48
  @blogpost.title = "sistemas operativos 3"
50
49
  @blogpost.save!
51
50
  @blogpost.reload
51
+
52
52
  @blogpost.versions.count.should == 2
53
53
  end
54
54
  end
@@ -16,7 +16,7 @@ class OpenStructTest < Test::Unit::TestCase
16
16
  @config.entries = entries
17
17
  @config.save
18
18
 
19
- from_db.entries["new_key"].should == "my new key"
19
+ from_db.entries.new_key.should == "my new key"
20
20
  end
21
21
  end
22
22
  end
@@ -7,7 +7,9 @@ class SetTest < Test::Unit::TestCase
7
7
 
8
8
  context "working with sets" do
9
9
  setup do
10
- @recipe = Recipe.create!(:ingredients => %w[salt sugar water salt sugar water])
10
+ @recipe = Recipe.new
11
+ @recipe.ingredients = Set.new(%w[salt sugar water salt sugar water])
12
+ @recipe.save
11
13
  end
12
14
 
13
15
  should "not have duplicates" do
@@ -20,6 +22,9 @@ class SetTest < Test::Unit::TestCase
20
22
  should "not add duplicates" do
21
23
  original_size = @recipe.ingredients.size
22
24
  @recipe.ingredients << "salt"
25
+ @recipe.save
26
+ @recipe.reload
27
+
23
28
  @recipe.ingredients.size.should == original_size
24
29
  end
25
30
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 6
8
- - 1
9
- version: 0.6.1
8
+ - 2
9
+ version: 0.6.2
10
10
  platform: ruby
11
11
  authors:
12
12
  - David A. Cuadrado
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2011-06-25 00:00:00 -05:00
17
+ date: 2011-07-15 00:00:00 -05:00
18
18
  default_executable: mongoid_console
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -87,8 +87,23 @@ dependencies:
87
87
  prerelease: false
88
88
  version_requirements: *id005
89
89
  - !ruby/object:Gem::Dependency
90
- name: yard
90
+ name: encryptor
91
91
  requirement: &id006 !ruby/object:Gem::Requirement
92
+ none: false
93
+ requirements:
94
+ - - ~>
95
+ - !ruby/object:Gem::Version
96
+ segments:
97
+ - 1
98
+ - 1
99
+ - 3
100
+ version: 1.1.3
101
+ type: :runtime
102
+ prerelease: false
103
+ version_requirements: *id006
104
+ - !ruby/object:Gem::Dependency
105
+ name: yard
106
+ requirement: &id007 !ruby/object:Gem::Requirement
92
107
  none: false
93
108
  requirements:
94
109
  - - ~>
@@ -100,10 +115,10 @@ dependencies:
100
115
  version: 0.6.0
101
116
  type: :development
102
117
  prerelease: false
103
- version_requirements: *id006
118
+ version_requirements: *id007
104
119
  - !ruby/object:Gem::Dependency
105
120
  name: bundler
106
- requirement: &id007 !ruby/object:Gem::Requirement
121
+ requirement: &id008 !ruby/object:Gem::Requirement
107
122
  none: false
108
123
  requirements:
109
124
  - - ~>
@@ -115,10 +130,10 @@ dependencies:
115
130
  version: 1.0.0
116
131
  type: :development
117
132
  prerelease: false
118
- version_requirements: *id007
133
+ version_requirements: *id008
119
134
  - !ruby/object:Gem::Dependency
120
135
  name: jeweler
121
- requirement: &id008 !ruby/object:Gem::Requirement
136
+ requirement: &id009 !ruby/object:Gem::Requirement
122
137
  none: false
123
138
  requirements:
124
139
  - - ~>
@@ -130,10 +145,10 @@ dependencies:
130
145
  version: 1.5.2
131
146
  type: :development
132
147
  prerelease: false
133
- version_requirements: *id008
148
+ version_requirements: *id009
134
149
  - !ruby/object:Gem::Dependency
135
150
  name: shoulda
136
- requirement: &id009 !ruby/object:Gem::Requirement
151
+ requirement: &id010 !ruby/object:Gem::Requirement
137
152
  none: false
138
153
  requirements:
139
154
  - - ~>
@@ -145,10 +160,10 @@ dependencies:
145
160
  version: 2.11.3
146
161
  type: :development
147
162
  prerelease: false
148
- version_requirements: *id009
163
+ version_requirements: *id010
149
164
  - !ruby/object:Gem::Dependency
150
165
  name: jnunemaker-matchy
151
- requirement: &id010 !ruby/object:Gem::Requirement
166
+ requirement: &id011 !ruby/object:Gem::Requirement
152
167
  none: false
153
168
  requirements:
154
169
  - - ~>
@@ -159,10 +174,10 @@ dependencies:
159
174
  version: "0.4"
160
175
  type: :development
161
176
  prerelease: false
162
- version_requirements: *id010
177
+ version_requirements: *id011
163
178
  - !ruby/object:Gem::Dependency
164
179
  name: shoulda
165
- requirement: &id011 !ruby/object:Gem::Requirement
180
+ requirement: &id012 !ruby/object:Gem::Requirement
166
181
  none: false
167
182
  requirements:
168
183
  - - ~>
@@ -174,10 +189,10 @@ dependencies:
174
189
  version: 2.11.3
175
190
  type: :development
176
191
  prerelease: false
177
- version_requirements: *id011
192
+ version_requirements: *id012
178
193
  - !ruby/object:Gem::Dependency
179
194
  name: mocha
180
- requirement: &id012 !ruby/object:Gem::Requirement
195
+ requirement: &id013 !ruby/object:Gem::Requirement
181
196
  none: false
182
197
  requirements:
183
198
  - - ~>
@@ -189,10 +204,10 @@ dependencies:
189
204
  version: 0.9.4
190
205
  type: :development
191
206
  prerelease: false
192
- version_requirements: *id012
207
+ version_requirements: *id013
193
208
  - !ruby/object:Gem::Dependency
194
209
  name: timecop
195
- requirement: &id013 !ruby/object:Gem::Requirement
210
+ requirement: &id014 !ruby/object:Gem::Requirement
196
211
  none: false
197
212
  requirements:
198
213
  - - ">="
@@ -202,7 +217,7 @@ dependencies:
202
217
  version: "0"
203
218
  type: :development
204
219
  prerelease: false
205
- version_requirements: *id013
220
+ version_requirements: *id014
206
221
  description: mongoid plugins
207
222
  email: krawek@gmail.com
208
223
  executables:
@@ -224,6 +239,7 @@ files:
224
239
  - lib/mongoid_ext.rb
225
240
  - lib/mongoid_ext/criteria_ext.rb
226
241
  - lib/mongoid_ext/document_ext.rb
242
+ - lib/mongoid_ext/encryptor.rb
227
243
  - lib/mongoid_ext/file.rb
228
244
  - lib/mongoid_ext/file_list.rb
229
245
  - lib/mongoid_ext/file_server.rb
@@ -252,6 +268,7 @@ files:
252
268
  - test/helper.rb
253
269
  - test/models.rb
254
270
  - test/support/custom_matchers.rb
271
+ - test/test_encryptor.rb
255
272
  - test/test_filter.rb
256
273
  - test/test_modifiers.rb
257
274
  - test/test_paranoia.rb