belongs_to_remote 0.0.1

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,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 06eb0cf18c4de2c9579aa36b2edccafd303cfe1f
4
+ data.tar.gz: 86d2d9cd17b7a595fe637984b3fd87ad2a47ddfb
5
+ SHA512:
6
+ metadata.gz: 97f8b36f4f8694fafe927774084aa6e100a608868770f66116c362280546aa0c85f7c7d30113fb696e9b5223f0df44c1ba4cbb10198d03a9aa98b02ef72d601f
7
+ data.tar.gz: a8bd5a4a57cc7738a6de511f7a2fade2a154c6f6fe25bf5a3ccd33832dfe492398ae6e7e2609f51014cacfa4e6d7d3fea2c48cd97bfe40a26b0dc66dda5227c6
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format doc
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in belongs_to_remote.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Alex Avoyants
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,42 @@
1
+ # belongs_to_remote
2
+
3
+ ActiveRecord belongs to ActiveResource association (e.g. `belongs_to_remote :user`).
4
+ Supports options :class_name, :foreign_key, :polymorphic
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ gem 'belongs_to_remote'
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install belongs_to_remote
19
+
20
+ ## Usage
21
+
22
+ Can be used with any object
23
+
24
+ ```ruby
25
+ class Product
26
+ include BelongsToRemote
27
+ attr_accessor :user_id, :depot_id, :booking_id, :owner_id, :owner_type
28
+
29
+ belongs_to_remote :user # defines method user => User.find(self.user_id)
30
+ belongs_to_remote :depot, class_name: "Store" # defines method depot => Store.find(self.depot_id)
31
+ belongs_to_remote :order, foreign_key: :booking_id # defines method order => Order.find(self.booking_id)
32
+ belongs_to_remote :owner, polymorphic: true # defines method owner, uses self.owner_type and self.owner_id
33
+ end
34
+ ```
35
+
36
+ ## Contributing
37
+
38
+ 1. Fork it ( http://github.com/shhavel/belongs_to_remote/fork )
39
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
40
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
41
+ 4. Push to the branch (`git push origin my-new-feature`)
42
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require "rspec/core/rake_task"
4
+ RSpec::Core::RakeTask.new do |t|
5
+ t.rspec_opts = ["-c", "-f documentation", "-r ./spec/spec_helper.rb"]
6
+ t.pattern = 'spec/**/*_spec.rb'
7
+ end
8
+ task default: :spec
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'belongs_to_remote/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "belongs_to_remote"
8
+ spec.version = BelongsToRemote::VERSION
9
+ spec.authors = ["Alex Avoyants"]
10
+ spec.email = ["shhavel@gmail.com"]
11
+ spec.summary = %q{ActiveRecord belongs to ActiveResource association (e.g. belongs_to_remote :user).}
12
+ spec.description = %q{ActiveRecord belongs to ActiveResource association (e.g. belongs_to_remote :user). Supports options :class_name, :foreign_key, :polymorphic}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "activesupport", '~> 3.2'
22
+ spec.add_dependency "activerecord", '~> 3.2'
23
+ spec.add_dependency "activeresource", '~> 3.2'
24
+ spec.add_development_dependency "bundler", "~> 1.5"
25
+ spec.add_development_dependency "rake"
26
+ spec.add_development_dependency "rspec"
27
+ end
@@ -0,0 +1,3 @@
1
+ module BelongsToRemote
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,54 @@
1
+ require "belongs_to_remote/version"
2
+ require "active_support/concern"
3
+ require "active_record"
4
+ require "active_resource"
5
+
6
+ module BelongsToRemote
7
+ extend ActiveSupport::Concern
8
+
9
+ module ClassMethods
10
+ ##
11
+ # Defines private instance method for one-to-one association
12
+ # with ActiveResource::Base (or ActiveRecord::Base).
13
+ # Model should have the foreign key attribute (default is "#{name}_id").
14
+ # For polymorphic association model should also have foreign type attribute
15
+ # (default is "#{name}_type"), which contains class name of association model.
16
+ #
17
+ # Params:
18
+ # - name {Symbol} of the association
19
+ # - options {Hash}, :class_name, :foreign_key, :polymorphic
20
+ #
21
+ # Defined method returns nil if none is found.
22
+ #
23
+ def belongs_to_remote(name, options = {})
24
+ if options[:polymorphic]
25
+ class_eval %(
26
+ def #{name}
27
+ return nil unless self.#{name.to_s + "_type"} && self.#{options[:foreign_key] || name.to_s + "_id" }
28
+ @#{name} ||= self.#{name.to_s + "_type"}.constantize.find(self.#{options[:foreign_key] || name.to_s + "_id" }) rescue nil
29
+ end
30
+ )
31
+ else
32
+ class_eval %(
33
+ def #{name}
34
+ return nil unless self.#{options[:foreign_key] || name.to_s + "_id" }
35
+ @#{name} ||= #{options[:class_name] || name.to_s.classify}.find(self.#{options[:foreign_key] || name.to_s + "_id" }) rescue nil
36
+ end
37
+ )
38
+ end
39
+ end
40
+ end
41
+ end
42
+
43
+ class ActiveRecord::Base
44
+ include BelongsToRemote
45
+ end
46
+
47
+ unless ActiveResource::Base.respond_to?(:belongs_to)
48
+ class ActiveResource::Base
49
+ include BelongsToRemote
50
+ class << self
51
+ alias_method :belongs_to, :belongs_to_remote
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,38 @@
1
+ require "spec_helper"
2
+
3
+ describe BelongsToRemote do
4
+ subject { Product.new }
5
+
6
+ describe "basic usage" do
7
+ it "has association method which finds belonging object" do
8
+ user = User.new(4)
9
+ subject.user_id = 4
10
+ subject.user.should == user
11
+ end
12
+ end
13
+
14
+ describe "option :class_name" do
15
+ it "has association method which finds belonging instance of provided :class_name" do
16
+ store = Store.new(2)
17
+ subject.depot_id = 2
18
+ subject.depot.should == store
19
+ end
20
+ end
21
+
22
+ describe "option :foreign_key" do
23
+ it "has association method which finds belonging object by adjusted :foreign_key" do
24
+ order = Order.new(5)
25
+ subject.booking_id = 5
26
+ subject.order.should == order
27
+ end
28
+ end
29
+
30
+ describe "option :polymorphic" do
31
+ it "has association method which finds belonging :polymorphic object" do
32
+ customer = Customer.new(4)
33
+ subject.owner_id = 4
34
+ subject.owner_type = 'Customer'
35
+ subject.owner.should == customer
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,7 @@
1
+ require "belongs_to_remote"
2
+
3
+ Dir["./spec/support/**/*.rb"].sort.each {|f| require f}
4
+
5
+ RSpec.configure do |config|
6
+ config.mock_with :rspec
7
+ end
@@ -0,0 +1,18 @@
1
+ class Base
2
+ attr_accessor :id
3
+
4
+ def initialize(id)
5
+ self.id = id
6
+ self.class.initialized_objects[id] = self
7
+ end
8
+
9
+ class << self
10
+ def initialized_objects
11
+ @initialized_objects ||= {}
12
+ end
13
+
14
+ def find(id)
15
+ initialized_objects[id]
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,4 @@
1
+ require_relative "base"
2
+
3
+ class Customer < Base
4
+ end
@@ -0,0 +1,4 @@
1
+ require_relative "base"
2
+
3
+ class Order < Base
4
+ end
@@ -0,0 +1,14 @@
1
+ require_relative "user"
2
+ require_relative "store"
3
+ require_relative "order"
4
+ require_relative "customer"
5
+
6
+ class Product
7
+ include BelongsToRemote
8
+ attr_accessor :user_id, :depot_id, :booking_id, :owner_id, :owner_type
9
+
10
+ belongs_to_remote :user
11
+ belongs_to_remote :depot, class_name: "Store"
12
+ belongs_to_remote :order, foreign_key: :booking_id
13
+ belongs_to_remote :owner, polymorphic: true
14
+ end
@@ -0,0 +1,4 @@
1
+ require_relative "base"
2
+
3
+ class Store < Base
4
+ end
@@ -0,0 +1,4 @@
1
+ require_relative "base"
2
+
3
+ class User < Base
4
+ end
metadata ADDED
@@ -0,0 +1,155 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: belongs_to_remote
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Alex Avoyants
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-06-07 00:00:00.000000000 Z
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.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '3.2'
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.2'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '3.2'
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.2'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '3.2'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '1.5'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: '1.5'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: ActiveRecord belongs to ActiveResource association (e.g. belongs_to_remote
98
+ :user). Supports options :class_name, :foreign_key, :polymorphic
99
+ email:
100
+ - shhavel@gmail.com
101
+ executables: []
102
+ extensions: []
103
+ extra_rdoc_files: []
104
+ files:
105
+ - .gitignore
106
+ - .rspec
107
+ - Gemfile
108
+ - LICENSE.txt
109
+ - README.md
110
+ - Rakefile
111
+ - belongs_to_remote.gemspec
112
+ - lib/belongs_to_remote.rb
113
+ - lib/belongs_to_remote/version.rb
114
+ - spec/belongs_to_remote_spec.rb
115
+ - spec/spec_helper.rb
116
+ - spec/support/base.rb
117
+ - spec/support/customer.rb
118
+ - spec/support/order.rb
119
+ - spec/support/product.rb
120
+ - spec/support/store.rb
121
+ - spec/support/user.rb
122
+ homepage: ''
123
+ licenses:
124
+ - MIT
125
+ metadata: {}
126
+ post_install_message:
127
+ rdoc_options: []
128
+ require_paths:
129
+ - lib
130
+ required_ruby_version: !ruby/object:Gem::Requirement
131
+ requirements:
132
+ - - '>='
133
+ - !ruby/object:Gem::Version
134
+ version: '0'
135
+ required_rubygems_version: !ruby/object:Gem::Requirement
136
+ requirements:
137
+ - - '>='
138
+ - !ruby/object:Gem::Version
139
+ version: '0'
140
+ requirements: []
141
+ rubyforge_project:
142
+ rubygems_version: 2.2.2
143
+ signing_key:
144
+ specification_version: 4
145
+ summary: ActiveRecord belongs to ActiveResource association (e.g. belongs_to_remote
146
+ :user).
147
+ test_files:
148
+ - spec/belongs_to_remote_spec.rb
149
+ - spec/spec_helper.rb
150
+ - spec/support/base.rb
151
+ - spec/support/customer.rb
152
+ - spec/support/order.rb
153
+ - spec/support/product.rb
154
+ - spec/support/store.rb
155
+ - spec/support/user.rb