Posterous theme by Cory Watilo

How to assign permissions for custom controllers in locomotive cms

In my previous post I've shown how to render locomotive pages from you own controllers, but then I've realised that these custom controllers can be accessed only by the administrators of the site. So how to give mere mortal authors the permissions to edit that content?

Actually quite simply. Locomotive uses Cancan permission gem to handle who can do  what so all I needed to do is to use little bit of meta programming and did once again pointed me into right direction.

I did this:

And woula my Author level users can manage their albums without me. Hope that helps someone figuring locomotive cms out.

How to render locomotive cms pages from your own rails controllers

UPDATE: it turns out all this code only works correctly on ruby 1.9.2, I've tried to deploy this to 1.8.7 ree server and the menu item did not show up in the admin interface and also the permission settings for authors did quite work.

Few days ago I've stumbled on to nice example how to add some custom functionality to your locomotivecms site. As far as I needed to fix and improve myliufoto.lt albums and previously used refinerycms had really undfriendly interface for adding photos I've decided to remake a whole site using locomotive.

After all the admin stuff was done I needed to show the album list and the photo pages. As far as my data was not in locomotive's own content models and I needed custom routing like /portfolio/album-url/3 and so on I could not use default locomotive rendering to do it.

With the help of did (creator of locomotivecms) I've made a custom method that can render any locomotive page with my own instance variables passed to liquid. Locomotive already has all the rendering and variable setting for liquid implemented in Locomotive::Render::InstanceMethods module, so after some wandering around we've decided to base our code on that.

Here's all the code you need to do that:

Basically I've copied the 'render_locomotive_page' method and made it to accept a path for a page i.e. 'portfolio/photo' that would match page's slug in the cms.

To make it all work few small changes needed to be made:

In the render.rb file:
1. params[:path] has to be set to 'path'
2. then the locomotive_context needed to be changed to include controller instance variables so added the method 'my_context' that uses original locomotive method

In the controller you want to use it:
3. your controller should 'include Locomotive::Routing::SiteDispatcher' so that it has access to some locomotive's methods like 'current_site'

Everything else is left to the locomotive to handle so it should not brake unless the whole rendering is rewritten.

 

ca-certificates-java errors out on ubuntu 10.04 lts in openvz container

I've encountered very interesting issue when trying to install python-setuptools. It went ahead doing stuff and then threw a bunch of errors like:

Nothing I did helped and there was no fix for it. Someone suggested installing Java7 to make this go away, but I've dropped that after wondering around through oracle site for a bit.

The solution came when I've stared to wonder what's my java version and wrote simple thing with not so simple error:

So java is trying to tell me that about 2gb of memory is not enough for it to start... And the memory bloat thing is a known thing for OpenVZ containers trying to run java stuff. Usually you need to have atleast 4gb of ram to start java although it does not use it all, but reserves.

The solution: went to control panel of vps and for time being increased memory up to 8gb everything installed.

Changing the background color of CKEditor editing area.

Very simple thing yet I've spent almost 20 minutes trying to figure it out and find how to do it.

The thing is that ckeditor uses white background for editing area and the sites background is dark, so text color was set to white by the client and when editing in cme nothing could be seen.

In order to feed ckeditor a stylesheet with all the style definitions that will be used in the editor area just use this:

CKEDITOR.config.contentsCss = "/css/admin/editor.css";

How to configure kohana 3.1 virtual host for nginx

I've strugled for a few hours until I finally found all the correct bits and pieces to make kohana with clean url rewritting work for nginx. There are several example configs scattered around the net, but they were all lacking some pieces. There were times when kohana just spat out some plain routing errors and other times when the index / was plain forbidden.

The results came when I finally stopped just copy pasting and really gave some thought into why it would not work as expected, then added 'index index.php' to location / and some other small tweaks here and there and vualia it finally showed the site as expected.

The final virtual host config:

Can we always trust wordpress plugins and themes?

I've launched several blogs using wordpress as the site engine and it was quite easy and pleasant. I've even did one quite complex website that was not really a blog and that had it's share of challenges to make it work on wordpress. Usually to launch a site several extra plugins are needed or a free/paid theme for it's design and so far everything just worked fine.

Yesterday was the day of doom. Suddenly one friend who just sent a newsletter with bunch of links going back to his site, said that it's not responding. I've checked the site and it just wasn't loading. After I've started digging and looked into php-fpm logs I saw that it's logging all requests coming to the site as slow because they took more than 5s in one particular line of Koi theme-utils.php file.. 

It turns out that every time the admin (I just had no time to check it without an active session) loads the page, curl_exec is used to call home and check theme's version. It would be good if they atleast have used reasonable timeout on that.. but no, it had the default timeout of 30s. So for 30s the site was just hanging there and waiting for themify.me to respond and well it wasn't. The quickest fix was just to add return true in the first line of that function and everything came back to normal.

The moral of the story is - wordpress is cool platform for blogging and it has a lot to offer, but be aware that if you can't check all the plugin and theme code that you use.. well then better be prepared for some surprises down the line. Of course maybe you'll get lucky and nothing similar will happen to you, but you've been warned.

Launching RefineryCMS on Ubuntu 8.04LTS

I needed a small website for my wife and so I've wondered out to find a tool for the task that would be written in Ruby.

First I've tried to launch Radiant, but had some incompatibilities with library extension and dropped that after 2-3 hours of struggle. Then I've remembered there was another promising Ruby CMS that is RefineryCMS.

It depends on Rmagick and it has rmagick version 2.12.2 in it's Gemfile... which turned out to be the source of a lot of pain as my production server is old and runs on 8.04 LTS Ubuntu. I was running rmagick 2.13.1 on the server with no trouble... but 2.12.2 refused to install saying that:

Can't install RMagick 2.12.2. Can't find the ImageMagick library or one of the dependent libraries. Check the mkmf.log file for more detailed information.

I've strugled with that for 4-5 hours reinstalled two different versions of Image Magick and then it hit me... In the Refinery Gemfile there's a comment above rmagick line that says:

# Specify a version of RMagick that works in your environment:

So it does not care about exact version... after that I've changed to 2.13.1 and it finally WORKED! That was a relief...

But that was not over .. as far as I've translated most of the Refinery to Lithuanian I've had to use git versions of gems and so after deployment I hit:

Please run `bundle install` (Bundler::GitError)

It turns out the only way to cure it is to use bundle install --deployment and to add .bundle/config to git, because in it there's a path to gems so that passenger knows where to look for them. Otherwise gems installed from git are put into different directory and passenger has a hard time finding them.

The resulting website is myliufoto.lt 

Tip on mongoid many to many relationship failing on save

So you're happily hacking your way with mongoid and you add some many to many relationship between the models. And it hits you as it hit me with a nasty error that does not really help to understand what you did wrong.

All I did was added relationships to two of my models:

 class Site
    references_many :users, :stored_as => :array, :inverse_of => :sites
 end

class User
    references_many :sites, :stored_as => :array, :inverse_of => :users
 end

On the next save I got this error that got me really confused:

Mongo::OperationFailure (invalid query):
app/controllers/admin/sites_controller.rb:46:in `create'
 app/controllers/admin/sites_controller.rb:44:in `create'

# those lines were
@site = Site.new(params[:site])
@site.users << current_user #44
respond_to do |format|
if @site.save #46

The solution is to delete all your existing records, because they had no relationships on them and will fail each and every time when you try to do that. It's good that I was just scaffolding my app and had no real data in there.