Giter Site home page Giter Site logo

tadpoll's Introduction

Tadpoll

Tadpoll is a Ruby Gem designed to easily create polls with mutually exclusive options for voters to vote on.

Installation

Add the following to your application's Gemfile:

gem 'tadpoll'

Followed by a bundle install.

Database Migrations

Tadpoll uses 3 tables: Vote, Poll, and Option to store all voting information. To generate and run the migration do:

rails generate tadpoll:migration
rake db:migrate

Usage

Voter Models

class User < ActiveRecord::Base
  is_voter
end

poll = Tadpoll::Poll.create_poll_with_options("Best P&R Character", ["Leslie Knope", "Ron Swanson"])
option = poll.options.first
@user.vote_for(option)

@user.voted_on_option?(option) # => true
@user.voted_on_poll?(poll) # => true

@user.unvote_for_poll(poll)
@user.voted_on_option?(option) # => false
@user.voted_on_poll?(poll) # => false

Polls

Polls have a name, and mutually exclusive options for voter models to vote on.

# create a poll with options
poll = Tadpoll::Poll.create_poll_with_options("Best Star Wars Character", ["Han Solo", "Leia Organa"])
poll.create_options(["Luke Skywalker"])
poll.options.map{|o| o.name} # => ["Han Solo", "Leia Organa", "Luke Skywalker"]

# voter models can vote for options on a poll
option = poll.options.first
@user.vote_for(option)

poll.voted_on? # => true
poll.voted_on_by?(@user) # => true

# remove votes for a user
poll.unvote(@user)
poll.voted_on_by?(@user) # => false

Options

Options belong to individual polls, and have many votes.

# create a poll and add new options
poll = Tadpoll::Poll.create_poll_with_options("Best GoT Character", ["Jon Snow", "Brienne of Tarth"])
Tadpoll::Option.new_option("Arya Stark", poll.id)
poll.options.map{|o| o.name} # => ["Jon Snow", "Brienne of Tarth", "Arya Stark"]

# vote on options
option = poll.options.first
option.vote(@user)

option.vote_count # => 1
option.voted_on? # => true
option.voted_on_by?(@user) # => true

# remove user votes
option.unvote(@user)
option.voted_on_by?(@user) # => false

Testing

All tests are located in the spec directory and follow the RSpec format.

License

The gem is available as open source under the terms of the MIT License.

tadpoll's People

Watchers

Kayla McEowen avatar

tadpoll's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.