Skip to content

Instantly share code, notes, and snippets.

Avatar
⛑️

Filip Bartuzi Bartuz

⛑️
View GitHub Profile
View checkout_controller.rb
# https://www.agileplannerapp.com/blog/building-agile-planner/refactoring-with-hexagonal-rails
def checkout
cart = CartCheckout.new(many, params)
if cart.save
redirect_to cart_path, notice: t('.success')
else
redirect_to cart_path, notice: t('.failure', reason: cart.error_message)
end
end
@Bartuz
Bartuz / validate_with_matcher.rb
Last active Nov 24, 2020 — forked from otaviomedeiros/validate_with_matcher.rb
RSpec matcher for validates_with
View validate_with_matcher.rb
# RSpec matcher for validates_with.
# https://gist.github.com/2032846
# Usage:
#
# describe User do
# it { should validate_with CustomValidator }
# end
RSpec::Matchers.define :validate_with do |expected_validator, options|
match do |subject|
@validator = subject.class.validators.find do |validator|
@Bartuz
Bartuz / Plea.markdown
Created Nov 11, 2015 — forked from justinko/Plea.markdown
Am I doing it wrong?
View Plea.markdown

Dear Rubyists,

I just lost a contract because of my code in a Rails project.

The specific code in question is related to a "posting a comment" feature. Here are the details:

In this project, "posting a comment" does not simply entail inserting a row into the database. It involves a procedure to yes, insert a row, but also detect its language, check for spam, send emails, and "share" it to Twitter and Facebook. I believe this algorithm should be encapsulated. I do not believe it belongs in a controller or a model. I do not believe Active Record callbacks should be used.

The "senior developer", whom is the stake holder's right hand man, said this:

@Bartuz
Bartuz / alias_matchers.md
Created Nov 9, 2015 — forked from JunichiIto/alias_matchers.md
List of alias matchers in RSpec 3
View alias_matchers.md

This list is based on aliases_spec.rb.

You can see also Module: RSpec::Matchers API.

matcher aliased to description
a_truthy_value be_truthy a truthy value
a_falsey_value be_falsey a falsey value
be_falsy be_falsey be falsy
a_falsy_value be_falsey a falsy value
@Bartuz
Bartuz / application_controller_spec.rb
Created Oct 23, 2015
before/after filters/actions rspec testing. Credits to: http://stackoverflow.com/a/20776916/2047418
View application_controller_spec.rb
# spec/controllers/application_controller_spec.rb
require 'spec_helper'
describe ApplicationController do
describe 'class' do
it { has_before_filters(:authenticate_user) }
end
end
View global-hook
1. Enable git templates:
git config --global init.templatedir '~/.git-templates'
This tells git to copy everything in ~/.git-templates to your per-project .git/ directory when you run git init
2. Create a directory to hold the global hooks:
mkdir -p ~/.git-templates/hooks
3. Write your hooks in ~/.git-templates/hooks
4. Make sure the hook is executable.
@Bartuz
Bartuz / file.rb
Created Sep 19, 2014
Ruby: Compare 2 hashes
View file.rb
class Hash
def diff(other)
(self.keys + other.keys).uniq.inject({}) do |memo, key|
unless self[key] == other[key]
if self[key].kind_of?(Hash) && other[key].kind_of?(Hash)
memo[key] = self[key].diff(other[key])
else
memo[key] = [self[key], other[key]]
end
end
View explanation.md

its isn't core to RSpec. One the of the focuses of RSpec is on the documentation aspects of tests. Unfortunately, its often leads to documentation output that is essentially lies. Consider this spec:

User = Struct.new(:name, :email)

describe User do
  subject { User.new("bob") }
  its(:name) { should == "bob" }
end
View .vimrc
if has("autocmd")
" remove trailing white spaces
autocmd BufWritePre * :%s/\s\+$//e
endif
View capybara cheat sheet
=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')