Thursday, March 19, 2015

minFraud Ruby gem

So I'm working on this new project that connects to the minFraud service, and there isn't a whole lot out there Ruby-wise for working with the API. The one gem that I did find is called minfraud-ruby. However, this gem seemed very limited. The minFraud service itself only requires you to send it an IP address, and a license key, but the gem also required city, state, zip and country. When you're working with IP addresses from countries outside of the US, some or all of these attributes will not be available. The gem also only returned a risk score. While the risk score is useful, there's much more useful data returned in the response from minFraud, such as ip_city, ip_region (which in the US is the state), ip_country_name, etc.
After working with the limitations for a while, I decided to fork the gem and give it a bit of an overhaul. I updated the required fields, so that now all you need is a license key and an IP address to get a response. Providing additional information will get you a more accurate response from the service, but this is not required. I also exposed a number of additional attributes in the transaction object, so you can retrieve things like city, state and country without any additional steps.

Happy coding, kids. Until next time..

Tuesday, July 9, 2013

Heroku fails precompiling assets with MySQL and Rails 4

I've been having a hell of a time deploying this app I'm working on to Heroku. For some reason, during the deployment process, while Heroku was precompiling assets, it would attempt to access the database at 127.0.0.1 and fail (obviously). I could precompile the assets locally, and deploy without a problem. At least, I thought it was without a problem. After implementing a WYSIWYG text editor into part of the app, the icons weren't showing up on the various buttons (for bold, italic, etc). So I decided to further investigate WHY my assets weren't precompiling on Heroku. I hopped in the Rails IRC channel, and a suggestion was proposed to try precompiling locally, with the environment set to production, but with a database that does not exist. Doing so should help to debug where the root of the cause was, and it did: Rails was failing on one of my validations. Enter: confusion. Why on earth is Rails attempting to run this validation whilst precompiling my assets? The validation in question is as follows:
validates :agree_to_contract, :acceptance => { :accept => true }, :on => :update, :if => :signer_full_name

agree_to_contract is a boolean, and signer_full_name is a string. I wanted the two fields to depend upon each other, and fail if one of them did not exist (there was a similar validation on signer_full_name for going in the other direction).
None of my initializers accessed the model which contained these validations, but yet - there was no way for me to remedy the issue. I attempted to wrap the validation in an ActiveSupport.on_load do end; still, the error persisted. This method seemed to be the culprit. It appeared as though the :acceptance block on the validation was causing the issue, but I was pretty much out of information to gather, so I headed over to the Rails issues tracker, to report a bug. After a bit of deliberation, and me producing some sample apps that displayed said behavior, it turned out that that this is, indeed, a bug. The cause is presently unknown, however, one of the contributors to the post hypothesized that the cause was eager_load! Hopefully the issue is resolved soon, and we can deploy checkboxes with validations to our heart's content ;)

Update:
A fix has been proposed, and it's working in my app! It's a pretty simple fix, too. edit config/environments/production.rb in your own app with the following:

replace config.eager_load = true with config.eager_load = true if config.eager_load.nil?

That's it. You should no longer have precompiling assets break with the :acceptance validation.

rails_admin with will_paginate undefined method `per'

So, I was going a bit crazy yesterday while working with the rails_admin gem. When I would click on my Users model, it would complain that I had an undefined method 'per'. Or, more specifically, the following:
NoMethodError (undefined method `per' for #<ActiveRecord::Relation::ActiveRecord_Relation_User:0x007fc945ba2b40>):
So, I did my usual rounds, searching Google and checking with the folks in the Rails IRC channel. It sounded like something related to pagination, but I hadn't touched anything that rails_admin would use related to pagination. I *had* added the will_paginate gem, so I tried commenting out the code I'd added to that, to no avail. Eventually, I got around to commenting out the will_paginate gem in my Gemfile, and voila! The error disappeared, and rails_admin worked as expected. Baffled, I added will_paginate to my search, and ran across this link. I added the work-around posted by @jackquack to config/initializers/will_paginate.rb:

(Note: Check updated code further down)
if defined?(WillPaginate)
  module WillPaginate
    module ActiveRecord
      module RelationMethods
        alias_method :per, :per_page
        alias_method :num_pages, :total_pages
        alias_method :total_count, :count
      end
    end
  end
end

I then uncommented the will_paginate code that I had commented out and cross my fingers. To my relief, upon booting up the server and going back in to rails_admin, everything was working!

So I decided to post about it in hopes of relieving someone else's /facedesk'ing :]

// Update 6/2017 I have just run into another issue a new issue with this solution:

! Unable to load application: NameError: undefined method `per_page' for module `WillPaginate::ActiveRecord::RelationMethods'

So the further solution that I have come across is to wrap the code in an ActiveSupport on_load block:
if defined?(WillPaginate)
  ActiveSupport.on_load :active_record do
    module WillPaginate
      module ActiveRecord
        module RelationMethods
          alias_method :per, :per_page
          alias_method :num_pages, :total_pages
        end
      end
    end
  end
end