Code Monkey home page Code Monkey logo

likeable's Introduction

Use Redis to Make your Ruby objects Likeable!

I no longer use this gem in production (it was written for Gowalla), if you do and want to help me maintain it, let me know @schneems.

You like this

Likeable will allow your models to be liked by users, just drop a few lines of code into your model and you're good to go.

    class Comment
      include Likeable

      # ...
    end

    class User
      include Likeable::UserMethods

      # ...
    end

    Likeable.setup do |likeable|
      likeable.redis   = Redis.new
    end

    comment = Comment.find(15)
    comment.like_count                  # => 0
    current_user.like!(comment)         # => #<Likeable::Like ... >
    comment.like_count                  # => 1
    comment.likes                       # => [#<Likeable::Like ... >]
    comment.likes.last.user             # => #<User ... >
    comment.likes.last.created_at       # => Wed Jul 27 19:34:32 -0500 2011

    comment.liked_by?(current_user)     # => true

    current_user.all_liked(Comment)     # => [#<Comment ...>, ...]

    liked_comment = Likeable.find_by_resource_id("Comment", 15)
    liked_comment == comment            # => true

This library doesn't do dislikes, if you want something with more flexibility check out opinions.

Screencast

You can view a screencast of likeable in action on youtube. There is also an example Likeable rails application that you can use to follow along.

Setup

Gemfile:

gem 'likeable'

Next set up your Redis connection in initializers/likeable.rb:

    Likeable.setup do |likeable|
      likeable.redis  = Redis.new
    end

Then add the Likeable::UserMethods module to models/user.rb:

    class User
      include Likeable::UserMethods
    end

Finally add Likeable module to any model you want to be liked:

    class Comment
      include Likeable
    end

Rails Info

If you're using Likeable in Rails this should help you get started

controllers/likes_controller.rb

  class LikesController < ApplicationController

    def create
      target = Likeable.find_by_resource_id(params[:resource_name], params[:resource_id])
      current_user.like!(target)
      redirect_to :back, :notice => 'success'
    end

    def destroy
      target = Likeable.find_by_resource_id(params[:resource_name], params[:resource_id])
      current_user.unlike!(target)
      redirect_to :back, :notice => 'success'
    end
  end

config/routes.rb

    delete  'likes/:resource_name/:resource_id' => "likes#destroy", :as => 'like'
    post    'likes/:resource_name/:resource_id' => "likes#create",  :as => 'like'

helpers/like_helper.rb

    def like_link_for(target)
      link_to "like it!!", like_path(:resource_name => target  .class, :resource_id => target.id), :method => :post
    end

    def unlike_link_for(target)
      link_to "unlike it!!", like_path(:resource_name => target.class, :resource_id => target.id), :method => :delete
    end

Then in any view you can simply call the helper methods to give your user a link

    <%- if @user.likes? @comment -%>
      <%= unlike_link_for @comment  %>
    <%- else -%>
      <%= like_link_for @comment %>
    <%- end -%>

Why

We chose Redis because it is screaming fast, and very simple to work with. By using redis for likeable we take load off of our relational database and speed up individual calls retrieve information about the "liked" state of an object. If you're not using redis in production, and don't want to, there are many other great liking/voting libraries out there such as thumbs up.

           RedisRed            RedisRedi
        RedisRedisRedi       RedisRedisRedisR
      RedisRedisRedisRedi   RedisRedisRedisRedi
     RedisRedisRedisRedisRedisRedisRe       Redi
    RedisRedisRedisRedisRedisRedisRe         Redi
   RedisRedisRedisRedisRedisRedisRedisR       Redi
   RedisRedisRedisRedisRedisRedisRedisRedis      R
  RedisRedisRedisRedisRedisRedisRedisRedisRedi  Red
  RedisRedisRedisRedisRedisRedisRedisRedisRedisRe R
  RedisRedisRedisRedisRedisRedisRedisRedisRedisRedi
  RedisRedisRedisRedisRedisRedisRedisRedisRedisRedi
   RedisRedisRedisRedisRedisRedisRedisRedisRedisRe
    RedisRedisRedisRedisRedisRedisRedisRedisRedis
      RedisRedisRedisRedisRedisRedisRedisRedisRe
        RedisRedisRedisRedisRedisRedisRedisRe
           RedisRedisRedisRedisRedisRedisR
              RedisRedisRedisRedisRedis
                RedisRedisRedisRedis
                   RedisRedisRed
                     RedisRedi
                       RedisR
                        Redi
                         Re

Authors

Richard Schneeman for Gowalla <3

Contribution

Fork away. If you want to chat about a feature idea, or a question you can find me on the twitters @schneems. Put any major changes into feature branches. Make sure all tests stay green, and make sure your changes are covered.

licensed under MIT License Copyright (c) 2011 Schneems. See LICENSE.txt for further details.

likeable's People

Contributors

bemurphy avatar kirs avatar leehambley avatar schneems 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

likeable's Issues

Likeable has some where calls

Likeable has 2 calls to where finders. It should probably rely on the find_one/find_many procs.

Also, .all_liked_by_ids is mapping using .to_i:

def all_liked_ids_by(user)
  key = user.like_key(self.to_s.downcase)
  ids = (Likeable.redis.hkeys(key)||[]).map(&:to_i)
end

when .to_s would probably work and then allow it to support hash ids like Mongoid uses.

I might hack on this if I get time later.

undefined method 'hkeys' for "localhost:5959":String

One of spec test is failing when deleting data that has "include Likeable":

  1. Product on destroy should destroy reverse association
    Failure/Error: relationship.destroy
    NoMethodError:
    undefined method `hkeys' for "localhost:6969":String
it 'should destroy reverse association' do
  @product.reverse_stock_strains.create(:club_id => Factory(:club).id)
  lambda {@product.destroy}.should change(Stock,:count).from(1).to(0)
end

Stock is the model that is including likeable. Since Stock is dependent on Product, it destroy Stock when Product is destroyed. That's when it throws the error.

localhost:6969 is where my redis test server:

RSpec.configure do |config|
REDIS_PID = "#{Rails.root}/tmp/pids/redis-test.pid"
REDIS_CACHE_PATH = "#{Rails.root}/tmp/cache/"

config.before(:suite) do
redis_options = {
"daemonize" => 'yes',
"pidfile" => REDIS_PID,
"port" => 6969,
"timeout" => 300,
"save 900" => 1,
"save 300" => 1,
"save 60" => 10000,
"dbfilename" => "dump.rdb",
"dir" => REDIS_CACHE_PATH,
"loglevel" => "debug",
"logfile" => "stdout",
"databases" => 16
}.map { |k, v| "#{k} #{v}" }.join('\n')
echo '#{redis_options}' | redis-server -
end
config.after(:suite) do
%x{
cat #{REDIS_PID} | xargs kill -QUIT
rm -f #{REDIS_CACHE_PATH}dump.rdb
}
end
end

is anyone else getting this error?

Rails 3.1 incompatible?

Tried to use in Rails 3.1 app but blows up on Likeable.setup ... method in initializer folder

/Users/millisami/gitcodes/Likeable/lib/likeable/module_methods.rb:107:in `block in include_in_class': undefined local variable or method `config' for Rails::Application:Class (NameError)
    from /Users/millisami/gitcodes/Likeable/lib/likeable/module_methods.rb:106:in `class_eval'
    from /Users/millisami/gitcodes/Likeable/lib/likeable/module_methods.rb:106:in `include_in_class'

What is the most efficient way to create a 'likes' stream?

Hello,

I have added the Likeable library to my app successfully, now each user can like my 'Posts' model. I am currently examining what is the most efficient way to show a 'likes stream' in each user's profile page, something like:

'User XXX liked your post 'Blablablabla' on 25 Dec 2011 User YYY liked your post 'BiriBiriBiriBiri' on 26 Dec 2011'

I was thinking of implementing a seperate 'list' per Post in my Redis instance, adding the above sentence whenever a like happens. On the other hand, the Likeable library already has the .likes class that lists likes (including users, date etc) for each Post.

I am thinking that using the .likes class may hinder my app's performance, indeed everytime a user accesses his profile page, my controller would have to iterate through all his Posts, check if there are any likes for each Post, then retrieve the required information (user id, username, date, etc) and compose the above sentence for every like of every post..

Any suggestions?

Many thanks for your help!
Alex

Users total likes

Hi, firstly it's worth noting that I'm new to redis. I would like to get a total of all likes from a single user across all models. From what I can see there isn't a method in the 'Likeable::UserMethods' class which can do this?

I know you've said your not maintaining this gem no more but I was wondering if you could give me some tips or hints on how I can implement the above function. Any help would be greatly appreciate as I'd like to learn redis a lot more.

Thank you.

Likes not added to redis

Hi,

I'm developing a rails3 application using RedisToGo Nano as my development environment and a Mini instance in Heroku for production. I have closely followed the provided Likeable sample application and added the Likeable::UserMethods to my user model as well as the Likeable module to my posts model (and also added the likes_controller.rb, routes.rb, and like_helper.rb code as appropriate).

Problem is some likes are registered in redis and some are not at all! The bad ones do not create a key at all, meaning, after clicking the like link I run the REDIS.keys command and do not see anything relevant to the specific record.

I used the link tag provided in the posts index view in the sample application. Every time I click the link, the POST request is executed correctly, it includes "resource_name"=>"Post", "resource_id"=>"XX" in the parameters, and returns a 'Success' notice as it should in my view.

My only deviation from the sample application is in the setup of redis, I did:
in config/environments/development.rb:


and in config/initializers/redis.rb

REDIS = Redis.new(:host => uri.host, :port => uri.port, :password => uri.password)

Any ideas?
Many thanks for your help,

How can I get most liked items?

For example, I have model Product with likable,
how can I get the most liked product (popular product) that ordered from high to low... :)

THank's :-)

find_by_resource_id always returns only the first result

I've tried the seq in rails c:

Likeable.find_by_resource_id("Comment", 15)
Likeable.find_by_resource_id("Comment", 16)
Likeable.find_by_resource_id("Comment", 56)

but I'm always getting 'Comment id: 15'

Here is the log what is going on:

>> Likeable.find_by_resource_id("Comment", 15)
  Comment Load (0.7ms)  SELECT "comments".* FROM "comments" WHERE "comments"."id" = 15 LIMIT 1
=> #<Comment id: 15, comment: "Your opinion about this product!!!", rating: 4, related: true, user_id: 8, product_id: 65, created_at: "2011-12-13 10:12:19", updated_at: "2011-12-13 10:12:19">
>> Likeable.find_by_resource_id("Comment", 16)
  Comment Load (0.7ms)  SELECT "comments".* FROM "comments" WHERE "comments"."id" = 15 LIMIT 1
=> #<Comment id: 15, comment: "Your opinion about this product!!!", rating: 4, related: true, user_id: 8, product_id: 65, created_at: "2011-12-13 10:12:19", updated_at: "2011-12-13 10:12:19">
>> Likeable.find_by_resource_id("Comment", 56)
  Comment Load (0.6ms)  SELECT "comments".* FROM "comments" WHERE "comments"."id" = 15 LIMIT 1
=> #<Comment id: 15, comment: "Your opinion about this product!!!", rating: 4, related: true, user_id: 8, product_id: 65, created_at: "2011-12-13 10:12:19", updated_at: "2011-12-13 10:12:19">

P.S. Rails 3.1.3 / Postgresql if matters.

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.