Skip to content

Instantly share code, notes, and snippets.

View tbprojects's full-sized avatar

Tomasz Borowski tbprojects

View GitHub Profile
@tbprojects
tbprojects / destroy.rb
Created August 3, 2015 13:25
When order does matter...
# GOOD - check if can object be destroyed and when it's possible destroy associated objects :-)
before_destroy :can_be_removed?
has_many :custom_values, dependent: :destroy
# BAD - destroys associated objects and then check if object can be destroyed :-(
has_many :custom_values, dependent: :destroy
before_destroy :can_be_removed?
window.localeEn = {
menu: {
secrets_of_newtown: 'Secrets of Newtown',
your_investigation: 'Your Adventure',
einder_story: 'Einder\'s Story',
rumors: 'Rumors'
},
read_story: 'Read Story',
subtitle: 'intriguing location based game<br/>settled in the world of H.P. Lovecraft',
newtown_section: {
@tbprojects
tbprojects / dequeuing.rb
Created January 2, 2015 11:51
Adds an ability to remove delayed jobs from the queue, that were added with ActiveJob.
module ActiveJob
module Enqueuing
module ClassMethods
def remove_delayed(*args)
job_or_instantiate(*args).remove_delayed
end
end
def remove_delayed
self.class.queue_adapter.remove_delayed(self)
@tbprojects
tbprojects / check_env_vars.rb
Created December 18, 2013 11:00
We are using figaro for storing services api_keys and we had a problem when one developer changed application.yml and forgot to inform other developers about it (but he did update application.example.yml). As a result of this the other developer experienced "bugs" in the application. Put this file to initializers to detect mismatch between appli…
require 'yaml'
example_vars = YAML.load_file('config/application.example.yml').keys
live_vars = Rails.env.production? ? (ENV.keys & example_vars) : YAML.load_file('config/application.yml').keys
unless live_vars.sort == example_vars.sort
raise 'There is a mismatch between application.yml and application.example.yml'
end
def send_changes(bid_id)
tender_diff = {label: 'Voice of Poland', name: ['Voice of Poland', 'X-Factor'], currency: ['EUR', 'PLN']}
item_diffs = [{label: 'Golf Ball', name: ['Golf Ball', 'Football Ball'], quantity: [4,5]},{label: 'Chess Board', name: ['Chess
Board', 'Agile Board']}]
diffs = {tender_diff: tender_diff, item_diffs: item_diffs}
EmailDelivery.deliver bid_id, diffs, {action: 'tender_changed_for_supplier'}
end
send_changes(bid_id)
@tbprojects
tbprojects / gist:5150521
Created March 13, 2013 09:26
Fetching production db via capistrano
namespace :server_db do
task :migrate, :roles => :app do
run "cd #{release_path} && RAILS_ENV=production rake db:migrate"
end
task :get, :roles => :app do
puts "This task is designed to get database from the server"
puts "Example: cap ENV server_db:get"
t = Time.now
db_name = "pg_dump_#{environment}_%04d%02d%02d_%02d%02d%02d.sql" % [t.year, t.month, t.day, t.hour, t.min, t.sec]
@tbprojects
tbprojects / dialog template
Created October 3, 2012 09:42
[Rails][Javascript] link_with_confirm - colorbox dialog
= content_tag :div, id: 'destroy_confirmation' do
= content_tag :div, class: 'colorbox-head' do
= content_tag :h2, options[:title] || t("global.confirm_title")
= content_tag :div, class: 'colorbox-body' do
= content_tag :p, options[:message] || t('global.confirm')
= content_tag :div, class: 'colorbox-foot' do
= link_to t('global.yes'), confirmed_url,
method: (options[:method] || 'GET'),
remote: options[:remote],
class: 'bt bt-primary'
@tbprojects
tbprojects / performed_actions_base.rb
Created October 1, 2012 14:45
[Ruby][ActiveRecord] log executing given methods with has_performable_actions
module PerformedActions
module Base
def self.included(base)
base.extend(ClassMethods)
end
def perform_action(action)
if self.class.get_performable_actions.include?(action.to_s)
PerformedAction.create!(subject: self,
performer_id: PerformedAction.current_user.try(:id),
@tbprojects
tbprojects / autocompleter_init.js
Created September 21, 2012 08:07
[jQuery] autocompleter matching requirement
var config = {
labelField: '#label_input'
valueField: '#value_input',
source: '/path_to_autocompleter_backend', //responds with json [{label: '...', value: '...'}, {label: '...', value: '...'}]
focus: function(event, ui) {
var self = config;
$(self.field).val( ui.item.label );
return false;
},
change: function(event, ui){
@tbprojects
tbprojects / suggestions_for_field.rb
Created September 21, 2012 07:43
[Ruby][ActiveRecord] autocompleter suggestions based on attributes with tags (comma separated)
# lib/modules/suggestions_for_field.rb
module SuggestionsByField
def self.included(base)
base.extend(ClassMethods)
end
module ClassMethods
def suggestions_for_field(field, query)
connection.execute("
select val from (