duplicate_it 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/duplicate_it.rb +51 -12
  2. metadata +6 -4
@@ -4,8 +4,8 @@ module DuplicateIt
4
4
  attr_reader :record
5
5
 
6
6
  def initialize(record)
7
- puts "Error saving #{record}"
8
- puts "Error trace: #{record.errors.inspect}"
7
+ Rails.logger.debug "Error saving #{record}"
8
+ Rails.logger.debug "Error trace: #{record.errors.inspect}"
9
9
  @record = record
10
10
  end
11
11
  end
@@ -15,11 +15,45 @@ module DuplicateIt
15
15
  end
16
16
 
17
17
  module ClassMethods
18
- # #Test method
19
- # def duplicate_it(*args)
20
- # puts "------- from duplicate_it --------"
21
- # puts args.inspect
22
- # end
18
+
19
+ #Setter method for ignorable_attributes
20
+ def ignore_attributes_while_duplicating(*args)
21
+ @@ignorable_attributes = []
22
+ args.each do |argument|
23
+ if self.attribute_names.include?(argument.to_s)
24
+ @@ignorable_attributes << argument.to_s
25
+ else
26
+ Rails.logger.debug "DuplicateIt: Invalid attribute '#{argument}' in use."
27
+ end
28
+ end
29
+ @@ignorable_attributes = @@ignorable_attributes.uniq
30
+ end
31
+
32
+ #Getter method for ignorable_attributes
33
+ def ignorable_attributes
34
+ @@ignorable_attributes ||= ["id", "type", "created_at", "updated_at"]
35
+ @@ignorable_attributes
36
+ end
37
+
38
+ #Setter method for ignorable_associations
39
+ def ignore_associations_while_duplicating(*args)
40
+ @@ignorable_associations = []
41
+ args.each do |argument|
42
+
43
+ if self.reflect_on_all_associations(:has_many).collect { |has_many_reflection| has_many_reflection.name }.include?(argument.to_sym)
44
+ @@ignorable_associations << argument.to_s
45
+ else
46
+ Rails.logger.debug "DuplicateIt: Invalid association '#{argument}' in use."
47
+ end
48
+ end
49
+ @@ignorable_associations = @@ignorable_associations.uniq
50
+ end
51
+
52
+ #Getter method for ignorable_associations
53
+ def ignorable_associations
54
+ @@ignorable_associations ||= []
55
+ @@ignorable_associations.collect { |x| x.to_sym }
56
+ end
23
57
  end
24
58
 
25
59
  def duplicate
@@ -30,9 +64,9 @@ module DuplicateIt
30
64
 
31
65
  #Creates a duplicate record from an existing record, ignoring 'ignorable_attributes'
32
66
  def create_duplicate_record(record)
33
- ignorable_attributes = ["id", "type", "created_at", "updated_at"]
67
+ #ignorable_attributes = ["id", "type", "created_at", "updated_at"]
34
68
  record_attributes = record.attributes
35
- ignorable_attributes.each { |ignorable_attribute| record_attributes.delete(ignorable_attribute) }
69
+ self.class.ignorable_attributes.each { |ignorable_attribute| record_attributes.delete(ignorable_attribute) }
36
70
 
37
71
  duplicate_record = record.class.new(record_attributes)
38
72
  if duplicate_record.save
@@ -63,9 +97,13 @@ module DuplicateIt
63
97
  duplicate_record = create_duplicate_record(record)
64
98
  unless duplicate_record.nil?
65
99
  record.class.reflect_on_all_associations(:has_many).each do |has_many_reflection|
66
- if record.send(has_many_reflection.name).count > 0
67
- record.send(has_many_reflection.name).each do |has_many_record|
68
- duplicate_record.send("#{has_many_reflection.name}=", (duplicate_record.send("#{has_many_reflection.name}") + [create_duplicate_of_self(has_many_record)]))
100
+ if self.class.ignorable_associations.include?(has_many_reflection.name)
101
+ Rails.logger.info "DuplicateIt: Ignoring association '#{has_many_reflection}' for #{record.inspect}."
102
+ else
103
+ if record.send(has_many_reflection.name).count > 0
104
+ record.send(has_many_reflection.name).each do |has_many_record|
105
+ duplicate_record.send("#{has_many_reflection.name}=", (duplicate_record.send("#{has_many_reflection.name}") + [create_duplicate_of_self(has_many_record)]))
106
+ end
69
107
  end
70
108
  end
71
109
  end
@@ -79,3 +117,4 @@ end
79
117
  class ActiveRecord::Base
80
118
  include DuplicateIt
81
119
  end
120
+
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: duplicate_it
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,10 +9,12 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-04-04 00:00:00.000000000Z
12
+ date: 2012-04-13 00:00:00.000000000Z
13
13
  dependencies: []
14
- description: A simple gem to dulicate a model instance, including its 'has_many' assocaited
15
- objects. Fixed typo and added standard error exception
14
+ description: A simple gem to duplicate a model instance, including its 'has_many'
15
+ assocaitions. Added capability to add ignorable_attributes and ignorable_associations
16
+ via ignore_attributes_while_duplicating and ignore_associations_while_duplicating
17
+ methods (available at the class level)
16
18
  email: praveen.kumar.sinha@gmail.com
17
19
  executables: []
18
20
  extensions: []