tadpoll 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 28c3afe72d6c4a263ee868ba436cada6885bd632
4
+ data.tar.gz: 89c5171dbe32f9f0712a44dd97323993ed18c805
5
+ SHA512:
6
+ metadata.gz: f7e3bc6cd4a7f8c643643c41a6bcbd2cb49d0766dd60e03dc6fe5bfa549d4a1c6c01427f8481b13f9879f159f3a4fe5f4f76c6ccbf375d6592ee511bf77706cf
7
+ data.tar.gz: a30595a886138169e90433ef347c59cf6ebd7e9dee87326384c8e47f7ed6b9bedc9d59874984a196e4374ac834c031e98c80775ad6d78195ddc80d15234bcec8
Binary file
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in tadpoll.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Kayla Gallatin
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,43 @@
1
+ # Tadpoll
2
+
3
+ Tadpoll is a Ruby Gem designed to easily create polls with mutually exclusive options for voters to vote on.
4
+
5
+ ## Installation
6
+
7
+ Add the following to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'tadpoll'
11
+ ```
12
+
13
+ Followed by a ``bundle install``.
14
+
15
+ ### Database Migrations
16
+
17
+ Tadpoll uses 3 tables: Vote, Poll, and Option to store all voting information. To
18
+ generate and run the migration do:
19
+
20
+ rails generate tadpoll:migration
21
+ rake db:migrate
22
+
23
+ ## Usage
24
+
25
+ ### Voter Models
26
+
27
+ ```ruby
28
+ class User < ActiveRecord::Base
29
+ is_voter
30
+ end
31
+
32
+ poll = Tadpoll::Poll.create_poll_with_options("Best Fruit", ["Orange", "Banana"])
33
+ option = poll.options.first
34
+ @user.vote_for(option)
35
+
36
+ @user.voted_on_option?(option) # => true
37
+ @user.voted_on_poll?(poll) # => true
38
+ ```
39
+
40
+ ## License
41
+
42
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
43
+
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "tadpoll"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,31 @@
1
+ require 'rails/generators/migration'
2
+
3
+ module Tadpoll
4
+ class MigrationGenerator < Rails::Generators::Base
5
+ include Rails::Generators::Migration
6
+
7
+ desc "Generates migration"
8
+
9
+ def self.orm
10
+ Rails::Generators.options[:rails][:orm]
11
+ end
12
+
13
+ def self.source_root
14
+ File.join(File.dirname(__FILE__), 'templates', (orm.to_s unless orm.class.eql?(String)) )
15
+ end
16
+
17
+ def self.orm_has_migration?
18
+ [:active_record].include? orm
19
+ end
20
+
21
+ def self.next_migration_number(path)
22
+ Time.now.utc.strftime("%Y%m%d%H%M%S")
23
+ end
24
+
25
+ def create_migration_file
26
+ if self.class.orm_has_migration?
27
+ migration_template 'migration.rb', 'db/migrate/tadpoll_migration.rb'
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,32 @@
1
+ class TadpollMigration < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :votes do |t|
4
+ t.references :voter
5
+ t.belongs_to :poll, index: true
6
+ t.belongs_to :option, index: true
7
+
8
+ t.timestamps
9
+ end
10
+
11
+ create_table :polls do |t|
12
+ t.string :name
13
+
14
+ t.timestamps
15
+ end
16
+
17
+ create_table :options do |t|
18
+ t.string :name
19
+ t.belongs_to :poll, index: true
20
+
21
+ t.timestamps
22
+ end
23
+
24
+ add_index :votes, [:voter_id]
25
+ end
26
+
27
+ def self.down
28
+ drop_table :votes
29
+ drop_table :polls
30
+ drop_table :options
31
+ end
32
+ end
@@ -0,0 +1,23 @@
1
+ require 'active_record'
2
+ require 'active_support/inflector'
3
+
4
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
5
+
6
+ module Tadpoll
7
+
8
+ if defined?(ActiveRecord::Base)
9
+ require 'tadpoll/version'
10
+ require 'tadpoll/option'
11
+ require 'tadpoll/poll'
12
+ require 'tadpoll/vote'
13
+ require 'tadpoll/voter'
14
+ require 'tadpoll/extenders/voter'
15
+ ActiveRecord::Base.extend Tadpoll::Extenders::Voter
16
+ end
17
+
18
+ end
19
+
20
+ require 'tadpoll/extenders/controller'
21
+ ActiveSupport.on_load(:action_controller) do
22
+ include Tadpoll::Extenders::Controller
23
+ end
@@ -0,0 +1,12 @@
1
+ module Tadpoll
2
+ module Extenders
3
+
4
+ module Controller
5
+
6
+ def voter_params(params_object = params[:vote])
7
+ params_object.permit(:voter_id, :vote)
8
+ end
9
+
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,22 @@
1
+ module Tadpoll
2
+ module Extenders
3
+ module Voter
4
+
5
+ def voter?
6
+ false
7
+ end
8
+
9
+ def is_voter(*args)
10
+ require 'tadpoll/voter'
11
+ include Tadpoll::Voter
12
+
13
+ class_eval do
14
+ def self.voter?
15
+ true
16
+ end
17
+ end
18
+ end
19
+
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,75 @@
1
+ module Tadpoll
2
+ class Option < ::ActiveRecord::Base
3
+
4
+ belongs_to :poll
5
+ has_many :votes
6
+ has_many :voters, :through => :votes
7
+
8
+ # Create Option
9
+ def self.new_option(name, poll_id)
10
+ return false if name.blank?
11
+
12
+ option = Tadpoll::Option.new(name: name, poll_id: poll_id)
13
+
14
+ if option.save
15
+ return option
16
+ else
17
+ return false
18
+ end
19
+ end
20
+
21
+ # Vote
22
+ def vote(voter)
23
+ return false if voter.nil?
24
+
25
+ if self.voted_on_by?(voter)
26
+ return false
27
+ else
28
+ vote = Tadpoll::Vote.new(
29
+ option: self,
30
+ voter: voter,
31
+ poll_id: self.poll_id
32
+ )
33
+ end
34
+
35
+ if vote.save
36
+ return true
37
+ else
38
+ return false
39
+ end
40
+ end
41
+
42
+ # Destory Votes with given parameters
43
+ def unvote(voter)
44
+ return false if voter.nil?
45
+ if self.voted_on_by?(voter)
46
+ _votes_ = find_votes_for(voter)
47
+ _votes_.each(&:destroy)
48
+ end
49
+ return true
50
+ end
51
+
52
+ # Votes with given parameters
53
+ def find_votes_for(voter)
54
+ votes.where(voter_id: voter.id)
55
+ end
56
+
57
+ # Count of Votes for an Option
58
+ def vote_count
59
+ votes.count
60
+ end
61
+
62
+ # T / F Has the Option been voted on
63
+ def voted_on?
64
+ votes.count > 0
65
+ end
66
+
67
+ # T / F Has this Option been voted on by given Voter
68
+ def voted_on_by?(voter)
69
+ votes = find_votes_for(voter)
70
+ votes.count > 0
71
+ end
72
+
73
+
74
+ end
75
+ end
@@ -0,0 +1,60 @@
1
+ module Tadpoll
2
+ class Poll < ::ActiveRecord::Base
3
+
4
+ has_many :options, dependent: :destroy
5
+ has_many :votes, dependent: :destroy
6
+ has_many :voters, :through => :votes
7
+
8
+ validates_presence_of :name
9
+
10
+ # Create new poll with option
11
+ def self.create_poll_with_options(name, options = [])
12
+ return false if name.blank?
13
+
14
+ poll = Tadpoll::Poll.new(name: name)
15
+
16
+ if poll.save
17
+ poll.create_options(poll, options)
18
+ return poll
19
+ else
20
+ return false
21
+ end
22
+ end
23
+
24
+ # Create new option for a poll
25
+ def create_options(poll, options = [])
26
+ if options.any?
27
+ options.each do |option|
28
+ Tadpoll::Option.new_option(option, poll.id)
29
+ end
30
+ end
31
+ end
32
+
33
+ # Remove vote from poll for given voter
34
+ def unvote(voter)
35
+ return false if voter.nil?
36
+ if self.voted_on_by?(voter)
37
+ _votes_ = find_votes_for({voter_id: voter.id})
38
+ _votes_.each(&:destroy)
39
+ end
40
+ return true
41
+ end
42
+
43
+ # Votes with given parameters
44
+ def find_votes_for(args = {})
45
+ votes.where(args)
46
+ end
47
+
48
+ # T / F Has this Poll been voted on
49
+ def voted_on?
50
+ votes.count > 0
51
+ end
52
+
53
+ # T / F Has this Poll been voted on by given Voter
54
+ def voted_on_by?(voter)
55
+ votes = find_votes_for({voter_id: voter.id})
56
+ votes.count > 0
57
+ end
58
+
59
+ end
60
+ end
@@ -0,0 +1,3 @@
1
+ module Tadpoll
2
+ VERSION = "1.1.0"
3
+ end
@@ -0,0 +1,17 @@
1
+ module Tadpoll
2
+ class Vote < ::ActiveRecord::Base
3
+
4
+ if defined?(ProtectedAttributes) || ::ActiveRecord::VERSION::MAJOR < 4
5
+ attr_accessible :voter_id, :voter
6
+ end
7
+
8
+ belongs_to :voter
9
+ belongs_to :poll
10
+ belongs_to :option
11
+
12
+ validates_presence_of :voter_id
13
+ validates_presence_of :poll_id
14
+ validates_presence_of :option_id
15
+
16
+ end
17
+ end
@@ -0,0 +1,42 @@
1
+ module Tadpoll
2
+ module Voter
3
+
4
+ def self.included(base)
5
+ base.class_eval do
6
+ has_many :votes, :class_name => 'Tadpoll::Vote', :dependent => :destroy
7
+ end
8
+ end
9
+
10
+ # Vote for an option
11
+ def vote_for(option)
12
+ option.vote(self)
13
+ end
14
+
15
+ # Unvote for an option
16
+ def unvote_for_option(option)
17
+ option.unvote(self)
18
+ end
19
+
20
+ # Unvote for a poll
21
+ def unvote_for_poll(poll)
22
+ poll.unvote(self)
23
+ end
24
+
25
+ # T / F votes for given params
26
+ def voted_on?(args = {})
27
+ found_votes = votes.where(args)
28
+ found_votes.count > 0
29
+ end
30
+
31
+ # T / F Voter voted on given poll
32
+ def voted_on_poll?(poll)
33
+ voted_on?({poll_id: poll.id})
34
+ end
35
+
36
+ # T / F Voter voted for given option
37
+ def voted_on_option?(option)
38
+ voted_on?({poll_id: option.poll_id, option_id: option.id})
39
+ end
40
+
41
+ end
42
+ end
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'tadpoll/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "tadpoll"
8
+ spec.version = Tadpoll::VERSION
9
+ spec.authors = ["Kayla"]
10
+ spec.email = ["kaylacgallatin@gmail.com"]
11
+
12
+ spec.summary = %q{Simple vote system}
13
+ spec.description = %q{Ruby Gem designed to easily create polls with mutually exclusive options for voters to vote on.}
14
+ spec.homepage = "https://github.com/KaylaGallatin/Tadpoll"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.12"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "rspec", "~> 3.2"
25
+ spec.add_development_dependency "sqlite3", '~> 1.3.9'
26
+ spec.add_development_dependency "activerecord"
27
+ end
metadata ADDED
@@ -0,0 +1,134 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tadpoll
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Kayla
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-05-31 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.12'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.12'
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: '3.2'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '3.2'
55
+ - !ruby/object:Gem::Dependency
56
+ name: sqlite3
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 1.3.9
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: 1.3.9
69
+ - !ruby/object:Gem::Dependency
70
+ name: activerecord
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
+ description: Ruby Gem designed to easily create polls with mutually exclusive options
84
+ for voters to vote on.
85
+ email:
86
+ - kaylacgallatin@gmail.com
87
+ executables: []
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - .DS_Store
92
+ - .gitignore
93
+ - Gemfile
94
+ - LICENSE.txt
95
+ - README.md
96
+ - Rakefile
97
+ - bin/console
98
+ - bin/setup
99
+ - lib/generators/tadpoll/migration/migration_generator.rb
100
+ - lib/generators/tadpoll/migration/templates/active_record/migration.rb
101
+ - lib/tadpoll.rb
102
+ - lib/tadpoll/extenders/controller.rb
103
+ - lib/tadpoll/extenders/voter.rb
104
+ - lib/tadpoll/option.rb
105
+ - lib/tadpoll/poll.rb
106
+ - lib/tadpoll/version.rb
107
+ - lib/tadpoll/vote.rb
108
+ - lib/tadpoll/voter.rb
109
+ - tadpoll.gemspec
110
+ homepage: https://github.com/KaylaGallatin/Tadpoll
111
+ licenses:
112
+ - MIT
113
+ metadata: {}
114
+ post_install_message:
115
+ rdoc_options: []
116
+ require_paths:
117
+ - lib
118
+ required_ruby_version: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - '>='
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ required_rubygems_version: !ruby/object:Gem::Requirement
124
+ requirements:
125
+ - - '>='
126
+ - !ruby/object:Gem::Version
127
+ version: '0'
128
+ requirements: []
129
+ rubyforge_project:
130
+ rubygems_version: 2.0.14
131
+ signing_key:
132
+ specification_version: 4
133
+ summary: Simple vote system
134
+ test_files: []