remote_association 0.0.4 → 0.0.5

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 ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ ZDRhODhkODdiMDlkMjY3MTdmYjRhMWE2NzY3MWYwODE3OWRjOGFhYw==
5
+ data.tar.gz: !binary |-
6
+ YmMxZWIzZDgyODNiYjM3YTJlZjQ3NDZkNTJkMGNhMDk4ODg3MDdjOA==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ YWYxZjBkNDIxNmEwNTJlZWNmZmQyNGM0MzkxY2Y3ZDEwZjgxYWJmZjdmNjI2
10
+ NjM0NjhhYTcxZDQwNjJjNjRjMTEzODVmZWE3YWI4MDQ0ZGE1OTBkOWZjZTJj
11
+ ZmFjZjBjM2EwMzRjM2FkYjZkMWUyM2I0OTQxNTUxMDM4NWI3MGM=
12
+ data.tar.gz: !binary |-
13
+ MmIyYTgwZWMwM2E0MWYyNTIwYjhkZWY3OTJhZWEyNzY5YjYzZmY0ZjliOTQy
14
+ N2IzOTBhODc3NjUzYzM5NTQ4MzZhZmZjMzE0NzI4MjkzM2FlY2IyY2EwMWEw
15
+ Y2VmNTQ3ZGMzZTVhYjVkY2NkYTY5YjBiMjc0MWJmYjIwZTAxZjE=
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ remote_association
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 1.9.3-p550
data/.travis.yml CHANGED
@@ -2,6 +2,8 @@ language: ruby
2
2
 
3
3
  rvm:
4
4
  - 1.9.3
5
+ - 2.0
6
+ - 2.1
5
7
 
6
8
  before_script:
7
9
  - "cp spec/config/database.example.yml spec/config/database.yml"
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- remote_association (0.0.4)
4
+ remote_association (0.0.5)
5
5
  activerecord (~> 3.2)
6
6
  activeresource (~> 3.2)
7
7
  activesupport (~> 3.2)
@@ -9,27 +9,27 @@ PATH
9
9
  GEM
10
10
  remote: https://rubygems.org/
11
11
  specs:
12
- activemodel (3.2.8)
13
- activesupport (= 3.2.8)
12
+ activemodel (3.2.19)
13
+ activesupport (= 3.2.19)
14
14
  builder (~> 3.0.0)
15
- activerecord (3.2.8)
16
- activemodel (= 3.2.8)
17
- activesupport (= 3.2.8)
15
+ activerecord (3.2.19)
16
+ activemodel (= 3.2.19)
17
+ activesupport (= 3.2.19)
18
18
  arel (~> 3.0.2)
19
19
  tzinfo (~> 0.3.29)
20
- activeresource (3.2.8)
21
- activemodel (= 3.2.8)
22
- activesupport (= 3.2.8)
23
- activesupport (3.2.8)
24
- i18n (~> 0.6)
20
+ activeresource (3.2.19)
21
+ activemodel (= 3.2.19)
22
+ activesupport (= 3.2.19)
23
+ activesupport (3.2.19)
24
+ i18n (~> 0.6, >= 0.6.4)
25
25
  multi_json (~> 1.0)
26
- arel (3.0.2)
27
- builder (3.0.3)
26
+ arel (3.0.3)
27
+ builder (3.0.4)
28
28
  database_cleaner (0.8.0)
29
29
  diff-lcs (1.1.3)
30
30
  fakeweb (1.3.0)
31
- i18n (0.6.1)
32
- multi_json (1.3.6)
31
+ i18n (0.6.11)
32
+ multi_json (1.10.1)
33
33
  pg (0.14.1)
34
34
  rake (0.9.2.2)
35
35
  rspec (2.11.0)
@@ -40,7 +40,7 @@ GEM
40
40
  rspec-expectations (2.11.3)
41
41
  diff-lcs (~> 1.1.3)
42
42
  rspec-mocks (2.11.3)
43
- tzinfo (0.3.33)
43
+ tzinfo (0.3.42)
44
44
 
45
45
  PLATFORMS
46
46
  ruby
@@ -21,6 +21,11 @@ module ActiveRecord
21
21
  settings = klass.activeresource_relations[r.to_sym]
22
22
  raise RemoteAssociation::SettingsNotFoundError, "Can't find settings for #{r} association" if settings.blank?
23
23
 
24
+ if settings[:polymorphic]
25
+ puts "Preload of polymorphic associations not supported"
26
+ next
27
+ end
28
+
24
29
  ar_class = settings[:class_name].constantize
25
30
 
26
31
  remote_associations << OpenStruct.new(
@@ -37,6 +37,13 @@ module RemoteAssociation
37
37
  # of "favorite_person_id".
38
38
  # [:primary_key]
39
39
  # Specify the http query parameter to find associated object used for the association. By default this is <tt>id</tt>.
40
+ # [:polymorphic]
41
+ # Specify this association is a polymorphic association by passing true.
42
+ # [:foreign_type]
43
+ # Specify the column used to store the associated object’s type, if this is a polymorphic association.
44
+ # By default this is guessed to be the name of the association with a “_type” suffix.
45
+ # So a class that defines a belongs_to_remote :owner, :polymorphic => true association
46
+ # will use “owner_type” as the default :foreign_type.
40
47
  # Example:
41
48
  # belongs_to_remote :firm, :primary_key => 'search[id_in]' #=> ...?firms.json?search%5Bid_in%5D%5B%5D=1
42
49
  #
@@ -45,34 +52,66 @@ module RemoteAssociation
45
52
  # belongs_to_remote :author, :class_name => "Person", :foreign_key => "author_id"
46
53
  def belongs_to_remote(remote_rel, options ={})
47
54
  rel_options = {
48
- class_name: remote_rel.to_s.classify,
49
- foreign_key: remote_rel.to_s.foreign_key,
55
+ class_name: remote_rel.to_s.classify,
56
+ foreign_key: remote_rel.to_s.foreign_key,
57
+ foreign_type: remote_rel.to_s + '_type',
50
58
  association_type: :belongs_to_remote,
51
59
  primary_key: primary_key
52
60
  }.merge(options.symbolize_keys)
53
61
 
54
62
  add_activeresource_relation(remote_rel.to_sym, rel_options)
55
63
 
56
- class_eval <<-RUBY, __FILE__, __LINE__+1
64
+ if rel_options[:polymorphic]
65
+ class_eval <<-RUBY, __FILE__, __LINE__+1
57
66
 
58
- attr_accessor :#{remote_rel}
67
+ attr_accessor :#{remote_rel}
59
68
 
60
- def #{remote_rel}
61
- if remote_resources_loaded?
62
- @#{remote_rel} ? @#{remote_rel}.first : nil
63
- else
64
- @#{remote_rel} ||= self.#{rel_options[:foreign_key]}.present? ? #{rel_options[:class_name]}.find(:first, params: self.class.build_params_hash_for_#{remote_rel}(self.#{rel_options[:foreign_key]})) : nil
69
+ def #{remote_rel}
70
+ if remote_resources_loaded?
71
+ @#{remote_rel} ? @#{remote_rel}.first : nil
72
+ else
73
+ @#{remote_rel} ||= if self.#{rel_options[:foreign_key]}.present? && self.#{rel_options[:foreign_class]}.present?
74
+ #{rel_options[:foreign_type]}.classify.constantize.find(:all, params: self.class.build_params_hash_for_#{remote_rel}(self.#{rel_options[:foreign_key]})).try(:first)
75
+ else
76
+ nil
77
+ end
78
+ end
65
79
  end
66
- end
67
80
 
68
- ##
69
- # Returns Hash with HTTP parameters to query remote API
70
- def self.build_params_hash_for_#{remote_rel}(keys)
71
- keys = [keys] unless keys.kind_of?(Array)
72
- {"#{rel_options[:primary_key]}" => keys}
73
- end
81
+ ##
82
+ # Returns Hash with HTTP parameters to query remote API
83
+ def self.build_params_hash_for_#{remote_rel}(keys)
84
+ keys = [keys] unless keys.kind_of?(Array)
85
+ {"#{rel_options[:primary_key]}" => keys}
86
+ end
87
+
88
+ RUBY
89
+ else
90
+ class_eval <<-RUBY, __FILE__, __LINE__+1
91
+
92
+ attr_accessor :#{remote_rel}
93
+
94
+ def #{remote_rel}
95
+ if remote_resources_loaded?
96
+ @#{remote_rel} ? @#{remote_rel}.first : nil
97
+ else
98
+ @#{remote_rel} ||= if self.#{rel_options[:foreign_key]}.present?
99
+ #{rel_options[:class_name]}.find(:all, params: self.class.build_params_hash_for_#{remote_rel}(self.#{rel_options[:foreign_key]})).try(:first)
100
+ else
101
+ nil
102
+ end
103
+ end
104
+ end
105
+
106
+ ##
107
+ # Returns Hash with HTTP parameters to query remote API
108
+ def self.build_params_hash_for_#{remote_rel}(keys)
109
+ keys = [keys] unless keys.kind_of?(Array)
110
+ {"#{rel_options[:primary_key]}" => keys}
111
+ end
74
112
 
75
- RUBY
113
+ RUBY
114
+ end
76
115
  end
77
116
  end
78
117
  end
@@ -1,3 +1,3 @@
1
1
  module RemoteAssociation
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
@@ -25,5 +25,4 @@ Gem::Specification.new do |gem|
25
25
  gem.add_development_dependency 'pg', '~> 0.14'
26
26
  gem.add_development_dependency 'database_cleaner', '~> 0.8'
27
27
  gem.add_development_dependency 'fakeweb'
28
-
29
28
  end
@@ -114,6 +114,66 @@ describe RemoteAssociation, "method :belongs_to_remote" do
114
114
  end
115
115
  end
116
116
 
117
+ context 'with polymorphic option' do
118
+ before(:each) do
119
+ unset_const(:PolymorphicProfile)
120
+ unset_const(:OwnerA)
121
+ class OwnerA < ActiveResource::Base
122
+ self.site = REMOTE_HOST
123
+ self.element_name = "owner_a"
124
+ end
125
+ class PolymorphicProfile < ActiveRecord::Base
126
+ include RemoteAssociation::Base
127
+ belongs_to_remote :owner, polymorphic: true
128
+ end
129
+ add_polymorphic_profile(1, 1, 'owner_a', "")
130
+
131
+ body_a = [{owner_a: {id: 1, name: "The Owner A"}}].to_json
132
+ FakeWeb.register_uri(:get, "#{REMOTE_HOST}/owner_as.json?id%5B%5D=1", body: body_a)
133
+ end
134
+
135
+ it 'has polymorphic option' do
136
+ PolymorphicProfile.first.owner.name.should eq('The Owner A')
137
+ end
138
+
139
+ pending 'should prefetch remote associations of models with defaults (single request)' do
140
+ add_polymorphic_profile(2, 2, 'owner_a', "")
141
+
142
+ full_body = [{owner_a: {id: 1, name: "The Owner A"}}, {owner_a: {id: 2, name: "The Owner A (other)"}}].to_json
143
+ FakeWeb.register_uri(:get, "#{REMOTE_HOST}/owner_as.json?id%5B%5D=1&id%5B%5D=2", body: full_body)
144
+
145
+ profiles = PolymorphicProfile.scoped.includes_remote(:owner).all
146
+ profiles.first.owner.name.should eq('The Owner A')
147
+ profiles.last.owner.name.should eq('The Owner A (other)')
148
+ end
149
+ end
150
+
151
+ context "resource unavailable" do
152
+ subject { Profile.first.user }
153
+ before do
154
+ unset_const(:Profile)
155
+ class Profile < ActiveRecord::Base
156
+ include RemoteAssociation::Base
157
+ belongs_to_remote :user
158
+ end
159
+ FakeWeb.register_uri(:get, "#{REMOTE_HOST}/users.json?id%5B%5D=1", status: '404', body: '<html>
160
+ <head><title>404 Not Found</title></head>
161
+ <body bgcolor="white">
162
+ <center><h1>404 Not Found</h1></center>
163
+ <hr><center>nginx/1.6.2</center>
164
+ </body>
165
+ </html>')
166
+ end
167
+
168
+ it "not raises error" do
169
+ expect { subject }.to_not raise_error
170
+ end
171
+
172
+ it "is nil" do
173
+ should be_nil
174
+ end
175
+ end
176
+
117
177
  context "safe when using several remotes" do
118
178
  before do
119
179
  unset_const(:User)
data/spec/spec_helper.rb CHANGED
@@ -42,6 +42,10 @@ def add_profile(id, user_id, like)
42
42
  ActiveRecord::Base.connection.execute("insert into \"public\".\"profiles\" (\"id\", \"user_id\", \"like\") values ( #{id}, #{user_id}, '#{like}');")
43
43
  end
44
44
 
45
+ def add_polymorphic_profile(id, owner_id, owner_type, like)
46
+ ActiveRecord::Base.connection.execute("insert into \"public\".\"polymorphic_profiles\" (\"id\", \"owner_id\", \"owner_type\", \"like\") values ( #{id}, #{owner_id}, '#{owner_type}', '#{like}');")
47
+ end
48
+
45
49
  REMOTE_HOST = "http://127.0.0.1:3000"
46
50
 
47
51
  def unset_const(const_name)
@@ -1,4 +1,5 @@
1
1
  require 'active_record'
2
+ require 'yaml'
2
3
 
3
4
  namespace :spec do
4
5
  namespace :db do
@@ -36,6 +37,17 @@ CREATE TABLE "public"."profiles" (
36
37
  WITH (OIDS=FALSE);
37
38
  ALTER TABLE "public"."profiles" OWNER TO "rails";
38
39
 
40
+ DROP TABLE IF EXISTS "public"."polymorphic_profiles";
41
+
42
+ CREATE TABLE "public"."polymorphic_profiles" (
43
+ "id" int4 NOT NULL,
44
+ "owner_id" int4,
45
+ "owner_type" varchar(255) NOT NULL,
46
+ "like" varchar(255) NOT NULL,
47
+ CONSTRAINT "polymorphic_profiles_pkey" PRIMARY KEY ("id") NOT DEFERRABLE INITIALLY IMMEDIATE
48
+ )
49
+ WITH (OIDS=FALSE);
50
+ ALTER TABLE "public"."polymorphic_profiles" OWNER TO "rails";
39
51
  SQL
40
52
  )
41
53
  end
@@ -64,6 +76,3 @@ SQL
64
76
  end
65
77
  end
66
78
  end
67
-
68
-
69
-
metadata CHANGED
@@ -1,20 +1,18 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: remote_association
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
5
- prerelease:
4
+ version: 0.0.5
6
5
  platform: ruby
7
6
  authors:
8
7
  - Denis Yagofarov
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2012-11-26 00:00:00.000000000 Z
11
+ date: 2014-11-06 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: activesupport
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
17
  - - ~>
20
18
  - !ruby/object:Gem::Version
@@ -22,7 +20,6 @@ dependencies:
22
20
  type: :runtime
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
24
  - - ~>
28
25
  - !ruby/object:Gem::Version
@@ -30,7 +27,6 @@ dependencies:
30
27
  - !ruby/object:Gem::Dependency
31
28
  name: activerecord
32
29
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
30
  requirements:
35
31
  - - ~>
36
32
  - !ruby/object:Gem::Version
@@ -38,7 +34,6 @@ dependencies:
38
34
  type: :runtime
39
35
  prerelease: false
40
36
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
37
  requirements:
43
38
  - - ~>
44
39
  - !ruby/object:Gem::Version
@@ -46,7 +41,6 @@ dependencies:
46
41
  - !ruby/object:Gem::Dependency
47
42
  name: activeresource
48
43
  requirement: !ruby/object:Gem::Requirement
49
- none: false
50
44
  requirements:
51
45
  - - ~>
52
46
  - !ruby/object:Gem::Version
@@ -54,7 +48,6 @@ dependencies:
54
48
  type: :runtime
55
49
  prerelease: false
56
50
  version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
51
  requirements:
59
52
  - - ~>
60
53
  - !ruby/object:Gem::Version
@@ -62,7 +55,6 @@ dependencies:
62
55
  - !ruby/object:Gem::Dependency
63
56
  name: rspec
64
57
  requirement: !ruby/object:Gem::Requirement
65
- none: false
66
58
  requirements:
67
59
  - - ~>
68
60
  - !ruby/object:Gem::Version
@@ -70,7 +62,6 @@ dependencies:
70
62
  type: :development
71
63
  prerelease: false
72
64
  version_requirements: !ruby/object:Gem::Requirement
73
- none: false
74
65
  requirements:
75
66
  - - ~>
76
67
  - !ruby/object:Gem::Version
@@ -78,7 +69,6 @@ dependencies:
78
69
  - !ruby/object:Gem::Dependency
79
70
  name: pg
80
71
  requirement: !ruby/object:Gem::Requirement
81
- none: false
82
72
  requirements:
83
73
  - - ~>
84
74
  - !ruby/object:Gem::Version
@@ -86,7 +76,6 @@ dependencies:
86
76
  type: :development
87
77
  prerelease: false
88
78
  version_requirements: !ruby/object:Gem::Requirement
89
- none: false
90
79
  requirements:
91
80
  - - ~>
92
81
  - !ruby/object:Gem::Version
@@ -94,7 +83,6 @@ dependencies:
94
83
  - !ruby/object:Gem::Dependency
95
84
  name: database_cleaner
96
85
  requirement: !ruby/object:Gem::Requirement
97
- none: false
98
86
  requirements:
99
87
  - - ~>
100
88
  - !ruby/object:Gem::Version
@@ -102,7 +90,6 @@ dependencies:
102
90
  type: :development
103
91
  prerelease: false
104
92
  version_requirements: !ruby/object:Gem::Requirement
105
- none: false
106
93
  requirements:
107
94
  - - ~>
108
95
  - !ruby/object:Gem::Version
@@ -110,7 +97,6 @@ dependencies:
110
97
  - !ruby/object:Gem::Dependency
111
98
  name: fakeweb
112
99
  requirement: !ruby/object:Gem::Requirement
113
- none: false
114
100
  requirements:
115
101
  - - ! '>='
116
102
  - !ruby/object:Gem::Version
@@ -118,7 +104,6 @@ dependencies:
118
104
  type: :development
119
105
  prerelease: false
120
106
  version_requirements: !ruby/object:Gem::Requirement
121
- none: false
122
107
  requirements:
123
108
  - - ! '>='
124
109
  - !ruby/object:Gem::Version
@@ -131,7 +116,8 @@ extensions: []
131
116
  extra_rdoc_files: []
132
117
  files:
133
118
  - .gitignore
134
- - .rvmrc
119
+ - .ruby-gemset
120
+ - .ruby-version
135
121
  - .travis.yml
136
122
  - Gemfile
137
123
  - Gemfile.lock
@@ -154,33 +140,26 @@ files:
154
140
  - spec/tasks/db_setup.rake
155
141
  homepage: https://github.com/denyago/remote_association
156
142
  licenses: []
143
+ metadata: {}
157
144
  post_install_message:
158
145
  rdoc_options: []
159
146
  require_paths:
160
147
  - lib
161
148
  required_ruby_version: !ruby/object:Gem::Requirement
162
- none: false
163
149
  requirements:
164
150
  - - ! '>='
165
151
  - !ruby/object:Gem::Version
166
152
  version: '0'
167
- segments:
168
- - 0
169
- hash: 2390194235140856121
170
153
  required_rubygems_version: !ruby/object:Gem::Requirement
171
- none: false
172
154
  requirements:
173
155
  - - ! '>='
174
156
  - !ruby/object:Gem::Version
175
157
  version: '0'
176
- segments:
177
- - 0
178
- hash: 2390194235140856121
179
158
  requirements: []
180
159
  rubyforge_project:
181
- rubygems_version: 1.8.24
160
+ rubygems_version: 2.4.2
182
161
  signing_key:
183
- specification_version: 3
162
+ specification_version: 4
184
163
  summary: Adds relations to ActiveResource models
185
164
  test_files:
186
165
  - spec/config/database.example.yml
data/.rvmrc DELETED
@@ -1,48 +0,0 @@
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-p194@remote_association"
10
-
11
- # Uncomment the following lines if you want to verify rvm version per project
12
- # rvmrc_rvm_version="1.14.3 (stable)" # 1.10.1 seams as a safe start
13
- # eval "$(echo ${rvm_version}.${rvmrc_rvm_version} | 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
- [[ -s "${rvm_path:-$HOME/.rvm}/hooks/after_use" ]] &&
27
- \. "${rvm_path:-$HOME/.rvm}/hooks/after_use" || true
28
- else
29
- # If the environment file has not yet been created, use the RVM CLI to select.
30
- rvm --create "$environment_id" || {
31
- echo "Failed to create RVM environment '${environment_id}'."
32
- return 1
33
- }
34
- fi
35
-
36
- # If you use bundler, this might be useful to you:
37
- # if [[ -s Gemfile ]] && {
38
- # ! builtin command -v bundle >/dev/null ||
39
- # builtin command -v bundle | GREP_OPTIONS= \grep $rvm_path/bin/bundle >/dev/null
40
- # }
41
- # then
42
- # printf "%b" "The rubygem 'bundler' is not installed. Installing it now.\n"
43
- # gem install bundler
44
- # fi
45
- # if [[ -s Gemfile ]] && builtin command -v bundle >/dev/null
46
- # then
47
- # bundle install | GREP_OPTIONS= \grep -vE '^Using|Your bundle is complete'
48
- # fi