Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/joker1007/activemodel-associations
has_many and belongs_to macro for Plain Ruby Object.
https://github.com/joker1007/activemodel-associations
Last synced: 2 days ago
JSON representation
has_many and belongs_to macro for Plain Ruby Object.
- Host: GitHub
- URL: https://github.com/joker1007/activemodel-associations
- Owner: joker1007
- License: mit
- Created: 2014-05-07T19:45:14.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2020-02-10T07:21:04.000Z (over 4 years ago)
- Last Synced: 2024-10-12T21:12:09.124Z (26 days ago)
- Language: Ruby
- Homepage:
- Size: 43.9 KB
- Stars: 79
- Watchers: 6
- Forks: 27
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
- awesome-list - activemodel-associations
README
# ActiveModel::Associations
[![Gem Version](https://badge.fury.io/rb/activemodel-associations.svg)](http://badge.fury.io/rb/activemodel-associations)
[![Build Status](https://travis-ci.org/joker1007/activemodel-associations.svg?branch=master)](https://travis-ci.org/joker1007/activemodel-associations)
[![Coverage Status](https://coveralls.io/repos/joker1007/activemodel-associations/badge.png)](https://coveralls.io/r/joker1007/activemodel-associations)
[![Code Climate](https://codeclimate.com/github/joker1007/activemodel-associations/badges/gpa.svg)](https://codeclimate.com/github/joker1007/activemodel-associations)`has_many` and `belongs_to` macro for Plain Ruby Object.
## Installation
Add this line to your application's Gemfile:
gem 'activemodel-associations'
And then execute:
$ bundle
Or install it yourself as:
$ gem install activemodel-associations
## Requirements
- Ruby-2.0.0 or later## Support
- activerecord-4.0.x
- activerecord-4.1.x
- activerecord-4.2.x
- activerecord-5.0.x## Usage
### belongs\_to
```ruby
class User < ActiveRecord::Base; endclass Comment
include ActiveModel::Model # need ActiveModel::Model
include ActiveModel::Associations # include thisattr_accessor :body, :user_id # belongs_to association need foreign_key attribute
belongs_to :user
# need hash like accessor, used internal Rails
def [](attr)
self.send(attr)
end# need hash like accessor, used internal Rails
def []=(attr, value)
self.send("#{attr}=", value)
end
enduser = User.create(name: "joker1007")
comment = Comment.new(user_id: user.id)
comment.user # =>
```### Polymorphic belongs\_to
```ruby
class User < ActiveRecord::Base; endclass Comment
include ActiveModel::Model # need ActiveModel::Model
include ActiveModel::Associations # include thisattr_accessor :body, :commenter_id, :commenter_type
belongs_to :commenter, polymorphic: true
# need hash like accessor, used internal Rails
def [](attr)
self.send(attr)
end# need hash like accessor, used internal Rails
def []=(attr, value)
self.send("#{attr}=", value)
end
enduser = User.create(name: "joker1007")
comment = Comment.new(commenter_id: user.id, commenter_type: "User")
comment.commenter # =>
```### has\_many
```ruby
class User < ActiveRecord::Base; endclass Group
include ActiveModel::Model
include ActiveModel::Associationsattr_accessor :name
attr_reader :user_idshas_many :users
def [](attr)
self.send(attr)
enddef []=(attr, value)
self.send("#{attr}=", value)
end
enduser = User.create(name: "joker1007")
group = Group.new(user_ids: [user.id])
group.users # => ActiveRecord::Relation (SELECT * from users WHERE id IN (#{user.id}))
group.users.find_by(name: "none") # => []
```## Limitation
Support associations is only `belongs_to` and simple `has_many`
`has_many` options is limited. following options is unsupported.`:through :dependent :source :source_type :counter_cache :as`
## Contributing
1. Fork it ( https://github.com/joker1007/activemodel-associations/fork )
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create a new Pull Request