GithubHelp home page GithubHelp logo

mercuryseries / neutral Goto Github PK

View Code? Open in Web Editor NEW

This project forked from petertoth/neutral

0.0 3.0 0.0 150 KB

Voting solution for Rails 4+ applications

License: MIT License

Ruby 95.82% CSS 3.22% JavaScript 0.96%

neutral's Introduction

Neutral

Neutral is a rails engine providing ajaxful positive/negative voting solution for your ActiveRecord objects along with couple of additional features.

Preview

Installation

Add following into your Gemfile

gem 'neutral'

Run

$ bundle

  Or install it yourself as

$ gem install neutral

Generation

Generate required files using

$ rails g neutral:install

And proceed to migrate newly created migration files

$ rake db:migrate

Usage

Make your ActiveRecord model voteable

class Post < ActiveRecord::Base
  neutral
end

And select your voter model

class User < ActiveRecord::Base
  neutral_voter
end

Note: If your voter model is not User configure this setting inside neutral.rb intializer

To display voting use voting_for helper in your view : 

<%= voting_for post %>

Customization

View

You can also pass two additional options to voting_for helper

  • icons : Icons used for positive/negative/remove
  • difference : Display difference count instead of positive/negative counts
    If the difference is positive i.e positive votes - negative votes > 0 then the color of the span is green, red otherwise

Example : 

<%= voting_for article, icons: :myfabulousicons, difference: true %>

Configuring Neutral

In config/initializers/neutral.rb you can configure these options :

  • can_change : Determine wheter a voter can change(update,delete) his vote
  • current_voter_method : This is most typically current_user but if not, change this to your own representation of a voter instance e.g current_client or current_customer. Also make sure it accessible from your ApplicationController.
  • require_login : Voter is/is not required to be authenticated in order to vote. Setting to false is not recommended
  • vote_owner_class : Configure an owner of a vote. Pass string of class name of the model you added neutral_voter into. For example 'User', 'Client' or 'Customer'
  • default_icon_set : This icon set is used when no explicit icon set is passed to voting_for helper

Font Awesome

This engine enables you to define your own icon sets using FontAwesome icons that provide representation of voting related actions such as like, dislike, remove, plus, minus, whatever ...

You can use any FontAwesome icon class available

Define an icon set in your neutral.rb initializer

#config/initializers/neutral.rb

Neutral.define do
  set :myicons do
    positive "fa-thumbs-up"
    negative "fa-thumbs-down"
    remove "fa-times"
  end

  set :fabulous do
    positive "fa-plus"
    # If any of action is omitted, definition from your default icon set is used.
    # In this case "fa-thumbs-down-o" would be used for negative and "fa-times" 
    # for remove action because because :thumbs(default set) contains these definitions.
  end
end

Finally use it as follows :

<%= voting_for voteable, icons: :myicons %>

If you ever need to access a definition of a given icon set proceed analogically as follows : Neutral.icons.thumbs.positive

i18n

Define your custom error messages for each of your locale in config/locales/neutral.yml

en:
  errors:
    duplication: "You have already selected this option!"
    cannot_change: "You are not permitted to change the vote!"
    require_login : "You must be logged in to vote!"

Uninstallation

Run : 

$ rails g neutral:uninstall

It :

  • comments neutral in your routes.rb

  • removes require neutral from your stylesheet manifest

  • removes neutral.rb initializer

  • removes neutral.yml locale file

  • removes database entities(engine's tables) and migration files - optional

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

License

MIT License. Copyright (c) 2014

neutral's People

Contributors

petertoth avatar mercuryseries avatar

Watchers

James Cloos avatar  avatar  avatar

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.