Skip to content

Instantly share code, notes, and snippets.

View zmajstor's full-sized avatar

Zoran Majstorovic zmajstor

View GitHub Profile
@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 / 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 / _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 / 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 / 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 # ...
require "minitest/mock"
require "test_helper"
class LoginTest < ActionDispatch::IntegrationTest
fixtures :pkis, :organizations, :users
def setup
@ldap_mock = Minitest::Mock.new
@ldap_mock.expect :tap, LdapStub.new
end
@zmajstor
zmajstor / my_view.html.erb
Created October 9, 2015 21:42
call ajax on SELECT change (in rails view)
<script type="text/javascript">
$(document).on("change", "#my_select", callAjax);
$.ajaxSetup({
headers: { 'X-CSRF-Token': '<%= form_authenticity_token.to_s %>' },
timeout: 30000, // timeout after 30 seconds
async: true,
});
function callAjax() {
@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 / a_simple_role_based_authorization.md
Last active August 29, 2015 14:27
simple role-based authorization by current controller#action
@zmajstor
zmajstor / foo.html.erb
Created July 2, 2015 13:43
Ajax in Rails View with X-CSRF-Token
<%= check_box_tag "foo[delivered]", nil, foo.delivered, data: { id: foo.id }, class: "delivered" %>
<script type="text/javascript">
$.ajaxSetup({
headers: { 'X-CSRF-Token': '<%= form_authenticity_token.to_s %>' },
timeout: 30000, // timeout after 30 seconds
async: true,
cache: false,
// dataType: "json", // The type of data that you're expecting back from the server