Skip to content

Commit

Permalink
Merge pull request #2 from d3sw/new_name
Browse files Browse the repository at this point in the history
Change name
  • Loading branch information
aalvarado authored Oct 6, 2017
2 parents 3f00a60 + 92c565a commit 9918ac6
Show file tree
Hide file tree
Showing 11 changed files with 82 additions and 89 deletions.
2 changes: 1 addition & 1 deletion Gemfile
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
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# ActiveRecord::QuickClone [![Build Status](https://travis-ci.org/aalvarado/active_record-quick_clone.svg?branch=master)](https://travis-ci.org/aalvarado/active_record-quick_clone)
# QuickClone [![Build Status](https://travis-ci.org/d3sw/quick_clone.svg?branch=master)](https://travis-ci.org/d3sw/quick_clone) [![Coverage Status](https://coveralls.io/repos/github/d3sw/quick_clone/badge.svg?branch=master)](https://coveralls.io/github/d3sw/quick_clone?branch=master)

Quickly clone an ActiveRecord object by providing a filter of attributes names.

Expand All @@ -7,7 +7,7 @@ Quickly clone an ActiveRecord object by providing a filter of attributes names.
Add this line to your application's Gemfile:

```ruby
gem 'active_record-quick_clone'
gem 'quick_clone'
```

And then execute:
Expand All @@ -16,7 +16,7 @@ And then execute:

Or install it yourself as:

$ gem install active_record-quick_clone
$ gem install quick_clone

## Usage

Expand Down Expand Up @@ -52,5 +52,5 @@ To install this gem onto your local machine, run `bundle exec rake install`. To

## Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/active_record-quick_clone.
Bug reports and pull requests are welcome on GitHub at https://github.com/d3dw/quick_clone.

7 changes: 0 additions & 7 deletions lib/active_record/quick_clone.rb

This file was deleted.

65 changes: 0 additions & 65 deletions lib/active_record/quick_clone/cloner.rb

This file was deleted.

5 changes: 0 additions & 5 deletions lib/active_record/quick_clone/version.rb

This file was deleted.

2 changes: 2 additions & 0 deletions lib/quick_clone.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
require 'quick_clone/version'
require 'quick_clone/cloner'
63 changes: 63 additions & 0 deletions lib/quick_clone/cloner.rb
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
3 changes: 3 additions & 0 deletions lib/quick_clone/version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module QuickClone
VERSION = "0.1.0".freeze
end
9 changes: 5 additions & 4 deletions active_record-quick_clone.gemspec → quick_clone.gemspec
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.
Expand All @@ -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"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
describe ActiveRecord::QuickClone::Cloner do
describe QuickClone::Cloner do
let(:user_name) { 'bitshifter' }
let(:user_alias) { 'teamsleep' }
let(:user) { User.create name: user_name, alias: user_alias }
Expand Down
5 changes: 3 additions & 2 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
require 'bundler/setup'
require 'active_record'
require 'active_record/quick_clone'
require 'quick_clone'
require 'support/schema'
require 'support/models'
require 'pry'

require 'coveralls'
Coveralls.wear!

RSpec.configure do |config|
# Enable flags like --only-failures and --next-failure
Expand Down

0 comments on commit 9918ac6

Please sign in to comment.