From 3bfcea6dfcee8b59e8d6d309a1bca0f4482b5fd7 Mon Sep 17 00:00:00 2001 From: Emil Palm Date: Wed, 19 Oct 2011 11:51:57 +0200 Subject: [PATCH] Adding tests and rdoc --- Gemfile | 4 ++++ Gemfile.lock | 2 ++ README.rdoc | 24 ++++++++++++++++-------- lib/active_record/clone.rb | 32 +++++++++++++++++++++++++++----- spec/activerecord_clone_spec.rb | 17 ++++++++++++++--- spec/spec_helper.rb | 4 ++-- spec/support/data.rb | 1 + spec/support/models.rb | 3 +++ spec/support/schema.rb | 8 ++++++++ 9 files changed, 77 insertions(+), 18 deletions(-) create mode 100644 spec/support/data.rb create mode 100644 spec/support/models.rb create mode 100644 spec/support/schema.rb diff --git a/Gemfile b/Gemfile index 8ac09ec..4ed4141 100644 --- a/Gemfile +++ b/Gemfile @@ -13,3 +13,7 @@ group :development do gem "jeweler", "~> 1.6.4" gem "rcov", ">= 0" end + +group :testing do + gem 'sqlite3' +end diff --git a/Gemfile.lock b/Gemfile.lock index 7da5120..0d3b35c 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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 diff --git a/README.rdoc b/README.rdoc index 61f12d7..1de4ecc 100644 --- a/README.rdoc +++ b/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 @@ -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. \ No newline at end of file diff --git a/lib/active_record/clone.rb b/lib/active_record/clone.rb index 9789cbd..4b8454e 100644 --- a/lib/active_record/clone.rb +++ b/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 @@ -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 diff --git a/spec/activerecord_clone_spec.rb b/spec/activerecord_clone_spec.rb index af5b6e3..dfa89f5 100644 --- a/spec/activerecord_clone_spec.rb +++ b/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 \ No newline at end of file diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 658dfeb..5d28f3e 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -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| diff --git a/spec/support/data.rb b/spec/support/data.rb new file mode 100644 index 0000000..8fe54a8 --- /dev/null +++ b/spec/support/data.rb @@ -0,0 +1 @@ +Post.create(:text => "Post!") \ No newline at end of file diff --git a/spec/support/models.rb b/spec/support/models.rb new file mode 100644 index 0000000..b3cb889 --- /dev/null +++ b/spec/support/models.rb @@ -0,0 +1,3 @@ +class Post < ActiveRecord::Base + +end \ No newline at end of file diff --git a/spec/support/schema.rb b/spec/support/schema.rb new file mode 100644 index 0000000..e49555a --- /dev/null +++ b/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 \ No newline at end of file