quaid 0.0.1 → 0.0.2

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/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ --fail-fast
3
+ --drb
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ language: ruby
2
+ rvm:
3
+ - "1.9.3"
4
+ services: mongodb
5
+ script: bundle exec rspec spec
data/Gemfile CHANGED
@@ -1,4 +1,25 @@
1
- source 'https://rubygems.org'
1
+ source :rubygems
2
2
 
3
3
  # Specify your gem's dependencies in quaid.gemspec
4
4
  gemspec
5
+
6
+ group :test do
7
+ gem 'rspec'
8
+ gem 'fuubar'
9
+ gem 'rb-fsevent'
10
+ gem 'spork'
11
+ gem 'faker'
12
+ gem 'database_cleaner'
13
+ gem 'factory_girl'
14
+ end
15
+
16
+ group :development do
17
+ gem 'terminal-notifier-guard'
18
+ gem 'guard'
19
+ gem 'guard-rspec'
20
+ gem 'guard-bundler'
21
+ gem 'guard-spork'
22
+ gem 'rb-inotify', :require => false
23
+ gem 'rb-fsevent', :require => false
24
+ gem 'rb-fchange', :require => false
25
+ end
data/Guardfile ADDED
@@ -0,0 +1,23 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ guard :bundler do
5
+ watch('Gemfile')
6
+ watch(/^.+\.gemspec/)
7
+ end
8
+
9
+ guard :spork do
10
+ watch('Gemfile')
11
+ watch('Gemfile.lock')
12
+ watch('spec/spec_helper.rb') { :rspec }
13
+ end
14
+
15
+ guard :rspec do
16
+ watch(%r{^spec/.+_spec\.rb$})
17
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
18
+ watch('spec/spec_helper.rb') { "spec" }
19
+
20
+ watch(%r{^spec/support/(.+)\.rb$}) { "spec" }
21
+ end
22
+
23
+
data/README.md CHANGED
@@ -1,3 +1,4 @@
1
+ [![Build Status](https://travis-ci.org/musicglue/quaid.png?branch=master)](https://travis-ci.org/musicglue/quaid)
1
2
  # Quaid
2
3
 
3
4
  TODO: Write a gem description
data/lib/mongoid/quaid.rb CHANGED
@@ -5,27 +5,48 @@ module Mongoid
5
5
  included do |klass|
6
6
  field :version, type: Integer, default: 0
7
7
 
8
- has_many :versions, class_name: self.to_s + "::Version", foreign_key: "owner_id"
8
+ has_many :versions, class_name: self.to_s + "::Version", foreign_key: "owner_id", dependent: :destroy
9
9
 
10
10
  def last_version
11
- self.class.new versions[1].attributes
11
+ self.class.new versions[1].try(:attributes)
12
12
  end
13
13
 
14
- klass.class_eval %Q{
15
- set_callback :save, :before do |doc|
16
- doc.version += 1
17
- end
14
+ set_callback :save, :before do |doc|
15
+ doc.version += 1
16
+ end
18
17
 
19
- set_callback :save, :after do |doc|
20
- attributes = MultiJson.decode MultiJson.encode doc
21
- Version.create(attributes.merge(owner_id: doc.id, deleted_at: DateTime.now))
18
+ set_callback :save, :after do |doc|
19
+ attributes = MultiJson.decode MultiJson.encode doc
20
+ doc.class::Version.create(attributes.merge(owner_id: doc.id))
21
+ doc.last_version.try(:set, {deleted_at: DateTime.now})
22
+ if doc.class.versions && doc.versions.count > doc.class.versions
23
+ doc.versions.last.delete
22
24
  end
25
+ end
23
26
 
27
+ module_eval <<-RUBY, __FILE__, __LINE__ + 1
28
+ class << self
29
+ attr_accessor :versions
30
+ def quaid opts={}
31
+ return unless opts[:versions]
32
+ @versions = opts[:versions]
33
+ end
34
+
35
+ def find_with_version id, version
36
+ v = Version.where(owner_id: id, version: version).first.try(:attributes)
37
+ if v.nil?
38
+ nil
39
+ else
40
+ new v
41
+ end
42
+ end
43
+ end
24
44
 
25
45
  class Version
26
46
  include Mongoid::Document
27
47
  include Mongoid::Timestamps
28
-
48
+ include Mongoid::Paranoia
49
+
29
50
  store_in collection: self.to_s.underscore.gsub("/version", "") + "_versions"
30
51
 
31
52
  def initialize(attributes={}, options=nil)
@@ -35,8 +56,10 @@ module Mongoid
35
56
 
36
57
  index owner_id: 1
37
58
  default_scope desc(:created_at)
59
+
60
+ field :deleted_at, type: DateTime
38
61
  end
39
- }
62
+ RUBY
40
63
  end
41
64
 
42
65
  module ClassMethods
data/lib/quaid/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Quaid
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
data/quaid.gemspec CHANGED
@@ -10,12 +10,13 @@ Gem::Specification.new do |gem|
10
10
  gem.email = ["john@musicglue.com", "adam@musicglue.com"]
11
11
  gem.description = %q{Total Recall for Mongoid}
12
12
  gem.summary = %q{...}
13
- gem.homepage = "http://github.com/musicglue/quaid"
13
+ gem.homepage = "https://github.com/musicglue/quaid"
14
14
 
15
15
  gem.files = `git ls-files`.split($/)
16
16
  gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
17
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
18
  gem.require_paths = ["lib"]
19
19
 
20
- gem.add_dependency 'mongoid', '~> 3.0.14'
20
+ gem.add_dependency 'mongoid', '~> 3.0.14'
21
+ gem.add_dependency 'activesupport', '~> 3.2.0'
21
22
  end
@@ -0,0 +1,5 @@
1
+ FactoryGirl.define do
2
+ factory :item do
3
+ name { Faker::Name.name }
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ FactoryGirl.define do
2
+ factory :list do
3
+ name { Faker::Name.name }
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ FactoryGirl.define do
2
+ factory :max_versions_project do
3
+ name { Faker::Name.name }
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ FactoryGirl.define do
2
+ factory :paranoid_project do
3
+ name { Faker::Name.name }
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ FactoryGirl.define do
2
+ factory :project do
3
+ name { Faker::Name.name }
4
+ end
5
+ end
@@ -0,0 +1,24 @@
1
+ require 'spec_helper'
2
+
3
+ describe Mongoid::Quaid do
4
+
5
+ before do
6
+ @list = create(:list)
7
+ @list.items << build(:item)
8
+ @list.save
9
+ @list.reload
10
+ end
11
+
12
+ it "should save a version on adding an item" do
13
+ @list.versions.size.should eq(2)
14
+ end
15
+
16
+ it "should correctly instantiate embedded docs in saved versions" do
17
+ @list.name = Faker::Name.name
18
+ @list.save
19
+ @list.reload
20
+ @list.last_version.items.first.class.should eq(Item)
21
+ @list.last_version.items.first.name.should eq(@list.items.first.name)
22
+ end
23
+
24
+ end
@@ -0,0 +1,18 @@
1
+ require 'spec_helper'
2
+
3
+ describe Mongoid::Quaid do
4
+
5
+ context "paranoid classes" do
6
+
7
+ before do
8
+ @project = create(:paranoid_project)
9
+ end
10
+
11
+ it "should not destroy associated documents when parent is destroyed" do
12
+ @project.destroy
13
+ ParanoidProject::Version.unscoped.count.should eq(1)
14
+ end
15
+
16
+ end
17
+
18
+ end
@@ -0,0 +1,78 @@
1
+ require 'spec_helper'
2
+
3
+ describe Mongoid::Quaid do
4
+
5
+ context "Make like Rekall and..." do
6
+
7
+ before do
8
+ @project = create(:project)
9
+ end
10
+
11
+ it "should include Quaid" do
12
+ @project.class.ancestors.should include(Mongoid::Quaid)
13
+ end
14
+
15
+ it "should create a Project::Version class" do
16
+ Project.should have_constant(:Version)
17
+ end
18
+
19
+ it "should persist versions for projects in the project_versions collection" do
20
+ Project::Version.collection_name.should eq(:project_versions)
21
+ end
22
+
23
+ it "should have a version of 1" do
24
+ @project.version.should eq(1)
25
+ end
26
+
27
+ it "should have created a saved version" do
28
+ Project::Version.count.should eq(1)
29
+ end
30
+
31
+ it "should have access to its last version through #last_version" do
32
+ @project.last_version.version.should eq(0)
33
+ end
34
+
35
+ it "should return an arbitrary version" do
36
+ old_name = @project.name
37
+ @project.name = Faker::Name.name
38
+ @project.save
39
+ old_proj = Project.find_with_version(@project.id, 1)
40
+ old_proj.version.should eq(1)
41
+ end
42
+
43
+ it "should create a new version with the current attributes if updated" do
44
+ old_name = @project.name
45
+ @project.name = Faker::Name.name
46
+ @project.save
47
+ @project.reload
48
+ @project.last_version.version.should eq(1)
49
+ @project.last_version.name.should eq(old_name)
50
+ end
51
+
52
+ it "should create a new version with each save" do
53
+ n = 10
54
+ lambda {
55
+ n.times {
56
+ @project.name = Faker::Name.name
57
+ @project.save
58
+ }
59
+ }.should change{ Project::Version.count }.from(1).to(n+1)
60
+ end
61
+
62
+ it "should order the versions by their creation date descending" do
63
+ 5.times {
64
+ @project.name = Faker::Name.name
65
+ @project.save
66
+ }
67
+ Project::Version.last.version.should eq(1)
68
+ end
69
+
70
+ it "should destroy associated versions when record is destroy" do
71
+ id = @project.id
72
+ @project.destroy
73
+ Project::Version.where(owner_id: id).count.should eq(0)
74
+ end
75
+
76
+ end
77
+
78
+ end
@@ -0,0 +1,26 @@
1
+ require 'spec_helper'
2
+
3
+ describe Mongoid::Quaid do
4
+
5
+ before do
6
+ @project = create(:max_versions_project)
7
+ end
8
+
9
+ it "should limit the maximum number of versions that can be stored" do
10
+ 5.times {
11
+ @project.name = Faker::Name.new
12
+ @project.save
13
+ }
14
+ @project.versions.count.should eq(5)
15
+ end
16
+
17
+ it "should retain the most recent versions when limited" do
18
+ 5.times {
19
+ @project.name = Faker::Name.new
20
+ @project.save
21
+ }
22
+ @project.reload
23
+ @project.versions.last.version.should eq(2)
24
+ end
25
+
26
+ end
data/spec/mongoid.yml ADDED
@@ -0,0 +1,9 @@
1
+ test:
2
+ sessions:
3
+ default:
4
+ hosts:
5
+ - localhost:27017
6
+ database: quaid-test
7
+ options:
8
+ identity_map_enabled: false
9
+ safe: false
@@ -0,0 +1,44 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+ require 'spork'
4
+ require 'guard/rspec'
5
+ require 'rspec'
6
+
7
+ Spork.prefork do
8
+ require 'factory_girl'
9
+ require 'database_cleaner'
10
+ require 'faker'
11
+ require 'mongoid'
12
+ require 'quaid'
13
+ Mongoid.load!(File.expand_path("../mongoid.yml", __FILE__), :test)
14
+
15
+ RSpec.configure do |config|
16
+ config.include FactoryGirl::Syntax::Methods
17
+
18
+ config.before(:suite) do
19
+ DatabaseCleaner.strategy = :truncation
20
+ DatabaseCleaner.clean
21
+ end
22
+
23
+ config.after(:each) do
24
+ DatabaseCleaner.clean
25
+ end
26
+ end
27
+
28
+ Dir['./spec/support/**/*.rb'].each{ |file| require file }
29
+ end
30
+
31
+ Spork.each_run do
32
+ require 'quaid'
33
+ FactoryGirl.find_definitions
34
+ end
35
+
36
+
37
+
38
+
39
+
40
+
41
+
42
+
43
+
44
+
@@ -0,0 +1,7 @@
1
+ class Item
2
+ include Mongoid::Document
3
+
4
+ field :name, type: String
5
+
6
+ embedded_in :list
7
+ end
@@ -0,0 +1,8 @@
1
+ class List
2
+ include Mongoid::Document
3
+ include Mongoid::Quaid
4
+
5
+ field :name, type: String
6
+ embeds_many :items
7
+
8
+ end
@@ -0,0 +1,8 @@
1
+ class MaxVersionsProject
2
+ include Mongoid::Document
3
+ include Mongoid::Quaid
4
+
5
+ field :name, type: String
6
+
7
+ quaid versions: 5
8
+ end
@@ -0,0 +1,8 @@
1
+ class ParanoidProject
2
+ include Mongoid::Document
3
+ include Mongoid::Quaid
4
+ include Mongoid::Paranoia
5
+
6
+ field :name, type: String
7
+
8
+ end
@@ -0,0 +1,6 @@
1
+ class Project
2
+ include Mongoid::Document
3
+ include Mongoid::Quaid
4
+
5
+ field :name, type: String
6
+ end
@@ -0,0 +1,5 @@
1
+ RSpec::Matchers.define :have_constant do |const|
2
+ match do |owner|
3
+ owner.const_defined?(const)
4
+ end
5
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: quaid
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -12,7 +12,7 @@ authors:
12
12
  autorequire:
13
13
  bindir: bin
14
14
  cert_chain: []
15
- date: 2012-12-03 00:00:00.000000000 Z
15
+ date: 2013-01-25 00:00:00.000000000 Z
16
16
  dependencies:
17
17
  - !ruby/object:Gem::Dependency
18
18
  name: mongoid
@@ -30,6 +30,22 @@ dependencies:
30
30
  - - ~>
31
31
  - !ruby/object:Gem::Version
32
32
  version: 3.0.14
33
+ - !ruby/object:Gem::Dependency
34
+ name: activesupport
35
+ requirement: !ruby/object:Gem::Requirement
36
+ none: false
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: 3.2.0
41
+ type: :runtime
42
+ prerelease: false
43
+ version_requirements: !ruby/object:Gem::Requirement
44
+ none: false
45
+ requirements:
46
+ - - ~>
47
+ - !ruby/object:Gem::Version
48
+ version: 3.2.0
33
49
  description: Total Recall for Mongoid
34
50
  email:
35
51
  - john@musicglue.com
@@ -39,7 +55,10 @@ extensions: []
39
55
  extra_rdoc_files: []
40
56
  files:
41
57
  - .gitignore
58
+ - .rspec
59
+ - .travis.yml
42
60
  - Gemfile
61
+ - Guardfile
43
62
  - LICENSE.txt
44
63
  - README.md
45
64
  - Rakefile
@@ -47,7 +66,24 @@ files:
47
66
  - lib/quaid.rb
48
67
  - lib/quaid/version.rb
49
68
  - quaid.gemspec
50
- homepage: http://github.com/musicglue/quaid
69
+ - spec/factories/item_factory.rb
70
+ - spec/factories/list_factory.rb
71
+ - spec/factories/max_versions_project_factory.rb
72
+ - spec/factories/paranoid_project_factory.rb
73
+ - spec/factories/project_factory.rb
74
+ - spec/lib/embedded_docs_spec.rb
75
+ - spec/lib/paranoia_spec.rb
76
+ - spec/lib/quaid_spec.rb
77
+ - spec/lib/version_limit_spec.rb
78
+ - spec/mongoid.yml
79
+ - spec/spec_helper.rb
80
+ - spec/support/models/item.rb
81
+ - spec/support/models/list.rb
82
+ - spec/support/models/max_versions_project.rb
83
+ - spec/support/models/paranoid_project.rb
84
+ - spec/support/models/project.rb
85
+ - spec/support/utilities.rb
86
+ homepage: https://github.com/musicglue/quaid
51
87
  licenses: []
52
88
  post_install_message:
53
89
  rdoc_options: []
@@ -71,5 +107,22 @@ rubygems_version: 1.8.24
71
107
  signing_key:
72
108
  specification_version: 3
73
109
  summary: ! '...'
74
- test_files: []
110
+ test_files:
111
+ - spec/factories/item_factory.rb
112
+ - spec/factories/list_factory.rb
113
+ - spec/factories/max_versions_project_factory.rb
114
+ - spec/factories/paranoid_project_factory.rb
115
+ - spec/factories/project_factory.rb
116
+ - spec/lib/embedded_docs_spec.rb
117
+ - spec/lib/paranoia_spec.rb
118
+ - spec/lib/quaid_spec.rb
119
+ - spec/lib/version_limit_spec.rb
120
+ - spec/mongoid.yml
121
+ - spec/spec_helper.rb
122
+ - spec/support/models/item.rb
123
+ - spec/support/models/list.rb
124
+ - spec/support/models/max_versions_project.rb
125
+ - spec/support/models/paranoid_project.rb
126
+ - spec/support/models/project.rb
127
+ - spec/support/utilities.rb
75
128
  has_rdoc: