set_from 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +12 -0
- data/Gemfile +13 -0
- data/MIT-LICENSE +19 -0
- data/README.md +25 -0
- data/Rakefile +34 -0
- data/lib/set_from.rb +70 -0
- data/lib/set_from/version.rb +3 -0
- data/set_from.gemspec +25 -0
- data/test/set_from_test.rb +102 -0
- data/test/test_helper.rb +12 -0
- metadata +112 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 1dcf49b58d5cb66b0b327cc8c1d44e20936ccdd6
|
4
|
+
data.tar.gz: c0cabc0419d7b528f250a19b925e2faa61f1fc9c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 39dd644d88cbd2734c43852f14d763f0bedba997fadd3baa1a61f5a5a8d7734300d6b5a6ee205a4495ef63e397c2270faaa948507520216d1e90f5481c0e8862
|
7
|
+
data.tar.gz: b726004c05e448787e743350d6e51b544544ca7b640c5db32c9c34b532be38a67ab2e866adf0458f170eed321215e6621fd22f19a285318053dfa2fdca0d45ce
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/MIT-LICENSE
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
Copyright (c) 2007 David Heinemeier Hansson
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
5
|
+
in the Software without restriction, including without limitation the rights
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
8
|
+
furnished to do so, subject to the following conditions:
|
9
|
+
|
10
|
+
The above copyright notice and this permission notice shall be included in
|
11
|
+
all copies or substantial portions of the Software.
|
12
|
+
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
## set_from
|
2
|
+
|
3
|
+
### Example
|
4
|
+
```
|
5
|
+
class School < ActiveRecord::Base
|
6
|
+
has_many :teachers
|
7
|
+
has_many :courses
|
8
|
+
end
|
9
|
+
|
10
|
+
class Teacher < ActiveRecord::Base
|
11
|
+
belongs_to :school
|
12
|
+
has_many :courses
|
13
|
+
end
|
14
|
+
|
15
|
+
class Course < ActiveRecord::Base
|
16
|
+
belongs_to :teacher
|
17
|
+
belongs_to :school
|
18
|
+
set_from :teacher, targets: [:school_id], update_for_change: true
|
19
|
+
end
|
20
|
+
|
21
|
+
school = School.create
|
22
|
+
teacher = Teacher.create(school: school)
|
23
|
+
course = teacher.courses.create
|
24
|
+
course.school_id # => school.id
|
25
|
+
```
|
data/Rakefile
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
require "rubygems"
|
2
|
+
require "bundler/setup"
|
3
|
+
Bundler::GemHelper.install_tasks
|
4
|
+
|
5
|
+
require "rake/testtask"
|
6
|
+
|
7
|
+
# Run the test with "rake" or "rake test"
|
8
|
+
desc "Default: run set_From unit tests."
|
9
|
+
task default: :test
|
10
|
+
|
11
|
+
desc "Test the set_From plugin."
|
12
|
+
Rake::TestTask.new(:test) do |t|
|
13
|
+
t.libs << "lib" << "test"
|
14
|
+
t.pattern = "test/**/*_test.rb"
|
15
|
+
t.verbose = false
|
16
|
+
end
|
17
|
+
|
18
|
+
begin
|
19
|
+
# Run the rdoc task to generate rdocs for this gem
|
20
|
+
require "rdoc/task"
|
21
|
+
RDoc::Task.new do |rdoc|
|
22
|
+
require "set_From/version"
|
23
|
+
version = SetFrom::VERSION
|
24
|
+
|
25
|
+
rdoc.rdoc_dir = "rdoc"
|
26
|
+
rdoc.title = "set_From #{version}"
|
27
|
+
rdoc.rdoc_files.include("README*")
|
28
|
+
rdoc.rdoc_files.include("lib/**/*.rb")
|
29
|
+
end
|
30
|
+
rescue LoadError
|
31
|
+
puts "RDocTask is not supported on this platform."
|
32
|
+
rescue StandardError
|
33
|
+
puts "RDocTask is not supported on this platform."
|
34
|
+
end
|
data/lib/set_from.rb
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
require "set_from/version"
|
2
|
+
module SetFrom
|
3
|
+
def self.included(base)
|
4
|
+
base.extend(ClassMethods)
|
5
|
+
end
|
6
|
+
|
7
|
+
# Example
|
8
|
+
# class School < ActiveRecord::Base
|
9
|
+
# has_many :teachers
|
10
|
+
# has_many :courses
|
11
|
+
# end
|
12
|
+
|
13
|
+
# class Teacher < ActiveRecord::Base
|
14
|
+
# belongs_to :school
|
15
|
+
# has_many :courses
|
16
|
+
# end
|
17
|
+
|
18
|
+
# class Course < ActiveRecord::Base
|
19
|
+
# belongs_to :teacher
|
20
|
+
# belongs_to :school
|
21
|
+
# set_from :teacher, targets: [:school_id], update_for_change: true
|
22
|
+
# end
|
23
|
+
|
24
|
+
# school = School.create
|
25
|
+
# teacher = Teacher.create(school: school)
|
26
|
+
# course = teacher.courses.create
|
27
|
+
# course.school_id # => school.id
|
28
|
+
|
29
|
+
module ClassMethods
|
30
|
+
def set_from(source, options={})
|
31
|
+
targets = options.fetch(:targets, [])
|
32
|
+
if targets.last.is_a?(Hash)
|
33
|
+
hash_params = targets.pop
|
34
|
+
targets_hash = Hash[targets.map{|key| [key, key]}]
|
35
|
+
targets_hash = targets_hash.merge(hash_params)
|
36
|
+
else
|
37
|
+
targets_hash = Hash[targets.map{|key| [key, key]}]
|
38
|
+
end
|
39
|
+
update_for_change = options.fetch(:update_for_change, false)
|
40
|
+
prefix = options.fetch(:prefix, false)
|
41
|
+
self.class_eval do
|
42
|
+
after_initialize "set_from_#{source}".to_sym, if: :new_record?
|
43
|
+
before_validation "set_from_#{source}".to_sym, if: :new_record?
|
44
|
+
if update_for_change
|
45
|
+
before_save "set_from_#{source}_when_change".to_sym, if: ->{ persisted? && send("#{source.to_s.foreign_key}_changed?")}
|
46
|
+
end
|
47
|
+
define_method "set_from_#{source}".to_sym do
|
48
|
+
from_source = self.send(source)
|
49
|
+
targets_hash.each do |source_target_method, set_target_method|
|
50
|
+
set_target_method = prefix ? "#{source}_#{set_target_method}" : set_target_method
|
51
|
+
if self.respond_to?(set_target_method) && self.send(set_target_method).blank? && from_source.present? && from_source.respond_to?(source_target_method)
|
52
|
+
self.send("#{set_target_method}=", from_source.send(source_target_method))
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
define_method "set_from_#{source}_when_change".to_sym do
|
57
|
+
from_source = self.send(source)
|
58
|
+
targets_hash.each do |source_target_method, set_target_method|
|
59
|
+
set_target_method = prefix ? "#{source}_#{set_target_method}" : set_target_method
|
60
|
+
if self.respond_to?(set_target_method) && from_source.present? && from_source.respond_to?(source_target_method)
|
61
|
+
self.send("#{set_target_method}=", from_source.send(source_target_method))
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
ActiveRecord::Base.send(:include, SetFrom) if defined?(ActiveRecord)
|
data/set_from.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
lib = File.expand_path('../lib', __FILE__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
require 'set_from/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "set_from"
|
7
|
+
spec.version = SetFrom::VERSION
|
8
|
+
spec.authors = ["Franky"]
|
9
|
+
spec.email = ["zhanglinjie412@gmail.com"]
|
10
|
+
spec.description = %q{A gem make a model column set from nother}
|
11
|
+
spec.summary = %q{A gem make a model column set from nother}
|
12
|
+
spec.homepage = "https://github.com/zhanglinjie/set_from"
|
13
|
+
spec.license = "MIT"
|
14
|
+
|
15
|
+
spec.files = `git ls-files`.split($/)
|
16
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
17
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.add_dependency 'activerecord', ">= 3.0"
|
21
|
+
|
22
|
+
spec.add_development_dependency 'bundler', '~> 1.3'
|
23
|
+
spec.add_development_dependency 'rake'
|
24
|
+
spec.add_development_dependency 'rspec'
|
25
|
+
end
|
@@ -0,0 +1,102 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
ActiveRecord::Base.establish_connection(
|
4
|
+
adapter: "sqlite3",
|
5
|
+
database: 'file:memdb1?mode=memory&cache=shared'
|
6
|
+
)
|
7
|
+
ActiveRecord::Schema.verbose = false
|
8
|
+
|
9
|
+
def setup_db
|
10
|
+
# AR caches columns options like defaults etc. Clear them!
|
11
|
+
ActiveRecord::Base.connection.create_table :schools do |t|
|
12
|
+
t.column :name, :string
|
13
|
+
t.column :created_at, :datetime
|
14
|
+
t.column :updated_at, :datetime
|
15
|
+
end
|
16
|
+
|
17
|
+
ActiveRecord::Base.connection.create_table :teachers do |t|
|
18
|
+
t.column :name, :string
|
19
|
+
t.column :school_id, :integer
|
20
|
+
t.column :created_at, :datetime
|
21
|
+
t.column :updated_at, :datetime
|
22
|
+
end
|
23
|
+
|
24
|
+
ActiveRecord::Base.connection.create_table :courses do |t|
|
25
|
+
t.column :name, :string
|
26
|
+
t.column :school_id, :integer
|
27
|
+
t.column :teacher_id, :integer
|
28
|
+
t.column :created_at, :datetime
|
29
|
+
t.column :updated_at, :datetime
|
30
|
+
end
|
31
|
+
|
32
|
+
ActiveRecord::Base.connection.schema_cache.clear!
|
33
|
+
[School, Teacher, Course].each do |klass|
|
34
|
+
klass.reset_column_information
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def teardown_db
|
39
|
+
if ActiveRecord::VERSION::MAJOR >= 5
|
40
|
+
tables = ActiveRecord::Base.connection.data_sources
|
41
|
+
else
|
42
|
+
tables = ActiveRecord::Base.connection.tables
|
43
|
+
end
|
44
|
+
|
45
|
+
tables.each do |table|
|
46
|
+
ActiveRecord::Base.connection.drop_table(table)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
class School < ActiveRecord::Base
|
51
|
+
has_many :teachers
|
52
|
+
has_many :courses
|
53
|
+
end
|
54
|
+
|
55
|
+
class Teacher < ActiveRecord::Base
|
56
|
+
belongs_to :school
|
57
|
+
has_many :courses
|
58
|
+
end
|
59
|
+
|
60
|
+
class Course < ActiveRecord::Base
|
61
|
+
belongs_to :school
|
62
|
+
belongs_to :teacher
|
63
|
+
set_from :teacher, targets: [:school_id], update_for_change: true
|
64
|
+
end
|
65
|
+
|
66
|
+
class SetFromTestCase < Minitest::Test
|
67
|
+
def teardown
|
68
|
+
teardown_db
|
69
|
+
end
|
70
|
+
|
71
|
+
def setup
|
72
|
+
setup_db
|
73
|
+
end
|
74
|
+
|
75
|
+
def test_set_from_when_initialize
|
76
|
+
school = School.create
|
77
|
+
teacher = Teacher.create(school: school)
|
78
|
+
course = teacher.courses.build
|
79
|
+
assert_equal school.id, course.school_id
|
80
|
+
end
|
81
|
+
|
82
|
+
def test_set_from_when_save
|
83
|
+
school = School.create
|
84
|
+
teacher = Teacher.create(school: school)
|
85
|
+
course = Course.new
|
86
|
+
course.teacher = teacher
|
87
|
+
course.save
|
88
|
+
assert_equal school.id, course.school_id
|
89
|
+
end
|
90
|
+
|
91
|
+
def test_set_from_when_change
|
92
|
+
school1 = School.create
|
93
|
+
teacher1 = Teacher.create(school: school1)
|
94
|
+
school2 = School.create
|
95
|
+
teacher2 = Teacher.create(school: school2)
|
96
|
+
course = teacher1.courses.create
|
97
|
+
assert_equal school1.id, course.school_id
|
98
|
+
course.update(teacher: teacher2)
|
99
|
+
assert_equal school2.id, course.school_id
|
100
|
+
end
|
101
|
+
|
102
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
require "rubygems"
|
2
|
+
require "bundler/setup"
|
3
|
+
begin
|
4
|
+
Bundler.setup(:default, :development)
|
5
|
+
rescue Bundler::BundlerError => e
|
6
|
+
$stderr.puts e.message
|
7
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
8
|
+
exit e.status_code
|
9
|
+
end
|
10
|
+
require "active_record"
|
11
|
+
require "minitest/autorun"
|
12
|
+
require "set_from"
|
metadata
ADDED
@@ -0,0 +1,112 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: set_from
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Franky
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-09-06 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: '3.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '3.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
|
+
description: A gem make a model column set from nother
|
70
|
+
email:
|
71
|
+
- zhanglinjie412@gmail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- Gemfile
|
78
|
+
- MIT-LICENSE
|
79
|
+
- README.md
|
80
|
+
- Rakefile
|
81
|
+
- lib/set_from.rb
|
82
|
+
- lib/set_from/version.rb
|
83
|
+
- set_from.gemspec
|
84
|
+
- test/set_from_test.rb
|
85
|
+
- test/test_helper.rb
|
86
|
+
homepage: https://github.com/zhanglinjie/set_from
|
87
|
+
licenses:
|
88
|
+
- MIT
|
89
|
+
metadata: {}
|
90
|
+
post_install_message:
|
91
|
+
rdoc_options: []
|
92
|
+
require_paths:
|
93
|
+
- lib
|
94
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
95
|
+
requirements:
|
96
|
+
- - ">="
|
97
|
+
- !ruby/object:Gem::Version
|
98
|
+
version: '0'
|
99
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
requirements: []
|
105
|
+
rubyforge_project:
|
106
|
+
rubygems_version: 2.5.1
|
107
|
+
signing_key:
|
108
|
+
specification_version: 4
|
109
|
+
summary: A gem make a model column set from nother
|
110
|
+
test_files:
|
111
|
+
- test/set_from_test.rb
|
112
|
+
- test/test_helper.rb
|