Stream of Ry

Yet Another Geek In The World

Archive for the ‘Programming’ Category

Today’s Development Notes

leave a comment

I encountered an error today while pushing to one of our webbies which was related to this.

Fixed it by upgrading the rubygems on our webby to 1.8.23 (it was at 1.4.x !! DX)

I’ve decided to upgrade one of our apps up to Rails 3.1.4. I’ve then encountered this while compiling my assets. Rolled back my sass-rails to 3.1.4 (it was at 3.1.6) to fix the compiling issue.

You can push another app to the same webby by running wn push. You just have to configure a new Nginx (in our case) config file in /opt/nginx/phd-sites/. I’m loving Webbynode more and more every time I deploy.

Written by rystraum

April 20th, 2012 at 5:59 pm

Posted in Programming

Tagged with , ,

Guide: Automatic Browser Refresh on Linux

leave a comment

This is a sort of follow up to my old post which wasn’t particularly helpful aside from providing links.
This time, I’ll be using xdotool and splitbrain’s fork of Watcher
with some slight modifications.

Read the rest of this entry »

Written by rystraum

April 7th, 2012 at 5:39 pm

Posted in Programming

Tagged with , ,

How to make autotest work on Linux

one comment

This is how I got autotest to work with Linux:

  1. Install autotest via: gem install autotest
  2. Create a .autotest file on your home folder which contains this:
          #!/bin/ruby
          require 'autotest/gnomenotify'
  3. Create a gnomenotify.rb at $GEM_HOME/gems/ZenTest-x.y.z/lib/autotest/ which contains:
          # -*- ruby -*-
          # Adapted from PeepCode RSpec screencast no 1, 
          # who got it from someone else!
          # Adapted with tip from http://ph7spot.com/articles/getting_started_with_autotest
          # by DitoCujo (http://ditoinfo.wordpress.com)
          module Autotest::GnomeNotify
            EXPIRATION_IN_SECONDS = 5
            ERROR_ICON = "gtk-dialog-error"
            SUCCESS_ICON = "gtk-dialog-info"
            def self.notify(title, msg, icon)
              options = "-t #{EXPIRATION_IN_SECONDS * 1000} -i #{icon}"
              system "notify-send #{options} '#{title}' '#{msg}'"
            end
            Autotest.add_hook :ran_command do |at|
              results = [at.results].flatten.join("\n")
              output = results.slice(/(\d+)\s+examples?,\s*(\d+)\s+failures?(,\s*(\d+)\s+not implemented)?/)
              if output
                if $~[2].to_i > 0
                  notify "FAIL", "#{output}", ERROR_ICON
                else
                  notify "Success!", "#{output}", SUCCESS_ICON
                end
              end
            end
          end
  4. Run autotest from rails root: autotest

I recommend getting the spork gem to speed up tests.

If I ever skipped some parts or you encounter any errors, please let me know as I’ve had this running for quite some time now and I don’t want to mess up my current setup just to recreate the steps.

Written by rystraum

January 3rd, 2012 at 3:22 pm

Posted in Programming

Tagged with

Rspec Shared Example for denying access to CRUD

one comment

I’m currently working on an app that has 4 user contexts: not logged in, logged in as subscriber, logged in as campaign manager and logged in as administrator. I guess you can imagine the horror for writing tests for them. As you’d probably guess, I only want to allow access of some controllers to a particular user context and barring everything else.

Spent an hour or two making the following shared context work:

shared_examples "denied on all resource methods" do |class_name, attributes|
  before(:each) do
    @subject = class_name.capitalize.constantize.create! attributes
  end
 
  actions = {
    :index => :get,
    :show => :get,
    :new => :get,
    :create => :post,
    :update => :put,
    :destroy => :delete
  }
 
  actions.each_pair do |action, verb|
    specify "I should be denied access to ##{action}" do
      send(verb, action, :id => @subject.id.to_s, class_name.to_sym => @subject)
      response.should_not be_success
    end
 
    specify "I should be redirected" do
      send(verb, action, :id => @subject.id.to_s, class_name.to_sym => @subject)
      response.should be_redirect
    end
  end
end

I call it like so (I’m using factory_girl):

  context "when logged in as campaign manager" do
    it_should_behave_like "denied on all resource methods", "category", Factory.attributes_for(:category)
  end

Thanks to David Chelimsky on this SO question. I used his answer as a baseline. :)

Written by rystraum

November 19th, 2011 at 2:31 am

Posted in Programming

Tagged with ,

Devise: set confirmable to false when environment is in development

leave a comment

I’m using Devise for authentication on the Ruby on Rails app that I’m writing. In my model, I happen to use

devise :confirmable

which sends out confirmation messages. I’m positive that this already works so I wanted to be able to create users without having to confirm but only during development.

So, I found out that adding the following onto your model does exactly what I wanted:

  def confirmation_required?
    if Rails.env.development?
      return false
    else
      return !confirmed?
    end
  end

Read the rest of this entry »

Written by rystraum

October 14th, 2011 at 8:24 pm

Posted in Programming

Tagged with ,