├── .gitignore ├── .travis.yml ├── Gemfile ├── LICENSE ├── MIT-LICENSE ├── README.md ├── Rakefile ├── forceps.gemspec ├── lib ├── forceps.rb ├── forceps │ ├── acts_as_copyable_model.rb │ ├── client.rb │ └── version.rb └── tasks │ └── forceps_tasks.rake └── test ├── blacklist_model_name_test.rb ├── callbacks_test.rb ├── clone_structures_test.rb ├── dummy ├── README.rdoc ├── Rakefile ├── app │ ├── assets │ │ ├── images │ │ │ └── .keep │ │ ├── javascripts │ │ │ └── application.js │ │ └── stylesheets │ │ │ └── application.css │ ├── controllers │ │ ├── application_controller.rb │ │ └── concerns │ │ │ └── .keep │ ├── helpers │ │ └── application_helper.rb │ ├── mailers │ │ └── .keep │ ├── models │ │ ├── .keep │ │ ├── address.rb │ │ ├── car.rb │ │ ├── cars │ │ │ └── german │ │ │ │ ├── compact_car.rb │ │ │ │ └── sports_car.rb │ │ ├── concerns │ │ │ └── .keep │ │ ├── invoice.rb │ │ ├── line_item.rb │ │ ├── product.rb │ │ ├── tag.rb │ │ └── user.rb │ └── views │ │ └── layouts │ │ └── application.html.erb ├── bin │ ├── bundle │ ├── rails │ └── rake ├── config.ru ├── config │ ├── application.rb │ ├── boot.rb │ ├── database.yml │ ├── environment.rb │ ├── environments │ │ ├── development.rb │ │ ├── production.rb │ │ └── test.rb │ ├── initializers │ │ ├── backtrace_silencers.rb │ │ ├── filter_parameter_logging.rb │ │ ├── inflections.rb │ │ ├── mime_types.rb │ │ ├── secret_token.rb │ │ ├── session_store.rb │ │ └── wrap_parameters.rb │ ├── locales │ │ └── en.yml │ └── routes.rb ├── db │ ├── migrate │ │ ├── 20140101112114_create_invoices.rb │ │ ├── 20140102125046_add_number_to_invoices.rb │ │ ├── 20140103172515_create_users.rb │ │ ├── 20140103172643_add_user_id_to_invoices.rb │ │ ├── 20140104145204_create_line_items.rb │ │ ├── 20140104145227_create_products.rb │ │ ├── 20140104150906_create_addresses.rb │ │ ├── 20140114111137_create_products_tags_join_table.rb │ │ ├── 20140114111219_create_tags.rb │ │ └── 20140117112556_add_type_to_products.rb │ └── schema.rb ├── lib │ └── assets │ │ └── .keep ├── log │ └── .keep └── public │ ├── 404.html │ ├── 422.html │ ├── 500.html │ └── favicon.ico ├── exclude_records_test.rb ├── fixtures ├── invoices.yml └── users.yml ├── reuse_existing_records_test.rb ├── sti_test.rb ├── support ├── minitest_ext.rb ├── remote_address.rb ├── remote_invoice.rb ├── remote_line_item.rb ├── remote_product.rb ├── remote_tag.rb └── remote_user.rb └── test_helper.rb /.gitignore: -------------------------------------------------------------------------------- 1 | .bundle/ 2 | log/*.log 3 | pkg/ 4 | test/dummy/db/*.sqlite3 5 | test/dummy/db/*.sqlite3-journal 6 | test/dummy/log/*.log 7 | test/dummy/tmp/ 8 | test/dummy/.sass-cache 9 | .idea/ 10 | Gemfile.lock 11 | .ruby-version 12 | .ruby-gemset 13 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: ruby 2 | env: 3 | - "RAILS_VERSION=4.2.7.1" 4 | 5 | rvm: 6 | - 2.3.2 7 | before_script: 8 | - cd test/dummy 9 | - RAILS_ENV=test rake db:create db:migrate 10 | - RAILS_ENV=remote rake db:create db:migrate 11 | script: 12 | - cd ${TRAVIS_BUILD_DIR} 13 | - rake test 14 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source "https://rubygems.org" 2 | 3 | # Declare your gem's dependencies in forceps.gemspec. 4 | # Bundler will treat runtime dependencies like base dependencies, and 5 | # development dependencies will be added by default to the :development group. 6 | gemspec 7 | 8 | # Declare any dependencies that are still in development here instead of in 9 | # your gemspec. These might include edge Rails or gems from your path or 10 | # Git. Remember to move these dependencies to your gemspec before releasing 11 | # your gem to rubygems.org. 12 | 13 | # To use debugger 14 | # gem 'debugger' 15 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2013 Jorge Manrubia 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /MIT-LICENSE: -------------------------------------------------------------------------------- 1 | Copyright 2013 YOURNAME 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining 4 | a copy of this software and associated documentation files (the 5 | "Software"), to deal in the Software without restriction, including 6 | without limitation the rights to use, copy, modify, merge, publish, 7 | distribute, sublicense, and/or sell copies of the Software, and to 8 | permit persons to whom the Software is furnished to do so, subject to 9 | the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be 12 | included in all copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 15 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 17 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 18 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 19 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 20 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Forceps 2 | 3 | Have you ever needed to copy a given user from a production database into your local box in order to debug some obscure bug? 4 | 5 | Forceps lets you copy related models from one database into another. The source and target databases must support an active record connection. Typically, your source database is a remote production database and your target database is a local development one. 6 | 7 | ## Installing 8 | 9 | In your `Gemfile`: 10 | 11 | ```ruby 12 | gem 'forceps' 13 | ``` 14 | 15 | ## Usage 16 | 17 | ### Configure a remote database connection 18 | 19 | Add a node labeled `remote` to your `database.yml` 20 | 21 | ```ruby 22 | remote: 23 | adapter: mysql2 24 | host: somehost.com 25 | port: 5432 26 | username: someuser 27 | password: somepassword 28 | database: somedatabase 29 | encoding: utf8 30 | ``` 31 | 32 | The low level connection mechanism doesn't matter thanks to Active Record. For example, this gem has been tested on [MySQL tunneled over SSH](http://chxo.com/be2/20040511_5667.html) and [on Heroku using Postgres credentials](https://devcenter.heroku.com/articles/heroku-postgresql#pg-credentials). 33 | 34 | ### Copy models 35 | 36 | To configure forceps you must invoke: 37 | 38 | ```ruby 39 | Forceps.configure 40 | ``` 41 | 42 | This will take each Active Record model defined in the project and define a class that will mirror it in the remote database. These classes will live in the `Forceps::Remote` namespace. 43 | 44 | For example, given: 45 | 46 | ```ruby 47 | class Invoice < ActiveRecord::Base 48 | end 49 | ``` 50 | 51 | You can use the generated class to download some invoice: 52 | 53 | ```ruby 54 | Forceps::Remote::Invoice.find(1234).copy_to_local 55 | ``` 56 | 57 | By default, Forceps will: 58 | 59 | 1. Create a new local object copying all the attributes of the remote object 60 | 2. Explore all the associations of the remote object, and copy the related objects applying (1) 61 | 62 | In most real-life situations you will want to tune this behavior: 63 | 64 | #### Exclude associated models 65 | 66 | Forceps lets you exclude associations from the automatic discovery process, in order to avoid downloading big chunks of unrelated data: 67 | 68 | For example: 69 | 70 | ```Ruby 71 | class Invoice < ActiveRecord::Base 72 | has_many :line_items 73 | end 74 | 75 | class LineItem < ActiveRecord::Base 76 | belongs_to :product 77 | end 78 | 79 | class Product < ActiveRecord::Base 80 | has_many :line_items 81 | end 82 | ``` 83 | 84 | If you execute: 85 | 86 | ```ruby 87 | Forceps.configure 88 | Forceps::Remote::Invoice.find(1234).copy_to_local 89 | ``` 90 | 91 | It could end up downloading all the line items in the database: 92 | 93 | ```ruby 94 | invoice 1 95 | line item 1 96 | product 1 97 | line item 2 # we are not interested in these line items 98 | line item 3 99 | line item 4 100 | line item 5 101 | ``` 102 | The option `exclude` lets you specify which associations you are not interested in: 103 | 104 | ```ruby 105 | Forceps.configure exclude: {Product => [:line_items]} 106 | ``` 107 | 108 | #### Reuse models 109 | 110 | Sometimes you don't want to clone an object. Instead, you want to use one that already exists in your database. 111 | 112 | For example: 113 | 114 | ```ruby 115 | class Product < ActiveRecord::Base 116 | has_and_belongs_to_many :tags 117 | end 118 | ``` 119 | 120 | What about if the tags are reused across all the products in your database? For situations like this you can use the `reuse` option. 121 | 122 | It can be used providing the name of a column that can be used to find the object: 123 | 124 | ```ruby 125 | Forceps.configure reuse: {Tag => :name} 126 | Forceps::Remote::Product.find(1234).copy_to_local # for each remote tag, it will try to find a tag with the same name 127 | ``` 128 | 129 | And, more generically, with a lambda that takes the remote object and returns the matched object (or nil if not found). The equivalent to the previous example would be: 130 | 131 | ```ruby 132 | Forceps.configure reuse: {Tag => ->(remote_tag) {Tag.find_by_name remote_tag.name}} 133 | Forceps::Remote::Product.find(1234).copy_to_local 134 | ``` 135 | 136 | When a `reuse` option is provided but the model can't be found locally, it will be cloned normally. 137 | 138 | #### Callbacks 139 | 140 | You can configure callbacks that will be invoked after each object is copied. You can use these callbacks to perform additional operations that are needed to perform the copy. For example: copying S3 assets: 141 | 142 | ```ruby 143 | Forceps.configure after_each: { 144 | Invoice => lambda do |local_invoice, remote_invoice| 145 | ... 146 | end 147 | } 148 | ``` 149 | 150 | ### Ignore a model by name 151 | 152 | Sometimes you have several models that all have a relationship to a model that you want to ignore. 153 | 154 | In this case it could be difficult to explicitly ignore each of those relationships. Instead, you can completely ignore a model anytime forceps tries to copy it. 155 | 156 | ```ruby 157 | Forceps.configure ignore_model: ['LineItem'] 158 | } 159 | ``` 160 | 161 | Then anytime this model is reached while copying a relation, it will be ignored. 162 | 163 | ### Rails and lazy loading 164 | 165 | In development, Rails loads classes lazily as they are used. Forceps will only know how to handle those classes defined when `Forceps.configure` is executed. You can make sure that all the Rails models are loaded before executing `Forceps.configure` with: 166 | 167 | ```ruby 168 | Rails.application.eager_load! 169 | ``` 170 | 171 | ### Using the generated remote classes 172 | 173 | When you invoke `Forceps.configure`, for each model class it will define an equivalent class in the namespace `Forceps::Remote`. These remote classes will be identical to the original ones with 2 differences: 174 | 175 | - They keep a connection with the remote database, instead of the connection defined by the current Rails environment. 176 | - Their associations are modified to reference the corresponding remote classes. 177 | 178 | These remote classes can be very handy if you want to prepare scripts that do something with your production data. 179 | 180 | ```ruby 181 | class Invoice < ActiveRecord::Base 182 | has_many :line_items 183 | end 184 | 185 | class LineItem < ActiveRecord::Base 186 | belongs_to :invoice 187 | end 188 | 189 | ... 190 | 191 | Forceps::Remote::Invoice.count # the number of invoices in the remote database 192 | invoice = Forceps::Remote::Invoice.find(1234) 193 | invoice.line_items.last.class # Forceps::Remote::LineItem 194 | ``` 195 | 196 | ## Compatibility 197 | 198 | Rails 3 and 4 199 | 200 | ## Run the test suite 201 | 202 | In order to run the test suite you must create the `test` and `remote` databases (both are local) 203 | 204 | ``` 205 | cd test/dummy 206 | RAILS_ENV=test rake db:create db:migrate 207 | RAILS_ENV=remote rake db:create db:migrate 208 | ``` 209 | 210 | ## Disclaimer 211 | 212 | - It is recommended to use a read-only connection for production databases. Forceps will never modify remote objects when copying them but prevention is definitely better than cure when it comes to production data. 213 | - Thanks to [bandzoogle](http://bandzoogle.com) for supporting the development of this project. 214 | 215 | Pull requests are welcomed! [Overview of how forceps works internally](http://jorgemanrubia.net/2014/02/04/forceps-import-models-from-remote-databases/). 216 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | begin 2 | require 'bundler/setup' 3 | rescue LoadError 4 | puts 'You must `gem install bundler` and `bundle install` to run rake tasks' 5 | end 6 | 7 | require 'rdoc/task' 8 | 9 | RDoc::Task.new(:rdoc) do |rdoc| 10 | rdoc.rdoc_dir = 'rdoc' 11 | rdoc.title = 'Forceps' 12 | rdoc.options << '--line-numbers' 13 | rdoc.rdoc_files.include('README.rdoc') 14 | rdoc.rdoc_files.include('lib/**/*.rb') 15 | end 16 | 17 | 18 | 19 | 20 | Bundler::GemHelper.install_tasks 21 | 22 | require 'rake/testtask' 23 | 24 | Rake::TestTask.new(:test) do |t| 25 | t.libs << 'lib' 26 | t.libs << 'test' 27 | t.pattern = 'test/**/*_test.rb' 28 | t.verbose = false 29 | end 30 | 31 | 32 | task default: :test 33 | -------------------------------------------------------------------------------- /forceps.gemspec: -------------------------------------------------------------------------------- 1 | $:.push File.expand_path("../lib", __FILE__) 2 | 3 | # Maintain your gem's version: 4 | require "forceps/version" 5 | 6 | # Describe your gem and declare its dependencies: 7 | Gem::Specification.new do |s| 8 | s.name = "forceps" 9 | s.version = Forceps::VERSION 10 | s.authors = ["Jorge Manrubia"] 11 | s.email = ["jorge.manrubia@gmail.com"] 12 | s.homepage = "https://github.com/jorgemanrubia/forceps" 13 | s.summary = "Copy active record models from remote databases" 14 | 15 | s.files = Dir["{app,config,db,lib}/**/*", "MIT-LICENSE", "Rakefile", "README.rdoc"] 16 | s.test_files = Dir["test/**/*"] 17 | 18 | if ENV['RAILS_VERSION'] 19 | s.add_dependency "rails", "= #{ENV['RAILS_VERSION']}" 20 | else 21 | s.add_dependency "rails", "~> 4.0" 22 | end 23 | 24 | s.add_dependency "logging", "~> 2.0.0" 25 | 26 | s.add_development_dependency "sqlite3", "~> 1.3.8" 27 | s.add_development_dependency "database_cleaner", "~> 1.2.0" 28 | s.add_development_dependency "awesome_print", "~> 1.2.0" 29 | s.add_development_dependency "minitest", "~> 5.0" 30 | end 31 | -------------------------------------------------------------------------------- /lib/forceps.rb: -------------------------------------------------------------------------------- 1 | require "forceps/client" 2 | require "forceps/acts_as_copyable_model" 3 | require 'logging' 4 | 5 | module Forceps 6 | def self.configure(options={}) 7 | client.configure(options) 8 | end 9 | 10 | def self.client 11 | @@client ||= Forceps::Client.new 12 | end 13 | 14 | def self.logger 15 | @@logger ||= begin 16 | logger = Logging.logger(STDOUT) 17 | logger.level = :debug 18 | logger 19 | end 20 | end 21 | 22 | def self.logger=(logger) 23 | @@logger = logger 24 | end 25 | 26 | module Remote 27 | 28 | end 29 | end 30 | 31 | -------------------------------------------------------------------------------- /lib/forceps/acts_as_copyable_model.rb: -------------------------------------------------------------------------------- 1 | module Forceps 2 | module ActsAsCopyableModel 3 | extend ActiveSupport::Concern 4 | 5 | def copy_to_local 6 | without_record_timestamps do 7 | DeepCopier.new(forceps_options).copy(self) 8 | end 9 | end 10 | 11 | private 12 | 13 | def without_record_timestamps 14 | self.class.base_class.record_timestamps = false 15 | yield 16 | ensure 17 | self.class.base_class.record_timestamps = true 18 | end 19 | 20 | def forceps_options 21 | Forceps.client.options 22 | end 23 | 24 | class DeepCopier 25 | attr_accessor :copied_remote_objects, :options, :level, :reused_local_objects 26 | 27 | def initialize(options) 28 | @copied_remote_objects = {} 29 | @reused_local_objects = Set.new 30 | @options = options 31 | @level = 0 32 | end 33 | 34 | def copy(remote_object) 35 | copy_associated_objects_in_belongs_to(remote_object) unless copied_remote_objects[remote_object] 36 | cached_local_copy(remote_object) || perform_copy(remote_object) 37 | end 38 | 39 | private 40 | 41 | def cached_local_copy(remote_object) 42 | cached_object = copied_remote_objects[remote_object] 43 | debug "#{as_trace(remote_object)} from cache..." if cached_object 44 | cached_object 45 | end 46 | 47 | def perform_copy(remote_object) 48 | copied_object = local_copy_with_simple_attributes(remote_object) 49 | copied_remote_objects[remote_object] = copied_object 50 | copy_associated_objects(copied_object, remote_object) unless was_reused?(copied_object) 51 | copied_object 52 | end 53 | 54 | def local_copy_with_simple_attributes(remote_object) 55 | if should_reuse_local_copy?(remote_object) 56 | find_or_clone_local_copy_with_simple_attributes(remote_object) 57 | else 58 | create_local_copy_with_simple_attributes(remote_object) 59 | end 60 | end 61 | 62 | def should_reuse_local_copy?(remote_object) 63 | finders_for_reusing_classes.include?(remote_object.class.base_class) 64 | end 65 | 66 | def finders_for_reusing_classes 67 | options[:reuse] || {} 68 | end 69 | 70 | def find_or_clone_local_copy_with_simple_attributes(remote_object) 71 | found_local_object = finder_for_remote_object(remote_object).call(remote_object) 72 | if found_local_object 73 | copy_simple_attributes(found_local_object, remote_object) 74 | reused_local_objects << found_local_object 75 | found_local_object 76 | else 77 | create_local_copy_with_simple_attributes(remote_object) 78 | end 79 | end 80 | 81 | def was_reused?(local_object) 82 | reused_local_objects.include? local_object 83 | end 84 | 85 | def find_local_copy_with_simple_attributes(remote_object) 86 | finder_for_remote_object(remote_object).call(remote_object) 87 | end 88 | 89 | def finder_for_remote_object(remote_object) 90 | finder = finders_for_reusing_classes[remote_object.class.base_class] 91 | finder = build_attribute_finder(remote_object, finder) if finder.is_a? Symbol 92 | finder 93 | end 94 | 95 | def build_attribute_finder(remote_object, attribute_name) 96 | value = remote_object.send(attribute_name) 97 | lambda do |object| 98 | object.class.base_class.where(attribute_name => value).first 99 | end 100 | end 101 | 102 | def create_local_copy_with_simple_attributes(remote_object) 103 | debug "#{as_trace(remote_object)} copying..." 104 | 105 | base_class = base_local_class_for(remote_object) 106 | 107 | disable_all_callbacks_for(base_class) 108 | 109 | cloned_object = base_class.new 110 | copy_attributes(cloned_object, simple_attributes_to_copy(remote_object)) 111 | cloned_object.save!(validate: false) 112 | invoke_callbacks(:after_each, cloned_object, remote_object) 113 | cloned_object 114 | end 115 | 116 | def base_local_class_for(remote_object) 117 | base_class = remote_object.class.base_class 118 | if has_sti_column?(remote_object) 119 | local_type = to_local_class_name(remote_object.type) 120 | base_class = local_type.constantize rescue base_class 121 | end 122 | base_class 123 | end 124 | 125 | def to_local_class_name(remote_class_name) 126 | remote_class_name.gsub('Forceps::Remote::', '') 127 | end 128 | 129 | def has_sti_column?(object) 130 | object.respond_to?(:type) && object.type.present? && object.type.is_a?(String) 131 | end 132 | 133 | def invoke_callbacks(callback_name, copied_object, remote_object) 134 | callback = callbacks_for(callback_name)[copied_object.class] 135 | return unless callback 136 | callback.call(copied_object, remote_object) 137 | end 138 | 139 | def callbacks_for(callback_name) 140 | options[callback_name] || {} 141 | end 142 | 143 | # Using setters explicitly to avoid having to mess with disabling mass protection in Rails 3 144 | def copy_attributes(target_object, attributes_map) 145 | make_type_attribute_point_to_local_class_if_needed(attributes_map) 146 | 147 | attributes_map.each do |attribute_name, attribute_value| 148 | target_object.send("#{attribute_name}=", attribute_value) rescue debug("The method '#{attribute_name}=' does not exist. Different schemas in the remote and local databases?") 149 | end 150 | end 151 | 152 | def make_type_attribute_point_to_local_class_if_needed(attributes_map) 153 | if attributes_map['type'].is_a?(String) 154 | attributes_map['type'] = to_local_class_name(attributes_map['type']) 155 | end 156 | end 157 | 158 | def disable_all_callbacks_for(base_class) 159 | [:create, :save, :update, :validate, :touch].each { |callback| base_class.reset_callbacks callback } 160 | end 161 | 162 | def simple_attributes_to_copy(remote_object) 163 | remote_object.attributes.except('id').reject do |attribute_name| 164 | attributes_to_exclude(remote_object).include? attribute_name.to_sym 165 | end 166 | end 167 | 168 | def attributes_to_exclude(remote_object) 169 | @attributes_to_exclude_map ||= {} 170 | @attributes_to_exclude_map[remote_object.class.base_class] ||= calculate_attributes_to_exclude(remote_object) 171 | end 172 | 173 | def calculate_attributes_to_exclude(remote_object) 174 | ((options[:exclude] && options[:exclude][remote_object.class.base_class]) || []).collect(&:to_sym) 175 | end 176 | 177 | def copy_simple_attributes(target_local_object, source_remote_object) 178 | debug "#{as_trace(source_remote_object)} reusing..." 179 | # update_columns skips callbacks too but not available in Rails 3 180 | copy_attributes(target_local_object, simple_attributes_to_copy(source_remote_object)) 181 | target_local_object.save!(validate: false) 182 | end 183 | 184 | def logger 185 | Forceps.logger 186 | end 187 | 188 | def increase_level 189 | @level += 1 190 | end 191 | 192 | def decrease_level 193 | @level -= 1 194 | end 195 | 196 | def as_trace(remote_object) 197 | "<#{remote_object.class.base_class.name} - #{remote_object.id}>" 198 | end 199 | 200 | def debug(message) 201 | left_margin = " "*level 202 | logger.debug "#{left_margin}#{message}" 203 | end 204 | 205 | def copy_associated_objects(local_object, remote_object) 206 | with_nested_logging do 207 | [:has_one, :has_many, :has_and_belongs_to_many].each do |association_kind| 208 | copy_objects_associated_by_association_kind(local_object, remote_object, association_kind) 209 | local_object.save!(validate: false) 210 | end 211 | end 212 | end 213 | 214 | def with_nested_logging 215 | increase_level 216 | yield 217 | decrease_level 218 | end 219 | 220 | def copy_objects_associated_by_association_kind(local_object, remote_object, association_kind) 221 | associations_to_copy(remote_object, association_kind).collect(&:name).each do |association_name| 222 | unless options.fetch(:ignore_model, []).include?(remote_object.class.base_class.name) 223 | send "copy_associated_objects_in_#{association_kind}", local_object, remote_object, association_name 224 | end 225 | end 226 | end 227 | 228 | def associations_to_copy(remote_object, association_kind) 229 | excluded_attributes = attributes_to_exclude(remote_object) 230 | remote_object.class.reflect_on_all_associations(association_kind).reject do |association| 231 | association.options[:through] || 232 | excluded_attributes.include?(:all_associations) || 233 | excluded_attributes.include?(association.name) || 234 | (!association.options[:polymorphic] && options.fetch(:ignore_model, []).include?(to_local_class_name(association.klass.name))) 235 | end 236 | end 237 | 238 | def copy_associated_objects_in_has_many(local_object, remote_object, association_name) 239 | remote_object.send(association_name).find_each do |remote_associated_object| 240 | local_object.send(association_name) << copy(remote_associated_object) 241 | end 242 | end 243 | 244 | def copy_associated_objects_in_has_one(local_object, remote_object, association_name) 245 | remote_associated_object = remote_object.send(association_name) 246 | local_object.send "#{association_name}=", remote_associated_object && copy(remote_associated_object) 247 | end 248 | 249 | def copy_associated_objects_in_belongs_to(remote_object) 250 | with_nested_logging do 251 | associations_to_copy(remote_object, :belongs_to).collect(&:name).each do |association_name| 252 | remote_associated_object = remote_object.send(association_name) 253 | copy(remote_associated_object) if remote_associated_object 254 | end 255 | end 256 | end 257 | 258 | def copy_associated_objects_in_has_and_belongs_to_many(local_object, remote_object, association_name) 259 | remote_object.send(association_name).find_each do |remote_associated_object| 260 | cloned_local_associated_object = copy(remote_associated_object) 261 | unless local_object.send(association_name).where(id: cloned_local_associated_object.id).exists? 262 | local_object.send(association_name) << cloned_local_associated_object 263 | end 264 | end 265 | end 266 | end 267 | end 268 | end 269 | -------------------------------------------------------------------------------- /lib/forceps/client.rb: -------------------------------------------------------------------------------- 1 | module Forceps 2 | class Client 3 | attr_reader :options 4 | 5 | def configure(options={}) 6 | @options = options.merge(default_options) 7 | 8 | declare_remote_model_classes 9 | make_associations_reference_remote_classes 10 | 11 | logger.debug "Classes handled by Forceps: #{model_classes.collect(&:name).inspect}" 12 | end 13 | 14 | private 15 | 16 | def logger 17 | Forceps.logger 18 | end 19 | 20 | def default_options 21 | {} 22 | end 23 | 24 | def model_classes 25 | @model_classes ||= filtered_model_classes 26 | end 27 | 28 | def filtered_model_classes 29 | (ActiveRecord::Base.descendants - model_classes_to_exclude).reject do |klass| 30 | klass.name.start_with?('HABTM_') 31 | end 32 | end 33 | 34 | def model_classes_to_exclude 35 | if Rails::VERSION::MAJOR >= 4 36 | [ActiveRecord::SchemaMigration] 37 | else 38 | [] 39 | end 40 | end 41 | 42 | def declare_remote_model_classes 43 | return if @remote_classes_defined 44 | model_classes.each { |remote_class| declare_remote_model_class(remote_class) } 45 | @remote_classes_defined = true 46 | end 47 | 48 | def declare_remote_model_class(klass) 49 | full_class_name = klass.name 50 | head = Forceps::Remote 51 | 52 | path = full_class_name.split("::") 53 | class_name = path.pop 54 | 55 | path.each do |module_name| 56 | if head.const_defined?(module_name, false) 57 | head = head.const_get(module_name, false) 58 | else 59 | head = head.const_set(module_name, Module.new) 60 | end 61 | end 62 | head.const_set(class_name, build_new_remote_class(klass)) 63 | 64 | remote_class_for(full_class_name).establish_connection :remote 65 | end 66 | 67 | def build_new_remote_class(local_class) 68 | needs_type_condition = (local_class.base_class != ActiveRecord::Base) && local_class.finder_needs_type_condition? 69 | Class.new(local_class) do 70 | self.table_name = local_class.table_name 71 | 72 | include Forceps::ActsAsCopyableModel 73 | 74 | # Intercept instantiation of records to make the 'type' column point to the corresponding remote class 75 | if Rails::VERSION::MAJOR >= 4 76 | def self.instantiate(record, column_types = {}) 77 | __make_sti_column_point_to_forceps_remote_class(record) 78 | super 79 | end 80 | else 81 | def self.instantiate(record) 82 | __make_sti_column_point_to_forceps_remote_class(record) 83 | super 84 | end 85 | end 86 | 87 | def self.sti_name 88 | name.gsub("Forceps::Remote::", "") 89 | end 90 | 91 | def self.__make_sti_column_point_to_forceps_remote_class(record) 92 | if record[inheritance_column].present? 93 | record[inheritance_column] = "Forceps::Remote::#{record[inheritance_column]}" 94 | end 95 | end 96 | 97 | # We don't want to include STI condition automatically (the base class extends the original one) 98 | unless needs_type_condition 99 | def self.finder_needs_type_condition? 100 | false 101 | end 102 | end 103 | end 104 | end 105 | 106 | def remote_class_for(full_class_name) 107 | head = Forceps::Remote 108 | full_class_name.split("::").each do |mod| 109 | head = head.const_get(mod) 110 | end 111 | head 112 | end 113 | 114 | def make_associations_reference_remote_classes 115 | model_classes.each do |model_class| 116 | make_associations_reference_remote_classes_for(model_class) 117 | end 118 | end 119 | 120 | def make_associations_reference_remote_classes_for(model_class) 121 | model_class._reflections.values.each do |association| 122 | next if association.class_name =~ /Forceps::Remote/ || association.class_name =~ /HABTM/ rescue next 123 | reference_remote_class(model_class, association) 124 | end 125 | end 126 | 127 | def reference_remote_class(model_class, association) 128 | remote_model_class = remote_class_for(model_class.name) 129 | 130 | if association.options[:polymorphic] 131 | reference_remote_class_in_polymorphic_association(association, remote_model_class) 132 | else 133 | reference_remote_class_in_normal_association(association, remote_model_class) 134 | end 135 | end 136 | 137 | def reference_remote_class_in_polymorphic_association(association, remote_model_class) 138 | foreign_type_attribute_name = association.foreign_type 139 | 140 | remote_model_class.send(:define_method, association.foreign_type) do 141 | "Forceps::Remote::#{super()}" 142 | end 143 | 144 | remote_model_class.send(:define_method, "[]") do |attribute_name| 145 | if (attribute_name.to_s == foreign_type_attribute_name) 146 | "Forceps::Remote::#{super(attribute_name)}" 147 | else 148 | super(attribute_name) 149 | end 150 | end 151 | end 152 | 153 | def reference_remote_class_in_normal_association(association, remote_model_class) 154 | related_remote_class = remote_class_for(association.klass.name) 155 | 156 | cloned_association = association.dup 157 | cloned_association.instance_variable_set("@klass", related_remote_class) 158 | ActiveRecord::Reflection.add_reflection(remote_model_class, cloned_association.name, cloned_association) 159 | end 160 | end 161 | end 162 | -------------------------------------------------------------------------------- /lib/forceps/version.rb: -------------------------------------------------------------------------------- 1 | module Forceps 2 | VERSION = "0.6.7" 3 | end 4 | -------------------------------------------------------------------------------- /lib/tasks/forceps_tasks.rake: -------------------------------------------------------------------------------- 1 | # desc "Explaining what the task does" 2 | # task :forceps do 3 | # # Task goes here 4 | # end 5 | -------------------------------------------------------------------------------- /test/blacklist_model_name_test.rb: -------------------------------------------------------------------------------- 1 | require 'test_helper' 2 | 3 | class ReuseExistingRecordsTest < ActiveSupport::TestCase 4 | def setup 5 | @remote_product = RemoteProduct.create!(name: 'MBP', price: '2000', id: 123456) 6 | end 7 | 8 | test "should completely ignore line items" do 9 | RemoteLineItem.create!(quantity: 3, product: @remote_product) 10 | RemoteLineItem.create!(quantity: 3, product: @remote_product) 11 | Forceps.configure ignore_model: ['LineItem'] 12 | 13 | copied_product = Forceps::Remote::Product.find(@remote_product.id).copy_to_local 14 | 15 | assert_equal 0, copied_product.line_items.count 16 | end 17 | end 18 | -------------------------------------------------------------------------------- /test/callbacks_test.rb: -------------------------------------------------------------------------------- 1 | require 'test_helper' 2 | 3 | class CallbacksTest < ActiveSupport::TestCase 4 | def setup 5 | @remote_product = RemoteProduct.create!(name: 'MBP', price: '2000', id: 123456).tap{|product| product.update_column :type, nil} 6 | end 7 | 8 | test "should invoke the configured 'after_each' callback" do 9 | after_each_callback_mock = MiniTest::Mock.new 10 | after_each_callback_mock.expect(:call, nil) do |local, remote| 11 | assert_equal Product.find_by_name('MBP'), local 12 | assert_identical @remote_product, remote 13 | end 14 | 15 | Forceps.configure :after_each => {Product => after_each_callback_mock} 16 | 17 | Forceps::Remote::Product.find(@remote_product.id).copy_to_local 18 | 19 | assert after_each_callback_mock.verify 20 | end 21 | end 22 | -------------------------------------------------------------------------------- /test/clone_structures_test.rb: -------------------------------------------------------------------------------- 1 | require 'test_helper' 2 | 3 | class CloneStructuresTest < ActiveSupport::TestCase 4 | 5 | def setup 6 | @remote_invoice = RemoteInvoice.create! number: 123, date: Time.now 7 | @remote_user = RemoteUser.create! name: 'Jorge' 8 | Forceps.configure 9 | end 10 | 11 | test "should download a single record" do 12 | Forceps::Remote::Invoice.find_by_number(@remote_invoice.number).copy_to_local 13 | assert_identical @remote_invoice.becomes(Invoice), Invoice.find_by_number(123) 14 | end 15 | 16 | test "should download object with 'has_many'" do 17 | 2.times { |index| @remote_user.invoices.create! number: index+1, date: "2014-1-#{index+1}" } 18 | 19 | Forceps::Remote::User.find(@remote_user.id).copy_to_local 20 | 21 | copied_user = User.find_by_name('Jorge') 22 | assert_identical @remote_user, copied_user 23 | 2.times { |index| assert_identical @remote_user.invoices[index], copied_user.invoices[index] } 24 | end 25 | 26 | test "should download object with 'belongs_to'" do 27 | @remote_invoice = @remote_user.invoices.create! number: 1234, date: "2014-1-3" 28 | 29 | Forceps::Remote::Invoice.find(@remote_invoice.id).copy_to_local 30 | 31 | copied_invoice = Invoice.find_by_number(1234) 32 | assert_identical @remote_invoice, copied_invoice 33 | end 34 | 35 | test "should download objects with 'has_and_belongs_to_many'" do 36 | remote_tags = 2.times.collect { |index| RemoteTag.create name: "tag #{index}" } 37 | remote_products = 2.times.collect do |index| 38 | RemoteProduct.create(name: "product #{index}").tap do |product| 39 | product.update_column :type, nil # we don't STI here 40 | end 41 | end 42 | remote_products.each { |remote_product| remote_tags.each {|remote_tag| remote_product.tags << remote_tag} } 43 | 44 | Forceps::Remote::Tag.find(remote_tags[0].id).copy_to_local 45 | 46 | assert_equal 2, Product.count 47 | assert_equal 2, Tag.count 48 | 49 | 2.times do |index| 50 | assert_not_nil tag = Tag.find_by_name("tag #{index}") 51 | assert_not_nil Product.find_by_name("product #{index}") 52 | assert_equal 2, tag.products.count 53 | end 54 | end 55 | 56 | test "should download object with 'has_one'" do 57 | remote_address = RemoteAddress.create!(street: 'Uria', city: 'Oviedo', country: 'Spain', user: @remote_user) 58 | 59 | Forceps::Remote::User.find(@remote_user.id).copy_to_local 60 | 61 | copied_user = User.find_by_name('Jorge') 62 | assert_identical remote_address, copied_user.address 63 | end 64 | 65 | end 66 | -------------------------------------------------------------------------------- /test/dummy/README.rdoc: -------------------------------------------------------------------------------- 1 | == README 2 | 3 | This README would normally document whatever steps are necessary to get the 4 | application up and running. 5 | 6 | Things you may want to cover: 7 | 8 | * Ruby version 9 | 10 | * System dependencies 11 | 12 | * Configuration 13 | 14 | * Database creation 15 | 16 | * Database initialization 17 | 18 | * How to run the test suite 19 | 20 | * Services (job queues, cache servers, search engines, etc.) 21 | 22 | * Deployment instructions 23 | 24 | * ... 25 | 26 | 27 | Please feel free to use a different markup language if you do not plan to run 28 | rake doc:app. 29 | -------------------------------------------------------------------------------- /test/dummy/Rakefile: -------------------------------------------------------------------------------- 1 | # Add your own tasks in files placed in lib/tasks ending in .rake, 2 | # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake. 3 | 4 | require File.expand_path('../config/application', __FILE__) 5 | 6 | Dummy::Application.load_tasks 7 | -------------------------------------------------------------------------------- /test/dummy/app/assets/images/.keep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jorgemanrubia/forceps/668ed512598eb6aa1e92ea5703dceee1a017cc72/test/dummy/app/assets/images/.keep -------------------------------------------------------------------------------- /test/dummy/app/assets/javascripts/application.js: -------------------------------------------------------------------------------- 1 | // This is a manifest file that'll be compiled into application.js, which will include all the files 2 | // listed below. 3 | // 4 | // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts, 5 | // or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path. 6 | // 7 | // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the 8 | // compiled file. 9 | // 10 | // Read Sprockets README (https://github.com/sstephenson/sprockets#sprockets-directives) for details 11 | // about supported directives. 12 | // 13 | //= require_tree . 14 | -------------------------------------------------------------------------------- /test/dummy/app/assets/stylesheets/application.css: -------------------------------------------------------------------------------- 1 | /* 2 | * This is a manifest file that'll be compiled into application.css, which will include all the files 3 | * listed below. 4 | * 5 | * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets, 6 | * or vendor/assets/stylesheets of plugins, if any, can be referenced here using a relative path. 7 | * 8 | * You're free to add application-wide styles to this file and they'll appear at the top of the 9 | * compiled file, but it's generally better to create a new file per style scope. 10 | * 11 | *= require_self 12 | *= require_tree . 13 | */ 14 | -------------------------------------------------------------------------------- /test/dummy/app/controllers/application_controller.rb: -------------------------------------------------------------------------------- 1 | class ApplicationController < ActionController::Base 2 | # Prevent CSRF attacks by raising an exception. 3 | # For APIs, you may want to use :null_session instead. 4 | protect_from_forgery with: :exception 5 | end 6 | -------------------------------------------------------------------------------- /test/dummy/app/controllers/concerns/.keep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jorgemanrubia/forceps/668ed512598eb6aa1e92ea5703dceee1a017cc72/test/dummy/app/controllers/concerns/.keep -------------------------------------------------------------------------------- /test/dummy/app/helpers/application_helper.rb: -------------------------------------------------------------------------------- 1 | module ApplicationHelper 2 | end 3 | -------------------------------------------------------------------------------- /test/dummy/app/mailers/.keep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jorgemanrubia/forceps/668ed512598eb6aa1e92ea5703dceee1a017cc72/test/dummy/app/mailers/.keep -------------------------------------------------------------------------------- /test/dummy/app/models/.keep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jorgemanrubia/forceps/668ed512598eb6aa1e92ea5703dceee1a017cc72/test/dummy/app/models/.keep -------------------------------------------------------------------------------- /test/dummy/app/models/address.rb: -------------------------------------------------------------------------------- 1 | class Address < ActiveRecord::Base 2 | belongs_to :user 3 | end 4 | -------------------------------------------------------------------------------- /test/dummy/app/models/car.rb: -------------------------------------------------------------------------------- 1 | class Car < Product 2 | def name=(new_name) 3 | super("CAR: #{new_name}") 4 | end 5 | end 6 | -------------------------------------------------------------------------------- /test/dummy/app/models/cars/german/compact_car.rb: -------------------------------------------------------------------------------- 1 | class Cars::German::CompactCar < Car 2 | def name=(new_name) 3 | super("GERMAN COMPACT CAR: #{new_name}") 4 | end 5 | end 6 | -------------------------------------------------------------------------------- /test/dummy/app/models/cars/german/sports_car.rb: -------------------------------------------------------------------------------- 1 | class Cars::German::SportsCar < Car 2 | def name=(new_name) 3 | super("GERMAN SPORTS CAR: #{new_name}") 4 | end 5 | end 6 | -------------------------------------------------------------------------------- /test/dummy/app/models/concerns/.keep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jorgemanrubia/forceps/668ed512598eb6aa1e92ea5703dceee1a017cc72/test/dummy/app/models/concerns/.keep -------------------------------------------------------------------------------- /test/dummy/app/models/invoice.rb: -------------------------------------------------------------------------------- 1 | class Invoice < ActiveRecord::Base 2 | belongs_to :user 3 | 4 | has_many :line_items 5 | 6 | has_one :address, through: :user 7 | end 8 | -------------------------------------------------------------------------------- /test/dummy/app/models/line_item.rb: -------------------------------------------------------------------------------- 1 | class LineItem < ActiveRecord::Base 2 | belongs_to :product 3 | belongs_to :invoice 4 | end 5 | -------------------------------------------------------------------------------- /test/dummy/app/models/product.rb: -------------------------------------------------------------------------------- 1 | class Product < ActiveRecord::Base 2 | has_many :line_items 3 | has_and_belongs_to_many :tags 4 | end 5 | -------------------------------------------------------------------------------- /test/dummy/app/models/tag.rb: -------------------------------------------------------------------------------- 1 | class Tag < ActiveRecord::Base 2 | has_and_belongs_to_many :products 3 | end 4 | -------------------------------------------------------------------------------- /test/dummy/app/models/user.rb: -------------------------------------------------------------------------------- 1 | class User < ActiveRecord::Base 2 | has_many :invoices 3 | has_one :address 4 | end 5 | -------------------------------------------------------------------------------- /test/dummy/app/views/layouts/application.html.erb: -------------------------------------------------------------------------------- 1 | 2 | 3 |
4 |You may have mistyped the address or the page may have moved.
55 |If you are the application owner check the logs for more information.
57 | 58 | 59 | -------------------------------------------------------------------------------- /test/dummy/public/422.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 |Maybe you tried to change something you didn't have access to.
55 |If you are the application owner check the logs for more information.
57 | 58 | 59 | -------------------------------------------------------------------------------- /test/dummy/public/500.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 |If you are the application owner check the logs for more information.
56 | 57 | 58 | -------------------------------------------------------------------------------- /test/dummy/public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jorgemanrubia/forceps/668ed512598eb6aa1e92ea5703dceee1a017cc72/test/dummy/public/favicon.ico -------------------------------------------------------------------------------- /test/exclude_records_test.rb: -------------------------------------------------------------------------------- 1 | require 'test_helper' 2 | 3 | class ExcludeRecordsTest < ActiveSupport::TestCase 4 | def setup 5 | end 6 | 7 | test "should exclude regular attributes in the 'exclude' option" do 8 | Forceps.configure exclude: {Address => [:street]} 9 | RemoteAddress.create! street: 'Uria', city: 'Oviedo' 10 | 11 | Forceps::Remote::Address.find_by_city('Oviedo').copy_to_local 12 | 13 | copied_address = Address.find_by_city('Oviedo') 14 | assert_nil copied_address.street 15 | end 16 | 17 | test "should exclude the associations set in the 'exclude' option" do 18 | remote_user = build_user_with_invoices(2) 19 | Forceps.configure exclude: {User => [:invoices]} 20 | 21 | Forceps::Remote::User.find(remote_user.id).copy_to_local 22 | 23 | copied_user = User.find_by_name('Jorge') 24 | assert_identical remote_user, copied_user 25 | assert_empty copied_user.invoices 26 | end 27 | 28 | test "should exclude all the associations when using :all_associations as the the 'exclude' option value" do 29 | remote_user = build_user_with_invoices(2) 30 | Forceps.configure exclude: {User => [:all_associations]} 31 | 32 | Forceps::Remote::User.find(remote_user.id).copy_to_local 33 | 34 | copied_user = User.find_by_name('Jorge') 35 | assert_empty copied_user.invoices 36 | end 37 | 38 | def build_user_with_invoices(invoices_count) 39 | RemoteUser.create!(name: 'Jorge').tap do |remote_user| 40 | invoices_count.times { |index| remote_user.invoices.create! number: index+1, date: "2014-1-#{index+1}" } 41 | end 42 | end 43 | end 44 | -------------------------------------------------------------------------------- /test/fixtures/invoices.yml: -------------------------------------------------------------------------------- 1 | # Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html 2 | 3 | invoice_1: 4 | number: 1 5 | date: 2014-1-1 6 | user: jorge 7 | 8 | invoice_2: 9 | number: 2 10 | date: 2014-1-2 11 | user: jorge -------------------------------------------------------------------------------- /test/fixtures/users.yml: -------------------------------------------------------------------------------- 1 | # Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html 2 | 3 | # This model initially had no columns defined. If you add columns to the 4 | # model remove the '{}' from the fixture names and add the columns immediately 5 | # below each fixture, per the syntax in the comments below 6 | # 7 | jorge: 8 | name: Jorge 9 | -------------------------------------------------------------------------------- /test/reuse_existing_records_test.rb: -------------------------------------------------------------------------------- 1 | require 'test_helper' 2 | 3 | class ReuseExistingRecordsTest < ActiveSupport::TestCase 4 | def setup 5 | @remote_product = RemoteProduct.create!(name: 'MBP', price: '2000', id: 123456).tap{|product| product.update_column :type, nil} 6 | @local_existing_product = Product.create!(id: @remote_product.id, name: 'MBP', price: '1000') 7 | @remote_line_item = RemoteLineItem.create! quantity: 3, product: @remote_product 8 | end 9 | 10 | test "should reuse the referenced project when specifying a matching attribute name" do 11 | Forceps.configure reuse: {Product => :id} 12 | Forceps::Remote::LineItem.find(@remote_line_item.id).copy_to_local 13 | assert_product_was_reused_and_updated 14 | end 15 | 16 | test "should reuse the referenced project when specifying a finder" do 17 | Forceps.configure reuse: {Product => lambda{|remote_product| Product.find_by_name(remote_product.name)}} 18 | Forceps::Remote::LineItem.find(@remote_line_item.id).copy_to_local 19 | assert_product_was_reused_and_updated 20 | end 21 | 22 | test "should clone the object when it is not found" do 23 | Forceps.configure reuse: {Product => lambda{|remote_product| Product.find_by_price('some not-existing price')}} 24 | Forceps::Remote::LineItem.find(@remote_line_item.id).copy_to_local 25 | assert_product_was_created 26 | end 27 | 28 | def assert_product_was_reused_and_updated 29 | assert_product_was_copied(1) 30 | end 31 | 32 | def assert_product_was_created 33 | assert_product_was_copied(2) 34 | end 35 | 36 | def assert_product_was_copied(expected_product_count) 37 | assert_equal expected_product_count, Product.count 38 | updated_product = LineItem.find_by_quantity(3).product 39 | assert_identical @remote_product, updated_product 40 | end 41 | end 42 | -------------------------------------------------------------------------------- /test/sti_test.rb: -------------------------------------------------------------------------------- 1 | require 'test_helper' 2 | 3 | 4 | class StiTest < ActiveSupport::TestCase 5 | def setup 6 | Forceps.configure 7 | create_remote_car({ name: 'audi' }, 'Car') 8 | create_remote_car({ name: 'A1' }, 'Cars::German::CompactCar') 9 | create_remote_car({ name: 'R8' }, 'Cars::German::SportsCar') 10 | end 11 | 12 | test "should instantiate the proper remote record when fetching a record through the parent class" do 13 | remote_car = Forceps::Remote::Product.find_by_name('audi') 14 | assert_instance_of Forceps::Remote::Car, remote_car 15 | end 16 | 17 | test "should be able to load remote objects by the STI class" do 18 | remote_car = Forceps::Remote::Car.find_by_name('audi') 19 | assert_instance_of Forceps::Remote::Car, remote_car 20 | end 21 | 22 | test "should work with namespaced models" do 23 | compact_car = Forceps::Remote::Product.find_by_name('A1') 24 | sports_car = Forceps::Remote::Product.find_by_name('R8') 25 | assert_instance_of Forceps::Remote::Cars::German::CompactCar, compact_car 26 | assert_instance_of Forceps::Remote::Cars::German::SportsCar, sports_car 27 | end 28 | 29 | test "should use the correct type with namespaces models" do 30 | Forceps::Remote::Product.find_by_name('R8').copy_to_local 31 | assert_equal Product.find_by_name('CAR: GERMAN SPORTS CAR: R8').type, "Cars::German::SportsCar" 32 | end 33 | 34 | test "should download child objects when using single table inheritance" do 35 | Forceps::Remote::Product.find_by_name('audi').copy_to_local 36 | copied_car = Product.find_by_name('CAR: audi') 37 | assert_not_nil copied_car 38 | assert_instance_of Car, copied_car 39 | end 40 | 41 | def create_remote_car(attributes, klass) 42 | RemoteProduct.create!(attributes).tap do |car| 43 | car.update_column :type, klass 44 | end 45 | end 46 | end 47 | -------------------------------------------------------------------------------- /test/support/minitest_ext.rb: -------------------------------------------------------------------------------- 1 | module Minitest 2 | module Assertions 3 | # Compares the equality at the attribute level 4 | def assert_identical expected, actual, msg = nil 5 | actual = actual.becomes(expected.class) if actual && actual.class != expected.class 6 | ignored_attributes = %w(id) 7 | assert_equal expected.class, actual.class 8 | assert_equal expected.attributes.except(*ignored_attributes), 9 | actual.attributes.except(*ignored_attributes) 10 | end 11 | end 12 | end 13 | 14 | -------------------------------------------------------------------------------- /test/support/remote_address.rb: -------------------------------------------------------------------------------- 1 | class RemoteAddress < Address 2 | establish_connection :remote 3 | 4 | belongs_to :user, class_name: 'RemoteUser' 5 | end 6 | -------------------------------------------------------------------------------- /test/support/remote_invoice.rb: -------------------------------------------------------------------------------- 1 | class RemoteInvoice < Invoice 2 | establish_connection :remote 3 | 4 | belongs_to :user, class_name: 'RemoteUser' 5 | has_many :line_items, class_name: 'RemoteLineItem' 6 | end 7 | -------------------------------------------------------------------------------- /test/support/remote_line_item.rb: -------------------------------------------------------------------------------- 1 | class RemoteLineItem < LineItem 2 | establish_connection :remote 3 | 4 | belongs_to :product, class_name: 'RemoteProduct' 5 | belongs_to :invoice, class_name: 'RemoteInvoice', foreign_key: 'invoice_id' 6 | end 7 | -------------------------------------------------------------------------------- /test/support/remote_product.rb: -------------------------------------------------------------------------------- 1 | class RemoteProduct < Product 2 | establish_connection :remote 3 | 4 | has_many :line_items, class_name: 'RemoteLineItem', foreign_key: 'product_id' 5 | has_and_belongs_to_many :tags, class_name: 'RemoteTag', join_table: 'products_tags', foreign_key: 'product_id', association_foreign_key: 'tag_id' 6 | end 7 | -------------------------------------------------------------------------------- /test/support/remote_tag.rb: -------------------------------------------------------------------------------- 1 | class RemoteTag < Tag 2 | establish_connection :remote 3 | 4 | has_and_belongs_to_many :products, class_name: 'RemoteProduct', join_table: 'products_tags', foreign_key: 'tag_id', association_foreign_key: 'product_id' 5 | end 6 | -------------------------------------------------------------------------------- /test/support/remote_user.rb: -------------------------------------------------------------------------------- 1 | class RemoteUser < User 2 | establish_connection :remote 3 | 4 | has_one :address, class_name: 'RemoteAddress', foreign_key: 'user_id' 5 | has_many :invoices, class_name: 'RemoteInvoice', foreign_key: 'user_id' 6 | end 7 | -------------------------------------------------------------------------------- /test/test_helper.rb: -------------------------------------------------------------------------------- 1 | # Configure Rails Environment 2 | ENV["RAILS_ENV"] = "test" 3 | 4 | require File.expand_path("../dummy/config/environment.rb", __FILE__) 5 | require "rails/test_help" 6 | require 'database_cleaner' 7 | require 'database_cleaner/active_record/base' 8 | require 'awesome_print' 9 | # require 'minitest/reporters' 10 | require 'minitest/mock' 11 | 12 | # MiniTest::Reporters.use! 13 | 14 | Rails.backtrace_cleaner.remove_silencers! 15 | 16 | # Load support files 17 | Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f } 18 | 19 | # Load fixtures from the engine 20 | ActiveSupport::TestCase.fixture_path = File.expand_path("../fixtures", __FILE__) 21 | ActiveSupport::TestCase.use_transactional_fixtures = false 22 | 23 | DatabaseCleaner::ActiveRecord.config_file_location = File.expand_path("../dummy/config/database.yml", __FILE__) 24 | 25 | DatabaseCleaner.strategy = :truncation 26 | DatabaseCleaner[:active_record, connection: :remote].strategy = :truncation 27 | 28 | Rails.application.eager_load! 29 | 30 | class ActiveSupport::TestCase 31 | setup do 32 | DatabaseCleaner.start 33 | DatabaseCleaner[:active_record, connection: :remote].start 34 | end 35 | teardown do 36 | DatabaseCleaner.clean 37 | DatabaseCleaner[:active_record, connection: :remote].clean 38 | end 39 | end 40 | --------------------------------------------------------------------------------