Skip to content

Instantly share code, notes, and snippets.

@xeviknal
xeviknal / cache_helper.rb
Last active December 23, 2015 12:59
Monkey patching can method in order to reach evaluated permissions in a block
module Admin::CacheHelper
def cache_with_permissions(name=[], options = nil, &cache_block)
permission_version = options && options[:permission_version] || :v0
permission_required = get_cache_key_for(name, permission_version, &cache_block)
name = name + permission_required
cache(name, options) { cache_block.call }
end
def get_cache_key_for(fragment_key, version, &cache_block)
@xeviknal
xeviknal / calendar_disable_ranges.js
Last active August 29, 2015 14:18
Disable ranges of time in jQuery DatePicker
var holidays = [ ['04/5/2015', '04/11/2015'], ['4/26/2015', '4/28/2015'], ['4/30/2015', '5/8/2015'] ];
function highlightDays(date) {
for (var i = 0; i < holidays.length; i++) {
var in_range = (new Date(holidays[i][0]) <= date) && (new Date(holidays[i][1]) >= date);
if (in_range) {
return [false];
}
}
@xeviknal
xeviknal / event.rb
Last active August 10, 2021 19:59
Socialize Models with Redis and Rails
# app/models/event.rb
# Event model has a set (event_users_ids) tracking all the users that has an inscription to it.
# The set update is made in the inscription model which models the relation between the event and its participants.
# The operation to get the friends participating in one event is done via redis set operations (&)
class Event < ActiveRecord::Base
include Redis::Objects
has_many :inscriptions
set :event_users_ids
@xeviknal
xeviknal / counter.rb
Created May 3, 2015 22:09
Redis Counter Cache - easy and non-locking
# app/models/concerns/counter.rb
require 'active_support/concern'
module Counter
module Updater
extend ActiveSupport::Concern
included do
after_create :update_counters
end
@xeviknal
xeviknal / create_events.rb
Last active August 29, 2015 14:24
Smart Status - keep updated status attribute depending on datetime model's attribute
class CreateEvent < ActiveRecord::Migration
def change
create_table :events do |t|
t.string :status
t.datetime :scheduled_at
t.datetime :started_at
t.datetime :finished_at
t.datetime :canceled_at
t.timestamps
@xeviknal
xeviknal / blog.mydomain.com
Created April 27, 2016 08:35
Nginx server setup for serving a Wordress instance
server {
listen 80;
gzip on;
gzip_min_length 1000;
gzip_proxied expired no-cache no-store private auth;
gzip_types text/plain application/xml application/json text/css application/javascript image/svg+xml;
root /var/www/et-blog/current;
@xeviknal
xeviknal / Event.java
Last active May 1, 2016 01:27
Easy and handmade serializing for java (influenced from Rails Active Model Serializer)
package com.et.models;
import com.et.util.EventGalleryParam;
import com.et.util.airbrake.AirbrakeLogFactory;
import com.et.util.persistence.ORM;
import org.apache.log4j.Logger;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Date;
@xeviknal
xeviknal / MultipleArgumentsExample.java
Last active June 27, 2016 11:35
Java allows multiple params definition on a method
public MultipleArgumentsExample {
public void say(String... words) {
for(int i = 0; i < words.length; i++)
System.out.println(words[i]);
}
public void say(String cheers, String... words) {
System.out.println(cheers);
say(words);
@xeviknal
xeviknal / BookingSpec.java
Created July 7, 2016 09:30
How do we expect private methods using EasyMock?
private void stubEditCall(Booking booking, int updatedGuestCount, Date updatedBookingAt,
Long updatedTableAreaId, boolean success) throws Exception {
Method[] methods = MemberMatcher.methods(Booking.class, "editCheckingETAvailability");
expectPrivate(booking, methods[0], updatedGuestCount, updatedBookingAt,
updatedTableAreaId).andReturn(success);
replay(booking, Booking.class);
}
@xeviknal
xeviknal / lets_derivate.sql
Created September 21, 2016 10:20
SQL: derivated table
SELECT
AVG(count)
FROM # here the derivate table comes
(SELECT
COUNT(*) AS count
FROM
`bets`
WHERE
state = 'pending'
GROUP BY user_id