Skip to content

Instantly share code, notes, and snippets.

@ryaz
ryaz / active_admin_remote_destroyable.rb
Created January 28, 2014 09:07
Remote destroy for actve admin index (table)
module ActiveAdminRemoteDestroyable
def self.included(dsl)
dsl.controller do
def destroy
destroy! do |format|
format.html
format.js {render js: %Q|$("tr##{resource.class.model_name.singular}_#{resource.id}").fadeOut();|}
end
end
end
@ryaz
ryaz / gist:8665720
Created January 28, 2014 11:03
view example
-# Expects: resource
= form_for resource, remote: true, validate: true, html: { class: "lab_test edit_#{resource.label}" } do |f|
= resource_form_header(resource)
- teaser = t("tests.#{resource.label}_resource.new_record_description")
- if resource.new_record? && teaser.exclude?('New Record')
%p= teaser
= render partial: 'generic_data/control_buttons', locals: { resource: resource }
@ryaz
ryaz / gist:8666409
Created January 28, 2014 11:55
module example
module WizardConcern
extend ActiveSupport::Concern
included do
has_many :step_statuses, dependent: :destroy
has_many :steps_completed, through: :step_statuses, source: :step, class_name: 'Step'
has_many :wizard_progresses, dependent: :destroy
delegate :wizards, to: :organization
@ryaz
ryaz / gist:8668605
Created January 28, 2014 14:23
chart.js one point
min = _.max(chartData.datasets[0].data)
max = _.min(chartData.datasets[0].data)
if max == min
opt = {
scaleOverride : true,
scaleSteps : 3,
scaleStepWidth : 1,
scaleStartValue : max - 2
}
@ryaz
ryaz / API.md
Last active August 29, 2015 13:57
api for kostia/jquery.li18n.js

API

$.li18n.currentLocale = locale

Set the current locale.

Default is 'en'.

$.li18n.currentLocale = 'de';
def dice_rand
(1..10).inject(10) {|s,i| s + rand(6)}
end
def count_dice_avg(number)
res = Hash.new(0)
number.times { x = dice_rand; res[x] += 1}
res.sort_by {|k,v| v}.reverse
end
@ryaz
ryaz / migrate_to_s3
Last active August 29, 2015 14:04
migrate_from_file_storage_to_s3
namespace :paperclip_migration do
desc "migrate files from file storage to s3"
task :migrate_to_s3 => :environment do
puts "Migrating... carrierwave..."
Asset1.find_each do |record|
path = record.avatar.path.to_s
if path != record.avatar.default_url
file_path = (Rails.root.join('public', path)
if File.exists?(file_path)
file = File.open(file_path)
@ryaz
ryaz / Reservation
Created October 14, 2014 11:13
Reservation
Write a migration and ActiveRecord model that tracks restaurant reservations. Assume there is a table in your relational database named "reservations". Reservations have a start time, an end time and a table number.
Write some ActiveRecord validations that check new reservations for overbooking of the same table in the restaurant. For example, table #10 cannot have 2 reservations for the same period of time. This validation(s) should check time overlap for both record creation and updates.
Unit tests are a must to make sure your double booking validations are working. (rspec and unittests)
@ryaz
ryaz / gist:6d953e7a7959c870e9fd
Created October 28, 2014 21:28
relink brew
brew list -1 | while read line; do brew unlink $line; brew link $line; done