codebrulee-attr_remote 0.0.2
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.
- data/LICENSE +7 -0
- data/README.rdoc +1 -0
- data/attr_remote.gemspec +31 -0
- data/lib/attr_remote.rb +102 -0
- data/test/db/test.db +0 -0
- data/test/factories.rb +3 -0
- data/test/helper.rb +46 -0
- data/test/models/bob.rb +9 -0
- data/test/models/remote_bob.rb +4 -0
- data/test/models/remote_user.rb +4 -0
- data/test/models/user.rb +3 -0
- data/test/test_attr_remote.rb +73 -0
- metadata +64 -0
data/LICENSE
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
(The MIT License)
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ‘Software’), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
4
|
+
|
5
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
6
|
+
|
7
|
+
THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
= attr_remote - Painlessly integrate ActiveResource into ActiveRecord.
|
data/attr_remote.gemspec
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = "attr_remote"
|
3
|
+
s.version = "0.0.2"
|
4
|
+
s.date = "2008-01-06"
|
5
|
+
s.summary = "Painlessly integrate ActiveResource into ActiveRecord."
|
6
|
+
s.email = "smithk14@gmail.com"
|
7
|
+
s.homepage = "http://github.com/codebrulee/attr_remote"
|
8
|
+
s.description = "Painlessly integrate ActiveResource into ActiveRecord."
|
9
|
+
s.has_rdoc = true
|
10
|
+
s.authors = ["Kevin Smith"]
|
11
|
+
s.files = [
|
12
|
+
"README.rdoc",
|
13
|
+
"attr_remote.gemspec",
|
14
|
+
"LICENSE",
|
15
|
+
"lib/attr_remote.rb"
|
16
|
+
]
|
17
|
+
|
18
|
+
s.test_files = [
|
19
|
+
"test/test_attr_remote.rb",
|
20
|
+
"test/helper.rb",
|
21
|
+
"test/factories.rb",
|
22
|
+
"test/db/test.db",
|
23
|
+
"test/models/bob.rb",
|
24
|
+
"test/models/remote_bob.rb",
|
25
|
+
"test/models/user.rb",
|
26
|
+
"test/models/remote_user.rb",
|
27
|
+
]
|
28
|
+
|
29
|
+
s.rdoc_options = ["--main", "README.rdoc"]
|
30
|
+
s.extra_rdoc_files = ["README.rdoc"]
|
31
|
+
end
|
data/lib/attr_remote.rb
ADDED
@@ -0,0 +1,102 @@
|
|
1
|
+
require 'active_record'
|
2
|
+
require 'active_support'
|
3
|
+
require 'active_resource'
|
4
|
+
|
5
|
+
module AttrRemote
|
6
|
+
module ClassMethods
|
7
|
+
def attr_remote(*remote_attrs)
|
8
|
+
remote_class = "Remote#{self.to_s}"
|
9
|
+
remote_instance_meth = remote_class.underscore
|
10
|
+
remote_instance_id = remote_class.foreign_key
|
11
|
+
|
12
|
+
class_eval <<-remote_access
|
13
|
+
# def remote_user
|
14
|
+
# @remote_user ||= RemoteUser.find(self.remote_user_id) rescue nil
|
15
|
+
# end
|
16
|
+
def #{remote_instance_meth}
|
17
|
+
@#{remote_instance_meth} ||= #{remote_class}.find(self.#{remote_instance_id}) rescue nil
|
18
|
+
end
|
19
|
+
|
20
|
+
# before_create :create_remote_user
|
21
|
+
before_create :create_#{remote_instance_meth}
|
22
|
+
|
23
|
+
# def create_remote_user
|
24
|
+
# unless self.remote_user_id
|
25
|
+
# remote_hash = {}
|
26
|
+
# self.class.remote_attributes.each do |attr|
|
27
|
+
# remote_hash[attr.to_sym] = self.send(attr.to_sym)
|
28
|
+
# end
|
29
|
+
# remote_hash[:validate_only] = true if validate_only
|
30
|
+
# @remote_user = RemoteUser.create(remote_hash)
|
31
|
+
#
|
32
|
+
# unless @remote_user.valid?
|
33
|
+
# @remote_user.errors.each do |attr, err|
|
34
|
+
# errors.add(attr, err)
|
35
|
+
# end
|
36
|
+
# return false
|
37
|
+
# else
|
38
|
+
# self.remote_user_id = @remote_user.id
|
39
|
+
# end
|
40
|
+
# end
|
41
|
+
# end
|
42
|
+
def create_#{remote_instance_meth}
|
43
|
+
unless self.#{remote_instance_id}
|
44
|
+
remote_hash = {}
|
45
|
+
self.class.remote_attributes.each do |attr|
|
46
|
+
remote_hash[attr.to_sym] = self.send(attr.to_sym)
|
47
|
+
end
|
48
|
+
@#{remote_instance_meth} = #{remote_class}.create(remote_hash)
|
49
|
+
|
50
|
+
unless @#{remote_instance_meth}.valid?
|
51
|
+
@#{remote_instance_meth}.errors.each do |attr, err|
|
52
|
+
errors.add(attr, err)
|
53
|
+
end
|
54
|
+
return false
|
55
|
+
else
|
56
|
+
self.#{remote_instance_id} = @#{remote_instance_meth}.id
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
private :create_#{remote_instance_meth}
|
61
|
+
|
62
|
+
# validate_on_create :validate_remote_user_on_create
|
63
|
+
validate_on_create :validate_#{remote_instance_meth}_on_create
|
64
|
+
|
65
|
+
# def validate_remote_user_on_create; end
|
66
|
+
def validate_#{remote_instance_meth}_on_create; end
|
67
|
+
remote_access
|
68
|
+
|
69
|
+
@remote_attributes = remote_attrs
|
70
|
+
remote_attributes.each do |attr|
|
71
|
+
class_eval <<-remote_attribute
|
72
|
+
def #{attr} # def username
|
73
|
+
remote_#{attr} || '' # remote_username || ''
|
74
|
+
end # end
|
75
|
+
|
76
|
+
def remote_#{attr} # def remote_username
|
77
|
+
if @#{attr} # if @username
|
78
|
+
@#{attr} # @username
|
79
|
+
elsif self.#{remote_instance_meth} # elsif self.remote_user
|
80
|
+
@#{attr} = self. # @username = self.
|
81
|
+
#{remote_instance_meth}. # remote_user.
|
82
|
+
#{attr} # username
|
83
|
+
else # else
|
84
|
+
nil # nil
|
85
|
+
end # end
|
86
|
+
end # end
|
87
|
+
|
88
|
+
def #{attr}=(attr_value) # def username=(attr_value)
|
89
|
+
@#{attr} = attr_value # @username = attr_value
|
90
|
+
end # end
|
91
|
+
remote_attribute
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
def remote_attributes
|
96
|
+
@remote_attributes ||= []
|
97
|
+
end
|
98
|
+
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
ActiveRecord::Base.extend AttrRemote::ClassMethods
|
data/test/db/test.db
ADDED
Binary file
|
data/test/factories.rb
ADDED
data/test/helper.rb
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'rubygems'
|
3
|
+
|
4
|
+
require File.join(File.dirname(__FILE__), '..', 'lib', 'attr_remote')
|
5
|
+
require 'active_resource/http_mock'
|
6
|
+
|
7
|
+
Dir['**/models/*.rb'].each do |model|
|
8
|
+
require model
|
9
|
+
end
|
10
|
+
|
11
|
+
require 'redgreen'
|
12
|
+
require 'context'
|
13
|
+
require 'factory_girl'
|
14
|
+
|
15
|
+
ActiveRecord::Base.establish_connection({
|
16
|
+
:adapter => 'sqlite3',
|
17
|
+
:dbfile => File.dirname(__FILE__) + '/db/test.db'
|
18
|
+
})
|
19
|
+
|
20
|
+
ActiveResource::HttpMock.respond_to do |mock|
|
21
|
+
# user is a successful creation
|
22
|
+
mock.get "/users/1.xml", {}, {
|
23
|
+
:name => "Kevin"
|
24
|
+
}.to_xml(:root => "user")
|
25
|
+
|
26
|
+
mock.post "/users.xml", {}, {
|
27
|
+
:name => "Kevin"
|
28
|
+
}.to_xml(:root => "user"), 200, {
|
29
|
+
'Location' => 'http://0.0.0.0:3000/users/1'
|
30
|
+
}
|
31
|
+
|
32
|
+
# bob is not so lucky
|
33
|
+
mock.post "/bobs.xml",
|
34
|
+
{},
|
35
|
+
returning(ActiveRecord::Errors.new(Bob.new)) { |errors|
|
36
|
+
errors.add(:email, "can't be blank")
|
37
|
+
}.to_xml, 422
|
38
|
+
end
|
39
|
+
|
40
|
+
class Test::Unit::TestCase
|
41
|
+
def teardown
|
42
|
+
# cleanup all our test data
|
43
|
+
User.delete_all
|
44
|
+
Bob.delete_all
|
45
|
+
end
|
46
|
+
end
|
data/test/models/bob.rb
ADDED
data/test/models/user.rb
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/helper'
|
2
|
+
|
3
|
+
class TestAttrRemote < Test::Unit::TestCase
|
4
|
+
context "remote object access" do
|
5
|
+
before do
|
6
|
+
@user = Factory(:user)
|
7
|
+
end
|
8
|
+
test "that an instance should have a method for the remote object" do
|
9
|
+
assert @user.respond_to?(:remote_user)
|
10
|
+
end
|
11
|
+
test "that the remote object should be looked up based on the id" do
|
12
|
+
assert_equal "Kevin", @user.remote_user.name
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
context "reading" do
|
17
|
+
test "that an instance should have a read method for the attribute" do
|
18
|
+
user = Factory(:user)
|
19
|
+
assert user.respond_to?(:name)
|
20
|
+
end
|
21
|
+
|
22
|
+
test "that the read method returns the correct value" do
|
23
|
+
user = Factory(:user)
|
24
|
+
assert_equal "Kevin", user.name
|
25
|
+
end
|
26
|
+
|
27
|
+
test "that after first read, an instance variable contains the value" do
|
28
|
+
user = Factory.build(:user, :name => nil, :remote_user_id => 1)
|
29
|
+
assert_nil user.instance_variable_get('@name')
|
30
|
+
user.name
|
31
|
+
assert_equal "Kevin", user.instance_variable_get('@name')
|
32
|
+
end
|
33
|
+
|
34
|
+
test "that an attribute will be empty if the remote side does not exist" do
|
35
|
+
user = Factory.build(:user, :remote_user_id => 666, :name => nil)
|
36
|
+
assert_equal '', user.name
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
context "creation" do
|
41
|
+
test "that an instance should have a writer method for the attribute" do
|
42
|
+
user = Factory(:user)
|
43
|
+
assert user.respond_to?(:name=)
|
44
|
+
end
|
45
|
+
test "that an instance reads correctly after writing" do
|
46
|
+
user = Factory.build(:user, :name => "test")
|
47
|
+
assert_equal "test", user.name
|
48
|
+
end
|
49
|
+
test "that the remote object id is set after creation" do
|
50
|
+
user = Factory(:user, :name => "Kevin")
|
51
|
+
assert_equal 1, user.remote_user_id
|
52
|
+
end
|
53
|
+
|
54
|
+
test "that remote errors halt save process" do
|
55
|
+
bob = Bob.new(:title => "title")
|
56
|
+
assert !bob.save
|
57
|
+
assert_not_nil bob.errors.on(:email)
|
58
|
+
end
|
59
|
+
|
60
|
+
test "that failure of local validations halts remote creation and therefore remote errors do not show up" do
|
61
|
+
bob = Bob.create(:title => nil)
|
62
|
+
assert !bob.save
|
63
|
+
assert_not_nil bob.errors.on(:title)
|
64
|
+
assert_nil bob.errors.on(:email)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
context "updates" do
|
69
|
+
end
|
70
|
+
|
71
|
+
context "deletes" do
|
72
|
+
end
|
73
|
+
end
|
metadata
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: codebrulee-attr_remote
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Kevin Smith
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2008-01-06 00:00:00 -08:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: Painlessly integrate ActiveResource into ActiveRecord.
|
17
|
+
email: smithk14@gmail.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- README.rdoc
|
24
|
+
files:
|
25
|
+
- README.rdoc
|
26
|
+
- attr_remote.gemspec
|
27
|
+
- LICENSE
|
28
|
+
- lib/attr_remote.rb
|
29
|
+
has_rdoc: true
|
30
|
+
homepage: http://github.com/codebrulee/attr_remote
|
31
|
+
post_install_message:
|
32
|
+
rdoc_options:
|
33
|
+
- --main
|
34
|
+
- README.rdoc
|
35
|
+
require_paths:
|
36
|
+
- lib
|
37
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: "0"
|
42
|
+
version:
|
43
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: "0"
|
48
|
+
version:
|
49
|
+
requirements: []
|
50
|
+
|
51
|
+
rubyforge_project:
|
52
|
+
rubygems_version: 1.2.0
|
53
|
+
signing_key:
|
54
|
+
specification_version: 2
|
55
|
+
summary: Painlessly integrate ActiveResource into ActiveRecord.
|
56
|
+
test_files:
|
57
|
+
- test/test_attr_remote.rb
|
58
|
+
- test/helper.rb
|
59
|
+
- test/factories.rb
|
60
|
+
- test/db/test.db
|
61
|
+
- test/models/bob.rb
|
62
|
+
- test/models/remote_bob.rb
|
63
|
+
- test/models/user.rb
|
64
|
+
- test/models/remote_user.rb
|