activerecord_clone 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -13,3 +13,7 @@ group :development do
13
13
  gem "jeweler", "~> 1.6.4"
14
14
  gem "rcov", ">= 0"
15
15
  end
16
+
17
+ group :testing do
18
+ gem 'sqlite3'
19
+ end
data/Gemfile.lock CHANGED
@@ -32,6 +32,7 @@ GEM
32
32
  rspec-expectations (2.3.0)
33
33
  diff-lcs (~> 1.1.2)
34
34
  rspec-mocks (2.3.0)
35
+ sqlite3 (1.3.4)
35
36
  tzinfo (0.3.30)
36
37
 
37
38
  PLATFORMS
@@ -44,3 +45,4 @@ DEPENDENCIES
44
45
  jeweler (~> 1.6.4)
45
46
  rcov
46
47
  rspec (~> 2.3.0)
48
+ sqlite3
data/README.rdoc CHANGED
@@ -1,6 +1,20 @@
1
1
  = activerecord_clone
2
2
 
3
- Description goes here.
3
+ Handles a simple task of cloning all attributes of a AR object
4
+ Default behaviour is to not clone the foreign_keys.
5
+
6
+ Possible options is:
7
+ :only => [] # only clone these attributes
8
+ :exclude => [] # Exlude these attributes, default is :id
9
+ :skip_relations => true|false #default is true
10
+
11
+ Can be configured either on a model layer using
12
+
13
+ class MyModel < ActiveRecord::Base
14
+ can_clone
15
+ end
16
+
17
+ Or can be configured upon the call to clone_ar.
4
18
 
5
19
  == Contributing to activerecord_clone
6
20
 
@@ -10,10 +24,4 @@ Description goes here.
10
24
  * Start a feature/bugfix branch
11
25
  * Commit and push until you are happy with your contribution
12
26
  * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
- * 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.
14
-
15
- == Copyright
16
-
17
- Copyright (c) 2011 science. See LICENSE.txt for
18
- further details.
19
-
27
+ * 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.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.1
1
+ 0.1.0
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "activerecord_clone"
8
- s.version = "0.0.1"
8
+ s.version = "0.1.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["science", "buffpojken"]
12
- s.date = "2011-10-18"
12
+ s.date = "2011-10-19"
13
13
  s.description = "Provides the method of 'clone_ar' that can clone your current object.\n Accepts options on both class level and method call level."
14
14
  s.email = "emil.palm@qubator.com"
15
15
  s.extra_rdoc_files = [
@@ -29,7 +29,10 @@ Gem::Specification.new do |s|
29
29
  "lib/active_record/clone.rb",
30
30
  "lib/activerecord_clone.rb",
31
31
  "spec/activerecord_clone_spec.rb",
32
- "spec/spec_helper.rb"
32
+ "spec/spec_helper.rb",
33
+ "spec/support/data.rb",
34
+ "spec/support/models.rb",
35
+ "spec/support/schema.rb"
33
36
  ]
34
37
  s.homepage = "http://github.com/mrevilme/activerecord_clone"
35
38
  s.licenses = ["MIT"]
@@ -1,4 +1,25 @@
1
+ require 'active_support'
2
+ require 'active_record'
3
+
1
4
  module ActiveRecord
5
+ # == Active Model Clone
6
+ #
7
+ # Handles a simple task of cloning all attributes of a AR object
8
+ # Default behaviour is to not clone the foreign_keys.
9
+ #
10
+ # Possible options is:
11
+ # :only => [] # only clone these attributes
12
+ # :exclude => [] # Exlude these attributes, default is :id
13
+ # :skip_relations => true|false #default is true
14
+ #
15
+ # Can be configured either on a model layer using
16
+ #
17
+ # class MyModel < ActiveRecord::Base
18
+ # can_clone
19
+ # end
20
+ #
21
+ # Or can be configured upon the call to clone_ar.
22
+
2
23
  module Clone
3
24
  extend ActiveSupport::Concern
4
25
 
@@ -10,15 +31,16 @@ module ActiveRecord
10
31
  end
11
32
 
12
33
  private
13
-
34
+ # :nodoc
14
35
  def foreing_keys
15
36
  self.reflect_on_all_associations.map { |assoc| assoc.association_foreign_key }
16
37
  end
17
38
 
39
+ # :nodoc
18
40
  def default_options
19
41
  {
20
42
  :skip_relations => true,
21
- :excluded => []
43
+ :excluded => [:id]
22
44
  }
23
45
  end
24
46
 
@@ -28,13 +50,13 @@ module ActiveRecord
28
50
  module InstanceMethods
29
51
 
30
52
  def clone_ar(options={})
31
- options = Account.instance_variable_get(:@options).merge(options)
53
+ options = (self.instance_variable_get(:@options) ? self.instance_variable_get(:@options) : self.class.send(:default_options)).merge(options)
32
54
  attrs = []
33
55
  if options[:only] and options[:only].is_a? Array
34
- attrs = self.attributes.reject {|item| options[:only].include? item}
56
+ attrs = self.attribute_names.reject {|item| options[:only].include? item}
35
57
  else
36
58
  excluded = options[:excluded] + (options[:skip_relations] ? self.class.send(:foreing_keys) : [])
37
- attrs = self.attribute_names
59
+ attrs = self.attribute_names.reject { |item| excluded.include? item}
38
60
  end
39
61
 
40
62
  newObj = self.class.new
@@ -1,7 +1,18 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
2
 
3
3
  describe "ActiverecordClone" do
4
- it "fails" do
5
- fail "hey buddy, you should probably rename this file and start specing for real"
4
+ context "clone_ar" do
5
+ it "clone_ar no options" do
6
+ post = Post.first
7
+ clone = post.clone_ar
8
+ clone.text.should include("Post!")
9
+ end
10
+ it "clone_ar exclude text" do
11
+ post = Post.first
12
+ clone = post.clone_ar :excluded => [:text]
13
+ clone.text == nil
14
+ end
15
+
6
16
  end
7
- end
17
+
18
+ end
data/spec/spec_helper.rb CHANGED
@@ -2,10 +2,10 @@ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
2
  $LOAD_PATH.unshift(File.dirname(__FILE__))
3
3
  require 'rspec'
4
4
  require 'activerecord_clone'
5
-
5
+ ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => File.dirname(__FILE__) + "/ar_clone_test.sqlite3")
6
6
  # Requires supporting files with custom matchers and macros, etc,
7
7
  # in ./support/ and its subdirectories.
8
- Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
8
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].reverse.each {|f| require f}
9
9
 
10
10
  RSpec.configure do |config|
11
11
 
@@ -0,0 +1 @@
1
+ Post.create(:text => "Post!")
@@ -0,0 +1,3 @@
1
+ class Post < ActiveRecord::Base
2
+
3
+ end
@@ -0,0 +1,8 @@
1
+ ActiveRecord::Schema.define do
2
+ self.verbose = false
3
+
4
+ create_table :posts, :force => true do |t|
5
+ t.string :text
6
+ t.timestamps
7
+ end
8
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerecord_clone
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,11 +10,11 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2011-10-18 00:00:00.000000000Z
13
+ date: 2011-10-19 00:00:00.000000000Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: activesupport
17
- requirement: &2152665260 !ruby/object:Gem::Requirement
17
+ requirement: &2152873640 !ruby/object:Gem::Requirement
18
18
  none: false
19
19
  requirements:
20
20
  - - ! '>='
@@ -22,10 +22,10 @@ dependencies:
22
22
  version: '0'
23
23
  type: :runtime
24
24
  prerelease: false
25
- version_requirements: *2152665260
25
+ version_requirements: *2152873640
26
26
  - !ruby/object:Gem::Dependency
27
27
  name: activerecord
28
- requirement: &2152664780 !ruby/object:Gem::Requirement
28
+ requirement: &2152873160 !ruby/object:Gem::Requirement
29
29
  none: false
30
30
  requirements:
31
31
  - - ! '>='
@@ -33,10 +33,10 @@ dependencies:
33
33
  version: '0'
34
34
  type: :runtime
35
35
  prerelease: false
36
- version_requirements: *2152664780
36
+ version_requirements: *2152873160
37
37
  - !ruby/object:Gem::Dependency
38
38
  name: rspec
39
- requirement: &2152664300 !ruby/object:Gem::Requirement
39
+ requirement: &2152872680 !ruby/object:Gem::Requirement
40
40
  none: false
41
41
  requirements:
42
42
  - - ~>
@@ -44,10 +44,10 @@ dependencies:
44
44
  version: 2.3.0
45
45
  type: :development
46
46
  prerelease: false
47
- version_requirements: *2152664300
47
+ version_requirements: *2152872680
48
48
  - !ruby/object:Gem::Dependency
49
49
  name: bundler
50
- requirement: &2152663820 !ruby/object:Gem::Requirement
50
+ requirement: &2152872200 !ruby/object:Gem::Requirement
51
51
  none: false
52
52
  requirements:
53
53
  - - ~>
@@ -55,10 +55,10 @@ dependencies:
55
55
  version: 1.0.0
56
56
  type: :development
57
57
  prerelease: false
58
- version_requirements: *2152663820
58
+ version_requirements: *2152872200
59
59
  - !ruby/object:Gem::Dependency
60
60
  name: jeweler
61
- requirement: &2152663340 !ruby/object:Gem::Requirement
61
+ requirement: &2152871720 !ruby/object:Gem::Requirement
62
62
  none: false
63
63
  requirements:
64
64
  - - ~>
@@ -66,10 +66,10 @@ dependencies:
66
66
  version: 1.6.4
67
67
  type: :development
68
68
  prerelease: false
69
- version_requirements: *2152663340
69
+ version_requirements: *2152871720
70
70
  - !ruby/object:Gem::Dependency
71
71
  name: rcov
72
- requirement: &2152662860 !ruby/object:Gem::Requirement
72
+ requirement: &2168729400 !ruby/object:Gem::Requirement
73
73
  none: false
74
74
  requirements:
75
75
  - - ! '>='
@@ -77,7 +77,7 @@ dependencies:
77
77
  version: '0'
78
78
  type: :development
79
79
  prerelease: false
80
- version_requirements: *2152662860
80
+ version_requirements: *2168729400
81
81
  description: ! "Provides the method of 'clone_ar' that can clone your current object.\n
82
82
  \ Accepts options on both class level and method call level."
83
83
  email: emil.palm@qubator.com
@@ -100,6 +100,9 @@ files:
100
100
  - lib/activerecord_clone.rb
101
101
  - spec/activerecord_clone_spec.rb
102
102
  - spec/spec_helper.rb
103
+ - spec/support/data.rb
104
+ - spec/support/models.rb
105
+ - spec/support/schema.rb
103
106
  homepage: http://github.com/mrevilme/activerecord_clone
104
107
  licenses:
105
108
  - MIT
@@ -115,7 +118,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
115
118
  version: '0'
116
119
  segments:
117
120
  - 0
118
- hash: -487633292764043746
121
+ hash: -753603999457149900
119
122
  required_rubygems_version: !ruby/object:Gem::Requirement
120
123
  none: false
121
124
  requirements: