acts_as_likeable 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +18 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +4 -0
- data/Rakefile +2 -0
- data/acts_as_likeable.gemspec +19 -0
- data/lib/acts_as_likeable/extenders/likeable.rb +24 -0
- data/lib/acts_as_likeable/extenders/liker.rb +24 -0
- data/lib/acts_as_likeable/likeable.rb +87 -0
- data/lib/acts_as_likeable/liker.rb +46 -0
- data/lib/acts_as_likeable/version.rb +3 -0
- data/lib/acts_as_likeable.rb +15 -0
- data/lib/generators/acts_as_likeable/migration/migration_generator.rb +32 -0
- data/lib/generators/acts_as_likeable/migration/templates/active_record/like.rb +11 -0
- data/lib/generators/acts_as_likeable/migration/templates/active_record/migration.rb +15 -0
- metadata +82 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Filvo Interactive
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
data/Rakefile
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/acts_as_likeable/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Filvo Interactive"]
|
6
|
+
gem.email = ["info@filvo.com"]
|
7
|
+
gem.description = %q{Acts as Likeable}
|
8
|
+
gem.summary = %q{Add Like functionality similar to Facebook}
|
9
|
+
gem.homepage = "https://github.com/filvo/acts_as_likeable"
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "acts_as_likeable"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = ActsAsLikeable::VERSION
|
17
|
+
|
18
|
+
gem.add_development_dependency 'rails', '~> 3.0'
|
19
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module ActsAsLikeable
|
2
|
+
module Extenders
|
3
|
+
|
4
|
+
module Likeable
|
5
|
+
|
6
|
+
def likeable?
|
7
|
+
false
|
8
|
+
end
|
9
|
+
|
10
|
+
def acts_as_likeable
|
11
|
+
require 'acts_as_likeable/likeable'
|
12
|
+
include ActsAsLikeable::Likeable
|
13
|
+
|
14
|
+
class_eval do
|
15
|
+
def self.likeable?
|
16
|
+
true
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module ActsAsLikeable
|
2
|
+
module Extenders
|
3
|
+
|
4
|
+
module Liker
|
5
|
+
|
6
|
+
def liker?
|
7
|
+
false
|
8
|
+
end
|
9
|
+
|
10
|
+
def acts_as_liker(*args)
|
11
|
+
require 'acts_as_likeable/liker'
|
12
|
+
include ActsAsLikeable::Liker
|
13
|
+
|
14
|
+
class_eval do
|
15
|
+
def self.liker?
|
16
|
+
true
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,87 @@
|
|
1
|
+
module ActsAsLikeable
|
2
|
+
module Likeable
|
3
|
+
|
4
|
+
def self.included base
|
5
|
+
base.class_eval do
|
6
|
+
belongs_to :likeable, :polymorphic => true
|
7
|
+
has_many :likes, :as => :likeable, :dependent => :delete_all do
|
8
|
+
def likers
|
9
|
+
includes(:liker).map(&:liker)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def default_conditions
|
16
|
+
{
|
17
|
+
:likeable_id => self.id,
|
18
|
+
:likeable_type => self.class.base_class.name.to_s
|
19
|
+
}
|
20
|
+
end
|
21
|
+
|
22
|
+
def liked_by liker
|
23
|
+
like :liker => liker
|
24
|
+
end
|
25
|
+
|
26
|
+
def unliked_by liker
|
27
|
+
self.unlike :liker => liker
|
28
|
+
end
|
29
|
+
|
30
|
+
def liked_by? liker
|
31
|
+
return false if liker.nil?
|
32
|
+
_likes_ = find_likes :liker_id => liker.id, :liker_type => liker.class.name
|
33
|
+
_likes_.count > 0
|
34
|
+
end
|
35
|
+
|
36
|
+
|
37
|
+
def like args = {}
|
38
|
+
return nil if args[:liker].nil?
|
39
|
+
|
40
|
+
# find the like
|
41
|
+
_likes_ = find_likes({
|
42
|
+
:liker_id => args[:liker].id,
|
43
|
+
:liker_type => args[:liker].class.name
|
44
|
+
})
|
45
|
+
|
46
|
+
if _likes_.count == 0
|
47
|
+
_like_ = Like.new(
|
48
|
+
:likeable => self,
|
49
|
+
:liker => args[:liker]
|
50
|
+
)
|
51
|
+
else
|
52
|
+
_like_ = _likes_.first
|
53
|
+
end
|
54
|
+
|
55
|
+
if _like_.save
|
56
|
+
update_likes_counter
|
57
|
+
_like_
|
58
|
+
else
|
59
|
+
nil
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
def unlike args = {}
|
64
|
+
return false if args[:liker].nil?
|
65
|
+
_likes_ = find_likes({
|
66
|
+
:liker_id => args[:liker].id,
|
67
|
+
:liker_type => args[:liker].class.name
|
68
|
+
})
|
69
|
+
return true if _likes_.count == 0
|
70
|
+
_likes_.each(&:destroy)
|
71
|
+
update_likes_counter
|
72
|
+
true
|
73
|
+
end
|
74
|
+
|
75
|
+
def update_likes_counter
|
76
|
+
if self.respond_to?(:cached_likes_total=)
|
77
|
+
self.class.where(:id => self.id).update_all(:cached_likes_total => self.likes.count)
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
def find_likes extra_conditions = {}
|
82
|
+
self.likes.where(extra_conditions)
|
83
|
+
end
|
84
|
+
|
85
|
+
|
86
|
+
end
|
87
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
module ActsAsLikeable
|
2
|
+
module Liker
|
3
|
+
|
4
|
+
def self.included(base)
|
5
|
+
base.class_eval do
|
6
|
+
belongs_to :liker, :polymorphic => true
|
7
|
+
has_many :likes, :as => :liker, :dependent => :delete_all do
|
8
|
+
def likeables
|
9
|
+
includes(:likeable).map(&:likeable)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
|
16
|
+
def liked model=nil
|
17
|
+
like :likeable => model
|
18
|
+
end
|
19
|
+
|
20
|
+
def unliked model=nil
|
21
|
+
unlike :likeable => model
|
22
|
+
end
|
23
|
+
|
24
|
+
def liked? likeable
|
25
|
+
__likes__ = find_likes(:likeable_id => likeable.id, :likeable_type => likeable.class.name)
|
26
|
+
__likes__.size > 0
|
27
|
+
end
|
28
|
+
|
29
|
+
|
30
|
+
# saving
|
31
|
+
def like args = {}
|
32
|
+
return nil if args[:likeable].nil?
|
33
|
+
args[:likeable].like args.merge({:liker => self})
|
34
|
+
end
|
35
|
+
|
36
|
+
def unlike args = {}
|
37
|
+
return false if args[:likeable].nil?
|
38
|
+
args[:likeable].unlike args.merge({:liker => self})
|
39
|
+
end
|
40
|
+
|
41
|
+
def find_likes(extra_conditions = {})
|
42
|
+
self.likes.where(extra_conditions)
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'active_record'
|
2
|
+
require 'active_support/inflector'
|
3
|
+
|
4
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
5
|
+
|
6
|
+
module ActsAsLikeable
|
7
|
+
|
8
|
+
if defined?(ActiveRecord::Base)
|
9
|
+
require 'acts_as_likeable/extenders/likeable'
|
10
|
+
require 'acts_as_likeable/extenders/liker'
|
11
|
+
ActiveRecord::Base.extend ActsAsLikeable::Extenders::Likeable
|
12
|
+
ActiveRecord::Base.extend ActsAsLikeable::Extenders::Liker
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'rails/generators/migration'
|
2
|
+
|
3
|
+
module ActsAsLikeable
|
4
|
+
class MigrationGenerator < Rails::Generators::Base
|
5
|
+
include Rails::Generators::Migration
|
6
|
+
|
7
|
+
desc "Generates migration for acts_as_likeable (likes table)"
|
8
|
+
|
9
|
+
def self.orm
|
10
|
+
Rails::Generators.options[:rails][:orm]
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.source_root
|
14
|
+
File.join(File.dirname(__FILE__), 'templates', (orm.to_s unless orm.class.eql?(String)) )
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.orm_has_migration?
|
18
|
+
[:active_record].include? orm
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.next_migration_number(path)
|
22
|
+
Time.now.strftime("%Y%m%d%H%M%S")
|
23
|
+
end
|
24
|
+
|
25
|
+
def create_migration_file
|
26
|
+
if self.class.orm_has_migration?
|
27
|
+
migration_template 'migration.rb', 'db/migrate/acts_as_likeable_migration'
|
28
|
+
copy_file 'like.rb', 'app/models/like.rb'
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
class Like < ::ActiveRecord::Base
|
2
|
+
attr_accessible :likeable_id, :likeable_type,
|
3
|
+
:liker_id, :liker_type,
|
4
|
+
:likeable, :liker
|
5
|
+
|
6
|
+
belongs_to :likeable, :polymorphic => true
|
7
|
+
belongs_to :liker, :polymorphic => true
|
8
|
+
|
9
|
+
validates_presence_of :likeable_id
|
10
|
+
validates_presence_of :liker_id
|
11
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
class ActsAsLikeableMigration < ActiveRecord::Migration
|
2
|
+
|
3
|
+
def self.up
|
4
|
+
create_table :likes do |t|
|
5
|
+
t.references :likeable, :polymorphic => true
|
6
|
+
t.references :liker, :polymorphic => true
|
7
|
+
|
8
|
+
t.timestamps
|
9
|
+
end
|
10
|
+
|
11
|
+
add_index :likes, [:likeable_id, :likeable_type]
|
12
|
+
add_index :likes, [:liker_id, :liker_type]
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
metadata
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: acts_as_likeable
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Filvo Interactive
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-08-06 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rails
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '3.0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '3.0'
|
30
|
+
description: Acts as Likeable
|
31
|
+
email:
|
32
|
+
- info@filvo.com
|
33
|
+
executables: []
|
34
|
+
extensions: []
|
35
|
+
extra_rdoc_files: []
|
36
|
+
files:
|
37
|
+
- .gitignore
|
38
|
+
- Gemfile
|
39
|
+
- LICENSE
|
40
|
+
- README.md
|
41
|
+
- Rakefile
|
42
|
+
- acts_as_likeable.gemspec
|
43
|
+
- lib/acts_as_likeable.rb
|
44
|
+
- lib/acts_as_likeable/extenders/likeable.rb
|
45
|
+
- lib/acts_as_likeable/extenders/liker.rb
|
46
|
+
- lib/acts_as_likeable/likeable.rb
|
47
|
+
- lib/acts_as_likeable/liker.rb
|
48
|
+
- lib/acts_as_likeable/version.rb
|
49
|
+
- lib/generators/acts_as_likeable/migration/migration_generator.rb
|
50
|
+
- lib/generators/acts_as_likeable/migration/templates/active_record/like.rb
|
51
|
+
- lib/generators/acts_as_likeable/migration/templates/active_record/migration.rb
|
52
|
+
homepage: https://github.com/filvo/acts_as_likeable
|
53
|
+
licenses: []
|
54
|
+
post_install_message:
|
55
|
+
rdoc_options: []
|
56
|
+
require_paths:
|
57
|
+
- lib
|
58
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
59
|
+
none: false
|
60
|
+
requirements:
|
61
|
+
- - ! '>='
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: '0'
|
64
|
+
segments:
|
65
|
+
- 0
|
66
|
+
hash: 3733592978116303062
|
67
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
68
|
+
none: false
|
69
|
+
requirements:
|
70
|
+
- - ! '>='
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: '0'
|
73
|
+
segments:
|
74
|
+
- 0
|
75
|
+
hash: 3733592978116303062
|
76
|
+
requirements: []
|
77
|
+
rubyforge_project:
|
78
|
+
rubygems_version: 1.8.24
|
79
|
+
signing_key:
|
80
|
+
specification_version: 3
|
81
|
+
summary: Add Like functionality similar to Facebook
|
82
|
+
test_files: []
|