Skip to content

Instantly share code, notes, and snippets.

View zmajstor's full-sized avatar

Zoran Majstorovic zmajstor

View GitHub Profile
@zmajstor
zmajstor / gem update fix
Created August 25, 2012 10:37
ERROR: Failed to build gem native extension.
download & install command line tools for xcode
ln -s gcc gcc-4.2
http://stackoverflow.com/questions/6119153/why-do-i-get-a-bcrypt-ruby-gem-install-error
@zmajstor
zmajstor / device_group.rb
Last active December 22, 2015 23:39
Rails 3 sample for grouping Devices into DeviceGroups (form_for ActiveRecord has_many :through Association)
class Device < ActiveRecord::Base
has_many :device_groupings, :dependent => :destroy
has_many :device_groups, :through => :device_groupings
end
class DeviceGroup < ActiveRecord::Base
has_many :device_groupings, :dependent => :destroy
has_many :devices, :through => :device_groupings
attr_accessible :name, :device_ids # ...
@zmajstor
zmajstor / device_groups_controller.rb
Created September 13, 2013 09:02
Rails 4 sample for grouping Devices into DeviceGroups (form_for ActiveRecord has_many :through Association)
class DeviceGroupsController < ApplicationController
# ...
private
def device_group_params
params.require(:device_group).permit(:title, {:group_ids => []} )
end
# ...
@zmajstor
zmajstor / _form.html.haml
Last active December 23, 2015 05:38
Rails nested form (Add/Remove nested resource without Controller for nested resource) inspired by http://davidlesches.com/blog/rails-nested-forms-using-jquery-and-simpleform and http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html * to destroy the associated model: add the _destroy key to the attributes hash, with a…
= simple_form_for @portfolio do |f|
= f.input :title, label: 'Portfolio Title'
= f.simple_fields_for :assets do |assets_form|
.duplicatable_nested_form
= assets_form.association :stock
= assets_form.input :amount, :input_html => { min: 0 }
= link_to 'Remove', '', :class => 'destroy_duplicate_nested_form'
= assets_form.input :id, as: :hidden
@zmajstor
zmajstor / application.html.erb
Created November 2, 2013 13:38
conditional include of css, js based on controller (previously disable require_tree . in application.css/js)
<% if current_page?(controller: 'foo_bar') %>
<%= stylesheet_link_tag "foo", media: "all" %>
<%= javascript_include_tag "bar" %>
<% end %>
@zmajstor
zmajstor / ldap_test.rb
Last active April 16, 2021 08:25
LDAP test
# ---- edit data below ------------
LDAP_HOST = 'promdmnet.cloudapp.net'
LDAP_PORT = 636 # 636 or 389
LDAP_BASE = "dc=promdm, dc=net"
LDAP_BIND_USER = "ldapbind@promdm.net" # format is username@domain
LDAP_BIND_PASS = "ldapbindpassword"
samaccountname = "zm"
password = "userpassword"
# ----- edit end ------------------
@zmajstor
zmajstor / MyModel.rb
Created January 6, 2014 14:58
Rails (ActiveRecord) after_save callback for single field change
class MyModel < ActiveRecord::Base
after_save :do_something, if: :my_filed_changed?
def do_something
# my_filed has been changed ... do something
end
end
@zmajstor
zmajstor / dry_scope.rb
Created February 10, 2014 11:20
AR DRY scoping of the nested models
class Collection < ActiveRecord::Base
has_many :products
scope :live, where("collections.end_date IS NOT NULL AND collections.end_date >= ?", Time.now.to_date)
end
class Product < ActiveRecord::Base
belongs_to :collection
# how to DRY this .where ?
scope :launched, joins(:collection).where("collections.end_date IS NOT NULL AND collections.end_date >= ?", Time.now.to_date)
@zmajstor
zmajstor / _collection.html.erb
Last active August 29, 2015 13:56
how to wait for jQuery .append to finsh
<div class="card">
<%= image_tag (collection.designer.cover_image_url), class: "center-block" %>
<div class="card-hover">
<span class="card-overlay">
<%= collection.designer.fullname %><br>
<%= collection.designer.live_collection.name %><br>
<%= collection.designer.location %>
</span>
</div>
</div>
@zmajstor
zmajstor / aviary_uploader.rb
Last active August 29, 2015 13:57
CarrierWave upload with Aviary and FileReader
# encoding: utf-8
class AviaryUploader < CarrierWave::Uploader::Base
# Include RMagick or MiniMagick support:
# include CarrierWave::RMagick
# include CarrierWave::MiniMagick
# Choose what kind of storage to use for this uploader:
# storage :file