Skip to main content

Posts

Rolify, Devise, Rspec and Capybara Webkit in Rails

So I was using Rolify, Devise, Rspec and Capybara Webkit in Rails and I ran into a problem with the Warden login_as helper when trying to do a feature spec with :js => true (if you didn't understand any of that, you are probably on the wrong blog). Anyways, while the I was able to log in an admin, Rolify was not able to determine the roles. This is in spite of the fact that I added a role in Factory Girl. FactoryGirl.define do factory :admin do email { Faker::Internet.email } password "password" password_confirmation "password" trait :administrator do after(:create) {|admin| admin.add_role(:administrator)} end end end This worked fine with js turned off, so why was it not working now? So the answer had to do with the following line in rails_helper.rb config.use_transactional_fixtures = true So I believe what happened in Capybara webkit is that it runs in a different thread and while Warden's login_as worked fine ...

Global Warming is a Year 2000 problem

Global Warming is a Year 2000 problem. What I mean is not that Global Warming has been solved (far from the case) nor that it is a problem that we could only have solved 15 years ago (though that might be closer to the truth). What I mean is that if we solve Global Warming, then people will think it was a hoax (or something we should not have worried about). Think back to the glory days of the 1990s. Media was full of stories about how all the world's computer systems were going to fail because programmers tried to save space on their systems by dropping the first 2 digits off the year field. Power stations and ATMs would stop working. Global financial crises would ensue (well, that did happen, but for other reasons...). Basically we were going back into the stone age unless we chucked large amounts of money at software developers to fix our computer systems and by and large that did happen. After we partied like it was 1999, apparently nothing happened. I am sure a couple ...

Source Location, i.e. Where the heck is this method defined?

Just a short one for now. Most of the time, if you are working on a well defined project, it's pretty easy to find where a function is if you want to change/tweak it. You can look in the class where it is defined and/or see if there are any mixins or decorators. Sometimes, it can be downright impossible to tell. Enter source_location Fire up your rails console and put in the following (for a class method :search on User) User.method(:search).source_location if you have an instance method User.new.method(:search).source_location This should give you something like the following [ "/Users/yourdrive/Projects/prokect/vendor/cache/thinking-sphinx-9b71ce0f7d9d/lib/thinking_sphinx/search_methods.rb" ,   367 ] This should at least give you some idea of what is going on...

Mixins in ES6

I had been looking for a good way to do mixins in ES6. Looks like I finally found one. http://raganwald.com/2015/06/10/mixins.html Essentially, create the mixin as a const Use Object.assign to add the properties to the Class const BookCollector = { addToCollection ( name ) { this . collection (). push ( name ); return this ; }, collection () { return this . _collected_books || ( this . _collected_books = []); } }; class Person { constructor ( first , last ) { this . rename ( first , last ); } fullName () { return this . firstName + " " + this . lastName ; } rename ( first , last ) { this . firstName = first ; this . lastName = last ; return this ; } }; Object . assign ( Person . prototype , BookCollector ); (I am copying and pasting this for personal reference in case the original site link goes down).

The perils of over testing

Test Driven Development (TDD) is a great thing. It allows us to write code "safe" in the knowledge that if we accidentally break something in another part of the system, we will be alerted to it and can re-write or fix accordingly. It's such a good thing that often in large teams people will insist on this idea of 100% code coverage (i.e. every piece of working code has a corresponding test). Also, some people are compelled to test every single permutation and variation of how someone will access their program/website. TDD does have some costs however. Some are evident, but others not so. Cost 1 - Tests take time to run True, automated testing is faster than manual testing, but they do take time to run. This time means every time you make a change to the code, you need to wait for the suite to finish running in order to feel that your code is "safe" for deployment. Most of this time is a necessary evil. A lot of it is unnecessary... Should you write...

ECMAScript 6

Today is a rather significant one for me as JavaScript/Rails programmer because Sprockets ( https://github.com/rails/sprockets ) upgraded to version 3 meaning I can now use the Sprockets-es6 gem ( https://github.com/josh/sprockets-es6 ) to transpile ES6 JavaScript to ES5. Now to those that didn't understand the last paragraph, here's what it means... The language we commonly call JavaScript is actually formally known as ECMAScript ( http://en.wikipedia.org/wiki/ECMAScript) . Since 2009 all the major browsers have been interpreting ECMAScript version 5. In fact they mostly still do. Version 6 is due to be formalised any day now (well, June 2015) which means that a whole new slew of functionality is due to be added to the language (class constructors, formal inheritance, fat arrows, etc...). Browser makers will start implementing these new features once the spec is complete. All of this sounds great, but for 2 things 1) I want to start using ES6 today 2) Even if the ne...

Opal - Ruby to JavaScript compiler

So I just found out about the Opal Ruby to JavaScript compiler and I am very intrigued. http://opalrb.org/ So for a few years now, the Ruby community has been encouraged to use CoffeeScript as a JavaScript "alternative" ( http://coffeescript.org/ ) but for some reason, as an avid JavaScript programmer, it never appealed to me. CoffeeScript offers a small amount of syntactic sugar on top of JavaScript, but it never seemed worth the learning curve for me to take it on. I don't actually mind JS's brackets (as they let you know where things begin and end cleanly) and significant white space in a programming language has always seemed like a bad idea too (it's bad enough that a misplaced semicolon can stop a program from running properly, but try searching for an extra white space character you can't even see!). Opal on the other hand, is actually Ruby compiled to JavaScript (not Ruby/Python-ish syntax). It looks, feels, and even smells like Ruby because it i...