has_many and belongs_to macro for Plain Ruby Object.
Ruby
Latest commit 8bbea47 Aug 11, 2016 @joker1007 v0.2.0
Permalink
Failed to load latest commit information.
gemfiles Update gemspec & activerecord-50.gemfile Aug 11, 2016
lib v0.2.0 Aug 11, 2016
spec Add subclass test Mar 19, 2015
.gitignore Support Rails 4.0.x May 8, 2014
.rspec init May 7, 2014
.travis.yml Fix .travis.yml Apr 17, 2016
Gemfile init May 7, 2014
LICENSE.txt init May 7, 2014
README.md Update README.md Aug 9, 2016
Rakefile Update gemfiles Apr 17, 2016
activemodel-associations.gemspec Update gemspec & activerecord-50.gemfile Aug 11, 2016

README.md

ActiveModel::Associations

Gem Version Build Status Coverage Status Code Climate

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

class User < ActiveRecord::Base; end

class Comment
  include ActiveModel::Model         # need ActiveModel::Model
  include ActiveModel::Associations  # include this

  attr_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
end

user = User.create(name: "joker1007")
comment = Comment.new(user_id: user.id)
comment.user # => <User {name: "joker1007"}>

Polymorphic belongs_to

class User < ActiveRecord::Base; end

class Comment
  include ActiveModel::Model         # need ActiveModel::Model
  include ActiveModel::Associations  # include this

  attr_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
end

user = User.create(name: "joker1007")
comment = Comment.new(commenter_id: user.id, commenter_type: "User")
comment.commenter # => <User {name: "joker1007"}>

has_many

class User < ActiveRecord::Base; end

class Group
  include ActiveModel::Model
  include ActiveModel::Associations

  attr_accessor :name
  attr_reader :user_ids

  has_many :users

  def [](attr)
    self.send(attr)
  end

  def []=(attr, value)
    self.send("#{attr}=", value)
  end
end

user = 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