active_remote 1.3.0 → 1.3.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -5,6 +5,7 @@
5
5
  .config
6
6
  .rvmrc
7
7
  .yardoc
8
+ .idea
8
9
  Gemfile.lock
9
10
  coverage
10
11
  pkg/*
@@ -39,7 +39,9 @@ module ActiveRemote
39
39
  #
40
40
  def belongs_to(belongs_to_klass, options={})
41
41
  perform_association( belongs_to_klass, options ) do |klass, obj|
42
- klass.search(:guid => obj.read_attribute(:"#{belongs_to_klass}_guid")).first
42
+ foreign_key = options.fetch(:foreign_key) { :"#{belongs_to_klass}_guid" }
43
+ association_guid = obj.read_attribute(foreign_key)
44
+ klass.search(:guid => association_guid).first
43
45
  end
44
46
  end
45
47
 
@@ -75,7 +77,8 @@ module ActiveRemote
75
77
  #
76
78
  def has_many(has_many_class, options={})
77
79
  perform_association( has_many_class, options ) do |klass, obj|
78
- klass.search(:"#{obj.class.name.demodulize.underscore}_guid" => obj.guid)
80
+ foreign_key = options.fetch(:foreign_key) { :"#{obj.class.name.demodulize.underscore}_guid" }
81
+ klass.search(foreign_key => obj.guid)
79
82
  end
80
83
  end
81
84
 
@@ -110,7 +113,8 @@ module ActiveRemote
110
113
  #
111
114
  def has_one(has_one_klass, options={})
112
115
  perform_association( has_one_klass, options ) do |klass, obj|
113
- klass.search(:"#{obj.class.name.demodulize.underscore}_guid" => obj.guid).first
116
+ foreign_key = options.fetch(:foreign_key) { :"#{obj.class.name.demodulize.underscore}_guid" }
117
+ klass.search(foreign_key => obj.guid).first
114
118
  end
115
119
  end
116
120
 
@@ -1,3 +1,3 @@
1
1
  module ActiveRemote
2
- VERSION = "1.3.0"
2
+ VERSION = "1.3.1"
3
3
  end
@@ -17,7 +17,7 @@ describe ActiveRemote::Association do
17
17
  subject.author.should eq record
18
18
  end
19
19
 
20
- it 'memorizes the result record' do
20
+ it 'memoizes the result record' do
21
21
  Author.should_receive(:search).once.with(:guid => subject.author_guid).and_return(records)
22
22
  3.times { subject.author.should eq record }
23
23
  end
@@ -42,6 +42,18 @@ describe ActiveRemote::Association do
42
42
  end
43
43
  end
44
44
 
45
+ context 'specific association with class name and foreign_key' do
46
+ let(:author_guid) { 'AUT-456' }
47
+
48
+ subject { Post.new(:author_guid => author_guid) }
49
+ it { should respond_to(:bestseller) }
50
+
51
+ it 'searches the associated model for a single record' do
52
+ Author.should_receive(:search).with(:guid => subject.bestseller_guid).and_return(records)
53
+ subject.author.should eq record
54
+ end
55
+ end
56
+
45
57
  end
46
58
 
47
59
  describe '.has_many' do
@@ -76,6 +88,15 @@ describe ActiveRemote::Association do
76
88
  subject.flagged_posts.should be_empty
77
89
  end
78
90
  end
91
+
92
+ context 'specific association with class name and foreign_key' do
93
+ it { should respond_to(:bestseller_posts) }
94
+
95
+ it 'searches the associated model for multiple record' do
96
+ Post.should_receive(:search).with(:bestseller_guid => subject.guid).and_return(records)
97
+ subject.bestseller_posts.should eq(records)
98
+ end
99
+ end
79
100
  end
80
101
 
81
102
  describe '.has_one' do
@@ -109,6 +130,15 @@ describe ActiveRemote::Association do
109
130
  subject.main_category.should eq record
110
131
  end
111
132
  end
133
+
134
+ context 'specific association with class name and foreign_key' do
135
+ it { should respond_to(:default_category) }
136
+
137
+ it 'searches the associated model for a single record' do
138
+ Category.should_receive(:search).with(:template_post_guid => subject.guid).and_return(records)
139
+ subject.default_category.should eq record
140
+ end
141
+ end
112
142
  end
113
143
 
114
144
  end
@@ -11,5 +11,6 @@ class Author < ::ActiveRemote::Base
11
11
 
12
12
  has_many :posts
13
13
  has_many :flagged_posts, :class_name => "::Post"
14
+ has_many :bestseller_posts, :class_name => "::Post", :foreign_key => :bestseller_guid
14
15
 
15
16
  end
@@ -8,7 +8,9 @@ class Category < ::ActiveRemote::Base
8
8
 
9
9
  attribute :guid
10
10
  attribute :name
11
+ attribute :post_id
11
12
 
12
13
  belongs_to :post
13
14
 
15
+ alias_method :template_post_guid, :post_id
14
16
  end
@@ -11,8 +11,11 @@ class Post < ::ActiveRemote::Base
11
11
  attribute :author_guid
12
12
 
13
13
  belongs_to :author
14
- belongs_to :coauthor, :class_name => ::Author
14
+ belongs_to :coauthor, :class_name => '::Author'
15
+ belongs_to :bestseller, :class_name => '::Author', :foreign_key => :bestseller_guid
15
16
  has_one :category
16
- has_one :main_category, :class_name => ::Category
17
+ has_one :main_category, :class_name => '::Category'
18
+ has_one :default_category, :class_name => '::Category', :foreign_key => :template_post_guid
17
19
 
20
+ alias_method :bestseller_guid, :author_guid
18
21
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_remote
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.0
4
+ version: 1.3.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-01-09 00:00:00.000000000 Z
12
+ date: 2013-01-15 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: active_attr
@@ -218,7 +218,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
218
218
  version: '0'
219
219
  segments:
220
220
  - 0
221
- hash: 1289251465950014823
221
+ hash: 760636464474141912
222
222
  required_rubygems_version: !ruby/object:Gem::Requirement
223
223
  none: false
224
224
  requirements:
@@ -227,7 +227,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
227
227
  version: '0'
228
228
  segments:
229
229
  - 0
230
- hash: 1289251465950014823
230
+ hash: 760636464474141912
231
231
  requirements: []
232
232
  rubyforge_project:
233
233
  rubygems_version: 1.8.24