{"id":18133316,"url":"https://github.com/andreapavoni/likes_tracker","last_synced_at":"2025-09-10T20:32:46.714Z","repository":{"id":56881224,"uuid":"5194687","full_name":"andreapavoni/likes_tracker","owner":"andreapavoni","description":"track likes between rails models using Redis backend","archived":false,"fork":false,"pushed_at":"2012-09-06T13:23:21.000Z","size":140,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-07-17T12:18:41.031Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/andreapavoni.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2012-07-26T16:50:44.000Z","updated_at":"2019-07-11T14:49:45.000Z","dependencies_parsed_at":"2022-08-20T13:00:43.402Z","dependency_job_id":null,"html_url":"https://github.com/andreapavoni/likes_tracker","commit_stats":null,"previous_names":["apeacox/likes_tracker"],"tags_count":4,"template":false,"template_full_name":null,"purl":"pkg:github/andreapavoni/likes_tracker","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andreapavoni%2Flikes_tracker","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andreapavoni%2Flikes_tracker/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andreapavoni%2Flikes_tracker/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andreapavoni%2Flikes_tracker/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/andreapavoni","download_url":"https://codeload.github.com/andreapavoni/likes_tracker/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andreapavoni%2Flikes_tracker/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":267105346,"owners_count":24036802,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-07-26T02:00:08.937Z","response_time":62,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-01T13:07:25.286Z","updated_at":"2025-07-26T02:14:00.827Z","avatar_url":"https://github.com/andreapavoni.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# LikesTracker [![Build Status](https://secure.travis-ci.org/apeacox/likes_tracker.png)](http://travis-ci.org/apeacox/likes_tracker)\n\nA Rails gem to track *likes* between two ```ActiveModel``` compliant models.  A common use case might be that a User likes a Post.\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n```gem 'likes_tracker'```\n\nAnd then execute:\n\n```$ bundle install```\n\nOr install it yourself as:\n\n```$ gem install likes_tracker```\n\n### Dependencies\n\n* [redis](http://redis.io)\n* ruby 1.9+ (it uses some 1.9's syntax)\n\n## Usage\n\nFirst of all, you need a ```$redis``` in your app, you might achieve this using an initializer:\n\n```\n# config/initializers/redis.rb\n$redis = Redis.new(host: 'localhost', port: '6379', db: '1')\n```\n\nGiven you have two models, say User and Post, and you want to track the *likes* a given Post receives by User(s). Include the LikesTracker module and use the methods it offers to setup models as *liker* and *liked*:\n\n```\n# app/models/post.rb\nclass Post \u003c ActiveRecord::Base\n  include LikesTracker\n  acts_as_liked_by :users\n\n  # rest of the code\nend\n\n# app/models/user.rb\nclass User \u003c ActiveRecord::Base\n  include LikesTracker\n  acts_as_liker_for :posts\n\n  # rest of the code\nend\n```\n\nNow your models will have some methods to manage the likes a model *gives* to another. Following the above example:\n\n```\n\u003e user.likes_post? post\n =\u003e false\n\n\u003e user.liked_posts\n =\u003e []\n\n\u003e post.likes_users_count\n =\u003e 0\n\n\u003e user.like_post! post\n =\u003e [true, true, 1.0]\n\n\u003e user.likes_post? post\n =\u003e true\n```\n\nAs you can see, the methods' names reflect model names (and they'll be properly namespaced on Redis). This means, that the same models can like several others, for example User might like another model called Photo or Comment, so you'll have methods like ```#like_comment!``` or ```#likes_photo?``` and so on.\n\nHow to find Posts liked by a User? There's a method for this, of course ;-)\n\n```\n\u003e user.liked_posts\n =\u003e [#\u003cPost id: 1, ...\u003e]\n```\nIt returns a *relation*, such as ```ActiveRecord::Relation```. Even if I haven't tested it yet, this *should* work with other ORMs like Mongoid.\n\n```\n# a silly example to show how it works\n\u003e user.liked_posts {|model, ids| p [model, ids] }\n =\u003e [Post(id: integer, ...), [\"1\"]]\n\n# the query executed by default\n\u003e user.liked_posts {|model, ids| model.where(id: ids) }\n =\u003e [#\u003cPost id: 1, ...\u003e]\n```\n\nLast but not least, here there're the remaining methods and examples:\n\n```\n# you can provide a *limit* parameter, if omitted it defaults to 5\n\u003e Post.most_liked(5)\n =\u003e [#\u003cPost id: 1, ...\u003e]\n\n# and it also accepts an  *offset* parameter, if omitted it defaults to 0\n\u003e Post.most_liked(5, 0)\n =\u003e [#\u003cPost id: 1, ...\u003e]\n\n# last but not least, it accepts a block, like you've already seen in above examples\n\u003e Post.most_liked(5, 0) {|model, ids| p [model, ids] }\n =\u003e [Post(id: integer, ...), [\"1\"]]\n\n\u003e post.likes_users_count\n =\u003e 1\n\n\u003e user.unlike_post! post\n =\u003e [true, true, 0.0]\n\n\u003e user.likes_post? post\n =\u003e false\n\n\u003e user.liked_posts\n =\u003e []\n\n\u003e post.likes_users_count\n =\u003e 0\n```\n\n## Contributing\n\n1. Fork it!\n2. Create your feature branch (`git checkout -b my-new-feature`)\n3. Commit your changes (`git commit -am 'Added some feature'`)\n4. Push to the branch (`git push origin my-new-feature`)\n5. Create new Pull Request\n\n### Testing\n\n* clone this repo\n* run `bundle install`\n* run `rspec spec`\n\n\n## License\nCopyright (c) 2012 Andrea Pavoni http://andreapavoni.com\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fandreapavoni%2Flikes_tracker","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fandreapavoni%2Flikes_tracker","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fandreapavoni%2Flikes_tracker/lists"}