четверг, 18 декабря 2014 г.

images in mail.ru letter (Не показываются картинки в письмах)

If you using image with direct link (for example hosted on s3) in letter, mail.ru parser will substitute it with point. Yes, regular point, like this - .

For ruby on rails + carrierwave + s3 you can use 
config.fog_use_ssl_for_aws = false
in carrierwave config (config/initializers/carrierwave.rb) to get all s3 links with "http" instead "https"

среда, 16 октября 2013 г.

Configure timezone on ubuntu server

Run:
dpkg-reconfigure tzdata
Choose your city.

Carrierwave and russian file names

When you upload a file with name like "картинка.jpg" you get someting like: "_______.jpg"
Oh no!

Let's use some source digging:

carrierwave/sanitized_file.rb
module CarrierWave
  class SanitizedFile
    attr_accessor :file
    class << self
      attr_writer :sanitize_regexp
      def sanitize_regexp
        @sanitize_regexp ||= /[^a-zA-Z0-9\.\-\+_]/
      end
    end
...
Redefine value of this attribute:
CarrierWave::SanitizedFile.sanitize_regexp = /[^a-zA-Zа-яА-ЯёЁ0-9\.\_\-\+\s\:]/
(thanks koHkB|/|cTadop)

 And put this code on the top of uploader file or in carrierwave initializer (if you want to use it in all your uploaders).

For russian filename basenames displayed like this:  %D0%BA%D0%B0%D1%80%D1%82%D0%B8%D0%BD%D0%BA%D0%B0.jpg" use URI.decode(string):
irb> URI.decode( "%D0%BA%D0%B0%D1%80%D1%82%D0%B8%D0%BD%D0%BA%D0%B0.jpg")
=> "картинка.jpg"

To read:
http://torubyonrails.blogspot.com/2013/05/carrierwave.html
carrierwave gem
URI::Escape

четверг, 5 сентября 2013 г.

Ruby array to sentence string

It's very simple:
irb> ["one","two","three"].to_sentence 
=> "one, two and three"
you can use different options to humanize output:
irb> ["one","two","three"].to_sentence(words_connector: " not equal ")
=> "one not equal two and three"
irb> ["to be", "not to be"].to_sentence(two_words_connector: " or ")
=> "to be or not to be"
But remember about your default locale in Rails!
# application.rb 
config.i18n.default_locale = :ru

irb> ["one","two","three"].to_sentence
=> "one, two и three"
To read:
Array#to_sentence (apidoc)

воскресенье, 1 сентября 2013 г.

Redirect from nginx /index.html to /

If you have not set index.html page on your app, you will see 404 error or, according to default nginx settings, this:


In our nginx configuration file use this code:
     location = /index.html {
        return 301 $scheme://$host;
      }
To read:
Nginx docs (location)

Returning 406 error with Rails

On page http://mysite/posts.blah i get 200 OK because Rails does not think that is wrong file extension.
If you want to close this pages with 406 error, you can do something like:

in ApplicationController: 
class ApplicationController < ActionController::Base
  ...
  before_filter :check format
  ...
  def check_format
    unless (Mime::EXTENSION_LOOKUP.values << Mime::ALL).include?(request.format)
      render file: 'public/404.html', layout: false, status: 406
    end
  end
  ...
end
Mime::EXTENSION_LOOKUP returns hash of all mime types registred in your app. In my case:
irb> Mime::EXTENSION_LOOKUP
{"html"=>text/html, "xhtml"=>text/html, "text"=>text/plain, "txt"=>text/plain, "js"=>text/javascript, "css"=>text/css, "ics"=>text/calendar, "csv"=>text/csv, "png"=>image/png, "jpeg"=>image/jpeg, "jpg"=>image/jpeg, "jpe"=>image/jpeg, "gif"=>image/gif, "bmp"=>image/bmp, "tiff"=>image/tiff, "tif"=>image/tiff, "mpeg"=>video/mpeg, "mpg"=>video/mpeg, "mpe"=>video/mpeg, "xml"=>application/xml, "rss"=>application/rss+xml, "atom"=>application/atom+xml, "yaml"=>application/x-yaml, "multipart_form"=>multipart/form-data, "url_encoded_form"=>application/x-www-form-urlencoded, "json"=>application/json, "pdf"=>application/pdf, "zip"=>application/zip, "xlsx"=>application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, "printable"=>text/html}
last two - my mime types from config/initializers/mime_types.rb initializer

Don't forget to set request header for ajax, by default it "*/*" (Mime::ALL).
Also this type commonly used by different statistics collectors and seo tools.

We add this mime type to list of allowed in this part:
Mime::EXTENSION_LOOKUP.values << Mime::ALL
but it's a good practise to use right headers in your requests.
In application.js.coffee:
  $.ajaxSetup
    beforeSend: (xhr) ->
      xhr.setRequestHeader("Accept", "application/json")

And this in ajax requests for example if you use async loading of some heavy html partials:
beforeSend: (xhr) ->
  xhr.setRequestHeader("Accept", "text/html")

To read:
How to limit the resource formats in the Rails routes file
Mime module api
Mime Type Resolution in Rails (Case 3)

пятница, 30 августа 2013 г.

Kill all linux processes by pattern

pkill -f programname
for example pkill -f rails kill all processes whose names contains "rails": rails server, rails console, etc.

about -f:
The pattern is normally only matched against the process name. When -f is set, the full command line is used.

To read: