Posts

Seamless ActiveRecord Model extension

Image
The Problem What if you have one very bloated model, like User, that has a ton of fields you use only once or twice in your whole application and yet paying price for it every time you fetch / create a new one. In this case what you want is to move some of these fields to another model (e.g. profile or user details). The Solution First step is to generate another model, let's name it UserProfile and link it to User model. You would also have to run a migration that adds UserProfile to all existing users. Now what you can do is to write something like: Now, of course, we could stop there, but why miss all the fun? What we want to do instead is: So how do we do that? Performance When I measured how speed and memory footprint correspond to number of columns I found that both increase roughly linear as number of columns grows. Precautions When mass-assigning attributes from controller while using CanCan gem (tested in Rails 3.2), e.g. (User.new(params[:user])), CanCan...

Parsing HTML generated outside of Angular

Image
It often happens that you have to work on some old project using jQuery. But in modern Web Development jQuery just doesn't cut it. So you write new features in Angular. But sometimes you have to modify existing code. It's not a problem as long as HTML is static, but if it's generated on server side Rails+jQuery style, then Angular won't know anything about it when it arrives to a browser. So we have make Angular parse this HTML. I remember a long time ago when I first encountered that problem I googled quite a lot but came up empty handed. So I had to find a way myself. Recently my colleagues stumbled on the same problem so I decided to share this little piece of code on the internets. Well... That's pretty much it. You shouldn't try running your angular app again. It's going to create a separate application. Also I want to share an interesting gotcha. If you inline your angular templates with <script type="text/ng-template" id="the...

AngularJS directive for slide-down animation with lazy rendering

Image
AngularJS directive for slide-down animation with lazy rendering In this blog post I am going to talk about a directive I created earlier that is available in my ng-slide-down Github repository. In the company I am working for we have a lot of widgets that are expanded via jQuery "slideToggle" function, thus when I make new components I have to adhere to existing style. As you have probably guessed new parts are written in Angular. Another requirement I had to satisfy is that we can have a lot of these widgets on a single and rendering them all in Angular would be wildly inefficient, so it had to only render HTML if widget was expanded. In order to use resulting directive all you need to is to add a directive ng-slide-down to your HTML element and pass a variable that is going to control it: <div ng-slide-down="slideDown"> Code So let's start writing the code ( fiddle ). Lazy rendering In the next step we are going to add an option to stop...

AngularJS + Socket.io

Image
AngularJS provides a very interactive user experience, just like Socket.io. When you combine them in a single page application awesomeness is squared. But some of the AngularJS mechanics produce buggy behavior for Socket.io. For example if you subscribe to an event in your controller and then go to another view scope this controller is destroyed, but event subscription is still stored in a global socket.io object. So when you go back and controller is reinstanciated you get a second subscription. After a while of you going back and forth there's a chaos! You don't want to receive 2+ messages from 1 event in your let's say chat application. So how can we deal we it? I'll let code and comments speak for itself ( coffeescript ): And here's how you use it: Some use case for using reconnect (it's so simple I should't have included it):

Readable NodeJS authorization

Image
Today I'm going to post a nifty piece of code for NodeJS authorization. Wow... It's been a while since I wrote something here. Let's get dirty, first of all there are 2 reasonable assumptions: You are using Express (or just Connect). You are using Passport (for authentication). Passport is used to get a current user and his role from Request object. For those who don't know: "authentication" is about finding out who the person is, classic example is email + password. Authorization is about what this person is allowed to do. In this example for clarity I'm going to use Coffeescript, but it can be easily translated into JS (by using for example js2coffee.org ). What I wanted to do is to use rules in middleware style that works well with NodeJS async model. The goal for authorization is to look something like that: This is the cleanest API I could come up with. Custom function can do 3 things: Allow action by returning next(true) For...

Flexible Mock

Image
Hey, I've been posting too many abstract shit lately, how about some code? So here's my simple Mock class and tests for it (to show how it works). Class class Mock def initialize(method_name, block = nil ) block||=Proc.new do |*args| args=args.first if args.length==1 yield(args) end self.class.send :define_method, method_name, block end end Examples class TestMock The problem with other mock libraries is that there's too much magic going on and syntax is tricky mock.expect :method, 'return', [:params] ... mock.verify # or mock.should_receive(:method).with("A", 1, 3).once In more complex examples it gets hard to express what you want and remember syntax. I prefer more control, that's why my decision is to create object with custom method. In this method you can do whatever you want. Another good thing of this approach is property of Proc to maintain context where it was called (look at...

Thoughts on programming podcasts

Image
Do you remember any movies or pictures about the "Gold Rush" in America? When thinking about getting knowledge today I imagine exactly that: a man with a sieve filtering out a lot of dirt just to find a tiny golden nugget. It's not like there is a website where you can go and read all the facts that YOU don't know but should. Acquiring knowledge is a work of going through a lot of information and finding missing parts for your puzzle. I probably sound like an obsessed person but that's because I am obsessed. Today I going to talk about one such dirt-gold source, namely podcasts. I've been listening to a lot of these lately and, honestly, 95% is useless crap: things that you do know things that you don't need to know things that you aren't ready to know But every now and then you stumble upon something brilliant so a 5% that's left make all the difference. Following suggestions will help you to get the maximum out of it: There's no u...