likeit 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.rdoc +106 -0
- data/Rakefile +14 -0
- data/init.rb +1 -0
- data/lib/generators/likeit_generator.rb +28 -0
- data/lib/generators/templates/migration.rb +16 -0
- data/lib/generators/templates/model.rb +11 -0
- data/lib/likeit/likeable.rb +13 -0
- data/lib/likeit/liker.rb +34 -0
- data/lib/likeit.rb +5 -0
- data/likeit.gemspec +30 -0
- metadata +86 -0
data/README.rdoc
ADDED
@@ -0,0 +1,106 @@
|
|
1
|
+
= Likeit
|
2
|
+
|
3
|
+
Rails gem for building a liking systems by using Active Records.
|
4
|
+
|
5
|
+
by Gagan Sharma <developer.gagan@gmail.com>
|
6
|
+
|
7
|
+
GitHub Project: https://github.com/gagansharma/likeit
|
8
|
+
|
9
|
+
-----
|
10
|
+
|
11
|
+
TODO List:
|
12
|
+
|
13
|
+
* Better documentation (Making use of rdoc and ri)
|
14
|
+
* Write tests
|
15
|
+
|
16
|
+
== Installation
|
17
|
+
|
18
|
+
=== Rails 3
|
19
|
+
|
20
|
+
If you would like to install it as gem, put this in your Gemfile:
|
21
|
+
|
22
|
+
gem 'likeit'
|
23
|
+
|
24
|
+
If you would rather like to install it as plugin, run this command in your console:
|
25
|
+
|
26
|
+
rails plugin install git://github.com/gagansharma/likeit.git
|
27
|
+
|
28
|
+
And then run:
|
29
|
+
|
30
|
+
rails generate likeit
|
31
|
+
rake db:migrate
|
32
|
+
|
33
|
+
== Usage
|
34
|
+
|
35
|
+
Just add `acts_as_liker` and `acts_as_likeable` to your models. You could mix them in the same model if you like. See the example below:
|
36
|
+
|
37
|
+
# A user can like on users or story
|
38
|
+
|
39
|
+
class User < ActiveRecord::Base
|
40
|
+
acts_as_liker
|
41
|
+
acts_as_likeable
|
42
|
+
end
|
43
|
+
|
44
|
+
class Story < ActiveRecord::Base
|
45
|
+
acts_as_likeable
|
46
|
+
end
|
47
|
+
|
48
|
+
------------------------------------------------------------------------------------------------
|
49
|
+
|
50
|
+
# To like:
|
51
|
+
|
52
|
+
@user = User.find(1)
|
53
|
+
@user2 = User.find(2)
|
54
|
+
@story = Story.find(1)
|
55
|
+
|
56
|
+
@user1.like(@story)
|
57
|
+
@user1.like(@user2)
|
58
|
+
|
59
|
+
------------------------------------------------------------------------------------------------
|
60
|
+
|
61
|
+
# To dis-like:
|
62
|
+
@user = User.find(1)
|
63
|
+
@user2 = User.find(2)
|
64
|
+
@story = Story.find(1)
|
65
|
+
|
66
|
+
@user1.dislike(@story)
|
67
|
+
@user1.dislike(@user2)
|
68
|
+
|
69
|
+
------------------------------------------------------------------------------------------------
|
70
|
+
|
71
|
+
# To check like status:
|
72
|
+
@user = User.find(1)
|
73
|
+
@user3 = User.find(3)
|
74
|
+
@story = Story.find(1)
|
75
|
+
|
76
|
+
@user1.is_like(@story) #checks if @user1 already likes the @story or not -> return true or false
|
77
|
+
@user1.is_like(@user3) #checks if @user1 already likes @user3 or not -> return true or false
|
78
|
+
|
79
|
+
------------------------------------------------------------------------------------------------
|
80
|
+
|
81
|
+
# To read all liked items by a user:
|
82
|
+
|
83
|
+
@user = User.find(1)
|
84
|
+
@user.likings #return all the users and stories which are liked by @user
|
85
|
+
|
86
|
+
------------------------------------------------------------------------------------------------
|
87
|
+
|
88
|
+
# To get all the users who liked any story or user:
|
89
|
+
|
90
|
+
@user = User.find(1)
|
91
|
+
@user2 = User.find(2)
|
92
|
+
@story = Story.find(1)
|
93
|
+
|
94
|
+
@user2.likes #return all the users who liked @user2
|
95
|
+
@story.likes #return all the users who liked @story
|
96
|
+
|
97
|
+
------------------------------------------------------------------------------------------------
|
98
|
+
|
99
|
+
# To get count of all the users who liked any story or user:
|
100
|
+
|
101
|
+
@user = User.find(1)
|
102
|
+
@user2 = User.find(2)
|
103
|
+
@story = Story.find(1)
|
104
|
+
|
105
|
+
@user2.likes.count #return count of all the users who liked @user2
|
106
|
+
@story.likes.count #return count of all the users who liked @story
|
data/Rakefile
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
require 'echoe'
|
4
|
+
|
5
|
+
Echoe.new('likeit', '0.0.1') do |p|
|
6
|
+
p.description = "A generic Liking system."
|
7
|
+
p.url = "https://github.com/gagansharma/likeit"
|
8
|
+
p.author = "Gagan Sharma"
|
9
|
+
p.email = "developer.gagan@gmail.com"
|
10
|
+
p.ignore_pattern = ["tmp/*", "script/*"]
|
11
|
+
p.development_dependencies = []
|
12
|
+
end
|
13
|
+
|
14
|
+
Dir["#{File.dirname(__FILE__)}/tasks/*.take"].sort.each { |ext| load ext }
|
data/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'likeit'
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'rails/generators'
|
2
|
+
require 'rails/generators/migration'
|
3
|
+
|
4
|
+
class LikeitGenerator < Rails::Generators::Base
|
5
|
+
include Rails::Generators::Migration
|
6
|
+
|
7
|
+
def self.source_root
|
8
|
+
@source_root ||= File.join(File.dirname(__FILE__), 'templates')
|
9
|
+
end
|
10
|
+
|
11
|
+
# Implement the required interface for Rails::Generators::Migration.
|
12
|
+
# taken from http://github.com/rails/rails/blob/master/activerecord/lib/generators/active_record.rb
|
13
|
+
def self.next_migration_number(dirname)
|
14
|
+
if ActiveRecord::Base.timestamped_migrations
|
15
|
+
Time.now.utc.strftime("%Y%m%d%H%M%S")
|
16
|
+
else
|
17
|
+
"%.3d" % (current_migration_number(dirname) + 1)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def create_migration_file
|
22
|
+
migration_template 'migration.rb', 'db/migrate/likeit_migration.rb'
|
23
|
+
end
|
24
|
+
|
25
|
+
def create_model
|
26
|
+
template 'model.rb', File.join('app/models', 'like.rb')
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
class LikeitMigration < ActiveRecord::Migration
|
2
|
+
def self.up
|
3
|
+
create_table :likes, :force => true do |t|
|
4
|
+
t.references :liker, :polymorphic => true, :null => false
|
5
|
+
t.references :likeable, :polymorphic => true, :null => false
|
6
|
+
t.timestamps
|
7
|
+
end
|
8
|
+
|
9
|
+
add_index :likes, [:liker_id, :liker_type]
|
10
|
+
add_index :likes, [:likeable_id, :likeable_type]
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.down
|
14
|
+
drop_table :likes
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
class Like < ActiveRecord::Base
|
2
|
+
belongs_to :liker, :polymorphic => true
|
3
|
+
belongs_to :likeable, :polymorphic => true
|
4
|
+
validates_uniqueness_of :liker_id, :scope => [:liker_type, :likeable_id, :likeable_type]
|
5
|
+
|
6
|
+
class << self
|
7
|
+
def likes_on(likeable)
|
8
|
+
where(["likeable_id = ? AND likeable_type = ?", likeable.id, likeable.class.name])
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
data/lib/likeit/liker.rb
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
module Likeit
|
2
|
+
module Liker
|
3
|
+
def self.included(base)
|
4
|
+
base.extend ClassMethods
|
5
|
+
end
|
6
|
+
|
7
|
+
module ClassMethods
|
8
|
+
def acts_as_liker
|
9
|
+
has_many :likings, :as => :liker, :dependent => :destroy, :class_name => "Like"
|
10
|
+
include Likeit::Liker::InstanceMethods
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
module InstanceMethods
|
15
|
+
def like(likeable)
|
16
|
+
self.likings.create(:likeable => likeable)
|
17
|
+
end
|
18
|
+
|
19
|
+
def dislike(likeable)
|
20
|
+
likes = self.likings.likes_on(likeable)
|
21
|
+
likes.destroy_all if likes.present?
|
22
|
+
end
|
23
|
+
|
24
|
+
def is_like(likeable)
|
25
|
+
likes = self.likings.likes_on(likeable)
|
26
|
+
if likes.present?
|
27
|
+
true
|
28
|
+
else
|
29
|
+
false
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
data/lib/likeit.rb
ADDED
data/likeit.gemspec
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = %q{likeit}
|
5
|
+
s.version = "0.0.1"
|
6
|
+
|
7
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
|
8
|
+
s.authors = ["Gagan Sharma"]
|
9
|
+
s.date = %q{2012-07-20}
|
10
|
+
s.description = %q{A generic liking system.}
|
11
|
+
s.email = %q{developer.gagan@gmail.com}
|
12
|
+
s.extra_rdoc_files = ["README.rdoc", "lib/likeit.rb", "lib/likeit/likeable.rb", "lib/likeit/liker.rb", "lib/generators/likeit_generator.rb", "lib/generators/templates/migration.rb", "lib/generators/templates/model.rb"]
|
13
|
+
s.files = ["README.rdoc", "Rakefile", "likeit.gemspec", "init.rb", "lib/likeit.rb", "lib/likeit/likeable.rb", "lib/likeit/liker.rb", "lib/generators/likeit_generator.rb", "lib/generators/templates/migration.rb", "lib/generators/templates/model.rb"]
|
14
|
+
s.homepage = %q{https://github.com/gagansharma/likeit}
|
15
|
+
s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Likeit", "--main", "README.rdoc"]
|
16
|
+
s.require_paths = ["lib"]
|
17
|
+
s.rubyforge_project = %q{likeit}
|
18
|
+
s.rubygems_version = %q{1.3.7}
|
19
|
+
s.summary = %q{A generic Liking system.}
|
20
|
+
|
21
|
+
if s.respond_to? :specification_version then
|
22
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
23
|
+
s.specification_version = 1
|
24
|
+
|
25
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
26
|
+
else
|
27
|
+
end
|
28
|
+
else
|
29
|
+
end
|
30
|
+
end
|
metadata
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: likeit
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Gagan Sharma
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2012-07-20 00:00:00 Z
|
19
|
+
dependencies: []
|
20
|
+
|
21
|
+
description: A generic liking system.
|
22
|
+
email: developer.gagan@gmail.com
|
23
|
+
executables: []
|
24
|
+
|
25
|
+
extensions: []
|
26
|
+
|
27
|
+
extra_rdoc_files:
|
28
|
+
- README.rdoc
|
29
|
+
- lib/likeit.rb
|
30
|
+
- lib/likeit/likeable.rb
|
31
|
+
- lib/likeit/liker.rb
|
32
|
+
- lib/generators/likeit_generator.rb
|
33
|
+
- lib/generators/templates/migration.rb
|
34
|
+
- lib/generators/templates/model.rb
|
35
|
+
files:
|
36
|
+
- README.rdoc
|
37
|
+
- Rakefile
|
38
|
+
- likeit.gemspec
|
39
|
+
- init.rb
|
40
|
+
- lib/likeit.rb
|
41
|
+
- lib/likeit/likeable.rb
|
42
|
+
- lib/likeit/liker.rb
|
43
|
+
- lib/generators/likeit_generator.rb
|
44
|
+
- lib/generators/templates/migration.rb
|
45
|
+
- lib/generators/templates/model.rb
|
46
|
+
homepage: https://github.com/gagansharma/likeit
|
47
|
+
licenses: []
|
48
|
+
|
49
|
+
post_install_message:
|
50
|
+
rdoc_options:
|
51
|
+
- --line-numbers
|
52
|
+
- --inline-source
|
53
|
+
- --title
|
54
|
+
- Likeit
|
55
|
+
- --main
|
56
|
+
- README.rdoc
|
57
|
+
require_paths:
|
58
|
+
- lib
|
59
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
60
|
+
none: false
|
61
|
+
requirements:
|
62
|
+
- - ">="
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
hash: 3
|
65
|
+
segments:
|
66
|
+
- 0
|
67
|
+
version: "0"
|
68
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
69
|
+
none: false
|
70
|
+
requirements:
|
71
|
+
- - ">="
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
hash: 11
|
74
|
+
segments:
|
75
|
+
- 1
|
76
|
+
- 2
|
77
|
+
version: "1.2"
|
78
|
+
requirements: []
|
79
|
+
|
80
|
+
rubyforge_project: likeit
|
81
|
+
rubygems_version: 1.8.15
|
82
|
+
signing_key:
|
83
|
+
specification_version: 1
|
84
|
+
summary: A generic Liking system.
|
85
|
+
test_files: []
|
86
|
+
|