GithubHelp home page GithubHelp logo

pelly / activemodel-associations Goto Github PK

View Code? Open in Web Editor NEW

This project forked from dskecse/activemodel-associations

0.0 2.0 0.0 46 KB

has_many and belongs_to macro for Plain Ruby Object.

License: MIT License

Ruby 100.00%

activemodel-associations's Introduction

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

activemodel-associations's People

Contributors

hanachin avatar joker1007 avatar kamipo avatar lowjoel avatar masa-iwasaki avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.