Permalink
Browse files

Adding tests and rdoc

  • Loading branch information...
mrevilme committed Oct 19, 2011
1 parent 74b88a2 commit 3bfcea6dfcee8b59e8d6d309a1bca0f4482b5fd7
Showing with 77 additions and 18 deletions.
  1. +4 −0 Gemfile
  2. +2 −0 Gemfile.lock
  3. +16 −8 README.rdoc
  4. +27 −5 lib/active_record/clone.rb
  5. +14 −3 spec/activerecord_clone_spec.rb
  6. +2 −2 spec/spec_helper.rb
  7. +1 −0 spec/support/data.rb
  8. +3 −0 spec/support/models.rb
  9. +8 −0 spec/support/schema.rb
View
@@ -13,3 +13,7 @@ group :development do
gem "jeweler", "~> 1.6.4"
gem "rcov", ">= 0"
end
+
+group :testing do
+ gem 'sqlite3'
+end
View
@@ -32,6 +32,7 @@ GEM
rspec-expectations (2.3.0)
diff-lcs (~> 1.1.2)
rspec-mocks (2.3.0)
+ sqlite3 (1.3.4)
tzinfo (0.3.30)
PLATFORMS
@@ -44,3 +45,4 @@ DEPENDENCIES
jeweler (~> 1.6.4)
rcov
rspec (~> 2.3.0)
+ sqlite3
View
@@ -1,6 +1,20 @@
= activerecord_clone
-Description goes here.
+Handles a simple task of cloning all attributes of a AR object
+Default behaviour is to not clone the foreign_keys.
+
+Possible options is:
+:only => [] # only clone these attributes
+:exclude => [] # Exlude these attributes, default is :id
+:skip_relations => true|false #default is true
+
+Can be configured either on a model layer using
+
+class MyModel < ActiveRecord::Base
+can_clone
+end
+
+Or can be configured upon the call to clone_ar.
== Contributing to activerecord_clone
@@ -10,10 +24,4 @@ Description goes here.
* Start a feature/bugfix branch
* Commit and push until you are happy with your contribution
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
-* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
-
-== Copyright
-
-Copyright (c) 2011 science. See LICENSE.txt for
-further details.
-
+* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
View
@@ -1,4 +1,25 @@
+require 'active_support'
+require 'active_record'
+
module ActiveRecord
+ # == Active Model Clone
+ #
+ # Handles a simple task of cloning all attributes of a AR object
+ # Default behaviour is to not clone the foreign_keys.
+ #
+ # Possible options is:
+ # :only => [] # only clone these attributes
+ # :exclude => [] # Exlude these attributes, default is :id
+ # :skip_relations => true|false #default is true
+ #
+ # Can be configured either on a model layer using
+ #
+ # class MyModel < ActiveRecord::Base
+ # can_clone
+ # end
+ #
+ # Or can be configured upon the call to clone_ar.
+
module Clone
extend ActiveSupport::Concern
@@ -10,15 +31,16 @@ def can_clone(options={})
end
private
-
+ # :nodoc
def foreing_keys
self.reflect_on_all_associations.map { |assoc| assoc.association_foreign_key }
end
+ # :nodoc
def default_options
{
:skip_relations => true,
- :excluded => []
+ :excluded => [:id]
}
end
@@ -28,13 +50,13 @@ def default_options
module InstanceMethods
def clone_ar(options={})
- options = Account.instance_variable_get(:@options).merge(options)
+ options = (self.instance_variable_get(:@options) ? self.instance_variable_get(:@options) : self.class.send(:default_options)).merge(options)
attrs = []
if options[:only] and options[:only].is_a? Array
- attrs = self.attributes.reject {|item| options[:only].include? item}
+ attrs = self.attribute_names.reject {|item| options[:only].include? item}
else
excluded = options[:excluded] + (options[:skip_relations] ? self.class.send(:foreing_keys) : [])
- attrs = self.attribute_names
+ attrs = self.attribute_names.reject { |item| excluded.include? item}
end
newObj = self.class.new
@@ -1,7 +1,18 @@
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
describe "ActiverecordClone" do
- it "fails" do
- fail "hey buddy, you should probably rename this file and start specing for real"
+ context "clone_ar" do
+ it "clone_ar no options" do
+ post = Post.first
+ clone = post.clone_ar
+ clone.text.should include("Post!")
+ end
+ it "clone_ar exclude text" do
+ post = Post.first
+ clone = post.clone_ar :excluded => [:text]
+ clone.text == nil
+ end
+
end
-end
+
+end
View
@@ -2,10 +2,10 @@
$LOAD_PATH.unshift(File.dirname(__FILE__))
require 'rspec'
require 'activerecord_clone'
-
+ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => File.dirname(__FILE__) + "/ar_clone_test.sqlite3")
# Requires supporting files with custom matchers and macros, etc,
# in ./support/ and its subdirectories.
-Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
+Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].reverse.each {|f| require f}
RSpec.configure do |config|
View
@@ -0,0 +1 @@
+Post.create(:text => "Post!")
View
@@ -0,0 +1,3 @@
+class Post < ActiveRecord::Base
+
+end
View
@@ -0,0 +1,8 @@
+ActiveRecord::Schema.define do
+ self.verbose = false
+
+ create_table :posts, :force => true do |t|
+ t.string :text
+ t.timestamps
+ end
+end

0 comments on commit 3bfcea6

Please sign in to comment.