Skip to main content

Posts

Showing posts with the label Rails

JavaScript on Rails 2016

Whilst I am primarily a JavaScript programmer, my framework of choice is Ruby on Rails. A lot of JS guys like Node or other frameworks which are JS all the way through, but there's a lot to like about Rails, even in 2016 and it's worth learning another language (Ruby) or two (SQL) in order to get the best environment on which to write your JavaScripts. Before I say what I like about Rails though, I will take you through what I don't like just to balance things out. What I don't like about Rails Asset Pipeline still does not let you use ES6 Modules.  Even at this late stage, one still cannot access ES6 Modules in Rails in a "Railsy" manner. You can either bypass the whole Asset Pipeline or else separate out the JS using Webpack or whatever, but in a standard Rails project, the fact that the asset is generated with unique hash in the file name before the extension pretty much nullifies its use for ES6 Modules which need a static location to store the fi...

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 ...

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...

Cucumber - my perspective

I have been using Cucumber ( http://cukes.info/ ) at my current gig for about a year now. My initial reaction was that I absolutely hated it. It didn't seem to make sense for a programmer to write out tests (features) in plain English and then write out a bunch of regular expressions to turn that plain English into runnable code. What a palaver! The other problem, is that the Cucumber tests were extremely fragile. Even making text and/or HTML changes would break things in lots of random places. Anyways, as it turns out, I don't really hate Cucumber, I just hate the way it is implemented in my current gig. Here are some lessons I learnt on the way... 1) Features are not supposed to be written by programmers. You can write features as a programmer, but you are not the intended audience. The reason why features are written in plain text is that they are supposed to be written by business owners. As a programmer though, you can use features to organize your thoughts in plain Engli...

JSON caching with Rails

So the other day, I needed to cache an action which was basically a proxy action to return JSON. Basically, 3rd party company X has an XML API for it's deals. Ideally we would use JavaScript to pull the feed, but unfortunately, the feed is using XML which means we need to use the server to pull this "deal" and then reformat it in JSON for our site to use. To render the JSON we were using a render call render offer.to_json It can get expensive to pull this deal over the wire every time, so we looked into using caches action and set it to expire in 5 minutes (because that is how often the deal feed updates). caches_action :index, :expires_in => 5.minutes (Oh, also we need to turn caching on in dev to see this happening) config.action_controller.perform_caching = true While this worked fine for the first call, I noticed that in the subsequent calls, the application type was being set to 'text/html' instead of 'application/json'. This was causing the ...

Patterns

Just a couple of links today (more details to follow). I found a great reference for JavaScript and jQuery Design Patterns here: http://addyosmani.com/resources/essentialjsdesignpatterns/book/ Also, I learned about the Presenter pattern today. We were having a discussion at work about what to do with presentation logic in Rails. On the one hand you don't want it in the controller because you want to keep your controllers thin, on the other hand you don't want it in the models either because it's presentational. You can't put it in the erb because they shouldn't contain logic. Enter the Presenter Pattern: http://donttrustthisguy.com/2008/05/29/using-presenters-in-rails-3/ http://blog.jayfields.com/2007/03/rails-presenter-pattern.html Enjoy your homework!

Rails file uploads: Limiting a user's uploads by space available

Let's say that you want to allow people to upload files to your server. Let's also say that we want to make sure any particular user does not hog up all the space available. Let's also say you want to give each user a variable limit as to how much they can upload (i.e. for tiered services). How would you go about doing that? Well, from a pseudo code perspective you would do the following. 1) Create a folder for each user based on the user id 2) Make sure all the user's uploads go to that folder 3) Before you do an upload, check against the user's settings to see how much space s/he is allowed 4) Check their folder to see how much space has been used 5) If they have no space left, message the user So let's see how we do this in practice... I guess we really want to see how much space is used, so let's start there on the User. In this example the user has a UUID as well as a space_allowed attribute which is set to a default value (and which an admin can change...

Freezing Gems

What is a gem and why would you want to freeze it? In Ruby, there are times when you want to access pieces of functionality that other people of written (3rd party libraries) and you normally have 2 options. You can install a plug in or install a gem. Normally the method you use is determined by which ever is made available by the author. Gems are installed on the host machine and are pretty handy when you want to run things in the command line or else across lots of projects, but their downside is that if you use a gem in a Rails project there is no automatic publishing mechanism when you deploy your site. You will need to log onto the remote host machine and install the gem manually. Plugins are specific to Rails and are similar to gems in that they are also 3rd party libraries. However they are associated with your Rails project as opposed to your machine so they will get posted to the server on a regular deploy. Freezing a gem is the process of transforming a gem into a plug in. Es...