voteable_yulin 0.1.0
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.
- checksums.yaml +7 -0
- data/lib/voteable_yulin.rb +54 -0
- metadata +43 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: a7aeb0597fd4053278379789184fadbd95fe12a7
|
4
|
+
data.tar.gz: c904ba9b5ffc63297af106b4652f253cb91a5972
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 7ccdbd43b6b69956a92954afb29f3f7e219db1e866a3f5368efe1e74a268c5655a7e4cc9a047259989dfc9e57c8cad7632b3640931cd939407d5531e32f41676
|
7
|
+
data.tar.gz: 64ff47572de707c7fa3aac8523afe0a35d498e3ab4d4957a0fdd6a28e1ff06cf8ff5c40c7aa68ae6defc184ceef1a184b3ba031cd3a91da4dec0f14669e34fa9
|
@@ -0,0 +1,54 @@
|
|
1
|
+
# using concerns
|
2
|
+
module Voteable
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
included do
|
6
|
+
has_many :votes, as: :voteable
|
7
|
+
end
|
8
|
+
|
9
|
+
def total_votes
|
10
|
+
up_votes - down_votes
|
11
|
+
end
|
12
|
+
|
13
|
+
def up_votes
|
14
|
+
self.votes.where(vote: true).count
|
15
|
+
end
|
16
|
+
|
17
|
+
def down_votes
|
18
|
+
self.votes.where(vote: false).count
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
# using normal metaprogramming
|
23
|
+
=begin
|
24
|
+
module Voteable
|
25
|
+
def self.included(base)
|
26
|
+
# the instance methods in InstanceMethods will be included
|
27
|
+
base.send(:include, InstanceMethods)
|
28
|
+
base.extend ClassMethods
|
29
|
+
base.class_eval do
|
30
|
+
my_class_method
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
module InstanceMethods
|
35
|
+
def total_votes
|
36
|
+
up_votes - down_votes
|
37
|
+
end
|
38
|
+
|
39
|
+
def up_votes
|
40
|
+
self.votes.where(vote: true).count
|
41
|
+
end
|
42
|
+
|
43
|
+
def down_votes
|
44
|
+
self.votes.where(vote: false).count
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
module ClassMethods
|
49
|
+
def my_class_method
|
50
|
+
has_many :votes, as: :voteable
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
=end
|
metadata
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: voteable_yulin
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Yulin Chen
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-12-04 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: This gem make your module can be voted.
|
14
|
+
email: gloverock2@gmail.com
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- lib/voteable_yulin.rb
|
20
|
+
homepage: http://github.com
|
21
|
+
licenses: []
|
22
|
+
metadata: {}
|
23
|
+
post_install_message:
|
24
|
+
rdoc_options: []
|
25
|
+
require_paths:
|
26
|
+
- lib
|
27
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
28
|
+
requirements:
|
29
|
+
- - ">="
|
30
|
+
- !ruby/object:Gem::Version
|
31
|
+
version: '0'
|
32
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
33
|
+
requirements:
|
34
|
+
- - ">="
|
35
|
+
- !ruby/object:Gem::Version
|
36
|
+
version: '0'
|
37
|
+
requirements: []
|
38
|
+
rubyforge_project:
|
39
|
+
rubygems_version: 2.2.2
|
40
|
+
signing_key:
|
41
|
+
specification_version: 4
|
42
|
+
summary: summary
|
43
|
+
test_files: []
|