simple_like 0.2.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/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +60 -0
- data/Rakefile +2 -0
- data/lib/generators/simple_like_generator.rb +18 -0
- data/lib/generators/templates/create_likes.rb +14 -0
- data/lib/generators/templates/like.rb +5 -0
- data/lib/simple_like/target.rb +16 -0
- data/lib/simple_like/user.rb +37 -0
- data/lib/simple_like/version.rb +3 -0
- data/lib/simple_like.rb +6 -0
- data/simple_like.gemspec +17 -0
- metadata +58 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Nick Pellant
|
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
@@ -0,0 +1,60 @@
|
|
1
|
+
# What is SimpleLike?
|
2
|
+
|
3
|
+
SimpleLike is a tiny gem that allows you to add simple 'like' button functionality to existing models.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'simple_like'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install simple_like
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
To make a model likeable, simply extend/include the SimpleLike module.
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
class Article
|
27
|
+
extend SimpleLike::Target
|
28
|
+
end
|
29
|
+
```
|
30
|
+
|
31
|
+
...and to enable a user to have the ability to like.
|
32
|
+
|
33
|
+
```ruby
|
34
|
+
class User
|
35
|
+
extend SimpleLike::User
|
36
|
+
end
|
37
|
+
```
|
38
|
+
|
39
|
+
### Methods
|
40
|
+
|
41
|
+
There are a few handy methods available to you.
|
42
|
+
|
43
|
+
```ruby
|
44
|
+
article = Article.first
|
45
|
+
|
46
|
+
# Like an object
|
47
|
+
current_user.like! article
|
48
|
+
# Check whether user has already liked object
|
49
|
+
current_user.like? article
|
50
|
+
# Destroy an existing like
|
51
|
+
current_user.destroy_like! article
|
52
|
+
```
|
53
|
+
|
54
|
+
## Contributing
|
55
|
+
|
56
|
+
1. Fork it
|
57
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
58
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
59
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
60
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'rails/generators/migration'
|
2
|
+
|
3
|
+
class SimpleLikeGenerator < Rails::Generators::Base
|
4
|
+
include Rails::Generators::Migration
|
5
|
+
|
6
|
+
def self.source_root
|
7
|
+
@_simple_like_source_root ||= File.expand_path("../templates", __FILE__)
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.next_migration_number(path)
|
11
|
+
Time.now.utc.strftime("%Y%m%d%H%M%S")
|
12
|
+
end
|
13
|
+
|
14
|
+
def create_model_file
|
15
|
+
template "like.rb", "app/models/like.rb"
|
16
|
+
migration_template "create_likes.rb", "db/migrate/create_likes.rb"
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
class CreateLikes < ActiveRecord::Migration
|
2
|
+
def change
|
3
|
+
create_table :likes do |t|
|
4
|
+
t.references :likeable, polymorphic: true
|
5
|
+
t.references :author, polymorphic: true
|
6
|
+
t.timestamps
|
7
|
+
|
8
|
+
add_index :likes, :likeable_type
|
9
|
+
add_index :likes, :likeable_id
|
10
|
+
add_index :likes, :author_type
|
11
|
+
add_index :likes, :author_id
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module SimpleLike
|
2
|
+
module Target
|
3
|
+
def self.extended(model_class)
|
4
|
+
model_class.instance_eval do
|
5
|
+
extend Target
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.included(model_class)
|
10
|
+
model_class.extend Target
|
11
|
+
end
|
12
|
+
|
13
|
+
IsLikeable = true
|
14
|
+
has_many :likes, { as: :likeable, dependant: :destroy }
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
module SimpleLike
|
2
|
+
module User
|
3
|
+
def self.extended(model_class)
|
4
|
+
model_class.instance_eval do
|
5
|
+
extend User
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.included(model_class)
|
10
|
+
model_class.extend User
|
11
|
+
end
|
12
|
+
|
13
|
+
|
14
|
+
def like!(instance)
|
15
|
+
if is_likeable?(instance)
|
16
|
+
instance.likes << Like.new(author: self) if !like?(instance)
|
17
|
+
instance.save!
|
18
|
+
else
|
19
|
+
# Return exception
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def like?(instance)
|
24
|
+
instance.likes.where(author: self).present?
|
25
|
+
end
|
26
|
+
|
27
|
+
def destroy_like!(instance)
|
28
|
+
likes = instance.likes.where(author: self)
|
29
|
+
likes.destroy_all if likes.present?
|
30
|
+
end
|
31
|
+
|
32
|
+
private
|
33
|
+
def is_likeable?(instance)
|
34
|
+
instance.const_defined?(:IsLikeable)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
data/lib/simple_like.rb
ADDED
data/simple_like.gemspec
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/simple_like/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Nick Pellant"]
|
6
|
+
gem.email = ["contact@nickpellant.com"]
|
7
|
+
gem.description = %q{SimpleLike adds like functionality to existing models.}
|
8
|
+
gem.summary = %q{Like functionality}
|
9
|
+
gem.homepage = ""
|
10
|
+
|
11
|
+
gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
12
|
+
gem.files = `git ls-files`.split("\n")
|
13
|
+
gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
14
|
+
gem.name = "simple_like"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = SimpleLike::VERSION
|
17
|
+
end
|
metadata
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: simple_like
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Nick Pellant
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-06-21 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: SimpleLike adds like functionality to existing models.
|
15
|
+
email:
|
16
|
+
- contact@nickpellant.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- .gitignore
|
22
|
+
- Gemfile
|
23
|
+
- LICENSE
|
24
|
+
- README.md
|
25
|
+
- Rakefile
|
26
|
+
- lib/generators/simple_like_generator.rb
|
27
|
+
- lib/generators/templates/create_likes.rb
|
28
|
+
- lib/generators/templates/like.rb
|
29
|
+
- lib/simple_like.rb
|
30
|
+
- lib/simple_like/target.rb
|
31
|
+
- lib/simple_like/user.rb
|
32
|
+
- lib/simple_like/version.rb
|
33
|
+
- simple_like.gemspec
|
34
|
+
homepage: ''
|
35
|
+
licenses: []
|
36
|
+
post_install_message:
|
37
|
+
rdoc_options: []
|
38
|
+
require_paths:
|
39
|
+
- lib
|
40
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
47
|
+
none: false
|
48
|
+
requirements:
|
49
|
+
- - ! '>='
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: '0'
|
52
|
+
requirements: []
|
53
|
+
rubyforge_project:
|
54
|
+
rubygems_version: 1.8.11
|
55
|
+
signing_key:
|
56
|
+
specification_version: 3
|
57
|
+
summary: Like functionality
|
58
|
+
test_files: []
|