active_resource_associatable 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.travis.yml +4 -0
- data/CODE_OF_CONDUCT.md +49 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +210 -0
- data/Rakefile +10 -0
- data/active_resource_associatable.gemspec +41 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/active_resource_associatable.rb +3 -0
- data/lib/active_resource_associatable/active_resource_associatable.rb +32 -0
- data/lib/active_resource_associatable/belongs_to_activeresource.rb +16 -0
- data/lib/active_resource_associatable/has_and_belongs_to_many_activeresources.rb +27 -0
- data/lib/active_resource_associatable/has_many_activeresources.rb +21 -0
- data/lib/active_resource_associatable/has_many_through_activeresources.rb +23 -0
- data/lib/active_resource_associatable/has_one_activeresource.rb +20 -0
- data/lib/active_resource_associatable/version.rb +3 -0
- metadata +205 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 619c44e184c2e4276d0ba6e8db7f9ff129d799c1
|
4
|
+
data.tar.gz: cecd5a19901d7381275bab358e57b143ed922086
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 94032c9a0a7ef99c1bf845953d85997251a12ee447776d5b750594b596a76331f2750a8937ee3a83d16f902796491c04c75998084d40776ec0e3d3c26cb50235
|
7
|
+
data.tar.gz: 10f65bdabd72d45ae56485909b8c0bd33711b912e2fee6e208cc7e62ee2150a7a84146cada8c8354087b69c1b3a000db3540ebe1b5494f61be9b486185a4ae60
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, and in the interest of
|
4
|
+
fostering an open and welcoming community, we pledge to respect all people who
|
5
|
+
contribute through reporting issues, posting feature requests, updating
|
6
|
+
documentation, submitting pull requests or patches, and other activities.
|
7
|
+
|
8
|
+
We are committed to making participation in this project a harassment-free
|
9
|
+
experience for everyone, regardless of level of experience, gender, gender
|
10
|
+
identity and expression, sexual orientation, disability, personal appearance,
|
11
|
+
body size, race, ethnicity, age, religion, or nationality.
|
12
|
+
|
13
|
+
Examples of unacceptable behavior by participants include:
|
14
|
+
|
15
|
+
* The use of sexualized language or imagery
|
16
|
+
* Personal attacks
|
17
|
+
* Trolling or insulting/derogatory comments
|
18
|
+
* Public or private harassment
|
19
|
+
* Publishing other's private information, such as physical or electronic
|
20
|
+
addresses, without explicit permission
|
21
|
+
* Other unethical or unprofessional conduct
|
22
|
+
|
23
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
24
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
25
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
26
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
27
|
+
threatening, offensive, or harmful.
|
28
|
+
|
29
|
+
By adopting this Code of Conduct, project maintainers commit themselves to
|
30
|
+
fairly and consistently applying these principles to every aspect of managing
|
31
|
+
this project. Project maintainers who do not follow or enforce the Code of
|
32
|
+
Conduct may be permanently removed from the project team.
|
33
|
+
|
34
|
+
This code of conduct applies both within project spaces and in public spaces
|
35
|
+
when an individual is representing the project or its community.
|
36
|
+
|
37
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
38
|
+
reported by contacting a project maintainer at neeraj.kumar@gmail.com. All
|
39
|
+
complaints will be reviewed and investigated and will result in a response that
|
40
|
+
is deemed necessary and appropriate to the circumstances. Maintainers are
|
41
|
+
obligated to maintain confidentiality with regard to the reporter of an
|
42
|
+
incident.
|
43
|
+
|
44
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
45
|
+
version 1.3.0, available at
|
46
|
+
[http://contributor-covenant.org/version/1/3/0/][version]
|
47
|
+
|
48
|
+
[homepage]: http://contributor-covenant.org
|
49
|
+
[version]: http://contributor-covenant.org/version/1/3/0/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Neeraj Kumar
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,210 @@
|
|
1
|
+
# ActiveResourceAssociatable
|
2
|
+
|
3
|
+
This gem is used to establish the association between your ActiveResource class to ActiveRecord class. Why? Because there is no other ruby library to provide such functionality. Its simple and taking care of all options, scopes etc with your associations.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'active_resource_associatable'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install active_resource_associatable
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
```include ActiveResourceAssociatable``` in your ActiveRecord or ActiveResource model. Now you can easily call the methods of either of your ActiveRecord or ActiveResource models. This gem facilitates you to call ActiveResource objects in the same way as you call in any of your ActiveRecord model. It supports calls ```has_one_activeresource```, ```belongs_to_activeresource```, ```has_many_activeresources```, ```has_and_belongs_to_many_activeresources``` and ```has_many_through_activeresources```.
|
24
|
+
|
25
|
+
### Has One ActiveResource
|
26
|
+
Account is non ActiveResource class which is maintaining has one relationship with ActiveRecord class User.
|
27
|
+
|
28
|
+
```ruby
|
29
|
+
class UserResource < ActiveResource::Base
|
30
|
+
self.site = ""
|
31
|
+
|
32
|
+
include ActiveResourceAssociatable
|
33
|
+
|
34
|
+
belongs_to_activeresource :account
|
35
|
+
end
|
36
|
+
```
|
37
|
+
```ruby
|
38
|
+
class Account < ActiveRecord::Base
|
39
|
+
include ActiveResourceAssociatable
|
40
|
+
|
41
|
+
has_one_activeresource :user, class_name: 'UserResource'
|
42
|
+
end
|
43
|
+
```
|
44
|
+
|
45
|
+
Whereas Reader is a ActiveResource class, but maintaining has one relationship with Account.
|
46
|
+
|
47
|
+
```ruby
|
48
|
+
class Reader < ActiveResource::Base
|
49
|
+
self.site = ""
|
50
|
+
|
51
|
+
include ActiveResourceAssociatable
|
52
|
+
|
53
|
+
has_one_activeresource :account
|
54
|
+
end
|
55
|
+
```
|
56
|
+
|
57
|
+
```ruby
|
58
|
+
class Account < ActiveRecord::Base
|
59
|
+
include ActiveResourceAssociatable
|
60
|
+
|
61
|
+
belongs_to_activeresource :reader
|
62
|
+
end
|
63
|
+
```
|
64
|
+
|
65
|
+
### Belongs To ActiveResource
|
66
|
+
The same example which explained above in Has One ActiveResource section could be an example of belongs_to_activeresource.
|
67
|
+
```belongs_to_activeresource``` can also be applied in case of ```has_many_activeresources`` associations.
|
68
|
+
|
69
|
+
### Has Many ActiveResources
|
70
|
+
User is ActiveResource class which maintains the has many relationships with non ActiveResource class Book.
|
71
|
+
|
72
|
+
```ruby
|
73
|
+
class UserResource < ActiveResource::Base
|
74
|
+
self.site = ""
|
75
|
+
|
76
|
+
include ActiveResourceAssociatable
|
77
|
+
|
78
|
+
has_many_activeresources :books
|
79
|
+
end
|
80
|
+
```
|
81
|
+
```ruby
|
82
|
+
class Book < ActiveRecord::Base
|
83
|
+
include ActiveResourceAssociatable
|
84
|
+
|
85
|
+
belongs_to_activeresource :user, class_name: 'UserResource'
|
86
|
+
end
|
87
|
+
```
|
88
|
+
Now you can easily call ```user.books``` which will return the array of objects of non ActiveResource class Book.
|
89
|
+
|
90
|
+
```ruby
|
91
|
+
class Library < ActiveRecord::Base
|
92
|
+
include ActiveResourceAssociatable
|
93
|
+
|
94
|
+
has_many_activeresources :readers
|
95
|
+
end
|
96
|
+
```
|
97
|
+
```has_many_activeresources``` also comes with options ```class_name```.
|
98
|
+
|
99
|
+
```ruby
|
100
|
+
class Reader < ActiveResource::Base
|
101
|
+
self.site = ""
|
102
|
+
|
103
|
+
include ActiveResourceAssociatable
|
104
|
+
|
105
|
+
has_many_activeresources :studying_materials, class_name: "Book"
|
106
|
+
end
|
107
|
+
```
|
108
|
+
```ruby
|
109
|
+
class Book < ActiveRecord::Base
|
110
|
+
include ActiveResourceAssociatable
|
111
|
+
|
112
|
+
belongs_to_activeresource :reader
|
113
|
+
end
|
114
|
+
```
|
115
|
+
Now you can call ```reader.studying_materials``` which will return the array of objects of non ActiveResource class Book.
|
116
|
+
|
117
|
+
User can also call books using a specific foreign key. For Example,
|
118
|
+
```ruby
|
119
|
+
class UserResource < ActiveResource::Base
|
120
|
+
self.site = ""
|
121
|
+
|
122
|
+
include ActiveResourceAssociatable
|
123
|
+
|
124
|
+
has_many_activeresources :books, foreign_key: "user_id"
|
125
|
+
end
|
126
|
+
```
|
127
|
+
|
128
|
+
### Has And Belongs To Many ActiveResources
|
129
|
+
```active_resource_associatable``` provides many to many associations between ActiveResource and ActiveRecord.
|
130
|
+
User class is a ActiveResource class and can have many images. Likewise, Image which is a ActiveRecord class can have many users.
|
131
|
+
|
132
|
+
```ruby
|
133
|
+
class User < ActiveResource::Base
|
134
|
+
self.site = ""
|
135
|
+
|
136
|
+
include ActiveResourceAssociatable
|
137
|
+
|
138
|
+
has_and_belongs_to_many_activeresources :images
|
139
|
+
end
|
140
|
+
```
|
141
|
+
```ruby
|
142
|
+
class Image < ActiveRecord::Base
|
143
|
+
include ActiveResourceAssociatable
|
144
|
+
|
145
|
+
has_and_belongs_to_many_activeresources :users
|
146
|
+
end
|
147
|
+
```
|
148
|
+
|
149
|
+
You can use class_name and foreign_key as in ```has_many_activeresources```.
|
150
|
+
```ruby
|
151
|
+
class UserResource < ActiveResource::Base
|
152
|
+
self.site = ""
|
153
|
+
|
154
|
+
include ActiveResourceAssociatable
|
155
|
+
|
156
|
+
has_and_belongs_to_many_activeresources :images, foreign_key: "user_id"
|
157
|
+
end
|
158
|
+
```
|
159
|
+
```ruby
|
160
|
+
class Image < ActiveRecord::Base
|
161
|
+
include ActiveResourceAssociatable
|
162
|
+
|
163
|
+
has_and_belongs_to_many_activeresources :users, class_name: 'UserResource'
|
164
|
+
end
|
165
|
+
```
|
166
|
+
|
167
|
+
### Has Many Through ActiveResources
|
168
|
+
```has_many_through_activeresources``` can also be achieved using ```active_resource_associatable```. User can have many friends through friendships. Similarly, a friend can also have many users through friendships.
|
169
|
+
|
170
|
+
```ruby
|
171
|
+
class UserResource < ActiveResource::Base
|
172
|
+
self.site = ""
|
173
|
+
|
174
|
+
include ActiveResourceAssociatable
|
175
|
+
|
176
|
+
has_many_through_activeresources :friends, through: :friendships
|
177
|
+
end
|
178
|
+
```
|
179
|
+
```ruby
|
180
|
+
class Friend < ActiveRecord::Base
|
181
|
+
|
182
|
+
include ActiveResourceAssociatable
|
183
|
+
|
184
|
+
has_many_through_activeresources :users, through: :friendships, class_name: 'UserResource'
|
185
|
+
end
|
186
|
+
```
|
187
|
+
```ruby
|
188
|
+
class Friendship < ActiveRecord::Base
|
189
|
+
include ActiveResourceAssociatable
|
190
|
+
|
191
|
+
belongs_to_activeresource :user
|
192
|
+
belongs_to :friend
|
193
|
+
end
|
194
|
+
```
|
195
|
+
|
196
|
+
## Development
|
197
|
+
|
198
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
199
|
+
|
200
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
201
|
+
|
202
|
+
## Contributing
|
203
|
+
|
204
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/neerajkumar/active_resource_associatable. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
205
|
+
|
206
|
+
|
207
|
+
## License
|
208
|
+
|
209
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
210
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'active_resource_associatable/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "active_resource_associatable"
|
8
|
+
spec.version = ActiveResourceAssociatable::VERSION
|
9
|
+
spec.authors = ["Neeraj Kumar"]
|
10
|
+
spec.email = ["neeraj.kumar@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{This rubygem is used to build the connection between any ActiveResource model and ActiveRecord model.}
|
13
|
+
spec.description = %q{This rubygem is used to build the connection between any ActiveResource model and ActiveRecord model.}
|
14
|
+
spec.homepage = "https://github.com/neerajkumar/active_resource_associatable."
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
# Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
|
18
|
+
# delete this section to allow pushing this gem to any host.
|
19
|
+
if spec.respond_to?(:metadata)
|
20
|
+
spec.metadata['allowed_push_host'] = "https://rubygems.org"
|
21
|
+
else
|
22
|
+
raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
|
23
|
+
end
|
24
|
+
|
25
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
26
|
+
spec.bindir = "exe"
|
27
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
28
|
+
spec.require_paths = ["lib"]
|
29
|
+
|
30
|
+
spec.add_development_dependency "bundler", "~> 1.11"
|
31
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
32
|
+
spec.add_development_dependency "minitest", "~> 5.0"
|
33
|
+
spec.add_development_dependency "activeresource", "~> 4.0.0"
|
34
|
+
spec.add_development_dependency "activesupport", "~> 4.2.5"
|
35
|
+
spec.add_development_dependency "activerecord", "~> 4.2.5"
|
36
|
+
spec.add_development_dependency "factory_girl"
|
37
|
+
spec.add_development_dependency "sqlite3"
|
38
|
+
spec.add_development_dependency "byebug"
|
39
|
+
|
40
|
+
spec.add_dependency "json"
|
41
|
+
end
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "active_resource_associatable"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
require_relative "./has_many_activeresources"
|
2
|
+
require_relative "./belongs_to_activeresource"
|
3
|
+
require_relative "./has_and_belongs_to_many_activeresources"
|
4
|
+
require_relative "./has_many_through_activeresources"
|
5
|
+
require_relative "./has_one_activeresource"
|
6
|
+
|
7
|
+
module ActiveResourceAssociatable
|
8
|
+
extend ActiveSupport::Concern
|
9
|
+
|
10
|
+
module ClassMethods
|
11
|
+
def has_many_activeresources(table_name, options = {})
|
12
|
+
AssociationBuilder::HasManyActiveResources.build(self, table_name, options)
|
13
|
+
end
|
14
|
+
|
15
|
+
def belongs_to_activeresource(klass_name, options = {})
|
16
|
+
AssociationBuilder::BelongsToActiveResource.build(self, klass_name, options)
|
17
|
+
end
|
18
|
+
|
19
|
+
def has_and_belongs_to_many_activeresources(klass_name, options = {})
|
20
|
+
AssociationBuilder::HasAndBelongsToManyActiveResources.build(self, klass_name, options)
|
21
|
+
end
|
22
|
+
|
23
|
+
def has_one_activeresource(klass_name, options={})
|
24
|
+
AssociationBuilder::HasOneActiveResource.build(self, klass_name, options)
|
25
|
+
end
|
26
|
+
|
27
|
+
def has_many_through_activeresources(klass_name, options)
|
28
|
+
AssociationBuilder::HasManyThroughActiveResources.build(self, klass_name, options)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module AssociationBuilder
|
2
|
+
|
3
|
+
class BelongsToActiveResource
|
4
|
+
|
5
|
+
def self.build(model, table_name, options)
|
6
|
+
klass_name = options[:class_name] || table_name
|
7
|
+
model.class_eval do
|
8
|
+
define_method("#{table_name.to_s.downcase}") do
|
9
|
+
klass_name.to_s.classify.constantize.find("#{self.send(table_name.to_s + '_id')}") if "#{self.send(table_name.to_s + '_id')}".present?
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module AssociationBuilder
|
2
|
+
|
3
|
+
class HasAndBelongsToManyActiveResources
|
4
|
+
|
5
|
+
def self.build(model, table_name, options)
|
6
|
+
klass_name = options[:class_name] || table_name
|
7
|
+
model.class_eval do
|
8
|
+
define_method(table_name) do
|
9
|
+
foreign_key = options[:foreign_key] || " #{self.class.to_s.tableize.gsub('_resources', '').singularize}_id"
|
10
|
+
sql_query = "select #{table_name.to_s.singularize}_id from #{[self.class.to_s.gsub('Resource', '').tableize, table_name.to_s.gsub('_resources', '')].sort.join('_')} where #{foreign_key}=#{self.id}"
|
11
|
+
if self.is_a?(ActiveResource::Base)
|
12
|
+
results = ActiveRecord::Base.connection.execute(sql_query)
|
13
|
+
resource_id = results.present? ? results[0]["#{klass_name.to_s.singularize}_id"] : nil
|
14
|
+
results.present? ? klass_name.to_s.classify.constantize.where(id: resource_id) : []
|
15
|
+
elsif self.is_a?(ActiveRecord::Base)
|
16
|
+
results = ActiveRecord::Base.connection.execute(sql_query)
|
17
|
+
resource_id = results.present? ? results[0]["#{klass_name.to_s.singularize}_id"] : nil
|
18
|
+
|
19
|
+
klass_name.to_s.classify.constantize.find(:all, params: { id: resource_id})
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module AssociationBuilder
|
2
|
+
|
3
|
+
class HasManyActiveResources
|
4
|
+
|
5
|
+
def self.build(model, table_name, options)
|
6
|
+
klass_name = options[:class_name] || table_name
|
7
|
+
model.class_eval do
|
8
|
+
define_method(table_name) do
|
9
|
+
foreign_key = options[:foreign_key] || "#{self.class.to_s.tableize.gsub('_resources', '').singularize}_id"
|
10
|
+
if self.is_a?(ActiveResource::Base)
|
11
|
+
klass_name.to_s.classify.constantize.where("#{foreign_key}": "#{self.id}")
|
12
|
+
elsif self.is_a?(ActiveRecord::Base)
|
13
|
+
[klass_name.to_s.classify.constantize.find(:all, params: { "#{foreign_key}": self.id})]
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module AssociationBuilder
|
2
|
+
|
3
|
+
class HasManyThroughActiveResources
|
4
|
+
|
5
|
+
def self.build(model, table_name, options)
|
6
|
+
middle_table = options[:through].to_s
|
7
|
+
klass_name = table_name.to_s
|
8
|
+
model.class_eval do
|
9
|
+
define_method(klass_name) do
|
10
|
+
if self.is_a?(ActiveResource::Base)
|
11
|
+
associated_ids = middle_table.classify.constantize.where("#{model.element_name.gsub('_resource', '')}_id = ?", self.id).pluck("#{klass_name.singularize}_id".to_sym)
|
12
|
+
klass_name.classify.constantize.where(id: associated_ids)
|
13
|
+
elsif self.is_a?(ActiveRecord::Base)
|
14
|
+
associated_ids = middle_table.classify.constantize.where("#{model.table_name.singularize}_id = ?", self.id).pluck("#{klass_name.singularize}_id".to_sym)
|
15
|
+
options[:class_name].classify.constantize.find(:all, params:{id: associated_ids})
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module AssociationBuilder
|
2
|
+
|
3
|
+
class HasOneActiveResource
|
4
|
+
|
5
|
+
def self.build(model, table_name, options)
|
6
|
+
klass_name = options[:class_name] || table_name
|
7
|
+
model.class_eval do
|
8
|
+
define_method("#{table_name.to_s.downcase}") do
|
9
|
+
if self.is_a?(ActiveResource::Base)
|
10
|
+
klass_name.to_s.classify.constantize.find_by("#{model.element_name}_id": self.id)
|
11
|
+
elsif self.is_a?(ActiveRecord::Base)
|
12
|
+
klass_name.to_s.classify.constantize.find("#{self.send(table_name.to_s + '_id')}")
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
metadata
ADDED
@@ -0,0 +1,205 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: active_resource_associatable
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Neeraj Kumar
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-08-23 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.11'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.11'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: minitest
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '5.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '5.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: activeresource
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 4.0.0
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 4.0.0
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: activesupport
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 4.2.5
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 4.2.5
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: activerecord
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: 4.2.5
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: 4.2.5
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: factory_girl
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: sqlite3
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: byebug
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - ">="
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '0'
|
132
|
+
type: :development
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - ">="
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '0'
|
139
|
+
- !ruby/object:Gem::Dependency
|
140
|
+
name: json
|
141
|
+
requirement: !ruby/object:Gem::Requirement
|
142
|
+
requirements:
|
143
|
+
- - ">="
|
144
|
+
- !ruby/object:Gem::Version
|
145
|
+
version: '0'
|
146
|
+
type: :runtime
|
147
|
+
prerelease: false
|
148
|
+
version_requirements: !ruby/object:Gem::Requirement
|
149
|
+
requirements:
|
150
|
+
- - ">="
|
151
|
+
- !ruby/object:Gem::Version
|
152
|
+
version: '0'
|
153
|
+
description: This rubygem is used to build the connection between any ActiveResource
|
154
|
+
model and ActiveRecord model.
|
155
|
+
email:
|
156
|
+
- neeraj.kumar@gmail.com
|
157
|
+
executables: []
|
158
|
+
extensions: []
|
159
|
+
extra_rdoc_files: []
|
160
|
+
files:
|
161
|
+
- ".gitignore"
|
162
|
+
- ".travis.yml"
|
163
|
+
- CODE_OF_CONDUCT.md
|
164
|
+
- Gemfile
|
165
|
+
- LICENSE.txt
|
166
|
+
- README.md
|
167
|
+
- Rakefile
|
168
|
+
- active_resource_associatable.gemspec
|
169
|
+
- bin/console
|
170
|
+
- bin/setup
|
171
|
+
- lib/active_resource_associatable.rb
|
172
|
+
- lib/active_resource_associatable/active_resource_associatable.rb
|
173
|
+
- lib/active_resource_associatable/belongs_to_activeresource.rb
|
174
|
+
- lib/active_resource_associatable/has_and_belongs_to_many_activeresources.rb
|
175
|
+
- lib/active_resource_associatable/has_many_activeresources.rb
|
176
|
+
- lib/active_resource_associatable/has_many_through_activeresources.rb
|
177
|
+
- lib/active_resource_associatable/has_one_activeresource.rb
|
178
|
+
- lib/active_resource_associatable/version.rb
|
179
|
+
homepage: https://github.com/neerajkumar/active_resource_associatable.
|
180
|
+
licenses:
|
181
|
+
- MIT
|
182
|
+
metadata:
|
183
|
+
allowed_push_host: https://rubygems.org
|
184
|
+
post_install_message:
|
185
|
+
rdoc_options: []
|
186
|
+
require_paths:
|
187
|
+
- lib
|
188
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
189
|
+
requirements:
|
190
|
+
- - ">="
|
191
|
+
- !ruby/object:Gem::Version
|
192
|
+
version: '0'
|
193
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
194
|
+
requirements:
|
195
|
+
- - ">="
|
196
|
+
- !ruby/object:Gem::Version
|
197
|
+
version: '0'
|
198
|
+
requirements: []
|
199
|
+
rubyforge_project:
|
200
|
+
rubygems_version: 2.4.8
|
201
|
+
signing_key:
|
202
|
+
specification_version: 4
|
203
|
+
summary: This rubygem is used to build the connection between any ActiveResource model
|
204
|
+
and ActiveRecord model.
|
205
|
+
test_files: []
|