Search code examples
ruby-on-railsrubysinatraacts-as-votable

Does anyone know any useful gem to create voting system in Sinatra?


Hi I am creating a small Sinatra app and trying to create a voting system in it.

I tried using acts_as_votable gem but it is giving me some error.

rake db:migrate                                                                                                                                  
rake aborted!                                                                                                                                         
TypeError: ActsAsVotable is not a class

Here is my migration file which I tried to make from source code of gem. class ActsAsVotable < ActiveRecord::Migration[5.1] def self.up create_table :votes do |t|

  t.references :votable, :polymorphic => true
  t.references :voter, :polymorphic => true

  t.boolean :vote_flag
  t.string :vote_scope
  t.integer :vote_weight

  t.timestamps
end

if ActiveRecord::VERSION::MAJOR < 4
  add_index :votes, [:votable_id, :votable_type]
  add_index :votes, [:voter_id, :voter_type]
end

add_index :votes, [:voter_id, :voter_type, :vote_scope]
add_index :votes, [:votable_id, :votable_type, :vote_scope]


end

  def self.down
    drop_table :votes
  end
end

I also created Acts_as_votable module from the gem source code. Please see below code

require 'active_record'
require 'active_support/inflector'

$LOAD_PATH.unshift(File.dirname(__FILE__))

module ActsAsVotable

  if defined?(ActiveRecord::Base)
    require 'acts_as_votable/extenders/votable'
    require 'acts_as_votable/extenders/voter'
    require 'acts_as_votable/vote'
    ActiveRecord::Base.extend ActsAsVotable::Extenders::Votable
    ActiveRecord::Base.extend ActsAsVotable::Extenders::Voter
  end

end

require 'acts_as_votable/extenders/controller'
ActiveSupport.on_load(:action_controller) do
  include ActsAsVotable::Extenders::Controller
end

Any suggestion to make this work or any other alternet solution for Sinatra?


Solution

  • As you can see in the "Acts As Votable" documentation, it's a gem integrated with the Rails framework, so I would not bet on it working correctly with Sinatra (even though you use ActiveRecord Migrations).

    In the end you can either switch to Rails or write the voting code yourself; you can see an example posted by Gerry or try "Create your first voting app with Sinatra".