Tales of coding and tips for myself and others http://tech.rytis.net Most recent posts at Tales of coding and tips for myself and others posterous.com Sun, 13 Nov 2011 01:51:00 -0800 How to assign permissions for custom controllers in locomotive cms http://tech.rytis.net/how-to-assign-permission-for-custom-controlle http://tech.rytis.net/how-to-assign-permission-for-custom-controlle

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:

1
2
3
4
5
6
7
8
9
10
11
# app/models/ability.rb
class Ability

  def setup_author_permissions_with_myliufoto!
    self.setup_author_permissions_without_myliufoto!
    can :manage, [Album, Photo]
  end

  alias_method_chain :setup_author_permissions!, :myliufoto

end

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

Permalink | Leave a comment  »

]]>
http://files.posterous.com/user_profile_pics/1150982/fb_photo.jpg http://posterous.com/users/4bmScqNIJpRL Rytis Lukoševičius pacifists Rytis Lukoševičius
Sun, 06 Nov 2011 04:48:00 -0800 How to render locomotive cms pages from your own rails controllers http://tech.rytis.net/how-to-render-locomotive-cms-pages-from-your http://tech.rytis.net/how-to-render-locomotive-cms-pages-from-your

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# config/application.rb
...
module Myliufoto
  class Application < Rails::Application

    def self.activate
      if Rails.env.development?
        load File.join(config.root, 'lib', 'locomotive', 'rails', 'render.rb')
      else
        require File.join(config.root, 'lib', 'locomotive', 'rails', 'render.rb')
      end
      ...
    end

    ....

    config.to_prepare &method(:activate).to_proc
  end
end

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.

 

Permalink | Leave a comment  »

]]>
http://files.posterous.com/user_profile_pics/1150982/fb_photo.jpg http://posterous.com/users/4bmScqNIJpRL Rytis Lukoševičius pacifists Rytis Lukoševičius
Wed, 17 Aug 2011 02:50:00 -0700 ca-certificates-java errors out on ubuntu 10.04 lts in openvz container http://tech.rytis.net/ca-certificates-java-errors-out-on-ubuntu-100 http://tech.rytis.net/ca-certificates-java-errors-out-on-ubuntu-100

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:

1
2
3
4
5
error adding brasil.gov.br/brasil.gov.br.crt
error adding cacert.org/cacert.org.crt
error adding debconf.org/ca.crt
error adding gouv.fr/cert_igca_dsa.crt
error adding gouv.fr/cert_igca_rsa.crt

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:

1
2
3
4
java -version
Error occurred during initialization of VM
Could not reserve enough space for object heap
Could not create the Java virtual machine.

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.

Permalink | Leave a comment  »

]]>
http://files.posterous.com/user_profile_pics/1150982/fb_photo.jpg http://posterous.com/users/4bmScqNIJpRL Rytis Lukoševičius pacifists Rytis Lukoševičius
Thu, 21 Jul 2011 08:07:18 -0700 Changing the background color of CKEditor editing area. http://tech.rytis.net/changing-the-background-color-of-ckeditor-edi http://tech.rytis.net/changing-the-background-color-of-ckeditor-edi

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";

Permalink | Leave a comment  »

]]>
http://files.posterous.com/user_profile_pics/1150982/fb_photo.jpg http://posterous.com/users/4bmScqNIJpRL Rytis Lukoševičius pacifists Rytis Lukoševičius
Wed, 13 Jul 2011 22:04:56 -0700 How to configure kohana 3.1 virtual host for nginx http://tech.rytis.net/how-to-configure-kohana-31-virtual-host-for-n http://tech.rytis.net/how-to-configure-kohana-31-virtual-host-for-n

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
server {
        listen 80;
        server_name example.com;
        access_log /home/username/logs/example.com.access.log main;
        error_log /home/username/logs/example.com.error.log warn;
        rewrite_log off;
root /home/username/www/example.com/;

location / {
index index.php;

if (-f $request_filename) {
             break;
         }

         try_files $uri $uri/ @kohana;
     }

location ~* ^/(modules|application|system) {
         return 403;
     }

location ~* \.php$ {
         # PHP FILES MIGHT BE TO HANDLED BY KOHANA
        try_files $uri $uri/ @kohana;
 
         fastcgi_pass 127.0.0.1:9000;
         fastcgi_index index.php;
         fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
         include /usr/local/nginx/conf/fastcgi_params;
}

     location @kohana
     {
         fastcgi_pass 127.0.0.1:9000;
         fastcgi_index index.php;
         include /usr/local/nginx/conf/fastcgi_params;
         fastcgi_param SCRIPT_FILENAME $document_root/index.php;
     }

}

Permalink | Leave a comment  »

]]>
http://files.posterous.com/user_profile_pics/1150982/fb_photo.jpg http://posterous.com/users/4bmScqNIJpRL Rytis Lukoševičius pacifists Rytis Lukoševičius
Thu, 14 Apr 2011 01:34:00 -0700 Can we always trust wordpress plugins and themes? http://tech.rytis.net/can-we-always-trust-wordpress-plugins-and-the http://tech.rytis.net/can-we-always-trust-wordpress-plugins-and-the

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.

Permalink | Leave a comment  »

]]>
http://files.posterous.com/user_profile_pics/1150982/fb_photo.jpg http://posterous.com/users/4bmScqNIJpRL Rytis Lukoševičius pacifists Rytis Lukoševičius
Sun, 05 Dec 2010 10:00:00 -0800 Launching RefineryCMS on Ubuntu 8.04LTS http://tech.rytis.net/launching-refinerycms-on-ubuntu-804lts http://tech.rytis.net/launching-refinerycms-on-ubuntu-804lts

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 

Permalink | Leave a comment  »

]]>
http://files.posterous.com/user_profile_pics/1150982/fb_photo.jpg http://posterous.com/users/4bmScqNIJpRL Rytis Lukoševičius pacifists Rytis Lukoševičius
Mon, 29 Nov 2010 07:20:00 -0800 Tip on mongoid many to many relationship failing on save http://tech.rytis.net/34258444 http://tech.rytis.net/34258444

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.

Permalink | Leave a comment  »

]]>
http://files.posterous.com/user_profile_pics/1150982/fb_photo.jpg http://posterous.com/users/4bmScqNIJpRL Rytis Lukoševičius pacifists Rytis Lukoševičius