Skip to content

Commit

Permalink
Adding tests and rdoc
Browse files Browse the repository at this point in the history
  • Loading branch information
emil-palm committed Oct 19, 2011
1 parent 74b88a2 commit 3bfcea6
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 18 deletions.
4 changes: 4 additions & 0 deletions Gemfile
Expand Up @@ -13,3 +13,7 @@ group :development do
gem "jeweler", "~> 1.6.4"
gem "rcov", ">= 0"
end

group :testing do
gem 'sqlite3'
end
2 changes: 2 additions & 0 deletions Gemfile.lock
Expand Up @@ -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
Expand All @@ -44,3 +45,4 @@ DEPENDENCIES
jeweler (~> 1.6.4)
rcov
rspec (~> 2.3.0)
sqlite3
24 changes: 16 additions & 8 deletions README.rdoc
@@ -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

Expand All @@ -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.
32 changes: 27 additions & 5 deletions lib/active_record/clone.rb
@@ -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

Expand All @@ -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

Expand All @@ -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
Expand Down
17 changes: 14 additions & 3 deletions spec/activerecord_clone_spec.rb
@@ -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
4 changes: 2 additions & 2 deletions spec/spec_helper.rb
Expand Up @@ -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|

Expand Down
1 change: 1 addition & 0 deletions spec/support/data.rb
@@ -0,0 +1 @@
Post.create(:text => "Post!")
3 changes: 3 additions & 0 deletions spec/support/models.rb
@@ -0,0 +1,3 @@
class Post < ActiveRecord::Base

end
8 changes: 8 additions & 0 deletions spec/support/schema.rb
@@ -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.