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 ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in simple_like.gemspec
4
+ gemspec
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,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -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,5 @@
1
+ class Like < ActiveRecord::Base
2
+ belongs_to :likeable, polymorphic: true
3
+ belongs_to :author, polymorphic: true
4
+ default_scope order: 'created_at ASC'
5
+ 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
@@ -0,0 +1,3 @@
1
+ module SimpleLike
2
+ VERSION = "0.2.1"
3
+ end
@@ -0,0 +1,6 @@
1
+ require 'simple_like/version'
2
+
3
+ module SimpleLike
4
+ autoload :Target, "simple_like/target"
5
+ autoload :User, "simple_like/user"
6
+ end
@@ -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: []