Code Monkey home page Code Monkey logo

socialization's Introduction

Socialization

Socialization is a Ruby Gem that allows any ActiveRecord model to Follow, Like and/or Mention any other model. ActiveRecord or Redis can be used as a data store.

The Follow feature is similar to Twitter's follow. For example, John follows Jane. Unlike Facebook's "friendship", Follow is a one-way concept. The fact that John follows Jane doesn't mean that Jane follows John.

The Like feature works just like a Facebook Like. For example, John likes Pulp Fiction.

The Mention feature was written with Facebook mentions in mind. For example, John mentions Jane in a comment. Typically, Jane would be highlighted in the comment user interface and possibly notified that John mentioned her. This Facebook feature is occasionally called Tagging, although tagging is generally something [entirely different](http://en.wikipedia.org/wiki/Tag_(metadata).

Specs Gem Version

Installation

Add the gem to the gemfile: gem "socialization"

Run the generator: rails generate socialization -s

Or if you want to use Redis as your data store: rails generate socialization -s --store=redis

This will generate three migration files (when using ActiveRecord) and three models named Follow, Like and Mention. You may delete any of the Follow, Like or Mention models and migrations if you don't need that functionality in your application.

Legacy Rails Support

This gem requires Rails 6 or better. Sorry!

Usage

Setup

Allow a model to be followed:

class Celebrity < ActiveRecord::Base
  ...
  acts_as_followable
  ...
end

Allow a model to be a follower:

class User < ActiveRecord::Base
  ...
  acts_as_follower
  ...
end

Allow a model to be liked:

class Movie < ActiveRecord::Base
  ...
  acts_as_likeable
  ...
end

Allow a model to like:

class User < ActiveRecord::Base
  ...
  acts_as_liker
  ...
end

Allow a model to be mentioned:

class User < ActiveRecord::Base
  ...
  acts_as_mentionable
  ...
end

Allow a model to mention:

class Comment < ActiveRecord::Base
  ...
  acts_as_mentioner
  ...
end

Or a more complex case where users can like and follow each other:

class User < ActiveRecord::Base
  ...
  acts_as_follower
  acts_as_followable
  acts_as_liker
  acts_as_likeable
  acts_as_mentionable
  ...
end

acts_as_follower Methods

Follow something

user.follow!(celebrity)

Stop following

user.unfollow!(celebrity)

Toggle

user.toggle_follow!(celebrity)

Is following?

user.follows?(celebrity)

What items are you following (given that an Item model is followed)?

user.followees(Item)

Number of followees (Requires followees_count column in db)

def change
  add_column :#{Table_name}, :followees_count, :integer, :default => 0
end

user.followees_count

acts_as_followable Methods

Find out if an objects follows

celebrity.followed_by?(user)

All followers

celebrity.followers(User)

Number of followers (Requires followers_count column in db)

def change
  add_column :#{Table_name}, :followers_count, :integer, :default => 0
end

celebrity.followers_count

acts_as_liker Methods

Like something

user.like!(movie)

Stop liking

user.unlike!(movie)

Toggle

user.toggle_like!(celebrity)

Likes?

user.likes?(movie)

Number of likees (Requires likees_count column in db)

def change
  add_column :#{Table_name}, :likees_count, :integer, :default => 0
end

user.likees_count

acts_as_likeable Methods

Find out if an objects likes

movie.liked_by?(user)

All likers

movie.likers(User)

Number of likers (Requires likers_count column in db)

def change
  add_column :#{Table_name}, :likers_count, :integer, :default => 0
end

movie.likers_count

acts_as_mentioner Methods

Note that a "mentioner" is the object containing the mention and not necessarily the actor. For example, John mentions Jane in a comment. The mentioner is the comment object, NOT John.

Mention something

comment.mention!(user)

Remove mention

comment.unmention!(user)

Toggle

user.toggle_mention!(celebrity)

Mentions?

comment.mentions?(user)

All mentionees

comment.mentionees(User)

Number of mentionees (Requires mentionees column in db)

def change
  add_column :#{Table_name}, : mentionees, :integer, :default => 0
end

user. mentionees_count

acts_as_mentionable Methods

Find out if an objects mentions

user.mentioned_by?(comment)

All mentioners

user.mentioners(Comment)

Number of mentioners (Requires mentioners_count column in db)

def change
  add_column :#{Table_name}, :mentioners_count, :integer, :default => 0
end

movie.mentioners_count

Documentation

You can find the compiled YARD documentation at http://rubydoc.info/github/cmer/socialization/frames. Documentation for methods inside include blocks is not currently generated although it exists in the code. A custom YARD filter needs to be written for YARD to pick those up.


Note on Patches/Pull Requests

  • Fork the project.
  • Make your feature addition or bug fix.
  • Add tests for it. This is important so I don't break it in a future version unintentionally.
  • Send me a pull request. Bonus points for topic branches.

Similar Projects

acts_as_follower is a similar project that I only discovered when I was 95% finished writing the first version of Socialization. I initially intended to name this project acts_as_follower only to find out the name was taken. You might want to check it out as well so see which one suits your needs better. Socialization is simpler, supports "Likes" and "Mentions" and easilly extendable; acts_as_follower has more "Follow" features, however.

Copyright

Copyright (c) 2012-2022 Carl Mercier -- Released under the MIT license.

socialization's People

Contributors

alainpilon avatar balvig avatar cmer avatar garyharan avatar jdugarte avatar jfrux avatar reiz avatar samnang avatar shettytejas avatar thmsobrmlr avatar tilsammans avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

socialization's Issues

Could not find generator socialization

Hi,

After installing the gem through the gemfile, I'm getting this error.

Found someone who had the same problem but apparently not resolved : http://stackoverflow.com/questions/22513225/gem-within-an-engine

Using rails 4 / ActiveRecord. (Gemfile there : https://github.com/davidfabreguette/worship/blob/master/Gemfile)

Tried to move the generators directly in my app to lib/generators but same issue, it's like generators files are not getting loaded. Any idea ??

Getting likes for multiple records at once

Hello,

I have the following:

movies = Movie.all

movies.each do |m|
  puts m.likers(User)
end

and this n*2 queries. Is there anyway to do an includes such that it on Movie.all such that it only queries users once.

Mass Assignment

The plugin doesn't account for the new Rails 3.2 strict mass assignment setting. It would be nice if the methods that added likes and follows used :without_protection => true or some other method to avoid mass assignment errors.

undefined local variable error

When I do this to my User object:

class User < ActiveRecord::Base
acts_as_liker
...
end

I get this error:

undefined local variable or method `acts_as_liker' for #Class:0x007f813b0f9df8

Any ideas?

Can not Migrate after installing the gem

Hello,
I have just installed the gem to my systemIt all went well
However, when running rake db:migrate, i ran into the following error
Could someone help?
Thanx

== 20140822180211 CreateLikes: migrating ======================================
-- create_table(:likes)
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:

PG::UndefinedTable: ERROR: relation "likers" does not exist
: CREATE TABLE "likes" ("id" serial primary key, "liker_type" character varying(255), "liker_id" integer, "likeable_type" character varying(255), "likeable_id" integer, "created_at" timestamp, CONSTRAINT fk_likes_liker_id FOREIGN KEY ("liker_id") REFERENCES "likers" ("id"), CONSTRAINT fk_likes_likeable_id FOREIGN KEY ("likeable_id") REFERENCES "likeables" ("id")) /usr/local/rvm/gems/ruby-2.1.1/gems/activerecord-4.1.1/lib/active_record/connection_adapters/postgresql/database_statements.rb:128:in `async_exec'

Activity of a user

I'm looking for a way to get all past like and follow objects for a user so that I can display them in an activity like interface. Someone pointed me to public_activity for that type of stuff but saving custom events using PublicActivity seems redundant given that there are tables that already have all the information I am looking for ๐Ÿ˜„

Any recommendation/tips?

And huge thanks for the gem, it has been a huge help to us building Somewhere so far. ๐Ÿ‘

Having trouble getting started...

I'm pretty new to rails and am trying to implement this gem, however, I'm having trouble with the most basic functionality of the gem.

edit ####

followers_count and followees_count throws error

Hey,
i get 'undefined method `followers_count' for # ' when i use followers_count on a user object.

I created the extra column by editing the migrations:
t.integer :followees_count, :default=>0
t.integer :followers_count, :default=>0

Activity conditions

I have a book tracking system, and I have this gem setup and working correctly to track a friendship model likes so:

@activities = PublicActivity::Activity.order("created_at desc").where(owner_id: current_user.followees(User), owner_type: "User").limit(10)

So that whenever a use adds/removes a book to a collection, it is tracked:

tracked owner: :user, recipient: :book

I also have set up activity tracking in the Book model, My issue is that I cannot include both in the stream. If I use the above @activities it tracks the activity in the friendship model, but will not track when new books are added or updated to the database. I can do that if I switch this to:

@activities = PublicActivity::Activity.order("created_at desc").where(trackable_type: "Book").limit(10)

but I would like to show both at once in one stream. I've tried this using various or statements but without luck, any ideas?

Gathering the Like counts is the slowest part of my SQL

When I get an array of my likeable items, having to loop through them to get the like count for each item seem to be the slowest part of my Active Record SQL. Is there a faster way to do it?

Currently, the ruby looks like this:

@project.scenes.each do|n|
    n.like_count = n.likers(User).count
end

which generates SQL like this:

SELECT COUNT(*) FROM "users" INNER JOIN likes ON likes.liker_id = users.id AND likes.liker_type = 'User' WHERE (likes.likeable_type = 'Scene') AND (likes.likeable_id = 857)

liking a like

Is it possible to like a like ?
i.e
class Like < ActiveRecord::Base
acts_as_like_store
acts_as_likeable

end

have anyone tried it? I have not tried it yet, but seems like it should work?

Bug with table prefix name: store but doesn't work correct

I have a Rails app that has table prefixes, like:

config.active_record.table_name_prefix = 'p_soc_'

When I try, for example, to "like" something:
user.like! photo

And then do this:
user.likes? photo

it returns false.
If I run the app without the table_prefix_name (recreating the database), it works perfect.

I was googling around and found a similar bug repor in the friendly_id github page: norman/friendly_id#328

EDIT: the problem has been fixed in the pull request from this link #32
I forked the project and updated the gem with that PR, and now this is ok.

im getting this error ....

ActionView::MissingTemplate at /users/follow

Missing template users/follow, application/follow

my controller

follow / follower

def follow
user = User.find(params[:id])
current_user.follow!(user)
end

def unfollow
user = User.find(params[:id])
current_user.unfollow!(user)
end

router

namespace :users do
get 'follow' , as: :follow
get 'unfollow', as: :unfollow
end

Creating a button to follow

I'm trying to make a button to get a user to follow an item

so, in rails console, i can make it work with

user = User.first item = Item.first user.follow!(item)

That works fine, however in my view, I want a button to trigger this action.

I'm using in my item#show view

<%= form_for(followitem(follow: @item)) do |f| %> <%= f.hidden_field :follow %> <%= f.submit "Follow", class: "btn btn-large btn-primary" %> <% end %>

and in my item_helper.rb

def followitem(it) current_user.follow!(it) end

Now I'm getting an error back saying that the item is not followable....

What am I doing wrong?

error: Place is not likeable! .. but it is..

I have this code

"@user.likes?(Place)" in show.html.erb
which generates this error "Place is not likeable!"
But in the place.rb model I have this: "acts_as_likeable" and I can like and unlike places no problem.

gem controller

I need help in implelmenting the controller for acts_as_likeable ?

Easy way to return the 'like' status for objects?

If I have a User object that can like a Movie, is there an easy way for me to return the like status when retrieving the Movie object? For example, I retrieve all recent movies and display them in a table and I want to know which ones have been favorited by the user.

Add custom scope while getting followees

How can I add my custom scope while getting objects that are being followed by user. i.e
user.followees(Post) => Return all posts that are being followed by user, but I want to get only those posts which are active. So how can I add my scope while getting results?

Two issues with implementation

Hey
Since this has not been asked, I would like to know your suggestion as how you will use your gem to show "Posts" associated with a followed user. There wasn't a coherent description in the guide.

This is what I tried
Post.where(id: current_user.followers.pluck(:id))
This throws an error
wrong number of arguments (0 for 1..2)

Also, as suggested in the guide to show the follower/followee count, there is a strange output.
The count in the view starts off with -2 and counts upwards from there.
Migration
add_column :users, :followers_count, :integer, :default => 0
User model by devise

Thanks.

Add "following" method to acts_as_follower

it'd be good if you could add a method "following" to acts_as_follower, listing all instances to which the follower instance is following to.
An early outline would be:

def following(followable_type)
  follower_type = self.class.base_class.name
  follower_id = self.id

  followable_string = followable_type.name
  followable_table = followable_string.downcase.pluralize

  join_query = "INNER JOIN follows ON follows.followable_id = "+ followable_table + ".id    AND follows.followable_type = '"+ followable_string +"'"

  followable_type.joins(join_query).where(:follows => {:follower_type => follower_type,   :follower_id => follower_id})
end

Many thanks!

Get all likes of a user

How can we create a function to get all things that a user is liking? Currently the only method I have is something like this:

def get_user_likes
    user = User.first
    our_likes = []
    Activity.all.each do |activity|
             if user.likes?(activity)
                  our_likes.push(activity)
             end
    end

    our_likes.uniq
end

But this is doing like 1 select query for each and every one... This won't scale well.

Unlike! did not remove the record, and a user will generate a lot of likes records with a likable model

Just like this:

+----+------------+----------+---------------+-------------+---------------------+
| id | liker_type | liker_id | likeable_type | likeable_id | created_at          |
+----+------------+----------+---------------+-------------+---------------------+
| 15 | User       |      200 | Flow::Service |         201 | 2015-12-02 09:32:41 |
| 16 | User       |      200 | Flow::Service |         201 | 2015-12-02 09:32:45 |
| 17 | User       |      200 | Flow::Service |         201 | 2015-12-02 09:34:21 |
| 18 | User       |      200 | Flow::Service |         201 | 2015-12-02 09:34:23 |
| 19 | User       |      200 | Flow::Service |         201 | 2015-12-02 09:36:21 |
| 20 | User       |      200 | Flow::Service |         201 | 2015-12-02 09:36:21 |
| 21 | User       |      200 | Flow::Service |         201 | 2015-12-02 09:36:21 |
| 22 | User       |      200 | Flow::Service |         201 | 2015-12-02 09:36:22 |
| 23 | User       |      200 | Flow::Service |         201 | 2015-12-02 09:36:22 |
| 24 | User       |      200 | Flow::Service |         201 | 2015-12-02 09:36:23 |
| 25 | User       |      200 | Flow::Service |         201 | 2015-12-02 09:36:23 |
| 26 | User       |      200 | Flow::Service |         201 | 2015-12-02 09:36:23 |
| 27 | User       |      200 | Flow::Service |         201 | 2015-12-02 09:36:24 |
| 28 | User       |      200 | Flow::Service |         201 | 2015-12-02 09:36:25 |
+----+------------+----------+---------------+-------------+---------------------+

Demo app?

It looks like the demo app was moved? Is there a new location? Thanks

organic pagination support for followers/likes

Hi,

I was looking for organic pagination support for followers/likes for any object.

followers = current_user.followers(User) returns array of collection where i don't want to pagination on the rails layer.

I would prefer to have a organic pagination support for this on db layer
something like this

current_user.followers(User).offset(0).limit(10)

i have not dig deep on the source code but something the following will also do fine.
current_user.followers(User, {offset: 0, limit: 10})

Touch models

Is it possible to touch the relation model when e.g a like is created? It would be useful to make use of russian dolls caching.

How to count followers and followes of user ?

I have USER model which act as #Follows / Follower


class User < ActiveRecord::Base
acts_as_follower
acts_as_followable
end


  • now i want to display all users Mr. A follows.
  • and all users who follows back Mr. A

in the documentation it shows as celebrity.followers(current_user) but here i have object as USER.

  • = user.followers(current_user)
    it given this error undefined method `table_name' for #User:0xfee8e14

thanks in advance...

Cannot use Socialization together with Act as Tenant.

Hello,

I am using Act_as_tenant gem in my application which is a multi tenant solution where account_id is added to all tables. I am having problems using it together with socialization. I get this error:

undefined method `primary_key' for Fixnum:Class

After adding this to the Like model:

acts_as_tenant(:account)

Any ideas what it can be?

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.