Skip to content

Instantly share code, notes, and snippets.

@maxivak
maxivak / gc_disable_request
Last active December 22, 2015 18:49
Rails. Disable GC during request
class ApplicationController < ActionController::Base
around_filter :gc_disable
private
def gc_disable
GC.disable
begin
yield
ensure
@maxivak
maxivak / rails-www-redirect-routes
Last active April 28, 2021 12:17
Redirect from WWW to a non-WWW version of site in Rails.details:http://maxivak.com/redirect-from-www-to-a-non-www-version-of-site-in-rails/
Foo::Application.routes.draw do
constraints(host: /^www\./i) do
match '(*any)' => redirect { |params, request|
URI.parse(request.url).tap { |uri| uri.host.sub!(/^www\./i, '') }.to_s
}
end
# other routes
# find all occurrences of $name in the string
formula = "$paramname > $value && $x>0"
s = formula.gsub(/\$([a-z_\d]+)/) do
name = $1
if name=='now'
v = Time.now.utc.now
else
@maxivak
maxivak / Activity_json_data
Last active March 20, 2018 02:40
ListView with images in Android. Tutorial - http://maxivak.com/listview-with-images-android/
// populate data from JSON array
public class MyActivity extends Activity {
List products;
ListView lvProducts;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
@maxivak
maxivak / ImageDownloader
Last active December 28, 2016 20:33
Android ImageDownloader
/*
* Copyright (C) 2010 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
// Main Activity
package com.example.mysamples;
import android.app.Activity;
import android.os.Bundle;
import android.widget.AbsListView;
import android.widget.GridView;
import java.util.ArrayList;
@maxivak
maxivak / db_script_mysql
Last active December 30, 2015 15:29
Rails Nested Forms Example - Formtastic, Cocoon. Tutorial is here - http://maxivak.com/rails-3-nested-models-in-one-form-using-formtastic-and-cocoon-gems/
CREATE TABLE IF NOT EXISTS `projects` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci ;
CREATE TABLE IF NOT EXISTS `tasks` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`project_id` int(11) NOT NULL,
`title` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
@maxivak
maxivak / session_rake_task
Created December 15, 2013 01:47
Rails sessions: rake task to clear old sessions. Details: http://maxivak.com/rails-clear-old-sessions-stored-in-database/
namespace :sessions do
desc "Clear expired sessions (more than 3 days old)"
task :cleanup => :environment do
sql = "DELETE FROM sessions WHERE (updated_at < '#{Date.today - 3.days}')"
ActiveRecord::Base.connection.execute(sql)
end
end
@maxivak
maxivak / rails_new_app_generate
Last active December 31, 2015 11:39
rails create new project
# specific version of Rails, with MySQL database
rails _VERSION_ new myapp -d mysql
examples:
rails _3.2.16_ new myapp -d mysql
@maxivak
maxivak / cron_rake_task
Last active December 31, 2015 15:19
Rails. Run rake task from cron
*/10 * * * * /bin/bash -l -c 'cd /var/www/apps/appname/current/ && RAILS_ENV=production bundle exec rake sessions:cleanup >> /var/www/logs/cron.log '