activerecord-draft_records 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Rodrigo Kochenburger
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,17 @@
1
+ = activerecord-draft_records
2
+
3
+ Description goes here.
4
+
5
+ == Note on Patches/Pull Requests
6
+
7
+ * Fork the project.
8
+ * Make your feature addition or bug fix.
9
+ * Add tests for it. This is important so I don't break it in a
10
+ future version unintentionally.
11
+ * Commit, do not mess with rakefile, version, or history.
12
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
13
+ * Send me a pull request. Bonus points for topic branches.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2010 Rodrigo Kochenburger. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,50 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "activerecord-draft_records"
8
+ gem.summary = "Add support for draft models (incomplete models) to ActiveRecord"
9
+ gem.email = "divoxx@gmail.com"
10
+ gem.homepage = "http://github.com/divoxx/activerecord-draft_records"
11
+ gem.authors = ["Rodrigo Kochenburger"]
12
+ gem.description = <<-DESC
13
+ Sometimes it's necessary to store incomplete records, without fullfilling all validations, to complete
14
+ and then validate the record. ActiveRecord::DraftRecords allows you to persist an record even if it's
15
+ invalid by tagging it as a draft.
16
+ DESC
17
+
18
+ gem.add_development_dependency "rspec", ">= 1.2.9"
19
+ gem.add_development_dependency "database_cleaner", ">= 0.5.2"
20
+ end
21
+ Jeweler::GemcutterTasks.new
22
+ rescue LoadError
23
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
24
+ end
25
+
26
+ require 'spec/rake/spectask'
27
+ Spec::Rake::SpecTask.new(:spec) do |spec|
28
+ spec.libs << 'lib' << 'spec'
29
+ spec.spec_files = FileList['spec/**/*_spec.rb']
30
+ end
31
+
32
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
33
+ spec.libs << 'lib' << 'spec'
34
+ spec.pattern = 'spec/**/*_spec.rb'
35
+ spec.rcov = true
36
+ end
37
+
38
+ task :spec => :check_dependencies
39
+
40
+ task :default => :spec
41
+
42
+ require 'rake/rdoctask'
43
+ Rake::RDocTask.new do |rdoc|
44
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
45
+
46
+ rdoc.rdoc_dir = 'rdoc'
47
+ rdoc.title = "activerecord-draft_records #{version}"
48
+ rdoc.rdoc_files.include('README*')
49
+ rdoc.rdoc_files.include('lib/**/*.rb')
50
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,53 @@
1
+ module ActiveRecord
2
+ module DraftRecords
3
+ def self.included(klass)
4
+ klass.extend(ClassMethods)
5
+ # Define default_scope to not include drafts
6
+ klass.send(:default_scope, :conditions => {:draft => false})
7
+ end
8
+
9
+ module ClassMethods
10
+ # Same as ActiveRecord::Base#create, but sets the record as a draft and save
11
+ # ignoring validations.
12
+ def create_as_draft(attributes = {}, &block)
13
+ if attributes.is_a?(Array)
14
+ attributes.collect { |attr| create_as_draft(attr, &block) }
15
+ else
16
+ object = new(attributes)
17
+ yield(object) if block_given?
18
+ object.save_as_draft
19
+ object
20
+ end
21
+ end
22
+
23
+ # Attempt to create the record, if it fails, save it as a draft
24
+ def create_or_draft(attributes = {}, &block)
25
+ if attributes.is_a?(Array)
26
+ attributes.collect { |attr| create_as_draft(attr, &block) }
27
+ else
28
+ object = new(attributes)
29
+ yield(object) if block_given?
30
+ object.save_or_draft
31
+ object
32
+ end
33
+ end
34
+
35
+ # Returns a ActiveRecord::Scope that fetchs all the drafts
36
+ def find_drafts(*args)
37
+ with_exclusive_scope { self.scoped(:conditions => {:draft => true}).find(*args) }
38
+ end
39
+ end
40
+
41
+ # Save the record as a draft, setting the record attribute 'draft' to true
42
+ # and saving the record ignoring the validations.
43
+ def save_as_draft
44
+ self.draft = true
45
+ save(false)
46
+ end
47
+
48
+ # Attempt to save the record, if any validation fails, save it as a draft.
49
+ def save_or_draft
50
+ save || save_as_draft
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,48 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "Integration features" do
4
+ it "should save the record as draft and skip validation" do
5
+ user = User.new :email => 'joe@example.com'
6
+ user.should_not be_valid
7
+ user.save_as_draft
8
+ user.should be_a_draft
9
+ end
10
+
11
+ it "should allow trying to save the object and saving as a draft if it fails" do
12
+ user = User.new :email => 'joe@example.com'
13
+ user.should_not be_valid
14
+ user.save_or_draft
15
+ user.should be_a_draft
16
+ end
17
+
18
+ it "should not save the record as draft if validations pass when calling save_or_draft" do
19
+ user = User.new :email => 'joe@example.com', :username => 'joe'
20
+ user.should be_valid
21
+ user.save_or_draft
22
+ user.should_not be_a_draft
23
+ end
24
+
25
+ it "should create a record as draft and skip validations" do
26
+ user = User.create_as_draft :email => 'joe@example.com'
27
+ user.should be_instance_of(User)
28
+ user.should_not be_a_new_record
29
+ user.should be_a_draft
30
+ end
31
+
32
+ it "should allow trying to create a record and saving as a draft if it fails" do
33
+ user = User.create_or_draft :email => 'joe@example.com'
34
+ user.should be_instance_of(User)
35
+ user.should_not be_a_new_record
36
+ user.should be_a_draft
37
+ end
38
+
39
+ it "should not return draft records on queries" do
40
+ user = User.create_as_draft :email => 'joe@example.com'
41
+ User.all.should_not include(user)
42
+ end
43
+
44
+ it "should provide a version of ActiveRecord::Base#find that will look for drafts" do
45
+ user = User.create_as_draft :email => 'joe@example.com'
46
+ User.find_drafts(:all).should include(user)
47
+ end
48
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,36 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'rubygems'
4
+ require 'active_record'
5
+ require 'active_record/draft_records'
6
+ require 'spec'
7
+ require 'spec/autorun'
8
+ require 'database_cleaner'
9
+ require 'logger'
10
+
11
+ # Initiate the connection
12
+ ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => ':memory:')
13
+ DatabaseCleaner.strategy = :transaction
14
+
15
+ # Define the database schema
16
+ ActiveRecord::Schema.define do
17
+ create_table :users do |t|
18
+ t.string :username, :email
19
+ t.boolean :draft
20
+ end
21
+ end
22
+
23
+ class User < ActiveRecord::Base
24
+ include ActiveRecord::DraftRecords
25
+ validates_presence_of :username, :email
26
+ end
27
+
28
+ Spec::Runner.configure do |config|
29
+ config.before(:each) do
30
+ DatabaseCleaner.start
31
+ end
32
+
33
+ config.after(:each) do
34
+ DatabaseCleaner.clean
35
+ end
36
+ end
metadata ADDED
@@ -0,0 +1,104 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: activerecord-draft_records
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - Rodrigo Kochenburger
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-05-17 00:00:00 -03:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rspec
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 1
29
+ - 2
30
+ - 9
31
+ version: 1.2.9
32
+ type: :development
33
+ version_requirements: *id001
34
+ - !ruby/object:Gem::Dependency
35
+ name: database_cleaner
36
+ prerelease: false
37
+ requirement: &id002 !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ segments:
42
+ - 0
43
+ - 5
44
+ - 2
45
+ version: 0.5.2
46
+ type: :development
47
+ version_requirements: *id002
48
+ description: |
49
+ Sometimes it's necessary to store incomplete records, without fullfilling all validations, to complete
50
+ and then validate the record. ActiveRecord::DraftRecords allows you to persist an record even if it's
51
+ invalid by tagging it as a draft.
52
+
53
+ email: divoxx@gmail.com
54
+ executables: []
55
+
56
+ extensions: []
57
+
58
+ extra_rdoc_files:
59
+ - LICENSE
60
+ - README.rdoc
61
+ files:
62
+ - .document
63
+ - .gitignore
64
+ - LICENSE
65
+ - README.rdoc
66
+ - Rakefile
67
+ - VERSION
68
+ - lib/active_record/draft_records.rb
69
+ - spec/features_spec.rb
70
+ - spec/spec.opts
71
+ - spec/spec_helper.rb
72
+ has_rdoc: true
73
+ homepage: http://github.com/divoxx/activerecord-draft_records
74
+ licenses: []
75
+
76
+ post_install_message:
77
+ rdoc_options:
78
+ - --charset=UTF-8
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ segments:
86
+ - 0
87
+ version: "0"
88
+ required_rubygems_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ segments:
93
+ - 0
94
+ version: "0"
95
+ requirements: []
96
+
97
+ rubyforge_project:
98
+ rubygems_version: 1.3.6
99
+ signing_key:
100
+ specification_version: 3
101
+ summary: Add support for draft models (incomplete models) to ActiveRecord
102
+ test_files:
103
+ - spec/features_spec.rb
104
+ - spec/spec_helper.rb