acts_as_liked 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +14 -0
- data/.rspec +3 -0
- data/.travis.yml +5 -0
- data/Gemfile +4 -0
- data/Guardfile +33 -0
- data/LICENSE.txt +22 -0
- data/README.md +74 -0
- data/Rakefile +8 -0
- data/acts_as_liked.gemspec +28 -0
- data/lib/acts_as_liked.rb +12 -0
- data/lib/acts_as_liked/likeable.rb +30 -0
- data/lib/acts_as_liked/liker.rb +37 -0
- data/lib/acts_as_liked/version.rb +3 -0
- data/lib/generators/acts_as_liked/migration_generator.rb +23 -0
- data/lib/generators/acts_as_liked/templates/migration.rb +17 -0
- data/lib/generators/acts_as_liked/templates/model.rb +4 -0
- data/lib/generators/acts_as_liked_generator.rb +25 -0
- data/lib/generators/templates/migration.rb +17 -0
- data/lib/generators/templates/model.rb +4 -0
- data/spec/generator_spec.rb +35 -0
- data/spec/likeable_spec.rb +45 -0
- data/spec/liker_spec.rb +91 -0
- data/spec/spec_helper.rb +101 -0
- data/spec/support/like_macros.rb +5 -0
- data/spec/support/matchers/have_many.rb +18 -0
- data/spec/support/models.rb +12 -0
- data/spec/support/schema.rb +21 -0
- metadata +191 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 89b280555a74b3018003f37dc97b717ade203e5e
|
4
|
+
data.tar.gz: 5de17db8bf816939f79248faaca154593e3db576
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 1217fe282fc125bdc937b28812229d0b1fc1e62c1c81a1cc6cc0ccfc06e1a4d8099ac7e591c755133c5f3df25dd6fbdcfe297bb3bcf88d239623a17eabecb94b
|
7
|
+
data.tar.gz: 161324021f14f557a7c5a552fe89b92cc3e516f4d986fadec33f9792a6a8fdf1cafeb19ca2ddd70e859c5f13a6ceb9c5ec937af81e3a1c21879f4dba9f45b408
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Guardfile
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
# A sample Guardfile
|
2
|
+
# More info at https://github.com/guard/guard#readme
|
3
|
+
|
4
|
+
# Note: The cmd option is now required due to the increasing number of ways
|
5
|
+
# rspec may be run, below are examples of the most common uses.
|
6
|
+
# * bundler: 'bundle exec rspec'
|
7
|
+
# * bundler binstubs: 'bin/rspec'
|
8
|
+
# * spring: 'bin/rsspec' (This will use spring if running and you have
|
9
|
+
# installed the spring binstubs per the docs)
|
10
|
+
# * zeus: 'zeus rspec' (requires the server to be started separetly)
|
11
|
+
# * 'just' rspec: 'rspec'
|
12
|
+
guard :rspec, cmd: 'bundle exec rspec' do
|
13
|
+
watch(%r{^spec/.+_spec\.rb$})
|
14
|
+
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
|
15
|
+
watch('spec/spec_helper.rb') { "spec" }
|
16
|
+
|
17
|
+
# Rails example
|
18
|
+
watch(%r{^app/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
|
19
|
+
watch(%r{^app/(.*)(\.erb|\.haml|\.slim)$}) { |m| "spec/#{m[1]}#{m[2]}_spec.rb" }
|
20
|
+
watch(%r{^app/controllers/(.+)_(controller)\.rb$}) { |m| ["spec/routing/#{m[1]}_routing_spec.rb", "spec/#{m[2]}s/#{m[1]}_#{m[2]}_spec.rb", "spec/acceptance/#{m[1]}_spec.rb"] }
|
21
|
+
watch(%r{^spec/support/(.+)\.rb$}) { "spec" }
|
22
|
+
watch('config/routes.rb') { "spec/routing" }
|
23
|
+
watch('app/controllers/application_controller.rb') { "spec/controllers" }
|
24
|
+
watch('spec/rails_helper.rb') { "spec" }
|
25
|
+
|
26
|
+
# Capybara features specs
|
27
|
+
watch(%r{^app/views/(.+)/.*\.(erb|haml|slim)$}) { |m| "spec/features/#{m[1]}_spec.rb" }
|
28
|
+
|
29
|
+
# Turnip features and steps
|
30
|
+
watch(%r{^spec/acceptance/(.+)\.feature$})
|
31
|
+
watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$}) { |m| Dir[File.join("**/#{m[1]}.feature")][0] || 'spec/acceptance' }
|
32
|
+
end
|
33
|
+
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Sung Won Cho
|
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,74 @@
|
|
1
|
+
# ActsAsLiked [![Build Status](https://travis-ci.org/sungwoncho/acts_as_liked.svg?branch=master)](https://travis-ci.org/sungwoncho/acts_as_liked)
|
2
|
+
|
3
|
+
Add like feature to any Active Record models through polymorphic association. Designate any models to act as a `Liker` or `Likeable`.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'acts_as_liked'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Run generator:
|
18
|
+
|
19
|
+
$ rails generate acts_as_liked
|
20
|
+
|
21
|
+
And don't forget to migrate your database
|
22
|
+
|
23
|
+
$ rake db:migrate
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
### Likeable models
|
28
|
+
|
29
|
+
Add `acts_as_likeable` to any models, and its instances can be liked by other models.
|
30
|
+
|
31
|
+
```ruby
|
32
|
+
class Food < ActiveRecord::Base
|
33
|
+
acts_as_likeable
|
34
|
+
end
|
35
|
+
```
|
36
|
+
|
37
|
+
### Liker models
|
38
|
+
|
39
|
+
Add `acts_as_liker` to any models, and it can like instances of other models.
|
40
|
+
|
41
|
+
```ruby
|
42
|
+
class Person < ActiveRecord::Base
|
43
|
+
acts_as_liker
|
44
|
+
end
|
45
|
+
```
|
46
|
+
|
47
|
+
### API
|
48
|
+
|
49
|
+
Liker:
|
50
|
+
|
51
|
+
```ruby
|
52
|
+
# Creates a new like record for @user, and @post
|
53
|
+
@user.like(@post)
|
54
|
+
|
55
|
+
# Destroys the like record
|
56
|
+
@user.unlike(@post)
|
57
|
+
|
58
|
+
# Check if @user has liked @post
|
59
|
+
@user.liked?(@post) # Check if the user has liked the post
|
60
|
+
```
|
61
|
+
|
62
|
+
Likeable:
|
63
|
+
|
64
|
+
```ruby
|
65
|
+
# Count the number of likes of @post
|
66
|
+
@post.like_count
|
67
|
+
|
68
|
+
# Check if @post is liked by @user
|
69
|
+
@post.liked_by?(@user)
|
70
|
+
```
|
71
|
+
|
72
|
+
## Contributing
|
73
|
+
|
74
|
+
Issues and pull reqeusts are welcomed.
|
data/Rakefile
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'acts_as_liked/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "acts_as_liked"
|
8
|
+
spec.version = ActsAsLiked::VERSION
|
9
|
+
spec.authors = ["Sung Won Cho"]
|
10
|
+
spec.email = ["mikeswcho@gmail.com"]
|
11
|
+
spec.summary = %q{Add like feature to any Active Record models}
|
12
|
+
spec.homepage = "https://github.com/sungwoncho/acts_as_liked"
|
13
|
+
spec.license = "MIT"
|
14
|
+
|
15
|
+
spec.files = `git ls-files -z`.split("\x0")
|
16
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
17
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.add_development_dependency "bundler", "~> 1.7"
|
21
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
22
|
+
spec.add_development_dependency "rspec"
|
23
|
+
spec.add_development_dependency "guard"
|
24
|
+
spec.add_development_dependency "guard-rspec"
|
25
|
+
spec.add_development_dependency "sqlite3"
|
26
|
+
spec.add_development_dependency "activerecord"
|
27
|
+
spec.add_development_dependency "generator_spec"
|
28
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
require "acts_as_liked/version"
|
2
|
+
require "active_support"
|
3
|
+
|
4
|
+
module ActsAsLiked
|
5
|
+
require 'acts_as_liked/likeable.rb'
|
6
|
+
require 'acts_as_liked/liker.rb'
|
7
|
+
end
|
8
|
+
|
9
|
+
ActiveSupport.on_load(:active_record) do
|
10
|
+
include ActsAsLiked::Likeable
|
11
|
+
include ActsAsLiked::Liker
|
12
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module ActsAsLiked
|
2
|
+
module Likeable
|
3
|
+
|
4
|
+
def self.included(base)
|
5
|
+
base.extend ClassMethods
|
6
|
+
end
|
7
|
+
|
8
|
+
module ClassMethods
|
9
|
+
def acts_as_likeable
|
10
|
+
has_many :likes, as: :likeable, dependent: :destroy
|
11
|
+
include ActsAsLiked::Likeable::InstanceMethods
|
12
|
+
end
|
13
|
+
|
14
|
+
def likeable?
|
15
|
+
true
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
module InstanceMethods
|
20
|
+
def like_count
|
21
|
+
likes.count
|
22
|
+
end
|
23
|
+
|
24
|
+
def liked_by?(liker)
|
25
|
+
likes.find_by(liker_id: liker.id, liker_type: liker.class.base_class.name).present?
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
module ActsAsLiked
|
2
|
+
module Liker
|
3
|
+
|
4
|
+
def self.included(base)
|
5
|
+
base.extend ClassMethods
|
6
|
+
end
|
7
|
+
|
8
|
+
module ClassMethods
|
9
|
+
def acts_as_liker
|
10
|
+
has_many :likes, as: :liker, dependent: :destroy
|
11
|
+
include ActsAsLiked::Liker::InstanceMethods
|
12
|
+
end
|
13
|
+
|
14
|
+
def liker?
|
15
|
+
true
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
module InstanceMethods
|
20
|
+
def liked?(likeable)
|
21
|
+
likes.find_by(likeable_id: likeable.id, likeable_type: likeable.class.base_class.name).present?
|
22
|
+
end
|
23
|
+
|
24
|
+
def like(likeable)
|
25
|
+
unless self.liked?(likeable)
|
26
|
+
likes.create(likeable_id: likeable.id, likeable_type: likeable.class.base_class.name)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def unlike(likeable)
|
31
|
+
like_record = likes.find_by(likeable_id: likeable.id, likeable_type: likeable.class.base_class.name)
|
32
|
+
like_record.try(:destroy)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'rails/generators'
|
2
|
+
require 'rails/generators/migration'
|
3
|
+
|
4
|
+
class MigrationGenerator < Rails::Generators::Base
|
5
|
+
include Rails::Generators::Migration
|
6
|
+
|
7
|
+
def self.source_root
|
8
|
+
File.join(File.dirname(__FILE__), 'templates')
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.next_migration_number(path)
|
12
|
+
Time.now.utc.strftime("%Y%m%d%H%M%S")
|
13
|
+
end
|
14
|
+
|
15
|
+
def create_migration_file
|
16
|
+
migration_template 'migration.rb', 'db/migrate/acts_as_liked_migration.rb'
|
17
|
+
end
|
18
|
+
|
19
|
+
def create_model
|
20
|
+
template 'model.rb', File.join('app/models', 'like.rb')
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
class ActsAsLikedMigration < ActiveRecord::Migration
|
2
|
+
def self.up
|
3
|
+
create_table :likes do |t|
|
4
|
+
t.references :likeable, polymorphic: true
|
5
|
+
t.references :liker, polymorphic: :true
|
6
|
+
|
7
|
+
t.timestamps
|
8
|
+
end
|
9
|
+
|
10
|
+
add_index :likes, [:likeable_id, :likeable_type]
|
11
|
+
add_index :likes, [:liker_id, :liker_type]
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.down
|
15
|
+
drop_table :likes
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'rails/generators'
|
2
|
+
require 'rails/generators/migration'
|
3
|
+
|
4
|
+
module ActsAsLiked
|
5
|
+
class ActsAsLikedGenerator < Rails::Generators::Base
|
6
|
+
include Rails::Generators::Migration
|
7
|
+
|
8
|
+
def self.source_root
|
9
|
+
File.join(File.dirname(__FILE__), 'templates')
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.next_migration_number(path)
|
13
|
+
Time.now.utc.strftime("%Y%m%d%H%M%S")
|
14
|
+
end
|
15
|
+
|
16
|
+
def create_migration_file
|
17
|
+
migration_template 'migration.rb', 'db/migrate/acts_as_liked_migration.rb'
|
18
|
+
end
|
19
|
+
|
20
|
+
def create_model
|
21
|
+
template 'model.rb', File.join('app/models', 'like.rb')
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
class ActsAsLikedMigration < ActiveRecord::Migration
|
2
|
+
def self.up
|
3
|
+
create_table :likes do |t|
|
4
|
+
t.references :likeable, polymorphic: true
|
5
|
+
t.references :liker, polymorphic: :true
|
6
|
+
|
7
|
+
t.timestamps
|
8
|
+
end
|
9
|
+
|
10
|
+
add_index :likes, [:likeable_id, :likeable_type]
|
11
|
+
add_index :likes, [:liker_id, :liker_type]
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.down
|
15
|
+
drop_table :likes
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'generator_spec/test_case'
|
2
|
+
require 'generators/acts_as_liked/migration_generator'
|
3
|
+
|
4
|
+
describe MigrationGenerator, type: :generator do
|
5
|
+
include GeneratorSpec::TestCase
|
6
|
+
|
7
|
+
destination File.expand_path("../tmp", File.dirname(__FILE__))
|
8
|
+
|
9
|
+
before :all do
|
10
|
+
prepare_destination
|
11
|
+
run_generator
|
12
|
+
end
|
13
|
+
|
14
|
+
after :all do
|
15
|
+
FileUtils.rm_r Dir.glob('tmp/*') # Clean up tmp after test
|
16
|
+
end
|
17
|
+
|
18
|
+
# it "creates a migration" do
|
19
|
+
# assert_file "db/migrate/#{Regexp.new([0-9]*)}_acts_as_likeable_migration.rb"
|
20
|
+
# end
|
21
|
+
|
22
|
+
it "creates model" do
|
23
|
+
assert_file "app/models/like.rb"
|
24
|
+
end
|
25
|
+
|
26
|
+
specify {
|
27
|
+
expect(destination_root).to have_structure {
|
28
|
+
directory "db" do
|
29
|
+
directory "migrate" do
|
30
|
+
migration "acts_as_liked_migration"
|
31
|
+
end
|
32
|
+
end
|
33
|
+
}
|
34
|
+
}
|
35
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Food, focus: true do
|
4
|
+
|
5
|
+
let(:charlie) { User.create(name: "Charlie") }
|
6
|
+
let(:cheese) { Food.create(name: "cheese") }
|
7
|
+
|
8
|
+
it "should be likeable" do
|
9
|
+
expect(Food).to be_likeable
|
10
|
+
end
|
11
|
+
|
12
|
+
describe "Association" do
|
13
|
+
it "should have many likes" do
|
14
|
+
association = Food.reflect_on_association(:likes)
|
15
|
+
expect(association.macro).to eq :has_many
|
16
|
+
end
|
17
|
+
|
18
|
+
it "should have many likes with dependent destroy" do
|
19
|
+
association = Food.reflect_on_association(:likes)
|
20
|
+
expect(association.options).to include :dependent => :destroy
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
describe "#like_count" do
|
25
|
+
it "should count likes" do
|
26
|
+
create_like(charlie, cheese)
|
27
|
+
expect(cheese.like_count).to eq 1
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
describe "#liked_by?" do
|
32
|
+
context "when liker has liked likeable" do
|
33
|
+
it "should return true" do
|
34
|
+
create_like(charlie, cheese)
|
35
|
+
expect(cheese.liked_by?(charlie)).to be true
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
context "when liker hasn't liked likeable" do
|
40
|
+
it "should return false" do
|
41
|
+
expect(cheese.liked_by?(charlie)).to be false
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
data/spec/liker_spec.rb
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe User do
|
4
|
+
|
5
|
+
let(:charlie) { User.create(name: "Charlie") }
|
6
|
+
let(:cheese) { Food.create(name: "cheese") }
|
7
|
+
|
8
|
+
it "should be a liker" do
|
9
|
+
expect(User).to be_liker
|
10
|
+
end
|
11
|
+
|
12
|
+
describe "instance methods" do
|
13
|
+
it "should be defined" do
|
14
|
+
expect(charlie).to respond_to(:like)
|
15
|
+
expect(charlie).to respond_to(:unlike)
|
16
|
+
expect(charlie).to respond_to(:liked?)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
describe "Association" do
|
21
|
+
it "should have many likes" do
|
22
|
+
association = User.reflect_on_association(:likes)
|
23
|
+
expect(association.macro).to eq :has_many
|
24
|
+
end
|
25
|
+
|
26
|
+
it "should have many likes with dependent destroy" do
|
27
|
+
association = User.reflect_on_association(:likes)
|
28
|
+
expect(association.options).to include :dependent => :destroy
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
describe "#liked?" do
|
33
|
+
context "when liker has liked likedable" do
|
34
|
+
it "should return true" do
|
35
|
+
create_like(charlie, cheese)
|
36
|
+
expect(charlie.liked?(cheese)).to be true
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
context "when liker hasn't liked likeable" do
|
41
|
+
it "should return false" do
|
42
|
+
expect(charlie.liked?(cheese)).to be false
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
describe "#like" do
|
48
|
+
context "when liker hasn't liked likeable" do
|
49
|
+
it "should create a like record" do
|
50
|
+
expect {
|
51
|
+
charlie.like(cheese)
|
52
|
+
}.to change(Like, :count).by(1)
|
53
|
+
end
|
54
|
+
|
55
|
+
it "should set association of the like record" do
|
56
|
+
charlie.like(cheese)
|
57
|
+
like = Like.find_by(likeable_id: cheese.id, likeable_type: cheese.class.base_class.name, liker_id: charlie.id)
|
58
|
+
expect(like.liker).to eq charlie
|
59
|
+
expect(like.likeable).to eq cheese
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
context "when liker has liked likeable already" do
|
64
|
+
it "should do nothing" do
|
65
|
+
create_like(charlie, cheese)
|
66
|
+
expect {
|
67
|
+
charlie.like(cheese)
|
68
|
+
}.to change(Like, :count).by(0)
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
describe "#unlike" do
|
74
|
+
context "when liker has liked likeable" do
|
75
|
+
it "should destroy the like record" do
|
76
|
+
create_like(charlie, cheese)
|
77
|
+
expect {
|
78
|
+
charlie.unlike(cheese)
|
79
|
+
}.to change(Like, :count).by(-1)
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
context "when liker has not liked likeable" do
|
84
|
+
it "should do nothing" do
|
85
|
+
expect {
|
86
|
+
charlie.unlike(cheese)
|
87
|
+
}.to change(Like, :count).by(0)
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,101 @@
|
|
1
|
+
require 'acts_as_liked'
|
2
|
+
require 'active_record'
|
3
|
+
require 'support/models'
|
4
|
+
|
5
|
+
require 'support/like_macros.rb'
|
6
|
+
|
7
|
+
ActiveRecord::Base.establish_connection adapter: "sqlite3", database: ":memory:"
|
8
|
+
|
9
|
+
load 'support/schema.rb' # Define schema
|
10
|
+
|
11
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
12
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
13
|
+
# The generated `.rspec` file contains `--require spec_helper` which will cause this
|
14
|
+
# file to always be loaded, without a need to explicitly require it in any files.
|
15
|
+
#
|
16
|
+
# Given that it is always loaded, you are encouraged to keep this file as
|
17
|
+
# light-weight as possible. Requiring heavyweight dependencies from this file
|
18
|
+
# will add to the boot time of your test suite on EVERY test run, even for an
|
19
|
+
# individual file that may not need all of that loaded. Instead, consider making
|
20
|
+
# a separate helper file that requires the additional dependencies and performs
|
21
|
+
# the additional setup, and require it from the spec files that actually need it.
|
22
|
+
#
|
23
|
+
# The `.rspec` file also contains a few flags that are not defaults but that
|
24
|
+
# users commonly want.
|
25
|
+
#
|
26
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
27
|
+
RSpec.configure do |config|
|
28
|
+
# rspec-expectations config goes here. You can use an alternate
|
29
|
+
# assertion/expectation library such as wrong or the stdlib/minitest
|
30
|
+
# assertions if you prefer.
|
31
|
+
config.expect_with :rspec do |expectations|
|
32
|
+
# This option will default to `true` in RSpec 4. It makes the `description`
|
33
|
+
# and `failure_message` of custom matchers include text for helper methods
|
34
|
+
# defined using `chain`, e.g.:
|
35
|
+
# be_bigger_than(2).and_smaller_than(4).description
|
36
|
+
# # => "be bigger than 2 and smaller than 4"
|
37
|
+
# ...rather than:
|
38
|
+
# # => "be bigger than 2"
|
39
|
+
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
|
40
|
+
end
|
41
|
+
|
42
|
+
# rspec-mocks config goes here. You can use an alternate test double
|
43
|
+
# library (such as bogus or mocha) by changing the `mock_with` option here.
|
44
|
+
config.mock_with :rspec do |mocks|
|
45
|
+
# Prevents you from mocking or stubbing a method that does not exist on
|
46
|
+
# a real object. This is generally recommended, and will default to
|
47
|
+
# `true` in RSpec 4.
|
48
|
+
mocks.verify_partial_doubles = true
|
49
|
+
end
|
50
|
+
|
51
|
+
# The settings below are suggested to provide a good initial experience
|
52
|
+
# with RSpec, but feel free to customize to your heart's content.
|
53
|
+
=begin
|
54
|
+
# These two settings work together to allow you to limit a spec run
|
55
|
+
# to individual examples or groups you care about by tagging them with
|
56
|
+
# `:focus` metadata. When nothing is tagged with `:focus`, all examples
|
57
|
+
# get run.
|
58
|
+
config.filter_run :focus
|
59
|
+
config.run_all_when_everything_filtered = true
|
60
|
+
|
61
|
+
# Limits the available syntax to the non-monkey patched syntax that is recommended.
|
62
|
+
# For more details, see:
|
63
|
+
# - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
|
64
|
+
# - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
|
65
|
+
# - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
|
66
|
+
config.disable_monkey_patching!
|
67
|
+
|
68
|
+
# This setting enables warnings. It's recommended, but in some cases may
|
69
|
+
# be too noisy due to issues in dependencies.
|
70
|
+
config.warnings = true
|
71
|
+
|
72
|
+
# Many RSpec users commonly either run the entire suite or an individual
|
73
|
+
# file, and it's useful to allow more verbose output when running an
|
74
|
+
# individual spec file.
|
75
|
+
if config.files_to_run.one?
|
76
|
+
# Use the documentation formatter for detailed output,
|
77
|
+
# unless a formatter has already been configured
|
78
|
+
# (e.g. via a command-line flag).
|
79
|
+
config.default_formatter = 'doc'
|
80
|
+
end
|
81
|
+
|
82
|
+
# Print the 10 slowest examples and example groups at the
|
83
|
+
# end of the spec run, to help surface which specs are running
|
84
|
+
# particularly slow.
|
85
|
+
config.profile_examples = 10
|
86
|
+
|
87
|
+
# Run specs in random order to surface order dependencies. If you find an
|
88
|
+
# order dependency and want to debug it, you can fix the order by providing
|
89
|
+
# the seed, which is printed after each run.
|
90
|
+
# --seed 1234
|
91
|
+
config.order = :random
|
92
|
+
|
93
|
+
# Seed global randomization in this process using the `--seed` CLI option.
|
94
|
+
# Setting this allows you to use `--seed` to deterministically reproduce
|
95
|
+
# test failures related to randomization by passing the same `--seed` value
|
96
|
+
# as the one that triggered the failure.
|
97
|
+
Kernel.srand config.seed
|
98
|
+
=end
|
99
|
+
|
100
|
+
config.include LikeMacros
|
101
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
RSpec::Matchers.define :have_many do |expected|
|
2
|
+
match do |actual|
|
3
|
+
association = actual.reflect_on_association(expected)
|
4
|
+
expect(association.macro).to eq :has_many
|
5
|
+
end
|
6
|
+
|
7
|
+
failure_message do |actual|
|
8
|
+
"expected #{actual} to have many #{expected}"
|
9
|
+
end
|
10
|
+
|
11
|
+
failure_message_when_negated do |actual|
|
12
|
+
"expected #{actual} not to have many #{expected}"
|
13
|
+
end
|
14
|
+
|
15
|
+
description do
|
16
|
+
"expect to have many #{expected}"
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
ActiveRecord::Schema.define do
|
2
|
+
|
3
|
+
create_table :likes do |t|
|
4
|
+
t.references :likeable, polymorphic: true
|
5
|
+
t.references :liker, polymorphic: :true
|
6
|
+
|
7
|
+
t.timestamps
|
8
|
+
end
|
9
|
+
|
10
|
+
add_index :likes, [:likeable_id, :likeable_type]
|
11
|
+
add_index :likes, [:liker_id, :liker_type]
|
12
|
+
|
13
|
+
create_table :users do |t|
|
14
|
+
t.string :name
|
15
|
+
end
|
16
|
+
|
17
|
+
create_table :foods do |t|
|
18
|
+
t.string :name
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
metadata
ADDED
@@ -0,0 +1,191 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: acts_as_liked
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.4
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Sung Won Cho
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-11-30 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.7'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.7'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: guard
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: guard-rspec
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: sqlite3
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: activerecord
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: generator_spec
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
description:
|
126
|
+
email:
|
127
|
+
- mikeswcho@gmail.com
|
128
|
+
executables: []
|
129
|
+
extensions: []
|
130
|
+
extra_rdoc_files: []
|
131
|
+
files:
|
132
|
+
- ".gitignore"
|
133
|
+
- ".rspec"
|
134
|
+
- ".travis.yml"
|
135
|
+
- Gemfile
|
136
|
+
- Guardfile
|
137
|
+
- LICENSE.txt
|
138
|
+
- README.md
|
139
|
+
- Rakefile
|
140
|
+
- acts_as_liked.gemspec
|
141
|
+
- lib/acts_as_liked.rb
|
142
|
+
- lib/acts_as_liked/likeable.rb
|
143
|
+
- lib/acts_as_liked/liker.rb
|
144
|
+
- lib/acts_as_liked/version.rb
|
145
|
+
- lib/generators/acts_as_liked/migration_generator.rb
|
146
|
+
- lib/generators/acts_as_liked/templates/migration.rb
|
147
|
+
- lib/generators/acts_as_liked/templates/model.rb
|
148
|
+
- lib/generators/acts_as_liked_generator.rb
|
149
|
+
- lib/generators/templates/migration.rb
|
150
|
+
- lib/generators/templates/model.rb
|
151
|
+
- spec/generator_spec.rb
|
152
|
+
- spec/likeable_spec.rb
|
153
|
+
- spec/liker_spec.rb
|
154
|
+
- spec/spec_helper.rb
|
155
|
+
- spec/support/like_macros.rb
|
156
|
+
- spec/support/matchers/have_many.rb
|
157
|
+
- spec/support/models.rb
|
158
|
+
- spec/support/schema.rb
|
159
|
+
homepage: https://github.com/sungwoncho/acts_as_liked
|
160
|
+
licenses:
|
161
|
+
- MIT
|
162
|
+
metadata: {}
|
163
|
+
post_install_message:
|
164
|
+
rdoc_options: []
|
165
|
+
require_paths:
|
166
|
+
- lib
|
167
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
168
|
+
requirements:
|
169
|
+
- - ">="
|
170
|
+
- !ruby/object:Gem::Version
|
171
|
+
version: '0'
|
172
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
173
|
+
requirements:
|
174
|
+
- - ">="
|
175
|
+
- !ruby/object:Gem::Version
|
176
|
+
version: '0'
|
177
|
+
requirements: []
|
178
|
+
rubyforge_project:
|
179
|
+
rubygems_version: 2.4.1
|
180
|
+
signing_key:
|
181
|
+
specification_version: 4
|
182
|
+
summary: Add like feature to any Active Record models
|
183
|
+
test_files:
|
184
|
+
- spec/generator_spec.rb
|
185
|
+
- spec/likeable_spec.rb
|
186
|
+
- spec/liker_spec.rb
|
187
|
+
- spec/spec_helper.rb
|
188
|
+
- spec/support/like_macros.rb
|
189
|
+
- spec/support/matchers/have_many.rb
|
190
|
+
- spec/support/models.rb
|
191
|
+
- spec/support/schema.rb
|