Skip to content

Instantly share code, notes, and snippets.

Zoran Majstorovic zmajstor

  • Zagreb, Croatia
View GitHub Profile
@zmajstor
zmajstor / gem update fix
Created Aug 25, 2012
ERROR: Failed to build gem native extension.
View gem update fix
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 Dec 22, 2015
Rails 3 sample for grouping Devices into DeviceGroups (form_for ActiveRecord has_many :through Association)
View device_group.rb
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 Sep 13, 2013
Rails 4 sample for grouping Devices into DeviceGroups (form_for ActiveRecord has_many :through Association)
View device_groups_controller.rb
class DeviceGroupsController < ApplicationController
# ...
private
def device_group_params
params.require(:device_group).permit(:title, {:group_ids => []} )
end
# ...
@zmajstor
zmajstor / _form.html.haml
Last active Dec 23, 2015
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…
View _form.html.haml
= 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 Nov 2, 2013
conditional include of css, js based on controller (previously disable require_tree . in application.css/js)
View application.html.erb
<% if current_page?(controller: 'foo_bar') %>
<%= stylesheet_link_tag "foo", media: "all" %>
<%= javascript_include_tag "bar" %>
<% end %>
View ldap_test.rb
# ---- 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 Jan 6, 2014
Rails (ActiveRecord) after_save callback for single field change
View MyModel.rb
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 Feb 10, 2014
AR DRY scoping of the nested models
View dry_scope.rb
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 Aug 29, 2015
how to wait for jQuery .append to finsh
View _collection.html.erb
<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 Aug 29, 2015
CarrierWave upload with Aviary and FileReader
View aviary_uploader.rb
# 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
You can’t perform that action at this time.