-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from d3sw/new_name
Change name
- Loading branch information
Showing
11 changed files
with
82 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
source 'https://rubygems.org' | ||
|
||
# Specify your gem's dependencies in active_record-quick_clone.gemspec | ||
# Specify your gem's dependencies in quick_clone.gemspec | ||
gemspec |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
require 'quick_clone/version' | ||
require 'quick_clone/cloner' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
module QuickClone | ||
class Cloner | ||
ATTRIBUTE_CLASSES = %w(String Symbol).freeze | ||
|
||
ABSOLUTE_EXCLUDE = [ | ||
:id, | ||
:created_at, | ||
:updated_at | ||
].freeze | ||
|
||
SINGULAR_ASSOCIATIONS = [ | ||
:has_one, | ||
:belongs_to | ||
].freeze | ||
|
||
def self.clone_from(original_object, filter) | ||
new(original_object, filter).clone | ||
end | ||
|
||
def initialize(original_object, filter) | ||
@filter = filter | ||
|
||
@original_object = original_object | ||
@new_object = original_object.class.new | ||
end | ||
|
||
def clone | ||
determine_clone(@original_object, @new_object, @filter) | ||
@new_object | ||
end | ||
|
||
private | ||
|
||
def determine_clone(original_object, new_object, filter) | ||
filter.each do |field| | ||
if ATTRIBUTE_CLASSES.include? field.class.name | ||
next if ABSOLUTE_EXCLUDE.include?(field.to_sym) | ||
copy_attribute(original_object, new_object, field) | ||
elsif field.is_a? Hash | ||
field.each do |association_name, association_filter| | ||
copy_association(original_object, new_object, association_name, association_filter) | ||
end | ||
end | ||
end | ||
end | ||
|
||
def copy_attribute(original_object, new_object, field) | ||
new_object.send("#{field}=", original_object.send(field)) | ||
end | ||
|
||
def copy_association(original_object, new_object, association_name, association_filter) | ||
if SINGULAR_ASSOCIATIONS.include?(original_object.class.reflect_on_association(association_name).macro) | ||
determine_clone(original_object.send(association_name), | ||
new_object.send("build_#{association_name}"), | ||
association_filter) | ||
else | ||
original_object.send(association_name).each do |association_original_object| | ||
determine_clone(association_original_object, new_object.send(association_name).build, association_filter) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module QuickClone | ||
VERSION = "0.1.0".freeze | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,15 @@ | ||
# coding: utf-8 | ||
lib = File.expand_path('../lib', __FILE__) | ||
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) | ||
require 'active_record/quick_clone/version' | ||
require 'quick_clone/version' | ||
|
||
Gem::Specification.new do |spec| | ||
spec.name = "active_record-quick_clone" | ||
spec.version = ActiveRecord::QuickClone::VERSION | ||
spec.name = "quick_clone" | ||
spec.version = QuickClone::VERSION | ||
spec.authors = ["Adan Alvarado"] | ||
spec.email = ["[email protected]"] | ||
spec.summary = %q{Provide a simple way of cloning ActiveRecord Records} | ||
spec.homepage = "https://github.com/aalvarado/active_record-quick_clone" | ||
spec.homepage = "https://github.com/aalvarado/quick_clone" | ||
|
||
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host' | ||
# to allow pushing to a single host or delete this section to allow pushing to any host. | ||
|
@@ -27,6 +27,7 @@ Gem::Specification.new do |spec| | |
|
||
spec.add_dependency "activerecord", ">= 4.0", "< 6.0" | ||
|
||
spec.add_development_dependency "coveralls" | ||
spec.add_development_dependency "bundler", "~> 1.14" | ||
spec.add_development_dependency "pry" | ||
spec.add_development_dependency "rake", "~> 10.0" | ||
|
2 changes: 1 addition & 1 deletion
2
.../active_record/quick_clone/cloner_spec.rb → spec/quick_clone/cloner_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters