{ Josh Rendek }

<3 Go & Kubernetes

There don’t seem to be any ‘dynamic’ ACL modules for rails ( CanCan is kind of there, but not quite ) – I want to be able to modify permissions on the fly, preferably from an administration page.

This is done simply with a Role table and a few methods in application_controller and your User model. This allows you to easily check the serialized Role hash {“foo” => [“edit”, “update”]} by calling current_user.can?(“foo”, “edit”) and you’ll know if they can edit the foo object.

I’m currently working on a project where I’m trying to make everything as extensible as possible. I have a Ticket model and I want everything to be ticketable (thats my polymorphic association).

The problem is that I want the URLs to be sensible and still have my polymorphic attribute loaded automatically without having to do to much hacking / un-dry code, so I wanted the URLs to be: /users/1/tickets/new.

This needs to be applied to any arbitrary number of models that has_many :tickets, :as => :ticketable .

The tickets controller needs to be able to get the object, and set it for a user to access it for the polymorphic form. instance_set_variable is the magic that lets this happen very easily. By having the before filter on create and new requests, the :get_object is called each request for those actions and the nested resources parent object is found and loaded into a variable called @ticketable .

The last bit of magic is the form_for that lets you create the ticket and have ticketable be automatically filled in by the rails engine with one little modification in the ticket_controllers.rb create method. Thats it!

I was doing some development work on rather large files (like 30 gigs each) and I forgot to exclude them from my backup script that runs so I inadvertently filled up my disk space really quickly causing MySQL to hang – needless to say everything should be back to normal now :).

Yesterday I was trying to install Ubuntu 10.10 (occasionally I give linux another try for a desktop) on my Macbook Pro – and boy did I run into issues. First, when I was reformatting my mac, the disc kept ejecting – so finally when I got it to load and stay in, the formatter would error out. After repeating this several dozen times with various CD/DVD’s and different burns of Ubuntu, I ran to Walmart and grabbed a Targus external DVD reader. After my mac kept ejecting cd’s every time I stuck them in, this managed to fix the problem. Everything worked first time, even discs with a few scratches/dust on them.

This has apparently been a long standing issue, and I’m surprised apple is still selling them with this DVD drive.

On another note, I just ordered another SSD for my macbook pro to reinstall OSX on since Ubuntu doesn’t seem to be panning out :).

Starcraft 2: Freezing when loading map

Oct 16, 2010 - 1 minutes

Starcraft 2 was freezing when I was loading maps ( after I accidentally forcequit it the other night :| ) -after trying the Blizzard Repair tool and uninstalling/reinstalling several times, a simple reboot was all that was needed to fix it. Hope this helps someone else!

Ruby and Ebay: Get list prices

Oct 3, 2010 - 2 minutes

I’ll eventually be turning my ruby ebay work into a gem, but for now here are little snippets:

Please note that this code automagically takes care of things like 4x SOMETHING – it’ll take the current list price and divide by four, if you don’t want that functionality, remove the divider. You also need rest-open-uri and hpricot for gems.

To get a list of prices:

 1def self.get_search_results(query)
 2            # API request variables
 3            endpoint = 'http://svcs.ebay.com/services/search/FindingService/v1';  # URL to call
 4            version = '1.0.0';  # API version supported by your application
 5            appid = 'YOUR APP ID';  # Replace with your own AppID
 6            globalid = 'EBAY-US';  # Global ID of the eBay site you want to search (e.g., EBAY-DE)
 7            safequery = URI.encode(query);  # Make the query URL-friendly
 8
 9            # Construct the findItemsByKeywords HTTP GET call
10            apicall = "#{endpoint}?";
11            apicall += "OPERATION-NAME=findItemsByKeywords";
12            apicall += "&SERVICE-VERSION=#{version}";
13            apicall += "&SECURITY-APPNAME=#{appid}";
14            apicall += "&GLOBAL-ID=#{globalid}";
15            apicall += "&keywords=#{safequery}";
16            apicall += "&paginationInput.entriesPerPage=25";
17
18            res = ""
19
20            open( apicall ).each { |s| res << s }
21            prices = []
22
23
24            doc = Hpricot.parse(res)
25            (doc/:item).each do |x|
26
27                divider = 1
28                get_count = (x/:title).inner_html.scan(/[0-9]/).first
29                p "#{(x/:title).inner_html}"
30                if !get_count.nil?
31                    divider = get_count.to_i
32                end
33                prices << (x/:sellingstatus/:currentprice).inner_html.to_f/divider
34            end
35
36            prices
37end

My office (underneath the desks) is a mess of cables. I have a dual monitor setup for my mac pro, usually having documentation (or a video) on the second screen, and coding going on in my main 28" HANNS-G. I love developing rails applications on my mac because of the tools I have available to me and how easy it is to access them. However I wanted to keep running applications on linux (since thats where they end up going) and doing some other specific linux things.

I have all my programs / apps in a directory called /apps/ and I wanted to use Nginx and the passenger gem to handle everything.

I want to do all my development on my Mac and running the applications on linux. I have a spare Dell Vostro 200 that used to be my gaming desktop that now has CentOS installed on it.

Goals: Lets say I have an application called foobar . When (on my mac) I go to foobar.dev, it should go to my linux box and load the foobar app from nginx+passenger .

To keep everything in sync, I’m going to use NFS and export the directory from my mac to my linux box. Thankfully on mac it is ridiculously easy to setup NFS.

Replace things in caps like XXX and YOURUSERNAME with your information. Obviously if your network is different or mask, change it. I would not do this on a public server. I did this on my private, firewalled home network.

Just add this to /etc/exports:

You may need to restart NFSD on your mac:

Once your NFS is working you should be able to issue a command:

and you’ll see your export list.

You can then mount this on your linux box:

Next step you should install WEBMIN. You can this in a few different ways, but for CentOS:

Login at https://YOURLINUXIP:10000/. We’re going to setup BIND and the DNS server. You could do this without webmin, but webmin lets you do it in a few clicks, and setting up DNS isn’t one of my strong points.

Navigate to Servers -> Bind DNS Server.

Click on “Create master zone”. Enter “dev” into the domain, so it should look like this:

This will install Nginx for you and configure /opt/nginx/conf/nginx.conf for you as well.

I’ve shrunk my nginx.conf down to this, and modified it to load *.dev as server and host names.

The important line is server_name   ~^(www.)?([-\w.]+).\w+$; This allows you to catch those domains.

My Mac is my database server (it has an SSD and 8 gigs of memory and is backed up, so I keep everything on it). Now I need to let my linux box connect to my mac pro’s mysql instance.

I use SequelPro to do my database management, and you need to run the following queries:

Run the query and you should be all set. You can start Nginx and your applications should start running when you go to foobar.dev or yourappname.dev !

I found this took a bit of time to do (although this tutorial should let you do it in a few minutes) but it saves me from having to worry about start/shutting down thin/mongrel’s and other stuff on my mac.

Let me know if you have any comments or questions!

Mac: I'm an IDE whore

Sep 10, 2010 - 1 minutes

I don’t know if I’ve posted about how many IDE’s I use but I’m always switching between TextMate/RubyMine and several others. I will say that I have been sticking more with the Java variant IDEs though since I got my Mac Pro with an SSD…. it makes uber-slow Java apps actually run at a near native speed.

Ruby: random stat

Sep 8, 2010 - 1 minutes

I have a program that takes a block of texts and then counts the most popular occurrences of words in that text. Processed a 680,354 character string in about 30 minutes on 2 Core i7s and an SSD. Probably could be optimized a bit I think.

Snippet: