Kaminari pager not working with Sinatra and Mongoid? - ruby

Can't get Kaminari to work with Sinatra and Mongoid. I'm getting this error:
NoMethodError at /api/events
undefined method `page' for #<Mongoid::Criteria:0x007fccb7828c38>
Here is minimal code to get the error:
Gemfile
source "https://rubygems.org"
gem 'mongoid'
gem 'sinatra'
gem 'kaminari-mongoid'
gem 'kaminari-sinatra'
server.rb
require 'mongoid'
require 'sinatra'
class Event
include Mongoid::Document
end
get '/events' do
Event.desc(:id).page(params[:page]).per(10)
end
I have tried require 'kaminari', require 'kaminari-sinatra', require 'kaminari-mongoid', all to no avail (I get LoadErrors). I've also tried register Kaminari::Helpers::SinatraHelpers as mentioned here, which also failed.
I've followed the instructions in detail, and have scoured Google and StackOverflow to no avail. This answer didn't work. I can't help thinking I'm missing something easy; I'm not a Ruby veteran. My hunch is it's something with Bundler. Any idea?

I ran into the problem as well. Unfortunately, kaminari-mongoid has a rails dependency (you can look in the gemspec file here: https://github.com/kaminari/kaminari-mongoid/blob/master/kaminari-mongoid.gemspec). Therefore, it is not possible to use both kaminari-sinatra and kaminari-mongoid.
This solved my problem. https://github.com/ajsharp/mongoid-pagination. Add it to your Gemfile and install with Bundler.
In your app.rb file, require 'mongoid-pagination'

Related

How to encode string in sinatra using AES?

Spent a lot of my time searching a way of ecrypting string in my app, but didn't find a right solution to use it in Sinatra. For example, I tried to 'require aes' gem (https://github.com/chicks/aes) also gibberish (https://github.com/defunkt/gibberish/blob/master/lib/gibberish.rb) gem but all time I see errors in browser.
I try to encrypt message field in my app:
require 'aes'
get '/auth/signup' do
user = User.new(url: Helpers.random, message: AES.encrypt("A super secret message", 'Here we go!'))
user.save
end
Please, help me how to solve this?!
When using Ruby gems you need to declare them up-front. The easiest way is:
gem 'aes'
require 'aes'
You can also use Bundler and declare them in a Gemfile like:
source 'https://rubygems.org/'
gem 'aes'
Then in your code:
require 'bundler/setup'
require 'aes'
Where Gemfile describes all your dependencies so you can easily reinstall them if necessary.
Once you've created the Gemfile you can do bundle install. If that has issues you'll be alerted. bundle check can verify everything's set up properly.

Uninitialized constant Phash

I started to learn ruby ​​recently. I trying to use a library pHash on ruby for my own project. I trying during for 2 days, but I don't understand what I'm doing wrong.I included old and new versions library, I installed in local directory and home directory. I got constantly error on compile ruby and in command line ubuntu. At last I tried to use a bundler/inline and got next error.
Code here:
require 'bundler/inline'
gemfile do
source 'https://rubygems.org'
gem 'pHash', require: 'ffi'
gem 'rspec', require: false
end
Phash::Text.new('first.txt') % Phash::Text.new('second.txt')
The last line I take from documentation.
require: 'ffi' seems unlikely to do what you need... and indeed the examples in the documentation all include a specific require call first. The one you referenced looks like this in full:
require 'phash/text'
Phash::Text.new('first.txt') % Phash::Text.new('second.txt')

Require order in a ruby script

I have a ruby script that requires 3 gems to work. My script starts as follows:
#!/usr/bin/env ruby
require 'httparty'
require 'imgkit'
require 'twitter'
Now, interestingly, the above code works, but only if I require httparty first or second. If I require it as the third dependency, I get the following:
'require': cannot load such file -- httparty (LoadError)
I'd love to learn why this is happening, so I can better understand how ruby handles gem dependencies. Many thanks!
Edit: I am using bundler. This is my Gemfile:
source 'https://rubygems.org'
ruby '2.2.2'
gem 'wkhtmltoimage-binary'
gem 'imgkit'
gem 'twitter'
gem 'rspec'
gem 'httparty'
Following tadman's advice, adding require 'bundler/setup' solved the problem. More in the docs. Thanks!

How to color unit tests with lib minitest or Test:Unit?

I would like to have unit tests output color in my dev environment. However, I can't make it work on Linux (Debian and Ubuntu). When I include the following libs:
require 'minitest/autorun'
require 'minitest/unit'
require 'minitest/pride'
I get:
/usr/local/rvm/gems/ruby-1.9.2-p136/gems/minitest-2.3.1/lib/minitest/pride.rb:35:in `<top (required)>': undefined method `output' for MiniTest::Unit:Class (NoMethodError)
caused by the code:
MiniTest::Unit.output = PrideIO.new(MiniTest::Unit.output)
I have seen a working Rspec variant. Unfortunately, my Ruby knowledge is not enough to see differences.
Give turn a whirl.
Add this to your Gemfile:
group :test do
gem 'turn', :require => false
end
step 1 : use the latest version of the gem (I think it will be fixed in Ruby 1.9.3)
gem install minitest
step 2 : require "minitest/pride" on the command line, not in your code
ruby -rminitest/pride your_ruby_script.rb
.. and in your code simply require 'minitest/autorun'
require 'minitest/autorun'
If you use Rubymine, just add
-rminitest
in the default configuration of the tests.
=> the configuration would like
-e $stdout.sync=true;$stderr.sync=true;load($0=ARGV.shift) -rminitest/pride
simply add these lines to your test_helper.rb file after require 'rails/test_help'
require "minitest/reporters"
Minitest::Reporters.use!
in your gemfile add
gem 'minitest-reporters', '~> 1.0.7'
this will get your rake test to be in red and green, but it also brings up the backtrace. to get rid of all those extra backtrace logs add this to your gemfile then bundle:
gem 'mini_backtrace'
then in config/initializers/backtrace_silencers.rb add this line to cut all the extra rvm stuff
Rails.backtrace_cleaner.add_silencer { |line| line =~ /rvm/ }
hope that works for you, let me know if you need more details.
See https://github.com/tenderlove/purdytest/issues/1. It seems to be a known bug with the minitest version shipped with 1.9.2. For the others, adding
gem "minitest"
at the very top of your file did the trick.
I currently use purdytest with 1.9.2
Try look at this post about using turn gem for nice looking and configurable output for minitest.
http://rawonrails.blogspot.com/2012/01/better-minitest-output-with-turn-gem.html
$ gem install mynyml-redgreen --source http://gemcutter.org
# in your test file
require 'redgreen'
redgreen and turn work nicely in conjunction with each other, by the way

Bundler gem installed from github get installed in a different location

I am trying to install a gem from github like this:
gem 'omniauth', :git => "git://github.com/intridea/omniauth.git", :require => "omniauth"
The problem is that the gem is not actually being loaded. The ruby objects are not there.
So, bundle show omniauth shows me: Users/felipe/.rvm/gems/ruby-1.9.2-p136/bundler/gems/omniauth-5972c94792cf
The problem is that the gem is being installed to a different location from the regular ones. I expected it to be `/Users/felipe/.rvm/gems/ruby-1.9.2-p136/gems/``
Any idea on how to fix this?
I think you're missing these two lines:
require "rubygems"
require "bundler/setup"
as you can see in Bundler's source code, "bundler/setup" is going to put gems managed by Bundler in the Ruby's load path:
https://github.com/carlhuda/bundler/blob/1-0-stable/lib/bundler/setup.rb#L22
Hope this helps :)
try changing the bundler line to.
gem 'omniauth', :git => "git://github.com/intridea/omniauth.git", :require => 'oa-oauth'
The problem is that your :require property was pointing to the wrong file to load. It is not always the same name as the library, by the way, when both lib name and require are the same you don't need to specify it, only when they differs.

Resources