Random Things in Random Time
Rails 3 resource renaming

Rails 2 way:

There was :as option in routes. Which rename routes.

Assume that you have next in routes.rb 

resources :blogs # /blogs

If you want to rename it from urls perspective you can:

resources :blogs, :as => ‘posts’ # /posts

But, in rails 3 this doesnt work.

As workaround I can propose next: 

resources :posts, :as => 'blogs', :controller => 'Blogs'

Rails 3 and mongodb through mongoid

I start playing with rails 3 and mongo. Found lot of articles on this theme, but only some steps works for me. Most useful is post by Nicola Racco.

I’ve created simple web app with authentication and attachments photo/video.

As js framework I use jQuery 1.4.2. For integration use smth like:

http://github.com/CodeOfficer/jquery-helpers-for-rails3

I use http://gist.github.com/399682, that found somewhere on github.

For authentication: devise

for attachements: carrierwave

ORM: mongoid

Paginations: Mongoid paginate + will_paginate for views

Here is my steps:

Install rails 3 beta 3

gem install rails --pre

Creating app

rails myproject --skip-activerecord

ensure that in config/application.rb


require 'action_controller/railtie'
require 'action_mailer/railtie'
require 'active_resource/railtie'
require 'rails/test_unit/railtie'

instead of:

require 'rails/all'

As ORM I choose Mongoid. Beacuse in devise 1.1.rc1 orm adapter only for mongoid and there are no adapter for mongo_mapper

gem "mongoid", "2.0.0.beta4"

Now lets start implement app logic

Authentication:

 gem 'devise', :git => 'git://github.com/plataformatec/devise.git'

I’ve installed device 1.1.rc1,

 bundle install

Next install devise

 rails generate devise_install

Rails will generate the file “config/initializers/devise.rb”, responsable of the Devise initialization. Opening the file you’ll see several options that you can configure as you like to customize the framework behavior. In this article the task is to make Devise to work with Mongoid, so we don’t care of these options. Let’s change the line:


require 'devise/orm/active_record'

in:


require 'devise/orm/mongoid'

Next steps following devise docs:

class User

  include Mongoid::Document

  include Mongoid::Timestamps

  devise :database_authenticatable, :registerable,

           :recoverable, :rememberable, :trackable#, :validatable

  has_many_related :posts

end

Pagination: 

Post.all.paginate(:page => params[:page], :per_page => 10)

for views simply use will_paginate

Attachements:

http://github.com/jnicklas/carrierwave

Uploader placed in app/uploaders

lass PhotoUploader < CarrierWave::Uploader::Base 

  include CarrierWave::Compatibility::Paperclip 

  include CarrierWave::RMagick 

  storage :file 

  def store_dir 

    ":rails_root/public/photos" 

  end 

  def paperclip_path 

    ":rails_root/public/photos/:style/:basename.:extension" 

  end 

  version :thumb do 

    process :resize_to_fit => [800,600] 

  end 

end

Model

require 'carrierwave/orm/mongoid' 

class Photo

  include Mongoid::Document 

  include Mongoid::Timestamps 

  field :title, :type => String 

  field :permalink, :type => String 

  field :content, :type => String 

  # field :hit, :type => Integer, :default => 0 

  field :photo, :type => String 

  field :photo_file_name, :type => String 

  belongs_to_related :map 

  mount_uploader :photo, PhotoUploader, :mount_on => :photo_file_name 

end

Controller

class PhotosController < ApplicationController

  def create

    photo = Photo.create(params[:photo])

    redirect_to(:back)

  end

And views:


<% form_tag videos_path, {:multipart => true} do %>

  <p>

    <label>Photo</label>

    <%= file_field_tag 'video[video]' %>

  </p>

  <%=submit_tag 'Upload'%>


<% end %> 

AR save(false) used less memory than save

When having lot of updates ( ~ 7983)

Memory used by daemon permanently growth upto 800Mb

THe problem is next

When saving record without validation save(false) max mem usage was 161 Mb

But there are no validations for this object

When I go through source code. Found that this issue because of valid? method.

Going more deeply I found that issue with new_record?

      def new_record?

        @new_record

      end

Strange )

Tested on MRI 1.8.7, 64-bit

AR 1.15.6

Ruby hash.with_indifferent_access

Different behavior in 1.8.7 and 1.8.6 with Rails 1.2.6

» {:sent => 1, “sent” => 0}.with_indifferent_access

=> {“sent”=>0}

» RUBY_VERSION

=> “1.8.6”

» RAILS_GEM_VERSION

=> “1.2.6”

» {:sent => 1, “sent” => 0}.with_indifferent_access

=> {“sent”=>1}

» RUBY_VERSION

=> “1.8.7”

» RAILS_GEM_VERSION

=> “1.2.6”

markoblogo:




Где деньги Юль? (via trulala)

markoblogo:

Где деньги Юль? (via trulala)

ld100:

Весь мак-софт должен полставляться в таком виде!

ld100:

Весь мак-софт должен полставляться в таком виде!

Nice Infrared palms wallpaper on my desktop

Nice Infrared palms wallpaper on my desktop

IE6 вышел в 2001 году, с тех пор успели разбомбить ВТЦ, захватить Афганистан, Ирак, выбрать по два раза Жору Куста с дядей Вовой, и по одному разу Обаму с Медведевым. За этот же срок прокатились всякие оранжевые революции, придумали Ладу Калину и Приору, телефоны проделали путь от 3310 до IPhone’ов и Nexus One, опять таки за это же время появились и достигли сегодняшних высот facebook, twitter, vkontakte и ещё уйма всего. А интернет технологии что, на месте что ли по-вашему стоят? Хороша ложка к обеду, IE6 своё давно отжил.
С ХабраХабра про Internet Explorer 6 (via ld100)
Screencast in background on iPhone

I’ve got couple of screencasts from some conferences. At some moment I want to listen it going to work. So I need play it in background but regular control will stop screencast after locking iPhone or exiting iPod.

But next trick is working. Run screencast then exit the iPod app and it will be stopped. Next you should just push play button on garniture (on headphones) and screencast plays in background!


ImageMagick on Ubuntu 8.04

Ubuntu 8.04 packages has 6.3.7 but i need at least 6.3.8.

I decide to build it from source. And got next from shell it works, also it works from Rails ./script/console. But it doesnt work from Passenger.

Solution

When building from sources

Dont forget configure with —disable-openmp

and after make install

run sudo ldconfig