belongs_to_remote 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/belongs_to_remote.gemspec +0 -3
- data/lib/belongs_to_remote.rb +27 -10
- data/lib/belongs_to_remote/version.rb +1 -1
- data/spec/belongs_to_remote_spec.rb +28 -8
- metadata +2 -44
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c35372784d2c2eef5027d3788615f4d41ad2368d
|
4
|
+
data.tar.gz: ad48ccd49527a54f92123f9b47e3ea12b1faa9a5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cf14fdba0fafa4432fdce694e2bc5071253020f02caf430bba1d1678ffd888de2fddf438f07f1590283e7ea75df3e57a0893371d1f027ba1aaf223033cffcb9c
|
7
|
+
data.tar.gz: 14e3e3112833aa2f0912180241599b8270311d24b727920a3ca8c94ca31407e06f9f23871d62207631f31926c383cc23d7eee28d05de3c32f1e3d65e7d0dc4b8
|
data/belongs_to_remote.gemspec
CHANGED
@@ -18,9 +18,6 @@ Gem::Specification.new do |spec|
|
|
18
18
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
19
|
spec.require_paths = ["lib"]
|
20
20
|
|
21
|
-
spec.add_dependency "activesupport", '>= 3.0'
|
22
|
-
spec.add_dependency "activerecord", '>= 3.0'
|
23
|
-
spec.add_dependency "activeresource", '>= 3.0'
|
24
21
|
spec.add_development_dependency "bundler", "~> 1.5"
|
25
22
|
spec.add_development_dependency "rake"
|
26
23
|
spec.add_development_dependency "rspec"
|
data/lib/belongs_to_remote.rb
CHANGED
@@ -1,12 +1,11 @@
|
|
1
1
|
require "belongs_to_remote/version"
|
2
|
-
require "active_support/concern"
|
3
|
-
require "active_record"
|
4
|
-
require "active_resource"
|
5
2
|
|
6
3
|
module BelongsToRemote
|
7
|
-
|
4
|
+
def self.included(base)
|
5
|
+
base.extend(ClassMethods)
|
6
|
+
end
|
8
7
|
|
9
|
-
|
8
|
+
module ClassMethods
|
10
9
|
##
|
11
10
|
# Defines private instance method for one-to-one association
|
12
11
|
# with ActiveResource::Base (or ActiveRecord::Base).
|
@@ -21,18 +20,28 @@ module BelongsToRemote
|
|
21
20
|
# Defined method returns nil if none is found.
|
22
21
|
#
|
23
22
|
def belongs_to_remote(name, options = {})
|
23
|
+
name.to_s.split('_').each(&:capitalize!).join
|
24
24
|
if options[:polymorphic]
|
25
25
|
class_eval %(
|
26
26
|
def #{name}
|
27
27
|
return nil unless self.#{name.to_s + "_type"} && self.#{options[:foreign_key] || name.to_s + "_id" }
|
28
|
-
@#{name} ||= self.#{name.to_s + "_type"}.
|
28
|
+
@#{name} ||= Object.const_get(self.#{name.to_s + "_type"}).find(self.#{options[:foreign_key] || name.to_s + "_id" }) rescue nil
|
29
|
+
end
|
30
|
+
|
31
|
+
def #{name}=(object)
|
32
|
+
self.#{options[:foreign_key] || name.to_s + "_id" } = object.__send__(object.class.respond_to?(:primary_key) ? object.class.primary_key.to_sym : :id)
|
33
|
+
self.#{name.to_s + "_type"} = object.class.to_s
|
29
34
|
end
|
30
35
|
)
|
31
36
|
else
|
32
37
|
class_eval %(
|
33
38
|
def #{name}
|
34
39
|
return nil unless self.#{options[:foreign_key] || name.to_s + "_id" }
|
35
|
-
@#{name} ||= #{options[:class_name] || name.to_s.
|
40
|
+
@#{name} ||= #{options[:class_name] || name.to_s.split('_').each(&:capitalize!).join}.find(self.#{options[:foreign_key] || name.to_s + "_id" }) rescue nil
|
41
|
+
end
|
42
|
+
|
43
|
+
def #{name}=(object)
|
44
|
+
self.#{options[:foreign_key] || name.to_s + "_id" } = object.__send__(object.class.respond_to?(:primary_key) ? object.class.primary_key.to_sym : :id)
|
36
45
|
end
|
37
46
|
)
|
38
47
|
end
|
@@ -40,11 +49,17 @@ module BelongsToRemote
|
|
40
49
|
end
|
41
50
|
end
|
42
51
|
|
43
|
-
|
44
|
-
|
52
|
+
begin
|
53
|
+
require 'active_record'
|
54
|
+
class ActiveRecord::Base
|
55
|
+
include BelongsToRemote
|
56
|
+
end
|
57
|
+
rescue LoadError
|
45
58
|
end
|
46
59
|
|
47
|
-
|
60
|
+
begin
|
61
|
+
require 'active_resource'
|
62
|
+
unless ActiveResource::Base.respond_to?(:belongs_to)
|
48
63
|
class ActiveResource::Base
|
49
64
|
include BelongsToRemote
|
50
65
|
class << self
|
@@ -52,3 +67,5 @@ unless ActiveResource::Base.respond_to?(:belongs_to)
|
|
52
67
|
end
|
53
68
|
end
|
54
69
|
end
|
70
|
+
rescue LoadError
|
71
|
+
end
|
@@ -2,37 +2,57 @@ require "spec_helper"
|
|
2
2
|
|
3
3
|
describe BelongsToRemote do
|
4
4
|
subject { Product.new }
|
5
|
+
let!(:user) { User.new(4) }
|
6
|
+
let!(:store) { Store.new(2) }
|
7
|
+
let!(:order) { Order.new(5) }
|
8
|
+
let!(:customer) { Customer.new(4) }
|
5
9
|
|
6
10
|
describe "basic usage" do
|
7
11
|
it "has association method which finds belonging object" do
|
8
|
-
user = User.new(4)
|
9
12
|
subject.user_id = 4
|
10
|
-
subject.user.
|
13
|
+
expect(subject.user).to eq user
|
11
14
|
end
|
12
15
|
end
|
13
16
|
|
14
17
|
describe "option :class_name" do
|
15
18
|
it "has association method which finds belonging instance of provided :class_name" do
|
16
|
-
store = Store.new(2)
|
17
19
|
subject.depot_id = 2
|
18
|
-
subject.depot.
|
20
|
+
expect(subject.depot).to eq store
|
19
21
|
end
|
20
22
|
end
|
21
23
|
|
22
24
|
describe "option :foreign_key" do
|
23
25
|
it "has association method which finds belonging object by adjusted :foreign_key" do
|
24
|
-
order = Order.new(5)
|
25
26
|
subject.booking_id = 5
|
26
|
-
subject.order.
|
27
|
+
expect(subject.order).to eq order
|
27
28
|
end
|
28
29
|
end
|
29
30
|
|
30
31
|
describe "option :polymorphic" do
|
31
32
|
it "has association method which finds belonging :polymorphic object" do
|
32
|
-
customer = Customer.new(4)
|
33
33
|
subject.owner_id = 4
|
34
34
|
subject.owner_type = 'Customer'
|
35
|
-
subject.owner.
|
35
|
+
expect(subject.owner).to eq customer
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
describe "writer method for association" do
|
40
|
+
it "assigns association id for regular association" do
|
41
|
+
subject.user = user
|
42
|
+
expect(subject.user_id).to eq user.id
|
43
|
+
subject.depot = store
|
44
|
+
expect(subject.depot_id).to eq store.id
|
45
|
+
end
|
46
|
+
|
47
|
+
it "assigns attribute foreign_key if provided option :foreign_key" do
|
48
|
+
subject.order = order
|
49
|
+
expect(subject.booking_id).to eq order.id
|
50
|
+
end
|
51
|
+
|
52
|
+
it "assigns association id and type for polymorphic association" do
|
53
|
+
subject.owner = customer
|
54
|
+
expect(subject.owner_id).to eq customer.id
|
55
|
+
expect(subject.owner_type).to eq "Customer"
|
36
56
|
end
|
37
57
|
end
|
38
58
|
end
|
metadata
CHANGED
@@ -1,57 +1,15 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: belongs_to_remote
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alex Avoyants
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2015-03-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
-
- !ruby/object:Gem::Dependency
|
14
|
-
name: activesupport
|
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: activerecord
|
29
|
-
requirement: !ruby/object:Gem::Requirement
|
30
|
-
requirements:
|
31
|
-
- - '>='
|
32
|
-
- !ruby/object:Gem::Version
|
33
|
-
version: '3.0'
|
34
|
-
type: :runtime
|
35
|
-
prerelease: false
|
36
|
-
version_requirements: !ruby/object:Gem::Requirement
|
37
|
-
requirements:
|
38
|
-
- - '>='
|
39
|
-
- !ruby/object:Gem::Version
|
40
|
-
version: '3.0'
|
41
|
-
- !ruby/object:Gem::Dependency
|
42
|
-
name: activeresource
|
43
|
-
requirement: !ruby/object:Gem::Requirement
|
44
|
-
requirements:
|
45
|
-
- - '>='
|
46
|
-
- !ruby/object:Gem::Version
|
47
|
-
version: '3.0'
|
48
|
-
type: :runtime
|
49
|
-
prerelease: false
|
50
|
-
version_requirements: !ruby/object:Gem::Requirement
|
51
|
-
requirements:
|
52
|
-
- - '>='
|
53
|
-
- !ruby/object:Gem::Version
|
54
|
-
version: '3.0'
|
55
13
|
- !ruby/object:Gem::Dependency
|
56
14
|
name: bundler
|
57
15
|
requirement: !ruby/object:Gem::Requirement
|