Will_paginate with mongoid (total_entries issue) - ruby-on-rails

When using mongoid with 'will_paginate' gem, it seems like method called 'total_entries' doesnt work which can affect perfomance

So I was using Mongoid with 'will_paginate' gem and I had this problem with 'total_entries' option, and to be more specific, I dont think it was working at all, which is not very good for perfomance in many situations...
After a while I found this gem but it didnt look like 'total_entries' method was working there so here's what I did:
1) Opened mongoid_paginator in will_paginate_mongoid location
2) Made a little fix, so it looked like this:
WillPaginate::Collection.create(options[:page], options[:per_page]) do |pager|
items_count = options[:total_entries] || self.count
fill_pager_with self.skip(options[:offset]).limit(options[:per_page]), items_count, pager
which I took from here and hallelujah, it works again, no more counts on every load.
Since I am a newbie I could make something foolish and I hope I will be corrected if so and hope it'll be helpful for somebody :)

Related

Faker gem - Where is the data being fetched

I'm trying to understand how the Faker rails gem works, so hopefully I can contribute to it. The project is found here: https://github.com/stympy/faker
Under ~/lib/faker/name.rb there will be code like this:
def first_name; fetch('name.first_name'); end
My problem is I don't understand where the hash with all the "name.first_name" is located.
Searching for "def fetch" shows me that the method is defined in lib/faker.rb.
fetch in turn calls translate, which delegates to I8n.translate.

Cannot find gem in controller

I'm sure this is an easy question but I'm having a hard time figuring out what to Google.
I'm trying to use the library ChunkyPNG.
I added it to my Gemfile and did a bundle install.
bundle list | grep "chunky"
* chunky_png (1.2.5)
So far so good.
I try using it in my controller:
image = ChunkyPNG::Canvas.from_data_url(params[:data]).to_image
(The docs for this method are available here)
It results in the following error:
NameError in MyController#create
uninitialized constant MyController::ChunkyPNG
Why is prepending the controller namespace? I imagine that's what is causing the error.
Otherwise, it means that ChunkyPNG is not install (and it clearly is).
Am I not able to use this gem upfront without writing some sort of rails plugin to wrap around it?
Thanks
EDIT:
Question has been answered, see #apneadiving's comment
In your controller, or somewhere else in the app do:
require 'chunky_png'

RoR: cannot get started with ezcrypto / ActiveCrypto

I want to encrypt a field stored in an ActiveRecord table. A few helpful people pointed me towards ezcrypto which appears to be what I want. Especially interesting is ActiveCrypto, as described in ezcrypto.rubyforge.org/files/README_ACTIVE_CRYPTO.html
But I can't get it working. I've added
gem 'ezcrypto', '0.7.2'
to my Gemfile, run bundle install, and extended my User model with:
require 'ezcrypto'
class User < ActiveRecord::Base
...
keyholder
...
end
but that dies with undefined local variable or method 'keyholder'.
Ideas on how to get this working? TIA.
While I was digging around for an answer, I came upon attr_encrypted, which
is actively supported
does what I need
worked the first time
So I don't need an answer to the OP (unless you feel compelled to do so...). Hope this helps someone else.

Geokit and rails 3

I am using the geokit gem and plugin with rails 3. It seems there is a known issue with them, which can be seen here http://github.com/andre/geokit-rails/issues#issue/15
Now, I tried to follow the solution provided at the bottom. I pasted that function definition, at the end of the file, just above acts_as_mapable, and just after the first time it was called, but nothing happened each time.
Any idea what else can be done?
Thanks
I ran into similar problems upgrading my app to rails 3. I'm still using Geokit for geocoding but Active Record scopes for distance based database queries. It's pretty convenient, and you still get all of the Active Record 3 goodness. Here's an example from my User model:
scope :near, lambda{ |*args|
origin = *args.first[:origin]
if (origin).is_a?(Array)
origin_lat, origin_lng = origin
else
origin_lat, origin_lng = origin.lat, origin.lng
end
origin_lat, origin_lng = deg2rad(origin_lat), deg2rad(origin_lng)
within = *args.first[:within]
{
:conditions => %(
(ACOS(COS(#{origin_lat})*COS(#{origin_lng})*COS(RADIANS(users.lat))*COS(RADIANS(users.lng))+
COS(#{origin_lat})*SIN(#{origin_lng})*COS(RADIANS(users.lat))*SIN(RADIANS(users.lng))+
SIN(#{origin_lat})*SIN(RADIANS(users.lat)))*3963) <= #{within}
),
:select => %( users.*,
(ACOS(COS(#{origin_lat})*COS(#{origin_lng})*COS(RADIANS(users.lat))*COS(RADIANS(users.lng))+
COS(#{origin_lat})*SIN(#{origin_lng})*COS(RADIANS(users.lat))*SIN(RADIANS(users.lng))+
SIN(#{origin_lat})*SIN(RADIANS(users.lat)))*3963) AS distance
)
}
}
Here's a blog post with a little more detail on the subject: http://stcorbett.com/code/distance-queries-with-rails-3-without-geokit/
jlecour's port to rails 3 should solve any issues you were having last year.
Make sure you're using mysql or postgres if you're doing distance calculations.
After trouble installing the geokit-rails3 gem on Rails 3.1 I moved to the geocoder gem. It has distance calculation as well (be sure to not forget the s in #your_model.nearby*s*(5)). There is also a Railscast.
Here is port of geokit to rails 3, incomplete through:
https://github.com/jlecour/geokit-rails3
For those still having trouble with geokit, i moved on to using mongodb... which has inbuilt distance search n all...
Hey Amit, Not sure if you sorted this out yet but I'll tell you what I did just in case.
I forked andre's geokit-rails source and then cloned it locally and added the code from this gist at line 34 of lib/geokit-rails/acts-as-mappable.rb, just after the line that reads module ClassMethods # :nodoc:.
Then I commited those changes back to my forked repo on github and used my fork to install the source as a plugin to my rails 3 app. That seemed to work straight away, but make sure you have the acts_as_mappable line added to whatever model you are wanting to do distance calculations on and make sure you have two float columns on that db named :lat and :lng.

Flickr gem : how to access user details?

https://github.com/ctagg/flickr/tree/master
I'm trying the example in the home page of the GEM Flickr.
require 'flickr'
flickr = Flickr.new('MY_KEY')
user = flickr.users('sco#scottraymond.net')
user.name
user.location
While I'm able to get the user object, I can't get any of the other attributes like name, location etc. How to get those details?
I worked through the problem I was experiencing above in my comment but I am not having difficulty getting people's attributes. The following code works as expected in a ruby script/console session:
require 'flickr'
f = Flickr.new('<<MY_KEY>>')
u = f.users('sco#scottraymond.net')
puts u.name
puts u.location
Substituting my username ('topherfangio') instead of Scott's, the name attribute works, but apparently my location is nil.
Could you describe a little bit more about exactly what you are getting? Any errors or is it just blank? Have you tried multiple users?
Edit 1: Just got your API key and it doesn't work for me either, try the following and see if that makes any difference. Also, are you using a non-commercial key or a commercial key?
f = Flickr.new(:api_key => 'MY_KEY', :shared_secret => 'MY_SECRET')
My guess is that it's not fully authenticating or something like that.
Edit 2: I'm a freaking liar...I was typing f = Flickr.new(...); f.name which was breaking. Your API key is working correctly for me. Perhaps it's your network or something?
To not entirely answer your question, I've been using the Fleakr gem and found it to be extremely straightforward.
Documentation is good and you do everything in the code once it is installed.
No tricks, just sudo gem install fleakr
It depends on the builder gem, so also do sudo gem install builder.
Once I switched to Fleakr my project finished quickly!
It looks like the flickr gem isn't being maintained. Uninstall the gem by doing a gem uninstall flickr, then install a more up to date branch of it by doing a gem install moonpxi-flickr

Resources