ibham 0.1.2 → 0.1.3

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 22773a5276f0ef750a2db5513314e96e0efe8093
4
+ data.tar.gz: 5ca1cf25287eb58391af77cec4d79555a98943cf
5
+ SHA512:
6
+ metadata.gz: 24045afcc41bec10e7e197f41191f402ae98402f9e2c43523e60104971e23d9325135d0c20b94e780786f836d81e41f2cefeba5916fd74dacb794ce7abf6433b
7
+ data.tar.gz: ac0b566e3d53016118eab374931e52102e40ad8b9bcd95925d1e65ad2b6b0a791ea09592185a7ed9201f15756b274fa19b52b84d893b59d4ae18a260c63cc8e1
data/CHANGELOG.md CHANGED
@@ -16,3 +16,9 @@
16
16
  * Remove database_cleaner gem
17
17
  * Refactor code
18
18
  * Add comments to methods
19
+
20
+ ## v0.1.3
21
+
22
+ * Drop support for Ruby < 1.9
23
+ * Add method: vote_result
24
+ * Clean up code and comments
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # Ibham [![Build Status](https://secure.travis-ci.org/bloc40/ibham.png)](http://travis-ci.org/bloc40/ibham) [![Code Climate](https://codeclimate.com/badge.png)](https://codeclimate.com/github/bloc40/ibham)
1
+ # Ibham [![Build Status](https://secure.travis-ci.org/bloc40/ibham.png)](http://travis-ci.org/bloc40/ibham) [![Code Climate](https://codeclimate.com/badge.png)](https://codeclimate.com/github/bloc40/ibham) [![Dependency Status](https://gemnasium.com/bloc40/ibham.png)](https://gemnasium.com/bloc40/ibham)
2
2
 
3
3
  Ibham is a gem that allows Rails apps to compute and display voting scores for active records models.
4
4
 
@@ -12,10 +12,6 @@ And then execute:
12
12
 
13
13
  $ bundle
14
14
 
15
- Or install it yourself as:
16
-
17
- $ gem install ibham
18
-
19
15
  Generate the migration
20
16
 
21
17
  $ rails g ibham:install
@@ -49,12 +45,6 @@ The user can cast votes in the following ways:
49
45
  user.vote_up(item) # Adds a positive vote
50
46
  user.vote_down(item) # Adds a negative vote
51
47
 
52
- or
53
-
54
- # The only allowed values are 1 and -1
55
- user.cast_vote(item, 1) # Adds a positive vote
56
- user.cast_vote(item, -1) # Adds a negative vote
57
-
58
48
  Since a user can cast only one vote per item, to check if the user can
59
49
  cast vote for an item:
60
50
 
@@ -64,13 +54,16 @@ cast vote for an item:
64
54
 
65
55
  To retrieve the votes:
66
56
 
67
- item.up_votes # Return the total number of positive votes
68
- item.down_votes # Return the total number of negative votes
57
+ item.up_votes # Returns the total number of positive votes
58
+ item.down_votes # Returns the total number of negative votes
59
+
60
+ item.up_percentage # Returns the percentage of positive votes
61
+ item.down_percentage # Returns the percentage of negative votes
69
62
 
70
- item.up_percentage # Return the percentage of positive votes
71
- item.down_percentage # Return the percentage of negative votes
63
+ item.vote_result # Retuns the result of votes
72
64
 
73
- For more information visit [[the documentation page](http://rubydoc.info/github/bloc40/ibham/master/frames)]
65
+ For more information visit [the documentation
66
+ page](http://rubydoc.info/github/bloc40/ibham/master/frames).
74
67
  ## Contributing
75
68
 
76
69
  1. Fork it
data/Rakefile CHANGED
@@ -2,7 +2,7 @@ require 'bundler/gem_tasks'
2
2
  require 'rake/testtask'
3
3
 
4
4
  desc 'Default: run specs.'
5
- task :default => :test
5
+ task default: :test
6
6
 
7
7
  Rake::TestTask.new do |test|
8
8
  test.pattern = './spec/**/*_spec.rb'
@@ -1,11 +1,11 @@
1
1
  class IbhamCreateVotes < ActiveRecord::Migration
2
2
  def change
3
3
  create_table :votes do |t|
4
- t.references :voter, :polymorphic => true
5
- t.references :voteable, :polymorphic => true
4
+ t.references :voter, polymorphic: true
5
+ t.references :voteable, polymorphic: true
6
6
  t.integer :value
7
7
 
8
- t.timestamps
8
+ t.timestamps null: false
9
9
  end
10
10
 
11
11
  add_index :votes, [:voter_id, :voter_type]
@@ -4,7 +4,8 @@ module Ibham
4
4
 
5
5
  module ClassMethods
6
6
 
7
- # To turn an abject to a voteable, call <tt>acts_as_voteable</tt> in the Active Record model class.
7
+ # To turn an abject to a voteable, call <tt>acts_as_voteable</tt> in the
8
+ # Active Record model class.
8
9
  #
9
10
  # class Item < ActiveRecord::Base
10
11
  # acts_as_voteable
@@ -20,33 +21,36 @@ module Ibham
20
21
  extend ActiveSupport::Concern
21
22
 
22
23
  included do
23
- has_many :votes, :as => :voteable, :dependent => :destroy
24
+ has_many :votes, as: :voteable, dependent: :destroy
24
25
  end
25
26
 
26
- # To ruturn the count of positive votes, call <tt>up_votes</tt> on the voteable object.
27
+ # To ruturn the count of positive votes, call <tt>up_votes</tt> on the
28
+ # voteable object.
27
29
  #
28
30
  # item.up_votes
29
31
  def up_votes
30
- votes.where(:value => ALLOWED_VALUE).count
32
+ votes.where(value: ALLOWED_VALUE).count
31
33
  end
32
34
 
33
- # To ruturn the count of negative votes, call <tt>down_votes</tt> on the voteable object.
35
+ # To ruturn the count of negative votes, call <tt>down_votes</tt> on the
36
+ # voteable object.
34
37
  #
35
38
  # item.down_votes
36
39
  def down_votes
37
- votes.where(:value => -ALLOWED_VALUE).count
40
+ votes.where(value: -ALLOWED_VALUE).count
38
41
  end
39
42
 
40
- # To ruturn the percentage of positive votes, call <tt>up_percentage</tt> on the voteable object.
41
- # This is ideal to use with sparkbars.
43
+ # To ruturn the percentage of positive votes, call <tt>up_percentage</tt>
44
+ # on the voteable object. This is ideal to use with sparkbars.
42
45
  #
43
46
  # item.up_percentage
44
47
  def up_percentage
45
48
  (up_votes * 100 / (votes.count.to_f + DELTA)).round
46
49
  end
47
50
 
48
- # To ruturn the percentage of positive votes, call <tt>down_percentage</tt> on the voteable object.
49
- # This is ideal to use with sparkbars.
51
+ # To ruturn the percentage of positive votes, call
52
+ # <tt>down_percentage</tt> on the voteable object. This is ideal to use
53
+ # with sparkbars.
50
54
  #
51
55
  # item.down_percentage
52
56
  def down_percentage
@@ -60,18 +64,28 @@ module Ibham
60
64
  votes.map(&:voter)
61
65
  end
62
66
 
63
- # To get the list of voters who casted positive votes, call <tt>up_voters</tt> on the voteable object.
67
+ # To get the list of voters who casted positive votes, call
68
+ # <tt>up_voters</tt> on the voteable object.
64
69
  #
65
70
  # item.up_voters
66
71
  def up_voters
67
- votes.where(:value => ALLOWED_VALUE).map(&:voter)
72
+ votes.where(value: ALLOWED_VALUE).map(&:voter)
68
73
  end
69
74
 
70
- # To get the list of voters who casted negative votes, call <tt>down_voters</tt> on the voteable object.
75
+ # To get the list of voters who casted negative votes, call
76
+ # <tt>down_voters</tt> on the voteable object.
71
77
  #
72
78
  # item.down_voters
73
79
  def down_voters
74
- votes.where(:value => -ALLOWED_VALUE).map(&:voter)
80
+ votes.where(value: -ALLOWED_VALUE).map(&:voter)
81
+ end
82
+
83
+ # To get the result of the votes, call <tt>vote_result</tt> on the
84
+ # voteable object.
85
+ #
86
+ # item.vote_result
87
+ def vote_result
88
+ votes.sum(:value)
75
89
  end
76
90
  end
77
91
  end
@@ -4,11 +4,10 @@ module Ibham
4
4
 
5
5
  module ClassMethods
6
6
 
7
- # To turn an abject to a voter, call <tt>acts_as_voter</tt> in the Active Record model class.
7
+ # To turn an abject to a voter, call <tt>acts_as_voter</tt> in the Active
8
+ # Record model class.
8
9
  #
9
- # class User < ActiveRecord::Base
10
- # acts_as_voter
11
- # end
10
+ # class User < ActiveRecord::Base acts_as_voter end
12
11
  def acts_as_voter
13
12
  class_eval do
14
13
  include Voter
@@ -20,7 +19,7 @@ module Ibham
20
19
  extend ActiveSupport::Concern
21
20
 
22
21
  included do
23
- has_many :votes, :as => :voter, :dependent => :destroy
22
+ has_many :votes, as: :voter, dependent: :destroy
24
23
  end
25
24
 
26
25
  # To check if the voter (the user for example) can vote for this item,
@@ -30,7 +29,7 @@ module Ibham
30
29
  #
31
30
  # This will return either true for false
32
31
  def can_vote_for?(voteable)
33
- votes.build(:voteable => voteable, :value => ALLOWED_VALUE).valid?
32
+ votes.build(voteable: voteable, value: ALLOWED_VALUE).valid?
34
33
  end
35
34
 
36
35
  # To cast a positive vote, the voter (e.g. the use) calls <tt>vote_up</tt>
@@ -38,29 +37,21 @@ module Ibham
38
37
  #
39
38
  # user.vote_up(item)
40
39
  #
41
- # This will add a new record with a positive vote for this item by this user in the votes table.
40
+ # This will add a new record with a positive vote for this item by this
41
+ # user in the votes table.
42
42
  def vote_up(voteable)
43
- votes.create(:voteable => voteable, :value => ALLOWED_VALUE)
43
+ votes.create(voteable: voteable, value: ALLOWED_VALUE)
44
44
  end
45
45
 
46
- # To cast a negative vote, the voter (e.g. the use) can call <tt>vote_up</tt>
47
- # with the object to vote for.
46
+ # To cast a negative vote, the voter (e.g. the use) can call
47
+ # <tt>vote_up</tt> with the object to vote for.
48
48
  #
49
49
  # user.vote_up(item)
50
50
  #
51
- # This will add a new record with a negative vote for this item by this user in the votes table.
51
+ # This will add a new record with a negative vote for this item by this
52
+ # user in the votes table.
52
53
  def vote_down(voteable)
53
- votes.create(:voteable => voteable, :value => -ALLOWED_VALUE)
54
- end
55
-
56
- # To allow the user to dynamically cast a vote, call <tt>cast_vote</tt> with the item
57
- # to vote for and the value (1 or -1)
58
- #
59
- # user.cast_vote(item, 1)
60
- #
61
- # This will add a new record with a positive vote.
62
- def cast_vote(voteable, value)
63
- votes.create(:voteable => voteable, :value => value)
54
+ votes.create(voteable: voteable, value: -ALLOWED_VALUE)
64
55
  end
65
56
  end
66
57
  end
data/lib/ibham/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Ibham
2
- VERSION = '0.1.2'
2
+ VERSION = '0.1.3'
3
3
  end
data/lib/models/vote.rb CHANGED
@@ -2,11 +2,9 @@ class Vote < ActiveRecord::Base
2
2
 
3
3
  ALLOWED_VALUE = 1
4
4
 
5
- attr_accessible :value, :voteable
5
+ belongs_to :voteable, polymorphic: true
6
+ belongs_to :voter, polymorphic: true
6
7
 
7
- belongs_to :voteable, :polymorphic => true
8
- belongs_to :voter, :polymorphic => true
9
-
10
- validates_uniqueness_of :voteable_id, :scope => :voter_id
11
- validates :value, :inclusion => { :in => [ALLOWED_VALUE, -ALLOWED_VALUE], :message => "should be either #{ALLOWED_VALUE} or -#{ALLOWED_VALUE}" }
8
+ validates_uniqueness_of :voteable_id, scope: :voter_id
9
+ validates :value, inclusion: { in: [ALLOWED_VALUE, -ALLOWED_VALUE], message: "should be either #{ALLOWED_VALUE} or -#{ALLOWED_VALUE}" }
12
10
  end
@@ -5,23 +5,23 @@ describe Vote do
5
5
  let(:item) { Item.create! }
6
6
 
7
7
  describe 'Validations' do
8
- it 'should be valid if the value is 1' do
9
- vote = Vote.new(:value => 1)
8
+ it 'is valid if the value is 1' do
9
+ vote = Vote.new(value: 1)
10
10
  vote.valid?.must_equal true
11
11
  end
12
12
 
13
- it 'should be valid if the value is -1' do
14
- vote = Vote.new(:value => -1)
13
+ it 'is valid if the value is -1' do
14
+ vote = Vote.new(value: -1)
15
15
  vote.valid?.must_equal true
16
16
  end
17
17
 
18
- it 'should fail if the value is other than 1 or -1' do
19
- vote = Vote.new(:value => 2)
18
+ it 'fails if the value is other than 1 or -1' do
19
+ vote = Vote.new(value: 2)
20
20
  vote.valid?.must_equal false
21
21
  vote.errors.full_messages.must_include 'Value should be either 1 or -1'
22
22
  end
23
23
 
24
- it 'should validate uniqueness of vote for voter and voteable' do
24
+ it 'validates uniqueness of vote for voter and voteable' do
25
25
  user.vote_up(item)
26
26
  Vote.count.must_equal 1
27
27
  user.vote_up(item)
@@ -15,55 +15,62 @@ describe 'ActsAsVoteable' do
15
15
  end
16
16
 
17
17
  describe '#up_votes' do
18
- it 'should return the number of positive votes for voteable' do
18
+ it 'returns the number of positive votes for voteable' do
19
19
  item.up_votes.must_equal 2
20
20
  end
21
21
  end
22
22
 
23
23
  describe '#down_votes' do
24
- it 'should return the number of negative votes for voteable' do
24
+ it 'returns the number of negative votes for voteable' do
25
25
  item.down_votes.must_equal 1
26
26
  end
27
27
  end
28
28
 
29
29
  describe '#up_percentage' do
30
- it 'should return the percentage of positive votes' do
30
+ it 'returns the percentage of positive votes' do
31
31
  item.up_percentage.must_equal 67
32
32
  end
33
33
  end
34
34
 
35
35
  describe '#down_percentage' do
36
- it 'should return the percentage of negative votes' do
36
+ it 'returns the percentage of negative votes' do
37
37
  item.down_percentage.must_equal 33
38
38
  end
39
39
 
40
- it 'should return 100% if the item has 1 negative vote' do
40
+ it 'returns 100% if the item has 1 negative vote' do
41
41
  item2.down_percentage.must_equal 100
42
42
  end
43
43
 
44
- it 'should return 0 if the item has 0 positive vote' do
44
+ it 'returns 0 if the item has 0 positive vote' do
45
45
  item2.up_percentage.must_equal 0
46
46
  end
47
47
  end
48
48
 
49
49
  describe '#voters' do
50
- it 'should return the list of voters' do
50
+ it 'returns the list of voters' do
51
51
  item.voters.sort.must_equal [user2, user1, user3].sort
52
52
  item2.voters.must_equal [user1]
53
53
  end
54
54
  end
55
55
 
56
56
  describe '#up_voters' do
57
- it 'should return the list of users who casted positive votes' do
57
+ it 'returns the list of users who casted positive votes' do
58
58
  item.up_voters.sort.must_equal [user1, user3].sort
59
59
  item2.up_voters.must_equal []
60
60
  end
61
61
  end
62
62
 
63
63
  describe '#down_voters' do
64
- it 'should return the list of users who casted negative votes' do
64
+ it 'returns the list of users who casted negative votes' do
65
65
  item.down_voters.sort.must_equal [user2]
66
66
  item2.down_voters.must_equal [user1]
67
67
  end
68
68
  end
69
+
70
+ describe '#vote_result' do
71
+ it 'returns the result of the votes per item' do
72
+ item.vote_result.must_equal 1
73
+ item2.vote_result.must_equal -1
74
+ end
75
+ end
69
76
  end
@@ -5,18 +5,18 @@ describe 'ActsAsVoter' do
5
5
  let(:item) { Item.create! }
6
6
 
7
7
  describe '#can_vote_for?' do
8
- it 'should return true if the voter can vote for an item' do
8
+ it 'returns true if the voter can vote for an item' do
9
9
  user.can_vote_for?(item).must_equal true
10
10
  end
11
11
 
12
- it 'should return false if the voter can not vote for an item' do
12
+ it 'returns false if the voter can not vote for an item' do
13
13
  user.vote_down(item)
14
14
  user.can_vote_for?(item).must_equal false
15
15
  end
16
16
  end
17
17
 
18
18
  describe '#vote_up' do
19
- it 'should create a positive vote' do
19
+ it 'creates a positive vote' do
20
20
  vote_count = Vote.count
21
21
  user.vote_up(item)
22
22
  Vote.count.must_equal vote_count + 1
@@ -25,27 +25,11 @@ describe 'ActsAsVoter' do
25
25
  end
26
26
 
27
27
  describe '#vote_down' do
28
- it 'should create a positive vote' do
28
+ it 'creates a positive vote' do
29
29
  vote_count = Vote.count
30
30
  user.vote_down(item)
31
31
  Vote.count.must_equal vote_count + 1
32
32
  Vote.last.value.must_equal -1
33
33
  end
34
34
  end
35
-
36
- describe '#cast_vote' do
37
- it 'should create a positive vote' do
38
- vote_count = Vote.count
39
- vote = user.cast_vote(item, 1)
40
- Vote.count.must_equal vote_count + 1
41
- Vote.last.value.must_equal 1
42
- end
43
-
44
- it 'should create a negative vote' do
45
- vote_count = Vote.count
46
- vote = user.cast_vote(item, -1)
47
- Vote.count.must_equal vote_count + 1
48
- Vote.last.value.must_equal -1
49
- end
50
- end
51
35
  end
data/spec/spec_helper.rb CHANGED
@@ -3,11 +3,6 @@ require 'sqlite3'
3
3
  require 'ibham'
4
4
  require 'minitest/autorun'
5
5
 
6
- db_config = {
7
- :adapter => 'sqlite3',
8
- :database => ':memory:'
9
- }
10
-
11
6
  ActiveRecord::Migration.verbose = false
12
7
 
13
8
  class MiniTest::Spec
@@ -18,8 +13,8 @@ class MiniTest::Spec
18
13
  end
19
14
 
20
15
  config = {
21
- :adapter => 'sqlite3',
22
- :database => ':memory:'
16
+ adapter: 'sqlite3',
17
+ database: ':memory:'
23
18
  }
24
19
 
25
20
  ActiveRecord::Base.establish_connection(config)
@@ -29,11 +24,11 @@ end
29
24
 
30
25
  ActiveRecord::Schema.define do
31
26
  create_table :votes do |t|
32
- t.references :voter, :polymorphic => true
33
- t.references :voteable, :polymorphic => true
27
+ t.references :voter, polymorphic: true
28
+ t.references :voteable, polymorphic: true
34
29
  t.integer :value
35
30
 
36
- t.timestamps
31
+ t.timestamps null: false
37
32
  end
38
33
 
39
34
  add_index :votes, [:voter_id, :voter_type]
@@ -41,12 +36,12 @@ ActiveRecord::Schema.define do
41
36
 
42
37
  create_table :users do |t|
43
38
  t.string :email
44
- t.timestamps
39
+ t.timestamps null: false
45
40
  end
46
41
 
47
42
  create_table :items do |t|
48
43
  t.string :name
49
- t.timestamps
44
+ t.timestamps null: false
50
45
  end
51
46
  end
52
47
 
metadata CHANGED
@@ -1,49 +1,57 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ibham
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
5
- prerelease:
4
+ version: 0.1.3
6
5
  platform: ruby
7
6
  authors:
8
7
  - Jamal El Milahi
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2012-10-10 00:00:00.000000000 Z
11
+ date: 2015-08-31 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: activerecord
16
- requirement: &70249569608880 !ruby/object:Gem::Requirement
17
- none: false
15
+ requirement: !ruby/object:Gem::Requirement
18
16
  requirements:
19
- - - ! '>='
17
+ - - ">="
20
18
  - !ruby/object:Gem::Version
21
19
  version: '0'
22
20
  type: :development
23
21
  prerelease: false
24
- version_requirements: *70249569608880
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
25
27
  - !ruby/object:Gem::Dependency
26
28
  name: rake
27
- requirement: &70249569608460 !ruby/object:Gem::Requirement
28
- none: false
29
+ requirement: !ruby/object:Gem::Requirement
29
30
  requirements:
30
- - - ! '>='
31
+ - - ">="
31
32
  - !ruby/object:Gem::Version
32
33
  version: '0'
33
34
  type: :development
34
35
  prerelease: false
35
- version_requirements: *70249569608460
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
36
41
  - !ruby/object:Gem::Dependency
37
42
  name: sqlite3
38
- requirement: &70249569608040 !ruby/object:Gem::Requirement
39
- none: false
43
+ requirement: !ruby/object:Gem::Requirement
40
44
  requirements:
41
- - - ! '>='
45
+ - - ">="
42
46
  - !ruby/object:Gem::Version
43
47
  version: '0'
44
48
  type: :development
45
49
  prerelease: false
46
- version_requirements: *70249569608040
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
47
55
  description: Voting System for Rails applications
48
56
  email:
49
57
  - jamal@elmilahi.com
@@ -51,8 +59,8 @@ executables: []
51
59
  extensions: []
52
60
  extra_rdoc_files: []
53
61
  files:
54
- - .gitignore
55
- - .travis.yml
62
+ - ".gitignore"
63
+ - ".travis.yml"
56
64
  - CHANGELOG.md
57
65
  - Gemfile
58
66
  - LICENSE.txt
@@ -72,27 +80,26 @@ files:
72
80
  - spec/spec_helper.rb
73
81
  homepage: https://github.com/bloc40/ibham
74
82
  licenses: []
83
+ metadata: {}
75
84
  post_install_message:
76
85
  rdoc_options: []
77
86
  require_paths:
78
87
  - lib
79
88
  required_ruby_version: !ruby/object:Gem::Requirement
80
- none: false
81
89
  requirements:
82
- - - ! '>='
90
+ - - ">="
83
91
  - !ruby/object:Gem::Version
84
92
  version: '0'
85
93
  required_rubygems_version: !ruby/object:Gem::Requirement
86
- none: false
87
94
  requirements:
88
- - - ! '>='
95
+ - - ">="
89
96
  - !ruby/object:Gem::Version
90
97
  version: '0'
91
98
  requirements: []
92
99
  rubyforge_project:
93
- rubygems_version: 1.8.17
100
+ rubygems_version: 2.4.5.1
94
101
  signing_key:
95
- specification_version: 3
102
+ specification_version: 4
96
103
  summary: Ibham is a gem that allows Rails apps to compute and display voting scores
97
104
  for active records models.
98
105
  test_files: