Skip to content

Instantly share code, notes, and snippets.

View equivalent's full-sized avatar
:octocat:
I'm IDDQD + IDKFA for Ruby on Rails

Tomas Valent equivalent

:octocat:
I'm IDDQD + IDKFA for Ruby on Rails
View GitHub Profile
@equivalent
equivalent / big_array_duplicates_bench.rb
Last active June 21, 2021 10:16
fetch elements that occurred multiple times in Ruby Array
require 'benchmark'
require 'faker'
ary = []
100.times do
ary << Faker::Name.name
end
n = 100_000
=Navigating=
visit('/projects')
visit(post_comments_path(post))
=Clicking links and buttons=
click_link('id-of-link')
click_link('Link Text')
click_button('Save')
click('Link Text') # Click either a link or a button
click('Button Value')
@equivalent
equivalent / bootstrap_breadcrumbs_builder.rb
Last active August 29, 2015 13:58 — forked from riyad/bootstrap_breadcrumbs_builder.rb
breadcrumbs_on_rails for Bootstrap 3 override
# The BootstrapBreadcrumbsBuilder is a Bootstrap compatible breadcrumb builder.
# It provides basic functionalities to render a breadcrumb navigation according to Bootstrap's conventions.
#
# Originally from https://gist.github.com/riyad/1933884/ modified for Bootstrap 3 and "Go to hell with separator, use Css Dumbass"
# policy
#
# You can use it with the :builder option on render_breadcrumbs:
# <%= render_breadcrumbs :builder => ::BootstrapBreadcrumbsBuilder" %>
#
#
1. Open a command prompt and navigate to /etc/nginx/ssl
2. issue "openssl req -new -newkey rsa:2048 -nodes -keyout server.key -out server.csr"
3. Get your certificate - Go to your certificate authority and give them the CSR
4. copy your new crt to the /etc/nginx/ssl and give it read priveledges
5. reconfigure your nginx.conf, here is mine. The first part redirects 80 to 443, the second part listens to 443 and is optimized to return Ruby on Rails application requests using gzip and unicorn.
# MySQL. Versions 4.1 and 5.0 are recommended.
#
# Install the MySQL driver:
# gem install mysql2
#
# And be sure to use new-style password hashing:
# http://dev.mysql.com/doc/refman/5.0/en/old-client.html
development:
adapter: mysql2
encoding: utf8
@equivalent
equivalent / spec-lib-tasks-notification_rake_spec.rb
Created March 7, 2013 12:16
How to test rake tasks with RSpec
# spec/lib/tasks/notification_rake_spec.rb
require 'spec_helper'
describe 'notifications:weekly' do
include_context "rake"
let(:users){ [double(:user)] }
let(:user_deliveries){ double(:user_deliveries) }
before{ User.stub(:all).and_return( users ) }
@equivalent
equivalent / app-model-document.rb
Last active October 29, 2018 07:47
Recursion example in Ruby on Rails model. Recursion will generate array of parents parents
class Document < ActiveRecord::Base
belongs_to :parent, class_name: 'Document'
def self.get_ancestors(who)
@tree ||= []
# @tree is instance variable of Document class object not document instance object
# so: Document.get_instance_variable('@tree')
if who.parent.nil?

My issues with Modules

In researching topics for RailsCasts I often read code in Rails and other gems. This is a great exercise to do. Not only will you pick up some coding tips, but it can help you better understand what makes code readable.

A common practice to organize code in gems is to divide it into modules. When this is done extensively I find it becomes very difficult to read. Before I explain further, a quick detour on instance_eval.

You can find instance_eval used in many DSLs: from routes to state machines. Here's an example from Thinking Sphinx.

class Article &lt; ActiveRecord::Base
# The BootstrapBreadcrumbsBuilder is a Bootstrap compatible breadcrumb builder.
# It provides basic functionalities to render a breadcrumb navigation according to Bootstrap's conventions.
#
# BootstrapBreadcrumbsBuilder accepts a limited set of options:
# * separator: what should be displayed as a separator between elements
#
# You can use it with the :builder option on render_breadcrumbs:
# <%= render_breadcrumbs :builder => ::BootstrapBreadcrumbsBuilder, :separator => "&raquo;" %>
#
# Note: You may need to adjust the autoload_paths in your config/application.rb file for rails to load this class:
@equivalent
equivalent / custom_simple_form_helper.rb
Created December 12, 2012 11:44
simple "monkey patch" way how to generate simple_form form with class representing model name ('document_name') and action ('new') ... I'm sure there is better practice to do this but this is fine for my requirement app/helpers/custom_simple_form_helper.rb
module CustomSimpleFormHelper
def simple_form_for(object, *args, &block)
options = args.extract_options!
action_classes = [object.class.model_name.underscore , (object.persisted? ? 'edit' : 'new')]
options[:html]={} unless options[:html]
if options[:html].empty?
options[:html][:class] = action_classes