reflections 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +15 -0
- data/.gitignore +19 -0
- data/.rspec +2 -0
- data/.rvmrc +62 -0
- data/Gemfile +4 -0
- data/Guardfile +12 -0
- data/LICENSE.txt +22 -0
- data/README.md +56 -0
- data/Rakefile +1 -0
- data/lib/reflections.rb +2 -0
- data/lib/reflections/active_record_extension.rb +14 -0
- data/lib/reflections/remapper.rb +49 -0
- data/lib/reflections/remappers.rb +2 -0
- data/lib/reflections/remappers/belongs_to.rb +39 -0
- data/lib/reflections/remappers/has_and_belongs_to_many.rb +37 -0
- data/lib/reflections/version.rb +3 -0
- data/reflections.gemspec +29 -0
- data/spec/remapper_spec.rb +99 -0
- data/spec/reporter_spec.rb +32 -0
- data/spec/support/active_record.rb +38 -0
- data/spec/support/spec_helper.rb +6 -0
- metadata +180 -0
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
OTk2OTIzYjI1MTJjYjM2NmUyMmMzMTVkYjdiMzA0NzgzMGExOWYyMw==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
ZDdmZjg3NTA4NDMxZDQ2MTNjMWM3ZDJlZjgyOTYwNDIzZjJmMmFkYQ==
|
7
|
+
SHA512:
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
ZDViMGNmNWFhNWIwYzMxNDgwODBkZDcyZDJmZDY1NDIyNzQxNDBjNTA5ODUy
|
10
|
+
NGQwOGVjYTBmZjUzNWRmOGMyOTk0N2FjYTc1OWFhOWIxZWRmNDU3MmY1MjBh
|
11
|
+
ZjE4MWJiOTU2ZjQ5NGRlMGIxNmI2ZTk3M2U4NWNhNDE5ZTNiZTU=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
MjMwY2Q5NWQxYTAwZGY2NjkzMjJkYzdkNDA1NDk3ZThhNWM3MTk0YTdkYWVi
|
14
|
+
ODhmN2RiNWYwNDdlOWRjY2MzMzBjM2MzMDY2MTIwODZjMzMyYzYzNzY5N2Zi
|
15
|
+
NDUxODBhNDI2NzQzMTIxNzYwNGUwZmRhNmJhZmJjM2JkYjJlNzk=
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rvmrc
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
#!/usr/bin/env bash
|
2
|
+
|
3
|
+
# This is an RVM Project .rvmrc file, used to automatically load the ruby
|
4
|
+
# development environment upon cd'ing into the directory
|
5
|
+
|
6
|
+
# First we specify our desired <ruby>[@<gemset>], the @gemset name is optional,
|
7
|
+
# Only full ruby name is supported here, for short names use:
|
8
|
+
# echo "rvm use 1.9.3" > .rvmrc
|
9
|
+
environment_id="ruby-1.9.3-p448@reflections_gem"
|
10
|
+
|
11
|
+
# Uncomment the following lines if you want to verify rvm version per project
|
12
|
+
# rvmrc_rvm_version="1.24.7 (latest)" # 1.10.1 seems like a safe start
|
13
|
+
# eval "$(echo ${rvm_version}.${rvmrc_rvm_version} | __rvm_awk -F. '{print "[[ "$1*65536+$2*256+$3" -ge "$4*65536+$5*256+$6" ]]"}' )" || {
|
14
|
+
# echo "This .rvmrc file requires at least RVM ${rvmrc_rvm_version}, aborting loading."
|
15
|
+
# return 1
|
16
|
+
# }
|
17
|
+
|
18
|
+
# First we attempt to load the desired environment directly from the environment
|
19
|
+
# file. This is very fast and efficient compared to running through the entire
|
20
|
+
# CLI and selector. If you want feedback on which environment was used then
|
21
|
+
# insert the word 'use' after --create as this triggers verbose mode.
|
22
|
+
if [[ -d "${rvm_path:-$HOME/.rvm}/environments"
|
23
|
+
&& -s "${rvm_path:-$HOME/.rvm}/environments/$environment_id" ]]
|
24
|
+
then
|
25
|
+
\. "${rvm_path:-$HOME/.rvm}/environments/$environment_id"
|
26
|
+
for __hook in "${rvm_path:-$HOME/.rvm}/hooks/after_use"*
|
27
|
+
do
|
28
|
+
if [[ -f "${__hook}" && -x "${__hook}" && -s "${__hook}" ]]
|
29
|
+
then \. "${__hook}" || true
|
30
|
+
fi
|
31
|
+
done
|
32
|
+
unset __hook
|
33
|
+
if (( ${rvm_use_flag:=1} >= 1 )) # display automatically
|
34
|
+
then
|
35
|
+
if [[ $- == *i* ]] # check for interactive shells
|
36
|
+
then printf "%b" "Using: $(tput setaf 2 2>/dev/null)$GEM_HOME$(tput sgr0 2>/dev/null)
|
37
|
+
" # show the user the ruby and gemset they are using in green
|
38
|
+
else printf "%b" "Using: $GEM_HOME
|
39
|
+
" # don't use colors in non-interactive shells
|
40
|
+
fi
|
41
|
+
fi
|
42
|
+
else
|
43
|
+
# If the environment file has not yet been created, use the RVM CLI to select.
|
44
|
+
rvm --create use "$environment_id" || {
|
45
|
+
echo "Failed to create RVM environment '${environment_id}'."
|
46
|
+
return 1
|
47
|
+
}
|
48
|
+
fi
|
49
|
+
|
50
|
+
# If you use bundler, this might be useful to you:
|
51
|
+
# if [[ -s Gemfile ]] && {
|
52
|
+
# ! builtin command -v bundle >/dev/null ||
|
53
|
+
# builtin command -v bundle | GREP_OPTIONS="" \grep $rvm_path/bin/bundle >/dev/null
|
54
|
+
# }
|
55
|
+
# then
|
56
|
+
# printf "%b" "The rubygem 'bundler' is not installed. Installing it now.\n"
|
57
|
+
# gem install bundler
|
58
|
+
# fi
|
59
|
+
# if [[ -s Gemfile ]] && builtin command -v bundle >/dev/null
|
60
|
+
# then
|
61
|
+
# bundle install | GREP_OPTIONS="" \grep -vE '^Using|Your bundle is complete'
|
62
|
+
# fi
|
data/Gemfile
ADDED
data/Guardfile
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
# More info at https://github.com/guard/guard#readme
|
2
|
+
|
3
|
+
guard :rspec do
|
4
|
+
watch('spec/spec_helper.rb') { "spec" }
|
5
|
+
watch(%r{^spec/.+_spec\.rb$}) { "spec" }
|
6
|
+
watch(%r{^lib/reflections.rb$}) { "spec" }
|
7
|
+
watch(%r{^lib/reflections/active_record_extension.rb$}) { "spec" }
|
8
|
+
watch(%r{^lib/reflections/remappers.rb$}) { "spec"}
|
9
|
+
watch(%r{^lib/reflections/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
|
10
|
+
watch(%r{^lib/reflections/remappers/(.+)\.rb$}) {"spec"}
|
11
|
+
end
|
12
|
+
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Benjamin Cavileer
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
# Reflections
|
2
|
+
|
3
|
+
Given an ActiveRecord object, it re-maps relationships to another object
|
4
|
+
of the same class. Useful for reconciling database with accounts for same user, etc.
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
gem 'reflections'
|
11
|
+
|
12
|
+
And then execute:
|
13
|
+
|
14
|
+
$ bundle
|
15
|
+
|
16
|
+
Or install it yourself as:
|
17
|
+
|
18
|
+
$ gem install reflections
|
19
|
+
|
20
|
+
## Usage
|
21
|
+
|
22
|
+
user1 = User.create
|
23
|
+
|
24
|
+
user2 = User.create
|
25
|
+
|
26
|
+
Widget.create user: user1
|
27
|
+
|
28
|
+
OtherClass.create user: user1
|
29
|
+
|
30
|
+
user1.map_associations_to user2
|
31
|
+
|
32
|
+
you can optionally control the associations
|
33
|
+
|
34
|
+
user1.map_associations_to(user2, types: %w(belongs_to has_and_belongs_to_many))
|
35
|
+
|
36
|
+
you can optionally control the updating of the associations
|
37
|
+
or create reports by passing a block
|
38
|
+
|
39
|
+
user1.map_associations_to(user2) do |record, association|
|
40
|
+
puts "Remapping #{association.macro} for #{record} from #{user1} to #{user2}"
|
41
|
+
false # don't actually do update
|
42
|
+
end
|
43
|
+
|
44
|
+
you can also control the classes remapped with :only and :exclude
|
45
|
+
|
46
|
+
user1.map_associations_to(user2, only: [Widget])
|
47
|
+
|
48
|
+
user1.map_associations_to(user2, except: [OtherClass])
|
49
|
+
|
50
|
+
## Contributing
|
51
|
+
|
52
|
+
1. Fork it
|
53
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
54
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
55
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
56
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/lib/reflections.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require_relative 'remapper'
|
2
|
+
require_relative 'remappers'
|
3
|
+
|
4
|
+
module Reflections
|
5
|
+
module ActiveRecordExtension
|
6
|
+
extend ActiveSupport::Concern
|
7
|
+
|
8
|
+
def map_associations_to(ar_obj, options={}, &block)
|
9
|
+
Remapper.new(self, ar_obj).remap(options, &block)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
ActiveRecord::Base.send(:include, ActiveRecordExtension)
|
14
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
module Reflections
|
2
|
+
class Remapper
|
3
|
+
REMAPPERS = []
|
4
|
+
attr_reader :from_obj, :to_obj
|
5
|
+
|
6
|
+
def initialize(from, to)
|
7
|
+
@from_obj = from
|
8
|
+
@to_obj = to
|
9
|
+
end
|
10
|
+
|
11
|
+
def remap(options={}, &block)
|
12
|
+
protect_remap_from_other_classes
|
13
|
+
@only_these_classes = options.fetch(:only) { ActiveRecord::Base.descendants }
|
14
|
+
@except_these_classes = options.fetch(:except) { [] }
|
15
|
+
remap_these = options.fetch(:types) { REMAPPERS }
|
16
|
+
remap_these.each do |remapper|
|
17
|
+
remapper_class = "Reflections::Remappers::#{remapper.to_s.camelize}".constantize
|
18
|
+
remapper_class.new(from_obj, to_obj).remap(ar_classes, &block)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
protected
|
23
|
+
|
24
|
+
def ar_classes
|
25
|
+
@only_these_classes - @except_these_classes
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
def protect_remap_from_other_classes
|
31
|
+
if from_obj.class != to_obj.class
|
32
|
+
raise NotSameClass, "#{from_obj.class} is not the same class as #{to_obj.class}"
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def update_record_or_yield(record, association)
|
37
|
+
if !block_given? || block_given? && yield(record, from_obj, to_obj)
|
38
|
+
update_record record, association
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def associations_for_class(ar_class)
|
43
|
+
filter = filter_for_class from_obj.class
|
44
|
+
associations(ar_class).select &filter
|
45
|
+
end
|
46
|
+
|
47
|
+
class NotSameClass < RuntimeError; end
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
module Reflections
|
2
|
+
module Remappers
|
3
|
+
class BelongsTo < Reflections::Remapper
|
4
|
+
REMAPPERS << 'belongs_to'
|
5
|
+
attr_reader :remapper
|
6
|
+
|
7
|
+
def remap(ar_classes, &block)
|
8
|
+
ar_classes.each do |ar_class|
|
9
|
+
associations_for_class(ar_class).each do |association|
|
10
|
+
foreign_key = association.foreign_key || "#{association.name}_id"
|
11
|
+
ar_class.where(foreign_key => from_obj).each do |record|
|
12
|
+
update_record_or_yield record, association, &block
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def filter_for_class(klass)
|
21
|
+
->(assoc) {
|
22
|
+
assoc.name == klass.to_s.underscore.to_sym || assoc.options[:class_name] == klass.model_name
|
23
|
+
}
|
24
|
+
end
|
25
|
+
|
26
|
+
def update_record(record, association)
|
27
|
+
record.update_attribute association.name, to_obj
|
28
|
+
end
|
29
|
+
|
30
|
+
def associations(ar_class)
|
31
|
+
ar_class.reflect_on_all_associations :belongs_to
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
|
39
|
+
|
@@ -0,0 +1,37 @@
|
|
1
|
+
module Reflections
|
2
|
+
module Remappers
|
3
|
+
class HasAndBelongsToMany < Reflections::Remapper
|
4
|
+
REMAPPERS << 'has_and_belongs_to_many'
|
5
|
+
|
6
|
+
def remap(ar_classes, &block)
|
7
|
+
ar_classes.each do |ar_class|
|
8
|
+
associations_for_class(ar_class).each do |association|
|
9
|
+
ar_class.includes(association.name).where("#{association.join_table}.#{association.foreign_key}").references(association.name).each do |record|
|
10
|
+
update_record_or_yield record, association, &block
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
def filter_for_class(klass)
|
19
|
+
->(assoc) {
|
20
|
+
assoc.name == klass.to_s.pluralize.underscore.to_sym || assoc.options[:class_name] == klass.model_name
|
21
|
+
}
|
22
|
+
end
|
23
|
+
|
24
|
+
def update_record(record, association)
|
25
|
+
association = record.send association.name
|
26
|
+
association.delete(from_obj)
|
27
|
+
association << to_obj
|
28
|
+
end
|
29
|
+
|
30
|
+
def associations(ar_class)
|
31
|
+
ar_class.reflect_on_all_associations :has_and_belongs_to_many
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
data/reflections.gemspec
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'reflections/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'reflections'
|
8
|
+
spec.version = Reflections::VERSION
|
9
|
+
spec.authors = ['Benjamin Cavileer']
|
10
|
+
spec.email = ['bcavileer@holmanauto.com']
|
11
|
+
spec.description = %q{ActiveRecord::Base Reflections}
|
12
|
+
spec.summary = %q{Will help our project}
|
13
|
+
spec.homepage = ''
|
14
|
+
spec.license = 'MIT'
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ['lib']
|
20
|
+
|
21
|
+
spec.add_runtime_dependency 'activerecord'
|
22
|
+
spec.add_development_dependency 'bundler', '~> 1.3'
|
23
|
+
spec.add_development_dependency 'rake'
|
24
|
+
spec.add_development_dependency 'rspec'
|
25
|
+
spec.add_development_dependency 'sqlite3'
|
26
|
+
spec.add_development_dependency 'database_cleaner'
|
27
|
+
spec.add_development_dependency 'guard-rspec'
|
28
|
+
spec.add_development_dependency 'pry'
|
29
|
+
end
|
@@ -0,0 +1,99 @@
|
|
1
|
+
require 'support/spec_helper'
|
2
|
+
|
3
|
+
describe 'Remapping' do
|
4
|
+
before(:each) { DatabaseCleaner.clean }
|
5
|
+
let(:user1) { User.create }
|
6
|
+
let(:user2) { User.create }
|
7
|
+
|
8
|
+
describe 'Remapping to a different class' do
|
9
|
+
it 'raises an error' do
|
10
|
+
expect { user1.map_associations_to OtherClass.new }.
|
11
|
+
to raise_error(Reflections::Remapper::NotSameClass)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
describe 'default belongs_to' do
|
16
|
+
it 're-maps default belongs_to associations' do
|
17
|
+
Widget.create user: user1
|
18
|
+
user1.map_associations_to user2
|
19
|
+
Widget.first.user.should eq(user2)
|
20
|
+
end
|
21
|
+
|
22
|
+
it "doesn't affect other records" do
|
23
|
+
widget1 = Widget.create user: user1
|
24
|
+
widget2 = Widget.create
|
25
|
+
user1.map_associations_to user2
|
26
|
+
widget1.reload.user.should eq(user2)
|
27
|
+
widget2.reload.should_not eq(user2)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
describe 'Remapping custom named belongs_to' do
|
32
|
+
it 're-maps custom named belongs_to associations' do
|
33
|
+
Widget.belongs_to :creator, class_name: 'User'
|
34
|
+
Widget.create creator: user1
|
35
|
+
user1.map_associations_to user2
|
36
|
+
Widget.first.creator.should eq(user2)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
describe 'Remapping has_and_belongs_to_many' do
|
41
|
+
it 're-maps has_and_belongs_to_many associations' do
|
42
|
+
Widget.has_and_belongs_to_many :users
|
43
|
+
Widget.create users: [user1]
|
44
|
+
user1.map_associations_to user2
|
45
|
+
users = Widget.first.users
|
46
|
+
users.should include(user2)
|
47
|
+
users.should_not include(user1)
|
48
|
+
end
|
49
|
+
|
50
|
+
it "doesn't affect other records" do
|
51
|
+
Widget.has_and_belongs_to_many :users
|
52
|
+
widget1 = Widget.create users: [user1]
|
53
|
+
widget2 = Widget.create
|
54
|
+
user1.map_associations_to user2
|
55
|
+
users = widget1.reload.users
|
56
|
+
users.should include(user2)
|
57
|
+
users.should_not include(user1)
|
58
|
+
widget2.reload.users.should_not include(user2)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
describe 'Remapping specific association type' do
|
63
|
+
it 'given symbol; re-maps has_and_belongs_to_many associations but not the belongs_to' do
|
64
|
+
Widget.has_and_belongs_to_many :users
|
65
|
+
Widget.create user: user1, users: [user1]
|
66
|
+
user1.map_associations_to user2, types: [:has_and_belongs_to_many]
|
67
|
+
Widget.first.users include(user2)
|
68
|
+
Widget.first.user.should eq(user1)
|
69
|
+
end
|
70
|
+
|
71
|
+
it 'given string; re-maps has_and_belongs_to_many associations but not the belongs_to' do
|
72
|
+
Widget.has_and_belongs_to_many :users
|
73
|
+
Widget.create user: user1, users: [user1]
|
74
|
+
user1.map_associations_to user2, types: ['has_and_belongs_to_many']
|
75
|
+
Widget.first.users include(user2)
|
76
|
+
Widget.first.user.should eq(user1)
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
describe 'Remapping specific classes' do
|
81
|
+
it 'only remaps the whitelisted classes' do
|
82
|
+
Widget.create user: user1
|
83
|
+
OtherClass.create user: user1
|
84
|
+
user1.map_associations_to(user2, only: [Widget])
|
85
|
+
Widget.first.user.should eq(user2)
|
86
|
+
OtherClass.first.user.should eq(user1)
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
describe 'Excluding specific classes' do
|
91
|
+
it 'it excludes the blacklisted classes' do
|
92
|
+
Widget.create user: user1
|
93
|
+
OtherClass.create user: user1
|
94
|
+
user1.map_associations_to(user2, except: [OtherClass])
|
95
|
+
Widget.first.user.should eq(user2)
|
96
|
+
OtherClass.first.user.should eq(user1)
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'support/spec_helper'
|
2
|
+
|
3
|
+
describe 'Reporting' do
|
4
|
+
before(:each) { DatabaseCleaner.clean }
|
5
|
+
let(:user1) { User.create }
|
6
|
+
let(:user2) { User.create }
|
7
|
+
before(:each) { Widget.create user: user1 }
|
8
|
+
|
9
|
+
describe 'when optional block returns true' do
|
10
|
+
it 're-maps default belongs_to associations' do
|
11
|
+
user1.map_associations_to(user2) { true }
|
12
|
+
Widget.first.user.should eq(user2)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
describe 'when optional block returns false' do
|
17
|
+
it 'does not re-map default belongs_to associations' do
|
18
|
+
user1.map_associations_to(user2) { false }
|
19
|
+
Widget.first.user.should eq(user1)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
describe 'using it for a report' do
|
24
|
+
it 'works?' do
|
25
|
+
user1.map_associations_to(user2) do |record, from_object, to_object|
|
26
|
+
record.should == Widget.first
|
27
|
+
from_object.should == user1
|
28
|
+
to_object.should == user2
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'active_record'
|
2
|
+
require 'database_cleaner'
|
3
|
+
|
4
|
+
DatabaseCleaner.strategy = :truncation
|
5
|
+
|
6
|
+
ActiveRecord::Base.establish_connection adapter: 'sqlite3', database: ':memory:'
|
7
|
+
|
8
|
+
ActiveRecord::Migration.verbose = false
|
9
|
+
|
10
|
+
ActiveRecord::Migration.create_table :users do |t|
|
11
|
+
t.timestamps
|
12
|
+
end
|
13
|
+
|
14
|
+
ActiveRecord::Migration.create_table :widgets do |t|
|
15
|
+
t.references :user
|
16
|
+
t.references :creator
|
17
|
+
t.timestamps
|
18
|
+
end
|
19
|
+
|
20
|
+
ActiveRecord::Migration.create_table :other_classes do |t|
|
21
|
+
t.references :user
|
22
|
+
t.timestamps
|
23
|
+
end
|
24
|
+
|
25
|
+
ActiveRecord::Migration.create_table :users_widgets do |t|
|
26
|
+
t.references :user
|
27
|
+
t.references :widget
|
28
|
+
end
|
29
|
+
|
30
|
+
class User < ActiveRecord::Base; end
|
31
|
+
|
32
|
+
class Widget < ActiveRecord::Base
|
33
|
+
belongs_to :user
|
34
|
+
end
|
35
|
+
|
36
|
+
class OtherClass < ActiveRecord::Base
|
37
|
+
belongs_to :user
|
38
|
+
end
|
metadata
ADDED
@@ -0,0 +1,180 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: reflections
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.2.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Benjamin Cavileer
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-01-29 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activerecord
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ! '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ! '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.3'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.3'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ! '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ! '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: sqlite3
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ! '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ! '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: database_cleaner
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ! '>='
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ! '>='
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: guard-rspec
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ! '>='
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ! '>='
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: pry
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ! '>='
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ! '>='
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
description: ActiveRecord::Base Reflections
|
126
|
+
email:
|
127
|
+
- bcavileer@holmanauto.com
|
128
|
+
executables: []
|
129
|
+
extensions: []
|
130
|
+
extra_rdoc_files: []
|
131
|
+
files:
|
132
|
+
- .gitignore
|
133
|
+
- .rspec
|
134
|
+
- .rvmrc
|
135
|
+
- Gemfile
|
136
|
+
- Guardfile
|
137
|
+
- LICENSE.txt
|
138
|
+
- README.md
|
139
|
+
- Rakefile
|
140
|
+
- lib/reflections.rb
|
141
|
+
- lib/reflections/active_record_extension.rb
|
142
|
+
- lib/reflections/remapper.rb
|
143
|
+
- lib/reflections/remappers.rb
|
144
|
+
- lib/reflections/remappers/belongs_to.rb
|
145
|
+
- lib/reflections/remappers/has_and_belongs_to_many.rb
|
146
|
+
- lib/reflections/version.rb
|
147
|
+
- reflections.gemspec
|
148
|
+
- spec/remapper_spec.rb
|
149
|
+
- spec/reporter_spec.rb
|
150
|
+
- spec/support/active_record.rb
|
151
|
+
- spec/support/spec_helper.rb
|
152
|
+
homepage: ''
|
153
|
+
licenses:
|
154
|
+
- MIT
|
155
|
+
metadata: {}
|
156
|
+
post_install_message:
|
157
|
+
rdoc_options: []
|
158
|
+
require_paths:
|
159
|
+
- lib
|
160
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
161
|
+
requirements:
|
162
|
+
- - ! '>='
|
163
|
+
- !ruby/object:Gem::Version
|
164
|
+
version: '0'
|
165
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
166
|
+
requirements:
|
167
|
+
- - ! '>='
|
168
|
+
- !ruby/object:Gem::Version
|
169
|
+
version: '0'
|
170
|
+
requirements: []
|
171
|
+
rubyforge_project:
|
172
|
+
rubygems_version: 2.2.1
|
173
|
+
signing_key:
|
174
|
+
specification_version: 4
|
175
|
+
summary: Will help our project
|
176
|
+
test_files:
|
177
|
+
- spec/remapper_spec.rb
|
178
|
+
- spec/reporter_spec.rb
|
179
|
+
- spec/support/active_record.rb
|
180
|
+
- spec/support/spec_helper.rb
|