Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

resonate

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

resonate

  • 0.3.1
  • Rubygems
  • Socket score

Version published
Maintainers
1
Created
Source

Resonate

Build Status Gem Version

Resonate provides a relational feature to your Rails application, such as follow, like, and so on.

Installation

Add this line to your application's Gemfile:

gem 'resonate'

And then execute:

$ bundle

Usage

Resonate supports two way to add relational feature.

  1. Add a feature to itself
  2. Add features to two models

Following example shows about User model, however, you can apply to any models.

1. Add follow feature to User

This case adds follow feature to User model.

At first, generate User and Follow model:

$ bin/rails g model User
$ bin/rails g model Follow user_id:integer target_user_id:integer

And migrate:

$ bin/rake db:migrate

Then, define Resonatable module to app/models/concerns/resonatable.rb:

module Resonatable
  include Resonate

  resonate :user, with: :user, by: :follow
end

At last, include this module from each models:

class User < ActiveRecord::Base
  include Resonatable
end

class Follow < ActiveRecord::Base
  include Resonatable
end

That's it. User instance has been added following methods:

  • user.follow(other_user)
  • user.unfollow(other_user)
  • user.following?(other_user)
  • user.following
  • other_user.followed_by?(user)
  • other_user.followers

Example:

user       = User.create
other_user = User.create

# Follow
user.follow other_user
user.following?(other_user)   #=> true
user.following                #=> <ActiveRecord::Associations::CollectionProxy [#<User id: 2, created_at: "2015-01-10 01:57:52", updated_at: "2015-01-10 01:57:52">]>
other_user.followed_by?(user) #=> true
other_user.followers          #=> <ActiveRecord::Associations::CollectionProxy [#<User id: 1, created_at: "2015-01-10 01:57:42", updated_at: "2015-01-10 01:57:42">]>

# Unfollow
user.unfollow other_user
user.following?(other_user)   #=> false
user.following                #=> <ActiveRecord::Associations::CollectionProxy []>
other_user.followed_by?(user) #=> false
other_user.followers          #=> <ActiveRecord::Associations::CollectionProxy []>

2. Add like feature to User and Post

This case adds like feature to User and Post model.

module Resonatable
  include Resonate

  resonate :user, with: :post, by: :like
end

class User < ActiveRecord::Base
  include Resonatable
end

class Post < ActiveRecord::Base
  include Resonatable
end

class Like < ActiveRecord::Base
  include Resonatable
end

User and Post instance has been added following methods:

  • user.like(post)
  • user.unlike(post)
  • user.liking?(post)
  • user.liking
  • post.liked_by?(user)
  • post.likers

At the same time, some resonate methods are able to be defined:

module Resonatable
  include Resonate

  resonate :user, with: :user, by: :follow
  resonate :user, with: :post, by: :like
end

Customization

If you want to use other foreign key name, you can define it by :foreign_key option.

resonate :user, with: :post, by: :like, foreign_key: :post_id # Default is `:target_post_id`

Contributing

  1. Fork it ( https://github.com/kami-zh/resonate/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

FAQs

Package last updated on 15 Feb 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc